☰
C++ SQL ORM
2026/11/2 10:42:53 网站建设 项目流程

测试代码

// // Created by www on 2024/8/7. // #include "sqlitepp/database.h" #include "sqlitepp/condition.h" #include <iostream> using namespace sqlitepp; using namespace sqlitepp::literals; enum class test_enum { hello }; void testCondition() { std::string s; { // == auto q = "t"_c == test_enum::hello; auto p = q.as_partial(); std::cout << p.query << std::endl; std::holds_alternative<orm::db_integer_type>(p.params[0]); } { // != auto q = "t"_c != test_enum::hello; auto p = q.as_partial(); std::cout << p.query << std::endl; } { // NOT auto q = !("t"_c == "hello"); auto p = q.as_partial(); std::cout << p.query << std::endl; } { // is NULL auto q = "t"_c == nullptr; auto p = q.as_partial(); std::cout << p.query << std::endl; } { // is NOT NULL auto q = "t"_c != nullptr; auto p = q.as_partial(); std::cout << p.query << std::endl; } { // AND auto q = "t"_c == "hello" && "t2"_c == "test"; auto p = q.as_partial(); std::cout << p.query << std::endl; } { // OR auto q = "t"_c == "hello" || "t2"_c == "test"; auto p = q.as_partial(); std::cout << p.query << std::endl; } { // BETWEEN auto q = "t"_c.between(static_cast<int64_t>(10),static_cast<int64_t>(20)); auto p = q.as_partial(); std::cout << p.query << std::endl; } { // NOT BETWEEN auto q = "t"_c.not_between(static_cast<int64_t>(10),static_cast<int64_t>(20)); auto p = q.as_partial(); std::cout << p.query << std::endl; } { // LIKE auto q = "t"_c.like("test%"); auto p = q.as_partial(); std::cout << p.query << std::endl; } { // GLOB auto q = "t"_c.glob("test%"); auto p = q.as_partial(); std::cout << p.query << std::endl; } { // > auto q = "t"_c > static_cast<int64_t>(10); auto p = q.as_partial(); std::cout << p.query << std::endl; } { // > auto q = "t"_c >= static_cast<int64_t>(10); auto p = q.as_partial(); std::cout << p.query << std::endl; } { // > auto q = "t"_c < static_cast<int64_t>(10); auto p = q.as_partial(); std::cout << p.query << std::endl; } { // > auto q = "t"_c <= static_cast<int64_t>(10); auto p = q.as_partial(); std::cout << p.query << std::endl; } } #include "include/sqlitepp/database.h" #include "include/sqlitepp/orm.h" using namespace sqlitepp; using namespace sqlitepp::orm; struct my_entity : orm::entity { enum class e_test { hello = 10, hello2 = 20 }; e_test t {e_test::hello}; std::optional<int64_t> test_optional {}; using entity::entity; static const orm::class_info _class_info; const orm::class_info& get_class_info() const noexcept override { return _class_info; } }; const orm::class_info my_entity::_class_info = orm::builder<my_entity>("e") .field("t", &my_entity::t) .field("test_optional", &my_entity::test_optional) .build(); void testORM() { { // ReadWriteEnum database db; my_entity e(db); auto& info = e.get_class_info(); auto* field = info.get_field_by_name("t"); auto val = field->getter(&e); std::cout << std::get<db_integer_type>(val) << std::endl; e.t = my_entity::e_test::hello2; val = field->getter(&e); std::cout << std::get<db_integer_type>(val) << std::endl; val = db_integer_type{10}; field->setter(&e, val); std::cout << (int)e.t << std::endl; } { // ReadWriteOptional database db; my_entity e(db); auto& info = e.get_class_info(); auto* field = info.get_field_by_name("test_optional"); auto val = field->getter(&e); e.test_optional = 100; val = field->getter(&e); std::cout << std::get<db_integer_type>(val) << std::endl; val = db_integer_type{10}; field->setter(&e, val); std::cout << e.test_optional.value() << std::endl; val = db_null_type{}; field->setter(&e, val); std::cout << e.test_optional.has_value() << std::endl; } { // IsModified database db; my_entity e(db); auto& info = e.get_class_info(); db.exec(generate_create_table(info)); std::cout << (e.is_modified()) << std::endl; e.save(); std::cout << (e.is_modified()) << std::endl; e.test_optional = 1337; std::cout << (e.is_modified()) << std::endl; e.save(); std::cout << (e.is_modified()) << std::endl; e.test_optional.reset(); std::cout << (e.is_modified()) << std::endl; e.reset(); std::cout << (e.is_modified()) << std::endl; std::cout << e.test_optional.value() << std::endl; } } int main() { database db; testCondition(); testORM(); return 0; }
输出
`t` = ? `t` <> ? NOT (`t` = ?) `t` IS NULL `t` IS NOT NULL (`t` = ?) AND (`t2` = ?) (`t` = ?) OR (`t2` = ?) `t` BETWEEN ? AND ? `t` NOT BETWEEN ? AND ? `t` LIKE ? `t` GLOB ? `t` > ? `t` >= ? `t` < ? `t` <= ? 10 20 10 100 10 0 1 0 1 0 1 0 1337
参考

https://github.com/Thalhammer/sqlitepp

我去年买了个表/sqlite3


创作不易,小小的支持一下吧!

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询