RelationProxy

A structure proxying to the actual Postgres table.

Allows short and readable querying, returning actual records or arrays of them. first, last, and all will be cached if appropriate, the rest will execute a query whenever a result is needed. Most functions return a reference to the same object, allowing you to chain them. Builder pattern, really.

Can be implicitly converted to an array of the requested structure.

Mostly meant to be used with RelationMixin, but can be used manually.

Constructors

this
this(ref Connection connection)

Basic constructor. RelationProxy requires a connection to operate on.

Alias This

all

Members

Aliases

and
alias and = where

Convenience alias, allows us to do proxy.where(..).and(...)

Functions

count
long count(string col = "*")

Selects the count of records with current filters. Default is *, but any column can be specified as a parameter. The column name is not further escaped.

find
Nullable!T find(U param)

Same as above, but always searches by the primary key

findBy
Nullable!T findBy(U[string] filters)

Finds a single record matching the filters. Equivalent to where(...).first.

insert
T insert(ref T record)

Inserts a new record, takes the record as a reference in order to update its PK value. Does not update any other values.

reload
auto ref reload()

Reloads the content with existing filters.

remove
auto remove(Tpk id)

Removes a single record, filtering it by the primary key's value.

removeAll
auto removeAll()

Simply deletes all the records matching the filters.

save
bool save(T record)

Updates the given record in the DB with all the current values.

updateAll
auto updateAll(U[string] updates)

Update all the records matching filters to new values from the AA.

where
auto ref where(U[string] filters)

Specifies filters according to the given AA. Filters will be joined with AND.

where
auto ref where(string filter, U params)

Same as above, but allowing you to specify a completely custom filter. The supplied string will be wrapped in (), can be called multiple times to specify multiple filters.

Properties

all
T[] all [@property getter]

Returns the actual content, executing the query if data has not yet been fetched from the database.

dup
RelationProxy!T dup [@property getter]

Makes a copy of just the filters, not any content;

first
Nullable!T first [@property getter]

Fetches the first record matching the filters.

last
string last [@property setter]

Same as first, but defaults to desceding order, giving you the last match.

or
auto ref or [@property getter]

Inserts an OR seperator between the filters.

toString
string toString [@property getter]

A basic toString implementation, mostly here to prevent querying when the object is implicitly converted to a string.

Examples

1 struct User
2 {
3 	// provides where, find, findBy, etc on the User struct
4 	mixin RelationMixin;
5 
6 	@PK @serial int id		string username;
7 }
8 
9 auto user1 = User.where(["id": 1]).first;
10 // Or better yet. Search directly by PK
11 auto betterUser1 = User.find(1);
12 // Delete some users
13 long nDeleted = User.where("username LIKE 'somethi%'").removeAll();

Meta