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
1 struct S { int i, float f }
2 
3 DBRow!int rowInt;
4 DBRow!S rowS;
5 DBRow!(Tuple!(string, bool)) rowTuple;
6 DBRow!(int[10]) rowSA;
7 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...
) {
T base;
enum hasStaticLength;
ColumnToIndexDelegate columnToIndex;
enum hasStaticLength;
enum hasStaticLength;
}

Alias This

base

Members

Static functions

checkReceivedFieldCount
void checkReceivedFieldCount(int fieldCount)

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

Examples

Default untyped (dynamic) DBRow:

1 DBRow!() row1;
2 DBRow!(Variant[]) row2;
3 
4 assert(is(typeof(row1.base == row2.base)));
5 
6 auto cmd = new PGCommand(conn, "SElECT typname, typlen FROM pg_type");
7 auto result = cmd.executeQuery;
8 
9 foreach (i, row; result)
10 {
11     writeln(i, " - ", row["typname"], ", ", row["typlen"]);
12 }
13 
14 result.close;

DBRow with only one field:

1 DBRow!int row;
2 row = 10;
3 row += 1;
4 assert(row == 11);
5 
6 DBRow!Variant untypedRow;
7 untypedRow = 10;

DBRow with more than one field:

1 struct S { int i; string s; }
2 alias Tuple!(int, "i", string, "s") TS;
3 
4 // all three rows are compatible
5 DBRow!S row1;
6 DBRow!TS row2;
7 DBRow!(int, "i", string, "s") row3;
8 
9 row1.i = row2.i = row3.i = 10;
10 row1.s = row2.s = row3.s = "abc";
11 
12 // these two rows are also compatible
13 DBRow!(int, int) row4;
14 DBRow!(int[2]) row5;
15 
16 row4[0] = row5[0] = 10;
17 row4[1] = row5[1] = 20;

Advanced example:

1 enum Axis { x, y, z }
2 struct SubRow1 { string s; int[] nums; int num; }
3 alias Tuple!(int, "num", string, "s") SubRow2;
4 struct Row { SubRow1 left; SubRow2[] right; Axis axis; string text; }
5 
6 auto cmd = new PGCommand(conn, "SELECT ROW('text', ARRAY[1, 2, 3], 100),
7                                 ARRAY[ROW(1, 'str'), ROW(2, 'aab')], 'x', 'anotherText'");
8 
9 auto row = cmd.executeRow!Row;
10 
11 assert(row.left.s == "text");
12 assert(row.left.nums == [1, 2, 3]);
13 assert(row.left.num == 100);
14 assert(row.right[0].num == 1 && row.right[0].s == "str");
15 assert(row.right[1].num == 2 && row.right[1].s == "aab");
16 assert(row.axis == Axis.x);
17 assert(row.s == "anotherText");

Meta