d-dbapi ALPHA-2 released

Myron Alexander someone at somewhere.com
Mon Jun 11 15:34:15 PDT 2007


Christian,

I have merged the two functions together as per your suggestion.

This is the final version:

> /* Fetch a type aware record from the database and populate the struct.
>  *
>  * With thanks to Chris Nicholson-Sauls, Lutger Blijdestijn, and Christian Kam.
>  */
> T fetchstruct(T) () {
> 
>    static if (is (T == struct)) {
> 
>       T t;
> 
>       /* Check if last struct field is a bool array of correct size to store
>        * null flags.
>        */ 
>       static if (is (typeof (t.tupleof[$-1]) == bool[t.tupleof.length-1])) {
>          const bool hasNullFlags = true;
>       } else {
>          const bool hasNullFlags = false;
>       }
> 
>       /* When the last field is a bool array of the correct size to store
>        * null flags, exclude from the type tuple.
>        */
>       static if (hasNullFlags) {
>          auto r = fetchone!(FieldTypeTuple!(T)[0..length-1])();
> 
>       } else {
>          auto r = fetchone!(FieldTypeTuple!(T))();
>       }
> 
>       /* Populate the structure from the fetched data.
>        */
>       foreach (i, v; r.t) {
>          // According to Christian, using v may not work every time.
>          t.tupleof[i] = r.t[i];
>       }
> 
>       /* If the structure contains nullFlags, populate it.
>        */
>       static if (hasNullFlags) {
> 
>          foreach (i, nullFlagValue; r.nullFlag) {
>             /* I use the name of the field instead of tupleof to guarantee
>              * the name so helper functions can be written.
>              */
>             t.nullFlag[i] = nullFlagValue;
>          }
>       }
>  
>       return t;
> 
>    } else {
>       static assert (0, "Invalid type, must be a struct.");
>    }
> }


Thanks,

Myron.



More information about the Digitalmars-d-announce mailing list