sql-build是一个基于c++11的SQL语句生成库,通过使用函数调用以生成SQL操作语句,也支持对象化的数据插入和更新。使用demo如下:
InsertModel i; i.insert("score", 100) .insert("name", std::string("six")) "age", (unsigned char)20, "address", "beijing", "create_time", time(NULL)) .into("user"); std::cout<<i.str()<<std::endl; // insert into user(score, name, age, address, create_time) values(100, 'six', 20, 'beijing', '2016-03-25 10:15:59') SelectModel s; s.select("id", "age", "name", "address") .from("user") .where(column("score") > 60 and (column("age") >= 20 or column("address").is_not_null())) .group_by("age") .having(column("age") > 10) .order_by("age desc") .limit(10) .offset(1); std::cout<<s<<std::endl; // select id, age, name, address from user where (score > 60) and ((age >= 20) or (address is not null)) group by age having age > 10 order by age desc limit 10 offset 1 std::vector<int> a = {1, 2, 3}; UpdateModel u; u.update("user") .set("name", "ddc") .set("age", 18, "score", 18) .set("address", "beijing") .where(column("id").in(a)); std::cout<<u<<std::endl; // update user set name = 'ddc', age = 18, score = 18, address = 'beijing' where id in (1, 2, 3) DeleteModel d; d._delete() .from("user") .where(column("id") == 1); std::cout<<d<<std::endl; // delete from user where id = 1 DataModel m; m["address"] = "chengdu"; m["age"] = 18; m.set("age", 20, "score", 80, "create_time", time(NULL)); u.reset(); u.update("user") .set(m); std::cout<<u<<std::endl; // update user set address = 'chengdu', age = 20, create_time = '2016-03-25 23:06:25', score = 80 i.reset(); i.insert(m) .into("user"); std::cout<<i<<std::endl; // insert into user(address, age, create_time, score) values('chengdu', 20, '2016-03-25 23:06:25', 80)点击空白处退出提示
评论