Connection

Represents the PostgreSQL connection and allows executing queries on it.

struct Connection {}

Constructors

this
this(string connString)

Connection constructor

Members

Functions

allResults
Result[] allResults()

Calls nextResult until the value returned is null, the returns them as an array.

begin
void begin()

Begins a transaction block.

close
void close()

Close the connection manually

commit
void commit()

Commits current transaction

count
long count(string filter = "", U vals = U.init)

Returns a count of the rows matching the filter in the specified relation. Filter can be empty or not given to select a count of all the rows in the relation.

ensureSchema
void ensureSchema(bool createType = false)

Generates and runs the DDL from the given structures

escapeIdentifier
string escapeIdentifier(string str)

Escapes an identifier (column, function, table name, ...) to be used in a query.

escapeLiteral
string escapeLiteral(string str)

Escapes a string, to be used in a query

exec
Result exec(string command)

Executes the given string directly

execParams
Result execParams(string command, T params)

Executes the given string with given params

execParams
Result execParams(string command, Value[] params, bool async = false)

ditto, but taking an array of params, instead of variadic template

find
T[] find(string filter = "", U vals = U.init)

Returns an array of the specified type, filtered with the given filter and params

findL
T[] findL(string filter, int limit, U vals)

Returns an array of the specified type, filtered with the given filter and params with length limited by limit

findOne
Nullable!T findOne(U id)

Returns the requested structure or a Nullable null value if no rows are returned

findOne
Nullable!T findOne(string filter, U vals)

Returns the requested structure, searches by the specified filter with given params

findOneBy
Nullable!T findOneBy(string col, U val)

Returns the requested structure, searches by the given column name with the given value If not rows are returned, a Nullable null value is returned

insert
bool insert(T val, bool async = false)

Inserts the given structure to the DB

insert
int insert(T vals, bool async = false)

Inserts the given array of structures to the DB as one query

insertAsync
void insertAsync(T val)

ditto, async

insertR
Result insertR(T val, string ret = "")

Inserts the given structure, returning whatever columns are specified by the second param as a normal Result.

insertR
Result insertR(T vals, string ret = "")

Inserts the given array structures in a singl query, returning whatever columns are specified by the second param as a normal Result.

isBusy
bool isBusy()

Equivalent to calling PQisBusy from libpq. Only useful if you're doing async stuff manually.

lastResult
Result lastResult()

Calls nextResult until null is returned, then retuns only the last non-null result.

nextResult
Result nextResult()

Blocks until a result is read, then returns it

releaseSavepoint
void releaseSavepoint(Savepoint s)

Destroys savepoint in the current transaction block.

remove
int remove(U id)

Deletes the record in the given table, by its PK

remove
int remove(string filter, U vals)

Deletes rows in the specified relation, filtered by the given filter string and values

removeAsync
bool removeAsync(string filter, U vals)

ditto, async

rollback
void rollback(Savepoint s = null)

Rollback the current transaction to savepoint.

savepoint
Savepoint savepoint(string name)

Creates savepoint in the current transaction block.

send
bool send(string command)

ditto, async

sendParams
void sendParams(string command, T params)

ditty, but async

sendParams
void sendParams(string command, Value[] params)

ditto, async

update
int update(string filter, string update, U vals)

Updates records filtered by the filter string, setting the values as specified in the update string. Both should be SQL-syntax

update
int update(U id, Value[string] updates, bool async = false)

Similar to above update, but instead of acceptign a filter and and update string, always filters by the PK and updates with absolute values from the updates AA.

update
int update(U id, T updates, bool async = false)

Similar to above, but accepts the whole structure as an update param. Filters by the PK, updates ALL the values in the filtered rows.

updateAsync
void updateAsync(string filter, string update, U vals)

ditto, async

Properties

db
const(string) db [@property getter]

Returns the name of the database currently selected

errorMessage
string errorMessage [@property getter]

Returns the last error message

host
const(string) host [@property getter]

ditto, but host

password
const(string) password [@property getter]

ditto, but password

port
const(ushort) port [@property getter]

ditto, but port

user
const(string) user [@property getter]

Returns the name of the current user

Examples

auto conn = Connection("host=localhost dbname=testdb user=testuser");
//conn.exec ...

Meta