H1 2015 - db access support in Phobos

Vadim Lopatin via Digitalmars-d digitalmars-d at puremagic.com
Tue Feb 3 02:33:23 PST 2015


On Tuesday, 3 February 2015 at 09:16:14 UTC, Robert burner 
Schadek wrote:
>> But for such high level DB library must be based on some lower 
>> level DB API (connector).
>> Like JDBC for JPA or Hibernate in Java.
>
> really, does it? there is no need for an abstraction layer. you 
> can have functions generate the correct source for mysql, 
> sqlite, you name it. e.g. just generate the mysql statement at 
> CT and pass the pointer to the sql c function. pass the 
> parameter. done. Dream big.

ORM operations are not a simple single query/statement.
They often use several queries to load dependent objects.

>
>> D may offer more convenient way for reading of field values ...
>> not used), but it doesn't know column types anyway.
>
> I don't get your point

E.g. you can write template method for ResultSet for easy getting 
column values into variables. E.g. read method can have variable 
number of ref args to place read data to. Template will check 
types of args, and do necessary conversions of data when reading.

// read into vars
long id;
string name;
int flags;
rs = statement.executeQuery("SELECT id, name, flags FROM user 
WHERE flags = 5 ORDER BY name");
while (rs.next(id, name, flags)) {
    writeln(id, " ", name, " ", flags);
}

or

// read into structure or class fields
struct User {
     long id;
     string name;
     int flags;
}
User row;
rs = statement.executeQuery("SELECT id, name, flags FROM user  
WHERE flags = 5 ORDER BY name");
while (rs.next(row)) {
    writeln(row.id, " ", row.name, " ", row.flags);
}

Of course, some CTFE/UDAs may be used for generation of field 
list, but it is not very helpful IMO.

// automatically generated SQL - based on type of struct/class 
params
User row;
rs = statement.executeSelect(row, "WHERE flags = 5 ORDER BY 
name");
// actually, sql "SELECT id, name, flags FROM user WHERE flags = 
5 ORDER BY name" will be used
while (rs.next(row)) {
    // every time row is read, struct fields will be updated with 
new values
    writeln(row.id, " ", row.name, " ", row.flags);
}

UDAs can be used to change mapping of class/struct name to table 
name, field names to column names, mark some fields/properties as 
non persistent.



More information about the Digitalmars-d mailing list