std.database

miazo via Digitalmars-d digitalmars-d at puremagic.com
Thu May 28 00:41:04 PDT 2015


On Thursday, 28 May 2015 at 05:12:34 UTC, Vadim Lopatin wrote:
> On Thursday, 28 May 2015 at 05:00:30 UTC, Rikki Cattermole 
> wrote:
>> On 28/05/2015 4:57 p.m., Robert burner Schadek wrote:
>>> On Thursday, 28 May 2015 at 04:45:52 UTC, Erik Smith wrote:
>>>>> Shouldn't the statement be reusable?
>>>>
>>>> Yes it should.  I added this use case:
>>>>
>>>> auto stmt = con.statement("insert into table values(?,?)");
>>>> stmt.execute("a",1);
>>>> stmt.execute("b",2);
>>>> stmt.execute("c",3);
>>>>
>>>
>>> struct Table;
>>>
>>> Table a, b, c;
>>>
>>> con.insert!Table(a);
>>> ...
>>>
>>> if you use CTFE to create the statement string there is no 
>>> reason to
>>> reuse it.
>>> it will be string literal, that's even better! Think Big. 
>>> Think D
>>>
>>> the other code is Java not D
>>
>> Then you open up table names, property serialization ext. ext.
>> Please no.
>> That is an ORM's job. I'm saying this from experience.
>
> Similar project: DDBC https://github.com/buggins/ddbc
> Inspired by Java JDBC API.
> Currently supports MySQL, PostreSQL, SQLite.

Some time ago I tried a similar thing (not to build a library but 
rather to learn D a bit), however I never had time to finish it. 
A brief sample of the interface for my Result object:


1. Result's structure not known in advance

// execute simple query using connection c and assign results to r
auto r = c.execute("SELECT int_column, string_column FROM 
Table;");

// output column names
writeln(r.names[0], "\t", r.names[1]);

// iterate thru result and output values for each row
for (; !r.empty; r.popFront()) {
	writeln(r.front[0], "\t", r.front[1]);
}


2. Result's structure known in advance

// define Sample_Record struct (struct data types must conform to 
column types from query)
struct Sample_Record {
	int i;
	string s;
}

// execute simple query using connection c and assign results to r
auto r = c.execute("SELECT int_column, string_column FROM 
Table;");

// assign results to sample_record struct
auto sample_record = r.getRecord!(Sample_Record);

// output sample_record struct values
writeln(sample_record.i, "\t", sample_record.s);


More information about the Digitalmars-d mailing list