Can your programming language do this?

Andrew Wiley debio264 at gmail.com
Tue Jan 25 08:24:39 PST 2011


On Tue, Jan 25, 2011 at 9:48 AM, Adam Ruppe <destructionator at gmail.com>wrote:

> Piotr Szturmaj wrote:
> > I already have working postgresql row querying. There are typed and
> > untyped rows. Untyped row is DBRow!(Variant[]), typed rows are
> > encapsulated using struct or tuple, for example:
>
> Very nice! I did something similar in two cases: one is the
> byStruct in mysql.d (currently commented out - it was rather buggy
> anyway) and the other is the next(T..) function in sqlite.
>
>
> byStruct translated a textual mysql row into the given struct, by
> translating the names:
>
> foreach(member; __traits(allMembers, S)) {
>        S.member = to!(typeof(S.member))(db_row[member]);
> }
>  return S;
>
> (pseudocode just to show the idea).
>
>
> My implementation was always a little buggy though, so I didn't
> use it much. I still like the idea though!
>
>
> The other thing is to fill in a bunch of local variables. This
> was my first attempt at database in D, written before we had
> immutable and half of Phobos. You can see some of it in sqlite.d:
>
> auto db_statement = db.prepare("SELECT id, name FROM users");
>
> string name;
> int id;
>
> db_statement.execute();
>
> while(db_statement.next(id, name)) {
>      // the variables id and name are now filled in with the row
> }
>
>
> It would fill them in positionally, and coerce the type from the
> db to match the variables passed in. I don't remember why I abandoned
> that. I think it was just a long gap between writing it and the next
> time I needed a database, which was mysql, so the sqlite module didn't
> get reused.
>
> It's a little tedious to use anyway though. I prefer having all
> the variables in the scope of the loop, rather than outside like
> it is here.
>
>
>
>
> But anyway, returning a Tuple!() is something I've never tried,
> I like the idea.
>
>
> > I've also written simple ORM mapping - creating and managing tables
> > based on struct layout. I see your solution is in opposite -
> > creating structs/classes based on SQL CREATE scripts.
>
> Yeah, the main reason there is all my attempts to go struct > sql
> ended up being pretty sucky.
>
> I'd start with a basic struct. Getting it to a simple create table
> command is pretty straightforward (hey, remember that post a month
> or two ago where someone did that in Go and was like "yay Go rocks!"
> That thread is actually what inspired the subject to this thread.)
>
> But, a real database has a lot of constraints on top of that -
> field lengths, foreign keys, checks, and so on.
>
> I tried two approaches: one was magic. Add stuff based on the names
> and types, so assume "int id" is a primary key, for example.
>
> Didn't work in practice. What about a table with a primary key
> that spans two columns?
>

I know I keep beating this horse, but in other languages, this is the
textbook example for why annotations are useful. Adding metadata to code,
even if it can only be examined at compile time, can be immensely useful.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20110125/d300db76/attachment.html>


More information about the Digitalmars-d mailing list