首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++模板元sql

C++模板元sql
EN

Code Review用户
提问于 2011-05-04 19:27:33
回答 1查看 1.4K关注 0票数 5

我编写了一个围绕boost-mpl的包装器,并将其转换为sql引擎。这是一个编译时sql的问题,与普通数据库无关。我还没有完全完成。我的界面有点问题。

在编译时运行的模板元程序。因此,输入数据总是在编译时出现的,这就是为什么我将输入组织到表中。

表格很简单:

代码语言:javascript
复制
#define DEF_COLUMN(name) struct Col##name {}; \
                         template<typename T> struct name \
                         { typedef typename mpl::pair<Col##name, T> type; };
struct TestTable1
{
    DEF_COLUMN(PrimaryKey)
    DEF_COLUMN(Storage)
    DEF_COLUMN(BaseType)
    typedef mpl::vector<ColPrimaryKey, ColStorage, ColBaseType> header;

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


struct TestTable2
{
    DEF_COLUMN(Tag)
    DEF_COLUMN(Attribute)

    typedef mpl::vector<
        mpl::map2<Tag<CTypeA1>::type, Attribute<CTypeB1>::type>,
        mpl::map2<Tag<CTypeA2>::type, Attribute<CTypeB1>::type>,
        mpl::map2<Tag<CTypeA2>::type, Attribute<CTypeB2>::type>
    > type;
};

您可以对其进行查询:

代码语言:javascript
复制
void TestQuery1()
{
    typedef Select<
            Columns<MplFunction<Column<TestTable1::ColStorage>, mpl::sizeof_> >,
            From<TestTable1,
               Join<TestTable2, Equal<TestTable1::ColPrimaryKey, TestTable2::ColTag> >
            >
    > TResultSet;
    DEBUG_TYPE((TResultSet));
    print_table<TResultSet::type>();
}

我有类型调试宏和表打印模板助手。

所以上面的指纹:

代码语言:javascript
复制
TResultSet => Select<Columns<MplFunction<Column<TestTable1::ColStorage>, boost::mpl::sizeof_>, mpl_::na>, From<TestTable1, Join<TestTable2, Equal<TestTable1::ColPrimaryKey, TestTable2::ColTag> > >, Where<True> >
+------------------------+
| TestTable1::ColStorage |
+------------------------+
| mpl_::size_t<8ul>      |
| mpl_::size_t<8ul>      |
| mpl_::size_t<4ul>      |
+------------------------+
3 rows in set

我有问题的语法,我如何改进这一点,以实现一个易于使用的元查询的事情?我想我得把列,列从

代码语言:javascript
复制
Select<Columns<MplFunction<Column< ... 

表达式。我需要好的行去拿东西和其他东西。有什么想法吗?我在哪里可以上传源呢?

因此,这提供了编译时类型,而不是运行时值。

代码语言:javascript
复制
void TestQuery3()
{
    typedef Select<
            Columns<Column<TestTable1::ColStorage> >,
            From<TestTable1>,
            Where<
                Equal<TestTable1::ColPrimaryKey, CTypeA1>
           >
    > TResultSet;
    DEBUG_TYPE((TResultSet));
    print_table<TResultSet::type>();

    // fetch one row
    typedef mpl::back<TResultSet::type>::type TResultRow;

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

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

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

这些指纹:

代码语言:javascript
复制
TResultSet => Select<Columns<Column<TestTable1::ColStorage>, mpl_::na>, From<TestTable1, mpl_::na>, Where<Equal<TestTable1::ColPrimaryKey, CTypeA1> > >
+------------------------+
| TestTable1::ColStorage |
+------------------------+
| int                    |
+------------------------+
1 row in set

testInteger:9
EN

回答 1

Code Review用户

发布于 2011-05-04 19:50:16

我还想看看Poco库是如何使用Tuple类实现类似的功能的。来自文档

代码语言:javascript
复制
typedef Poco::Tuple<std::string, std::string, int> Person;
typedef std::vector<Person> People;

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

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

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

for (People::const_iterator it = people.begin(); it != people.end(); ++it)
{
    std::cout << "Name: " << it->get<0>() << 
        ", Address: " << it->get<1>() << 
        ", Age: " << it->get<2>() <<std::endl;
}
票数 3
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/2238

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档