Checks if received field count matches field count of this row type.
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");
Data row returned from database servers.
DBRow may be instantiated with any number of arguments. It subtypes base type which depends on that number:
It is default dynamic row, which can handle arbitrary number of columns and any of their types.
If there is only one argument, the semantics depend on its type:
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:
In this case row may handle result that either:
DBRow's instantiated with dynamic array (and thus default Variant[]) provide additional bracket syntax for accessing fields:
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: