DBRow

Data row returned from database servers.

DBRow may be instantiated with any number of arguments. It subtypes base type which depends on that number:

Number of argumentsBase type
0Variant[]

It is default dynamic row, which can handle arbitrary number of columns and any of their types.
1
struct S { int i, float f }

DBRow!int rowInt;
DBRow!S rowS;
DBRow!(Tuple!(string, bool)) rowTuple;
DBRow!(int[10]) rowSA;
DBRow!(bool[]) rowDA;
Specs itself, more precisely Specs[0]
>= 2
DBRow!(int, string) row1; // two arguments
DBRow!(int, "i") row2; // two arguments
Tuple!Specs

If there is only one argument, the semantics depend on its type:

TypeSemantics
base type, such as intRow contains only one column of that type
structRow columns are mapped to fields of the struct in the same order
TupleRow columns are mapped to tuple fields in the same order
static arrayRow columns are mapped to array items, they share the same type
dynamic arraySame as static array, except that column count may change during runtime

Note: String types are treated as base types.

There is an exception for RDBMSes which are capable of returning arrays and/or composite types. If such a database server returns array or composite in one column it may be mapped to DBRow as if it was many columns. For example:

struct S { string field1; int field2; }
DBRow!S row;

In this case row may handle result that either:

  • has two columns convertible to respectively, string and int
  • has one column with composite type compatible with S

DBRow's instantiated with dynamic array (and thus default Variant[]) provide additional bracket syntax for accessing fields:

auto value = row["columnName"];

There are cases when result contains duplicate column names. Normally column name inside brackets refers to the first column of that name. To access other columns with that name, use additional index parameter:

auto value = row["columnName", 1]; // second column named "columnName"

auto value = row["columnName", 0]; // first column named "columnName"
auto value = row["columnName"]; // same as above
struct DBRow (
Specs...
) {}

Alias This

base

Members

Aliases

T
alias T = Variant[]
Undocumented in source.
T
alias T = Specs[0]
Undocumented in source.
T
alias T = Tuple!Specs
Undocumented in source.
fieldTypes
alias fieldTypes = ArrayTypeTuple!T
Undocumented in source.
fieldTypes
alias fieldTypes = FieldTypeTuple!T
Undocumented in source.
fieldTypes
alias fieldTypes = TypeTuple!T
Undocumented in source.

Functions

opIndex
ElemType opIndex(string column, size_t index)
Undocumented in source. Be warned that the author may not have intended to support it.
opIndex
ElemType opIndex(string column)
Undocumented in source. Be warned that the author may not have intended to support it.
opIndex
ElemType opIndex(size_t index)
Undocumented in source. Be warned that the author may not have intended to support it.
opIndexAssign
ElemType opIndexAssign(ElemType value, string column, size_t index)
Undocumented in source. Be warned that the author may not have intended to support it.
opIndexAssign
ElemType opIndexAssign(ElemType value, string column)
Undocumented in source. Be warned that the author may not have intended to support it.
opIndexAssign
ElemType opIndexAssign(ElemType value, size_t index)
Undocumented in source. Be warned that the author may not have intended to support it.
set
void set(U value)
Undocumented in source. Be warned that the author may not have intended to support it.
set
void set(T value)
Undocumented in source. Be warned that the author may not have intended to support it.
setLength
void setLength(size_t length)
Undocumented in source. Be warned that the author may not have intended to support it.
setNull
void setNull(size_t index)
Undocumented in source. Be warned that the author may not have intended to support it.
setNull
void setNull()
Undocumented in source. Be warned that the author may not have intended to support it.
setNull
void setNull()
Undocumented in source. Be warned that the author may not have intended to support it.
toString
string toString()
Undocumented in source. Be warned that the author may not have intended to support it.

Manifest constants

hasStaticLength
enum hasStaticLength;
Undocumented in source.
hasStaticLength
enum hasStaticLength;
Undocumented in source.
hasStaticLength
enum hasStaticLength;
Undocumented in source.

Mixins

__anonymous
mixin elmnt!T
Undocumented in source.

Mixin templates

elmnt
mixintemplate elmnt(U : U[])
Undocumented in source.

Static functions

checkReceivedFieldCount
void checkReceivedFieldCount(int fieldCount)

Checks if received field count matches field count of this row type.

Templates

ArrayTypeTuple
template ArrayTypeTuple(AT : U[N], U, size_t N)
Undocumented in source.

Variables

base
T base;
Undocumented in source.
columnToIndex
ColumnToIndexDelegate columnToIndex;
Undocumented in source.

Examples

Default untyped (dynamic) DBRow:

DBRow!() row1;
DBRow!(Variant[]) row2;

assert(is(typeof(row1.base == row2.base)));

auto cmd = new PGCommand(conn, "SElECT typname, typlen FROM pg_type");
auto result = cmd.executeQuery;

foreach (i, row; result)
{
    writeln(i, " - ", row["typname"], ", ", row["typlen"]);
}

result.close;

DBRow with only one field:

DBRow!int row;
row = 10;
row += 1;
assert(row == 11);

DBRow!Variant untypedRow;
untypedRow = 10;

DBRow with more than one field:

struct S { int i; string s; }
alias Tuple!(int, "i", string, "s") TS;

// all three rows are compatible
DBRow!S row1;
DBRow!TS row2;
DBRow!(int, "i", string, "s") row3;

row1.i = row2.i = row3.i = 10;
row1.s = row2.s = row3.s = "abc";

// these two rows are also compatible
DBRow!(int, int) row4;
DBRow!(int[2]) row5;

row4[0] = row5[0] = 10;
row4[1] = row5[1] = 20;

Advanced example:

enum Axis { x, y, z }
struct SubRow1 { string s; int[] nums; int num; }
alias Tuple!(int, "num", string, "s") SubRow2;
struct Row { SubRow1 left; SubRow2[] right; Axis axis; string text; }

auto cmd = new PGCommand(conn, "SELECT ROW('text', ARRAY[1, 2, 3], 100),
                                ARRAY[ROW(1, 'str'), ROW(2, 'aab')], 'x', 'anotherText'");

auto row = cmd.executeRow!Row;

assert(row.left.s == "text");
assert(row.left.nums == [1, 2, 3]);
assert(row.left.num == 100);
assert(row.right[0].num == 1 && row.right[0].s == "str");
assert(row.right[1].num == 2 && row.right[1].s == "aab");
assert(row.axis == Axis.x);
assert(row.s == "anotherText");

Meta