[std.database]
Steve Teale
steve.teale at britseyeview.com
Mon Oct 10 21:07:51 PDT 2011
On Mon, 10 Oct 2011 23:08:30 -0400, Johann MacDonagh wrote:
> I've written up a prototype for a "LINQ" style database querying
> mechanism in D (read about "LINQ to SQL" if you've never heard of it).
> Legally speaking, it has very little to do with LINQ, but the concept is
> similar.
>
> Basically, it allows you to write code like this:
>
> auto x = new SqliteConnection("mydata.db");
>
> foreach(y; x.MyTable.where("someField > 10")) {
> // y is a wrapper around Variant[string] with some opDispatch magic
> writeln(to!string(y.MyField));
> writeln(to!int(y.SomeOtherField));
> }
>
> Of course, "MyTable" is handled via opDispatch. The SqliteConnection
> doesn't care what tables are available in "mydata.db". You can also do
> much more. Such as:
>
> x.MyTable.startAt(20).limit(10).where("blah").select("somefield",
> "sometingElse");
>
> In addition, you should be able to do something like this (don't think
> I've implemented this yet):
>
> x.MyTable.select!MyStruct();
>
> Doing that would return a range of MyStruct structs, rather than the
> wrapper around Variant[string] like above. This would allow you to do:
>
> auto x = new SqliteConnection("mydata.db");
>
> foreach(y; x.MyTable.where("someField > 10").select!MyStruct()) {
> // y is a wrapper around Variant[string] with some opDispatch magic
> writeln(y.MyField); // No more needing the to! template
> writeln(y.SomeOtherField);
> }
>
> Of course, this would allow you to find typos in field names at compile
> time (provided your struct is kept in sync with the database), and means
> you don't have to go through the Variant[string] for all your database
> accesses.
>
> To implement this, a database "driver" would have to have a shared
> opDispatch implementation (perhaps done with a mixin or maybe with an
> abstract class), and it would have to be able to translate the "query"
> into a SQL query that works with their underlying database system.
>
> I have a working prototype somewhere that works with Sqlite, and it
> seems to work very nicely. Clearly a system like this shows off what D
> can do out of the box (opDispatch), and makes writing scripts very easy.
>
> Let me know if this is something you think should be part of
> std.database (or whatever we end up calling it).
I was lying in bed last night and realized that Variant[string] was
attractive for various purposes. It's kind of like a Javascript Object.
That and the possibilities with strictly name structs creates some
interesting possibilities.
More information about the Digitalmars-d
mailing list