diff --git a/include/dbng.hpp b/include/dbng.hpp index 3f0c5193..fc1be5e5 100644 --- a/include/dbng.hpp +++ b/include/dbng.hpp @@ -34,14 +34,23 @@ class dbng { } template - int insert(const T &t, bool get_insert_id = false, Args &&...args) { - return db_.insert(t, get_insert_id, std::forward(args)...); + int insert(const T &t, Args &&...args) { + return db_.insert(t, std::forward(args)...); } template - int insert(const std::vector &t, bool get_insert_id = false, - Args &&...args) { - return db_.insert(t, get_insert_id, std::forward(args)...); + int insert(const std::vector &v, Args &&...args) { + return db_.insert(v, std::forward(args)...); + } + + template + int replace(const T &t, Args &&...args) { + return db_.replace(t, std::forward(args)...); + } + + template + int replace(const std::vector &v, Args &&...args) { + return db_.replace(v, std::forward(args)...); } template @@ -50,18 +59,18 @@ class dbng { } template - int update(const std::vector &t, Args &&...args) { - return db_.update(t, std::forward(args)...); + int update(const std::vector &v, Args &&...args) { + return db_.update(v, std::forward(args)...); } template - int replace(const T &t, Args &&...args) { - return db_.replace(t, std::forward(args)...); + uint64_t get_insert_id_after_insert(const T &t, Args &&...args) { + return db_.get_insert_id_after_insert(t, std::forward(args)...); } template - int replace(const std::vector &t, Args &&...args) { - return db_.replace(t, std::forward(args)...); + uint64_t get_insert_id_after_insert(const std::vector &v, Args &&...args) { + return db_.get_insert_id_after_insert(v, std::forward(args)...); } template diff --git a/include/mysql.hpp b/include/mysql.hpp index a8275e73..5824159c 100644 --- a/include/mysql.hpp +++ b/include/mysql.hpp @@ -99,26 +99,23 @@ class mysql { } template - int insert(const T &t, bool get_insert_id = false, Args &&...args) { - return insert_impl(OptType::insert, t, get_insert_id, - std::forward(args)...); + int insert(const T &t, Args &&...args) { + return insert_impl(OptType::insert, t, std::forward(args)...); } template - int insert(const std::vector &t, bool get_insert_id = false, - Args &&...args) { - return insert_impl(OptType::insert, t, get_insert_id, - std::forward(args)...); + int insert(const std::vector &v, Args &&...args) { + return insert_impl(OptType::insert, v, std::forward(args)...); } template int replace(const T &t, Args &&...args) { - return insert_impl(OptType::replace, t, false, std::forward(args)...); + return insert_impl(OptType::replace, t, std::forward(args)...); } template - int replace(const std::vector &t, Args &&...args) { - return insert_impl(OptType::replace, t, false, std::forward(args)...); + int replace(const std::vector &v, Args &&...args) { + return insert_impl(OptType::replace, v, std::forward(args)...); } template @@ -127,8 +124,22 @@ class mysql { } template - int update(const std::vector &t, Args &&...args) { - return update_impl(t, std::forward(args)...); + int update(const std::vector &v, Args &&...args) { + return update_impl(v, std::forward(args)...); + } + + template + uint64_t get_insert_id_after_insert(const T &t, Args &&...args) { + auto res = insert_or_update_impl(t, generate_insert_sql(true), + OptType::insert, true); + return res.has_value() ? res.value() : 0; + } + + template + uint64_t get_insert_id_after_insert(const std::vector &v, Args &&...args) { + auto res = insert_or_update_impl(v, generate_insert_sql(true), + OptType::insert, true); + return res.has_value() ? res.value() : 0; } template @@ -669,95 +680,102 @@ class mysql { } template - int insert_impl(OptType type, const T &t, bool get_insert_id, - Args &&...args) { - std::string sql = generate_insert_sql(type == OptType::insert); - return insert_or_update_impl(t, sql, type, get_insert_id); + int insert_impl(OptType type, const T &t, Args &&...args) { + auto res = insert_or_update_impl( + t, generate_insert_sql(type == OptType::insert), type); + return res.has_value() ? res.value() : INT_MIN; } template - int insert_impl(OptType type, const std::vector &v, bool get_insert_id, - Args &&...args) { - std::string sql = generate_insert_sql(type == OptType::insert); - return insert_or_update_impl(v, sql, type, get_insert_id); + int insert_impl(OptType type, const std::vector &v, Args &&...args) { + auto res = insert_or_update_impl( + v, generate_insert_sql(type == OptType::insert), type); + return res.has_value() ? res.value() : INT_MIN; } template int update_impl(const T &t, Args &&...args) { bool condition = true; - std::string sql = - generate_update_sql(condition, std::forward(args)...); - return insert_or_update_impl(t, sql, OptType::update, false, condition); + auto sql = generate_update_sql(condition, std::forward(args)...); + auto res = insert_or_update_impl(t, sql, OptType::update, false, condition); + return res.has_value() ? res.value() : INT_MIN; } template int update_impl(const std::vector &v, Args &&...args) { bool condition = true; - std::string sql = - generate_update_sql(condition, std::forward(args)...); - return insert_or_update_impl(v, sql, OptType::update, false, condition); + auto sql = generate_update_sql(condition, std::forward(args)...); + auto res = insert_or_update_impl(v, sql, OptType::update, false, condition); + return res.has_value() ? res.value() : INT_MIN; } template - int insert_or_update_impl(const T &t, const std::string &sql, OptType type, - bool get_insert_id = false, bool condition = true) { + std::optional insert_or_update_impl(const T &t, + const std::string &sql, + OptType type, + bool get_insert_id = false, + bool condition = true) { #ifdef ORMPP_ENABLE_LOG std::cout << sql << std::endl; #endif stmt_ = mysql_stmt_init(con_); if (!stmt_) { set_last_error(mysql_error(con_)); - return INT_MIN; + return std::nullopt; } if (mysql_stmt_prepare(stmt_, sql.c_str(), (int)sql.size())) { set_last_error(mysql_stmt_error(stmt_)); - return INT_MIN; + return std::nullopt; } auto guard = guard_statment(stmt_); if (stmt_execute(t, type, condition) == INT_MIN) { - set_last_error(mysql_stmt_error(stmt_)); - return INT_MIN; + return std::nullopt; } return get_insert_id ? stmt_->mysql->insert_id : 1; } template - int insert_or_update_impl(const std::vector &v, const std::string &sql, - OptType type, bool get_insert_id = false, - bool condition = true) { + std::optional insert_or_update_impl(const std::vector &v, + const std::string &sql, + OptType type, + bool get_insert_id = false, + bool condition = true) { #ifdef ORMPP_ENABLE_LOG std::cout << sql << std::endl; #endif stmt_ = mysql_stmt_init(con_); if (!stmt_) { set_last_error(mysql_error(con_)); - return INT_MIN; + return std::nullopt; } if (mysql_stmt_prepare(stmt_, sql.c_str(), (int)sql.size())) { set_last_error(mysql_stmt_error(stmt_)); - return INT_MIN; + return std::nullopt; } auto guard = guard_statment(stmt_); - if (!begin()) { - return INT_MIN; + if (!get_insert_id && !begin()) { + return std::nullopt; } for (auto &item : v) { if (stmt_execute(item, type, condition) == INT_MIN) { rollback(); - return INT_MIN; + return std::nullopt; } } - return commit() ? get_insert_id ? stmt_->mysql->insert_id : (int)v.size() - : INT_MIN; + if (!get_insert_id && !commit()) { + return std::nullopt; + } + + return get_insert_id ? stmt_->mysql->insert_id : (int)v.size(); } template diff --git a/include/postgresql.hpp b/include/postgresql.hpp index 98c53603..a98be4b6 100644 --- a/include/postgresql.hpp +++ b/include/postgresql.hpp @@ -74,42 +74,53 @@ class postgresql { } template - constexpr int insert(const T &t, Args &&...args) { + int insert(const T &t, Args &&...args) { return insert_impl(OptType::insert, t, std::forward(args)...); } template - constexpr int insert(const std::vector &v, Args &&...args) { + int insert(const std::vector &v, Args &&...args) { return insert_impl(OptType::insert, v, std::forward(args)...); } template - constexpr int replace(const T &t, Args &&...args) { + int replace(const T &t, Args &&...args) { return insert_impl(OptType::replace, t, std::forward(args)...); } template - constexpr int replace(const std::vector &v, Args &&...args) { + int replace(const std::vector &v, Args &&...args) { return insert_impl(OptType::replace, v, std::forward(args)...); } template - constexpr int update(const T &t, Args &&...args) { + int update(const T &t, Args &&...args) { return update_impl(t, std::forward(args)...); } template - constexpr int update(const std::vector &v, Args &&...args) { + int update(const std::vector &v, Args &&...args) { return update_impl(v, std::forward(args)...); } template - constexpr std::enable_if_t, std::vector> query( + uint64_t get_insert_id_after_insert(const T &t, Args &&...args) { + auto res = insert_or_update_impl(t, generate_insert_sql(true), + OptType::insert, true); + return res.has_value() ? res.value() : 0; + } + + template + uint64_t get_insert_id_after_insert(const std::vector &v, Args &&...args) { + auto res = insert_or_update_impl(v, generate_insert_sql(true), + OptType::insert, true); + return res.has_value() ? res.value() : 0; + } + + template + std::enable_if_t, std::vector> query( Args &&...args) { std::string sql = generate_query_sql(args...); -#ifdef ORMPP_ENABLE_LOG - std::cout << sql << std::endl; -#endif constexpr auto SIZE = iguana::get_value(); if (!prepare(sql)) @@ -142,9 +153,6 @@ class postgresql { constexpr auto SIZE = std::tuple_size_v; std::string sql = s; -#ifdef ORMPP_ENABLE_LOG - std::cout << sql << std::endl; -#endif constexpr auto Args_Size = sizeof...(Args); if (Args_Size != 0) { if (Args_Size != std::count(sql.begin(), sql.end(), '$')) @@ -362,6 +370,9 @@ class postgresql { template bool prepare(const std::string &sql) { +#ifdef ORMPP_ENABLE_LOG + std::cout << sql << std::endl; +#endif res_ = PQprepare(con_, "", sql.data(), (int)iguana::get_value(), nullptr); auto guard = guard_statment(res_); @@ -369,7 +380,8 @@ class postgresql { } template - constexpr int stmt_execute(const T &t, OptType type, bool condition) { + std::optional stmt_execute(const T &t, OptType type, + bool condition) { std::vector> param_values; iguana::for_each(t, [&t, ¶m_values, type, this](auto item, auto i) { if (type == OptType::insert && @@ -387,8 +399,9 @@ class postgresql { }); } - if (param_values.empty()) - return INT_MIN; + if (param_values.empty()) { + return std::nullopt; + } std::vector param_values_buf; for (auto &item : param_values) { @@ -399,77 +412,99 @@ class postgresql { param_values_buf.data(), NULL, NULL, 0); auto guard = guard_statment(res_); - return PQresultStatus(res_) == PGRES_COMMAND_OK ? 1 : INT_MIN; + auto status = PQresultStatus(res_); + + if (status == PGRES_TUPLES_OK) { + return std::strtoull(PQgetvalue(res_, 0, 0), nullptr, 10); + } + else if (status == PGRES_COMMAND_OK) { + return 1; + } + + return std::nullopt; } template - constexpr int insert_impl(OptType type, const T &t, Args &&...args) { - std::string sql = generate_insert_sql(type == OptType::insert, - std::forward(args)...); - return insert_or_update_impl(t, sql, type); + int insert_impl(OptType type, const T &t, Args &&...args) { + auto res = insert_or_update_impl( + t, + generate_insert_sql(type == OptType::insert, + std::forward(args)...), + type); + return res.has_value() ? res.value() : INT_MIN; } template - constexpr int insert_impl(OptType type, const std::vector &v, - Args &&...args) { - std::string sql = generate_insert_sql(type == OptType::insert, - std::forward(args)...); - return insert_or_update_impl(v, sql, type); + int insert_impl(OptType type, const std::vector &v, Args &&...args) { + auto res = insert_or_update_impl( + v, + generate_insert_sql(type == OptType::insert, + std::forward(args)...), + type); + return res.has_value() ? res.value() : INT_MIN; } template int update_impl(const T &t, Args &&...args) { bool condition = true; - std::string sql = - generate_update_sql(condition, std::forward(args)...); - return insert_or_update_impl(t, sql, OptType::update, condition); + auto sql = generate_update_sql(condition, std::forward(args)...); + auto res = insert_or_update_impl(t, sql, OptType::update, false, condition); + return res.has_value() ? res.value() : INT_MIN; } template int update_impl(const std::vector &v, Args &&...args) { bool condition = true; - std::string sql = - generate_update_sql(condition, std::forward(args)...); - return insert_or_update_impl(v, sql, OptType::update, condition); + auto sql = generate_update_sql(condition, std::forward(args)...); + auto res = insert_or_update_impl(v, sql, OptType::update, false, condition); + return res.has_value() ? res.value() : INT_MIN; } template - int insert_or_update_impl(const T &t, const std::string &sql, OptType type, - bool condition = true) { -#ifdef ORMPP_ENABLE_LOG - std::cout << sql << std::endl; -#endif - if (!prepare(sql)) { - return INT_MIN; + std::optional insert_or_update_impl(const T &t, + const std::string &sql, + OptType type, + bool get_insert_id = false, + bool condition = true) { + if (!prepare(get_insert_id + ? sql + "returning " + get_auto_key().data() + : sql)) { + return std::nullopt; } + return stmt_execute(t, type, condition); } template - int insert_or_update_impl(const std::vector &v, const std::string &sql, - OptType type, bool condition = true) { -#ifdef ORMPP_ENABLE_LOG - std::cout << sql << std::endl; -#endif + std::optional insert_or_update_impl(const std::vector &v, + const std::string &sql, + OptType type, + bool get_insert_id = false, + bool condition = true) { if (!begin()) { - return INT_MIN; + return std::nullopt; } - if (!prepare(sql)) { - return INT_MIN; + if (!prepare(get_insert_id + ? sql + "returning " + get_auto_key().data() + : sql)) { + return std::nullopt; } + std::optional res = {0}; for (auto &item : v) { - if (stmt_execute(item, type, condition) == INT_MIN) { + res = stmt_execute(item, type, condition); + if (!res.has_value()) { rollback(); - return INT_MIN; + return std::nullopt; } } - if (!commit()) - return INT_MIN; + if (!commit()) { + return std::nullopt; + } - return (int)v.size(); + return get_insert_id ? res : (int)v.size(); } template diff --git a/include/sqlite.hpp b/include/sqlite.hpp index 0771f0e8..b08492d2 100644 --- a/include/sqlite.hpp +++ b/include/sqlite.hpp @@ -72,26 +72,23 @@ class sqlite { } template - int insert(const T &t, bool get_insert_id = false, Args &&...args) { - return insert_impl(OptType::insert, t, get_insert_id, - std::forward(args)...); + int insert(const T &t, Args &&...args) { + return insert_impl(OptType::insert, t, std::forward(args)...); } template - int insert(const std::vector &t, bool get_insert_id = false, - Args &&...args) { - return insert_impl(OptType::insert, t, get_insert_id, - std::forward(args)...); + int insert(const std::vector &v, Args &&...args) { + return insert_impl(OptType::insert, v, std::forward(args)...); } template int replace(const T &t, Args &&...args) { - return insert_impl(OptType::replace, t, false, std::forward(args)...); + return insert_impl(OptType::replace, t, std::forward(args)...); } template - int replace(const std::vector &t, Args &&...args) { - return insert_impl(OptType::replace, t, false, std::forward(args)...); + int replace(const std::vector &v, Args &&...args) { + return insert_impl(OptType::replace, v, std::forward(args)...); } template @@ -100,8 +97,22 @@ class sqlite { } template - int update(const std::vector &t, Args &&...args) { - return update_impl(t, std::forward(args)...); + int update(const std::vector &v, Args &&...args) { + return update_impl(v, std::forward(args)...); + } + + template + uint64_t get_insert_id_after_insert(const T &t, Args &&...args) { + auto res = insert_or_update_impl(t, generate_insert_sql(true), + OptType::insert, true); + return res.has_value() ? res.value() : 0; + } + + template + uint64_t get_insert_id_after_insert(const std::vector &v, Args &&...args) { + auto res = insert_or_update_impl(v, generate_insert_sql(true), + OptType::insert, true); + return res.has_value() ? res.value() : 0; } template @@ -369,7 +380,7 @@ class sqlite { } template - constexpr int stmt_execute(const T &t, OptType type, bool condition) { + int stmt_execute(const T &t, OptType type, bool condition) { int index = 0; bool bind_ok = true; iguana::for_each(t, [&t, &bind_ok, &index, type, this](auto item, auto i) { @@ -486,92 +497,98 @@ class sqlite { } template - int insert_impl(OptType type, const T &t, bool get_insert_id, - Args &&...args) { - std::string sql = generate_insert_sql(type == OptType::insert); - return insert_or_update_impl(t, sql, type, get_insert_id); + int insert_impl(OptType type, const T &t, Args &&...args) { + auto res = insert_or_update_impl( + t, generate_insert_sql(type == OptType::insert), type); + return res.has_value() ? res.value() : INT_MIN; } template - int insert_impl(OptType type, const std::vector &v, bool get_insert_id, - Args &&...args) { - std::string sql = generate_insert_sql(type == OptType::insert); - return insert_or_update_impl(v, sql, type, get_insert_id); + int insert_impl(OptType type, const std::vector &v, Args &&...args) { + auto res = insert_or_update_impl( + v, generate_insert_sql(type == OptType::insert), type); + return res.has_value() ? res.value() : INT_MIN; } template int update_impl(const T &t, Args &&...args) { bool condition = true; - std::string sql = - generate_update_sql(condition, std::forward(args)...); - return insert_or_update_impl(t, sql, OptType::update, false, condition); + auto sql = generate_update_sql(condition, std::forward(args)...); + auto res = insert_or_update_impl(t, sql, OptType::update, false, condition); + return res.has_value() ? res.value() : INT_MIN; } template int update_impl(const std::vector &v, Args &&...args) { bool condition = true; - std::string sql = - generate_update_sql(condition, std::forward(args)...); - return insert_or_update_impl(v, sql, OptType::update, false, condition); + auto sql = generate_update_sql(condition, std::forward(args)...); + auto res = insert_or_update_impl(v, sql, OptType::update, false, condition); + return res.has_value() ? res.value() : INT_MIN; } template - int insert_or_update_impl(const T &t, const std::string &sql, OptType type, - bool get_insert_id = false, bool condition = true) { + std::optional insert_or_update_impl(const T &t, + const std::string &sql, + OptType type, + bool get_insert_id = false, + bool condition = true) { #ifdef ORMPP_ENABLE_LOG std::cout << sql << std::endl; #endif if (sqlite3_prepare_v2(handle_, sql.data(), (int)sql.size(), &stmt_, nullptr) != SQLITE_OK) { set_last_error(sqlite3_errmsg(handle_)); - return INT_MIN; + return std::nullopt; } auto guard = guard_statment(stmt_); if (stmt_execute(t, type, condition) == INT_MIN) { - set_last_error(sqlite3_errmsg(handle_)); - return INT_MIN; + return std::nullopt; } return get_insert_id ? sqlite3_last_insert_rowid(handle_) : 1; } template - int insert_or_update_impl(const std::vector &v, const std::string &sql, - OptType type, bool get_insert_id = false, - bool condition = true) { + std::optional insert_or_update_impl(const std::vector &v, + const std::string &sql, + OptType type, + bool get_insert_id = false, + bool condition = true) { #ifdef ORMPP_ENABLE_LOG std::cout << sql << std::endl; #endif if (sqlite3_prepare_v2(handle_, sql.data(), (int)sql.size(), &stmt_, nullptr) != SQLITE_OK) { set_last_error(sqlite3_errmsg(handle_)); - return INT_MIN; + return std::nullopt; } auto guard = guard_statment(stmt_); if (!begin()) { - return INT_MIN; + return std::nullopt; } for (auto &item : v) { if (stmt_execute(item, type, condition) == INT_MIN) { rollback(); - return INT_MIN; + return std::nullopt; } if (sqlite3_reset(stmt_) != SQLITE_OK) { rollback(); set_last_error(sqlite3_errmsg(handle_)); - return INT_MIN; + return std::nullopt; } } - return commit() ? get_insert_id ? sqlite3_last_insert_rowid(handle_) - : (int)v.size() - : INT_MIN; + if (!commit()) { + return std::nullopt; + } + + return get_insert_id ? sqlite3_last_insert_rowid(handle_) : (int)v.size(); } private: diff --git a/include/utility.hpp b/include/utility.hpp index 3850dde9..3c1a5f73 100644 --- a/include/utility.hpp +++ b/include/utility.hpp @@ -19,6 +19,12 @@ inline int add_auto_key_field(std::string_view key, std::string_view value) { return 0; } +template +inline auto get_auto_key() { + auto it = g_ormpp_auto_key_map.find(iguana::get_name()); + return it == g_ormpp_auto_key_map.end() ? "" : it->second; +} + template inline auto is_auto_key(std::string_view field_name) { auto it = g_ormpp_auto_key_map.find(iguana::get_name()); @@ -38,7 +44,9 @@ inline int add_conflict_key_field(std::string_view key, return 0; } -inline auto get_conflict_key(std::string_view key) { +template +inline auto get_conflict_key() { + auto key = iguana::get_name(); auto it = g_ormpp_conflict_key_map.find(key); if (it == g_ormpp_conflict_key_map.end()) { auto auto_key = g_ormpp_auto_key_map.find(key); @@ -207,7 +215,7 @@ inline std::vector get_conflict_keys() { if (!res.empty()) { return res; } - std::stringstream s(get_conflict_key(iguana::get_name()).data()); + std::stringstream s(get_conflict_key().data()); while (s.good()) { std::string str; getline(s, str, ','); @@ -270,7 +278,7 @@ inline std::string generate_insert_sql(bool insert, Args &&...args) { append(conflict, args...); } else { - conflict += get_conflict_key(iguana::get_name()); + conflict += get_conflict_key(); } conflict += ")"; append(sql, fields, values, conflict, "do update set", set); diff --git a/tests/test_ormpp.cpp b/tests/test_ormpp.cpp index 7071597d..15a67e21 100644 --- a/tests/test_ormpp.cpp +++ b/tests/test_ormpp.cpp @@ -1122,7 +1122,22 @@ TEST_CASE("create table with unique") { CHECK(vec3.size() == 1); } #endif - +#ifdef ORMPP_ENABLE_PG + dbng postgres; + if (postgres.connect(ip, username, password, db)) { + postgres.execute("drop table if exists person"); + postgres.create_datatable(ormpp_auto_key{"id"}, + ormpp_unique{{"name", "age"}}); + postgres.insert({"purecpp"}); + auto vec1 = postgres.query("order by id"); + auto vec2 = postgres.query("limit 1"); + CHECK(vec1.size() == 1); + CHECK(vec2.size() == 1); + postgres.insert({"purecpp"}); + auto vec3 = postgres.query(); + CHECK(vec3.size() == 1); + } +#endif #ifdef ORMPP_ENABLE_SQLITE3 dbng sqlite; if (sqlite.connect(db)) { @@ -1141,7 +1156,7 @@ TEST_CASE("create table with unique") { #endif } -TEST_CASE("get insert id") { +TEST_CASE("get insert id after insert") { #ifdef ORMPP_ENABLE_MYSQL dbng mysql; if (mysql.connect(ip, username, password, db)) { @@ -1149,11 +1164,26 @@ TEST_CASE("get insert id") { mysql.create_datatable(ormpp_auto_key{"id"}); mysql.insert({"purecpp"}); mysql.insert({"purecpp"}); - int id = mysql.insert({"purecpp"}, true); + auto id = mysql.get_insert_id_after_insert({"purecpp"}); CHECK(id == 3); + id = mysql.get_insert_id_after_insert({{"purecpp"}, {"purecpp"}}); + CHECK(id == 5); + } +#endif +#ifdef ORMPP_ENABLE_PG + dbng postgres; + if (postgres.connect(ip, username, password, db)) { + postgres.execute("drop table if exists person"); + postgres.create_datatable(ormpp_auto_key{"id"}); + postgres.insert({"purecpp"}); + postgres.insert({"purecpp"}); + auto id = postgres.get_insert_id_after_insert({"purecpp"}); + CHECK(id == 3); + id = + postgres.get_insert_id_after_insert({{"purecpp"}, {"purecpp"}}); + CHECK(id == 5); } #endif - #ifdef ORMPP_ENABLE_SQLITE3 dbng sqlite; if (sqlite.connect(db)) { @@ -1161,8 +1191,10 @@ TEST_CASE("get insert id") { sqlite.create_datatable(ormpp_auto_key{"id"}); sqlite.insert({"purecpp"}); sqlite.insert({"purecpp"}); - int id = sqlite.insert({"purecpp"}, true); + auto id = sqlite.get_insert_id_after_insert({"purecpp"}); CHECK(id == 3); + id = sqlite.get_insert_id_after_insert({{"purecpp"}, {"purecpp"}}); + CHECK(id == 5); } #endif } @@ -1218,6 +1250,7 @@ TEST_CASE("query tuple_optional_t") { #ifdef ORMPP_ENABLE_MYSQL dbng mysql; if (mysql.connect(ip, username, password, db)) { + mysql.execute("drop table if exists tuple_optional_t"); mysql.create_datatable(ormpp_auto_key{"id"}); mysql.insert({"purecpp", 6}); mysql.insert({std::nullopt}); @@ -1247,6 +1280,7 @@ TEST_CASE("query tuple_optional_t") { #ifdef ORMPP_ENABLE_PG dbng postgres; if (postgres.connect(ip, username, password, db)) { + postgres.execute("drop table if exists tuple_optional_t"); postgres.create_datatable(ormpp_auto_key{"id"}); postgres.insert({"purecpp", 6}); postgres.insert({std::nullopt}); @@ -1276,6 +1310,7 @@ TEST_CASE("query tuple_optional_t") { #ifdef ORMPP_ENABLE_SQLITE3 dbng sqlite; if (sqlite.connect(db)) { + sqlite.execute("drop table if exists tuple_optional_t"); sqlite.create_datatable(ormpp_auto_key{"id"}); sqlite.insert({"purecpp", 6}); sqlite.insert({std::nullopt});