[std.database]

Steve Teale steve.teale at britseyeview.com
Sun Oct 9 09:15:29 PDT 2011


There was some discussion prior to this thread about the relative virtues 
of binding to structs or binding to arrays of Variants. I was thinking 
about this, and have experimented with Variants in my trial MySQL 
implementation. My conclusions below - do they make sense?

Using Variant to capture the output of prepared queries or provide input, 
as opposed to a struct, gets you out of the realm of what must be 
determined at compile time, but only at the expense of extra DB server 
round trip(s).

If you want to use them in a deterministic way with a database table of 
known and stable structure you have to bind them. To bind them you must 
be able to determine their type. But with Variants you can't do that 
until they are initialized. So, with a struct you must have something 
like:

struct MyTableQuery42
{
   byte col1;
   float col2;
   char[] col3;
}

This can be bound for output without doing any explicit initialization of 
an instance, since you can write a template function to bind it that 
discovers everything you need to know about the struct at compile time.

If using variants for a set of out parameters you must have something 
equivalent to:

Variant[3] va;
va[0] = cast(byte) 0;
va[1] = 0.0F;
va[2] = cast(char[]) [];

So you have to have exactly the same information at compile time for the 
Variant array as you do for the struct - you still have to specify a set 
of types.

The difference is that if you have prepared a statement, you can go to 
the server and ask for the relevant metadata. With a struct you can use 
this to check if the struct is a match for the query. With an array of 
Variants you can make if conform to the query.

However, in a large number of cases, using the struct you won't need to 
bother with the metadata, because you 'know' the types of the query 
result. You don't have to bother with them for the Variant array either, 
but in that case you have to provide a function like

Variant[] MyTableQuery42Init() { ... }

which 'knows' the same stuff.

It's probably true to say that the syntax/semantics of the interface will 
suck slightly more in the Variant case than in the struct case.

Steve


More information about the Digitalmars-d mailing list