Overload session::get_last_insert_id() for std::int64_t too

It may be more convenient to work with this type than with "long long",
so provide an overload allowing to retrieve the ID as it too.

Closes #1232.

Closes #1325.
This commit is contained in:
Matheus Gabriel Werny
2026-01-24 16:15:32 +01:00
committed by Vadim Zeitlin
parent b2e4cf3c27
commit 66baded292
3 changed files with 35 additions and 7 deletions

View File

@@ -17,11 +17,13 @@
// std
#include <cstddef>
#include <cstdint>
#include <memory>
#include <ostream>
#include <sstream>
#include <string>
#include <forward_list>
#include <type_traits>
namespace soci
{
@@ -138,6 +140,16 @@ public:
// return the last value auto-generated in this session).
bool get_last_insert_id(std::string const & table, long long & value);
// Overload provided for platforms where std::int64_t is not long long.
template<typename T = std::int64_t, typename = std::enable_if_t<! std::is_same<T, long long>::value>>
bool get_last_insert_id(std::string const & table, std::int64_t & value)
{
long long tmp = -1;
const bool result = get_last_insert_id(table, tmp);
value = tmp;
return result;
}
// Returns once_temp_type for the internally composed query
// for the list of tables in the current schema.
// Since this query usually returns multiple results (for multiple tables),

View File

@@ -741,10 +741,18 @@ TEST_CASE("MySQL last insert id", "[mysql][last-insert-id]")
soci::session sql(backEnd, connectString);
table_creator_for_get_last_insert_id tableCreator(sql);
sql << "insert into soci_test () values ()";
long long id;
bool result = sql.get_last_insert_id("soci_test", id);
CHECK(result == true);
CHECK(id == 42);
{
long long id = -1;
CHECK(sql.get_last_insert_id("soci_test", id));
CHECK(id == 42);
}
{
std::int64_t id = -1;
CHECK(sql.get_last_insert_id("soci_test", id));
CHECK(id == 42);
}
}
TEST_CASE("MySQL DDL with metadata", "[mysql][ddl]")

View File

@@ -203,9 +203,17 @@ TEST_CASE("SQLite get_last_insert_id works with AUTOINCREMENT",
sql << "insert into t(name) values('x')";
sql << "insert into t(name) values('y')";
long long val;
sql.get_last_insert_id("t", val);
CHECK(val == 2);
{
long long val = -1;
CHECK(sql.get_last_insert_id("t", val));
CHECK(val == 2);
}
{
std::int64_t val = -1;
CHECK(sql.get_last_insert_id("t", val));
CHECK(val == 2);
}
}
TEST_CASE("SQLite get_last_insert_id with AUTOINCREMENT does not reuse IDs when rows deleted",