[GSOC] Database API draft proposal

Piotr Szturmaj bncrbme at jadamspam.pl
Wed Apr 6 03:00:17 PDT 2011


Masahiro Nakagawa wrote:
>> [1] https://github.com/pszturmaj/ddb 
>> http://pszturmaj.github.com/ddb/db.html
>>
> 
> Hmm.. In what way is your new module different from DDBI?
> What's the new features?

I should state here that work on DDB is in progress, so it's subject to
change. However, notable differences from DDBI are typed rows, where one
can map structs/tuples/arrays or base types directly to the result. For
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; // map result to Row struct

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");

This is done without intermediate state, such as Variant. In case of
PostgreSQL binary encoding, values are directly read into struct fields.
Also, typed rows form the basis of the ORM.

Dynamic rows are also first class citizens:

cmd = new PGCommand(conn, "SELECT * FROM table");
auto result = cmd.executeQuery; // range of DBRow!(Variant[])

foreach (row; result)
{
    writeln(row["column"]);
}

result.close;

Some parts of the Connection/Command classes are currently modeled after
ADO.NET.


More information about the Digitalmars-d mailing list