C++ template meta sql I wrote a wrapper around boost-mpl and transformed it to an sql engine. It is a compile time sql thing, not related to normal databases. I have not finished it completely. I have some problem with the interface.

A template meta program run in compile time. So input data always present in compile time that is why I organized the inputs to tables.

Tables are simple:

#define DEF_COLUMN(name) struct Col##name {}; \
                         template&lt;typename T&gt; struct name \
                         { typedef typename mpl::pair&lt;Col##name, T&gt; type; };
struct TestTable1
{
    DEF_COLUMN(PrimaryKey)
    DEF_COLUMN(Storage)
    DEF_COLUMN(BaseType)
    typedef mpl::vector&lt;ColPrimaryKey, ColStorage, ColBaseType&gt; header;

    // sample table
    // PrimaryKey | Storage |  BaseType
    // -----------+---------+-----------
    // CTypeA1    | int     | CTypeABase
    // CTypeA2    | string  | CTypeABase
    typedef mpl::vector&lt;
        mpl::map3&lt;PrimaryKey&lt;CTypeA1&gt;::type, Storage&lt;int&gt;::type,         BaseType&lt;CTypeABase&gt;::type&gt;,
        mpl::map3&lt;PrimaryKey&lt;CTypeA2&gt;::type, Storage&lt;std::string&gt;::type, BaseType&lt;CTypeABase&gt;::type&gt;
    &gt; type;
};


struct TestTable2
{
    DEF_COLUMN(Tag)
    DEF_COLUMN(Attribute)

    typedef mpl::vector&lt;
        mpl::map2&lt;Tag&lt;CTypeA1&gt;::type, Attribute&lt;CTypeB1&gt;::type&gt;,
        mpl::map2&lt;Tag&lt;CTypeA2&gt;::type, Attribute&lt;CTypeB1&gt;::type&gt;,
        mpl::map2&lt;Tag&lt;CTypeA2&gt;::type, Attribute&lt;CTypeB2&gt;::type&gt;
    &gt; type;
};


And you can make queries on it:

void TestQuery1()
{
    typedef Select&lt;
            Columns&lt;MplFunction&lt;Column&lt;TestTable1::ColStorage&gt;, mpl::sizeof_&gt; &gt;,
            From&lt;TestTable1,
               Join&lt;TestTable2, Equal&lt;TestTable1::ColPrimaryKey, TestTable2::ColTag&gt; &gt;
            &gt;
    &gt; TResultSet;
    DEBUG_TYPE((TResultSet));
    print_table&lt;TResultSet::type&gt;();
}


I have type debug macro and table print template helper.

So the above prints:

TResultSet =&gt; Select&lt;Columns&lt;MplFunction&lt;Column&lt;TestTable1::ColStorage&gt;, boost::mpl::sizeof_&gt;, mpl_::na&gt;, From&lt;TestTable1, Join&lt;TestTable2, Equal&lt;TestTable1::ColPrimaryKey, TestTable2::ColTag&gt; &gt; &gt;, Where&lt;True&gt; &gt;
+------------------------+
| TestTable1::ColStorage |
+------------------------+
| mpl_::size_t&lt;8ul&gt;      |
| mpl_::size_t&lt;8ul&gt;      |
| mpl_::size_t&lt;4ul&gt;      |
+------------------------+
3 rows in set


I have problem with the syntax, how can I improve this, to achive an easy to use meta query thing? I think I have to eliminate the Columns, Column thing from the 

Select&lt;Columns&lt;MplFunction&lt;Column&lt; ... 


expression. And I need good Row fetch things and others. Any idea? And where can I upload the source?

So this gives back compile time types, not runtime values.

void TestQuery3()
{
    typedef Select&lt;
            Columns&lt;Column&lt;TestTable1::ColStorage&gt; &gt;,
            From&lt;TestTable1&gt;,
            Where&lt;
                Equal&lt;TestTable1::ColPrimaryKey, CTypeA1&gt;
           &gt;
    &gt; TResultSet;
    DEBUG_TYPE((TResultSet));
    print_table&lt;TResultSet::type&gt;();

    // fetch one row
    typedef mpl::back&lt;TResultSet::type&gt;::type TResultRow;

    // get column ColStorage which is an int, TRes will be an int
    typedef mpl::at&lt;TResultRow, TestTable1::ColStorage&gt;::type TRes;

    // use the "int" type
    TRes testInteger = 9;

    // prints test value
    std::cout &lt;&lt; "testInteger:" &lt;&lt; testInteger &lt;&lt; std::endl;
}


this prints:

TResultSet =&gt; Select&lt;Columns&lt;Column&lt;TestTable1::ColStorage&gt;, mpl_::na&gt;, From&lt;TestTable1, mpl_::na&gt;, Where&lt;Equal&lt;TestTable1::ColPrimaryKey, CTypeA1&gt; &gt; &gt;
+------------------------+
| TestTable1::ColStorage |
+------------------------+
| int                    |
+------------------------+
1 row in set

testInteger:9
You my also want to have a look at how the Poco library achieves a similar thing using a Tuple class. From the docs:

typedef Poco::Tuple&lt;std::string, std::string, int&gt; Person;
typedef std::vector&lt;Person&gt; People;

People people;
people.push_back(Person("Bart Simpson", "Springfield", 12));
people.push_back(Person("Lisa Simpson", "Springfield", 10));

Statement insert(session);
insert &lt;&lt; "INSERT INTO Person VALUES(:name, :address, :age)",
    use(people), now;

Statement select(session);
select &lt;&lt; "SELECT Name, Address, Age FROM Person",
    into(people), now;

for (People::const_iterator it = people.begin(); it != people.end(); ++it)
{
    std::cout &lt;&lt; "Name: " &lt;&lt; it-&gt;get&lt;0&gt;() &lt;&lt; 
        ", Address: " &lt;&lt; it-&gt;get&lt;1&gt;() &lt;&lt; 
        ", Age: " &lt;&lt; it-&gt;get&lt;2&gt;() &lt;&lt;std::endl;
}