微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

sqlpp11 C++ 安全 SQL 模版库

程序名称:sqlpp11

授权协议: BSD

操作系统: 跨平台

开发语言: C/C++

sqlpp11 介绍

sqlpp11 是 C++ 的类型安全 sql 模版库。

sqlpp11 是一个嵌入式领域特定语言(EDSL)的代表模版库:

  • 为表和列定义类型

  • 在编译的时候检查结构类型安全查询(语法错误,类型错误,命名错误,甚至是一些语义错误

  • 通过迭代 query-specific 结构得到解析结果

示例

CREATE TABLE foo (
    id bigint,
    name varchar(50),
    hasFun bool
);

假设有个数据库连接对象:

TabFoo foo;
Db db(/* some arguments*/);
// selecting zero or more results, iterating over the results
for (const auto& row : db(select(foo.name, foo.hasFun).from(foo).where(foo.id > 17 and foo.name.like("%bar%"))))
{
    if (row.name.is_null())
        std::cerr << "name is null, will convert to empty string" << std::endl;
    std::string name = row.name;   // string-like fields are implicitly convertible to string
    bool hasFun = row.hasFun;          // bool fields are implicitly convertible to bool
}
// selecting ALL columns of a table
for (const auto& row : db(select(all_of(foo)).from(foo).where(foo.hasFun or foo.name == "joker")))
{
    int64_t id = row.id; // numeric fields are implicitly convertible to numeric c++ types
}
// selecting zero or one row, showing off with an alias:
sqlPP_ALIAS_PROVIDER(cheese);
if (const auto& row = db(select(foo.name.as(cheese)).from(foo).where(foo.id == 17)))
{
    std::cerr << "found: " << row.cheese << std::endl;
}
// selecting a single row with a single result:
return db(select(count(foo.id)).from(foo).where(true)).front().count;
Of course there are joins and subqueries, more functions, order_by, group_by etc.
These will be documented soon.
// A sample insert
db(insert_into(foo).set(foo.id = 17, foo.name = "bar", foo.hasFun = true));
// A sample update
db(update(foo).set(foo.hasFun = not foo.hasFun).where(foo.name != "nobody"));
// A sample delete
db(remove_from(foo).where(not foo.hasFun));

sqlpp11 官网

https://github.com/rbock/sqlpp11

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐