H1 2015 - db access support in Phobos
Vadim Lopatin via Digitalmars-d
digitalmars-d at puremagic.com
Tue Feb 3 06:41:01 PST 2015
On Tuesday, 3 February 2015 at 10:49:07 UTC, Robert burner
Schadek wrote:
> On Tuesday, 3 February 2015 at 10:33:25 UTC, Vadim Lopatin
> wrote:
>>
>> ORM operations are not a simple single query/statement.
>> They often use several queries to load dependent objects.
>
> make it an output range
>
>>
>> Of course, some CTFE/UDAs may be used for generation of field
>> list, but it is not very helpful IMO.
>
> IMO writing:
>
> foreach(it; db.select<User>("...")) {
> }
>
> is epic. you have entered std.(range|algorithm) land.
Implemented in v0.2.17 for select.
test:
auto ds = new ConnectionPoolDataSourceImpl(new
SQLITEDriver(), "ddbctest.sqlite");
auto conn = ds.getConnection();
scope(exit) conn.close();
Statement stmt = conn.createStatement();
scope(exit) stmt.close();
// test data preparation
stmt.executeUpdate("DROP TABLE IF EXISTS user");
stmt.executeUpdate("CREATE TABLE user (id INTEGER PRIMARY
KEY, name VARCHAR(255) NOT NULL, flags int null)");
stmt.executeUpdate(`INSERT INTO user (id, name, flags) VALUES
(1, "John", 5), (2, "Andrei", 2), (3, "Walter", 2), (4, "Rikki",
3), (5, "Iain", 0), (6, "Robert", 1)`);
// our POD struct
struct User {
long id;
string name;
int flags;
}
// let's execute select and nump query result
foreach(user; stmt.select!User.where("id < 6").orderBy("name
desc")) {
writeln("id:", user.id, " name:", user.name, " flags:",
user.flags);
}
Output is:
------------------
id:3 name:Walter flags:2
id:4 name:Rikki flags:3
id:1 name:John flags:5
id:5 name:Iain flags:0
id:2 name:Andrei flags:2
------------------
Where and orderBy are optional - without them always reads all
rows in default order.
Possible improvements: ability to specify field list to read only
necessary fields.
More information about the Digitalmars-d
mailing list