sqlpp11是C++的类型安全SQL模版库。
sqlpp11是一个嵌入式领域特定语言(EDSL)的代表模版库:
为表和列定义类型
在编译的时候检查结构类型安全查询(语法错误,类型错误,命名错误,甚至是一些语义错误)
通过迭代query-specific结构得到解析结果
示例
CREATE TABLE foo ( id bigit, ame varchar(50), hasFu bool);假设有个数据库连接对象:
TabFoo foo;Db db(/* some argumets*/);// selectig zero or more results, iteratig over the resultsfor (cost auto& row : db(select(foo.ame, foo.hasFu).from(foo).where(foo.id > 17 ad foo.ame.like("%bar%")))){ if (row.ame.is_ull()) std::cerr << "ame is ull, will covert to empty strig" << std::edl; std::strig ame = row.ame; // strig-like fields are implicitly covertible to strig bool hasFu = row.hasFu; // bool fields are implicitly covertible to bool}// selectig ALL colums of a tablefor (cost auto& row : db(select(all_of(foo)).from(foo).where(foo.hasFu or foo.ame == "joker"))){ it64_t id = row.id; // umeric fields are implicitly covertible to umeric c++ types}// selectig zero or oe row, showig off with a alias:SQLPP_ALIAS_PROVIDER(cheese);if (cost auto& row = db(select(foo.ame.as(cheese)).from(foo).where(foo.id == 17))){ std::cerr << "foud: " << row.cheese << std::edl;}// selectig a sigle row with a sigle result:retur db(select(cout(foo.id)).from(foo).where(true)).frot().cout;Of course there are jois ad subqueries, more fuctios, order_by, group_by etc.These will be documeted soo.// A sample isertdb(isert_ito(foo).set(foo.id = 17, foo.ame = "bar", foo.hasFu = true));// A sample updatedb(update(foo).set(foo.hasFu = ot foo.hasFu).where(foo.ame != "obody"));// A sample deletedb(remove_from(foo).where(ot foo.hasFu));
评论