[std.database] at compile time

Justin Whear justin at economicmodeling.com
Mon Oct 17 10:01:20 PDT 2011


Sure, here it is: http://pastebin.com/KSyuk8HN

Usage would be something like:

import std.stdio;
import ccb.io.automap_table;

mixin( MapTables!(import("school_system.sql"), true) );

void main()
{
    Student a = new Student;
    a.student_name = "John Smith";
    a.term = 1;
    writeln(a.student_name, ", ", a.term);
    students.insert(new Student());
    students.commit();
}

The sql file can be generated like so:
$ mysqldump -d -h dbhost -u username -p password database_name


Note that it's super hacky, literally written on the spur of the moment. I 
have a much cleaner, more robust version, but it currently sends the 
compiler into an infinite loop and I haven't touched it in a couple months.

Justin


> Am 14.10.2011 22:29, schrieb Justin Whear:
>> This is actually possible now. I wrote a little CTFE parser for CREATE
>> TABLE... statements, used mysql to dump a db, then used import() to
>> include the dump and parse it at compile-time. I was actually using it to
>> generate classes which mapped to the contents of each table (i.e. a
>> "things" table would result in a "Thing" class which mapped the fields as
>> properties). Obviously you need to keep the db dump up to date via an
>> external tool.
> 
> Cool! Hesitate to share the code?
> 


More information about the Digitalmars-d mailing list