From: jh9216.park Date: Thu, 15 Dec 2022 06:25:12 +0000 (-0500) Subject: Support 64bit integer in tizen-database X-Git-Tag: accepted/tizen/unified/20221222.170405~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F96%2F285596%2F3;p=platform%2Fcore%2Fbase%2Fbundle.git Support 64bit integer in tizen-database - Bind method for 'int64_t' type was added in class 'Sql' Change-Id: I7bbafd18505cbe2594296edd754e264c94074ea6 Signed-off-by: jh9216.park --- diff --git a/tests/tizen-database_unittests/src/test_database.cc b/tests/tizen-database_unittests/src/test_database.cc index 897dbd6..f524af9 100644 --- a/tests/tizen-database_unittests/src/test_database.cc +++ b/tests/tizen-database_unittests/src/test_database.cc @@ -149,6 +149,21 @@ class DatabaseTest : public ::testing::Test { ASSERT_TRUE(r2); } + void SetInt64() { + int64_t i64 = 123456789000LL; + tizen_base::Database db(TEST_DB, SQLITE_OPEN_READWRITE); + auto q = tizen_base::Database::Sql(Q_INSERT) + .Bind("gogo") + .Bind(i64) + .Bind(9.216) + .Bind(std::vector {'9', '2', '1', '6' }); + + auto r1 = db.Exec(q); + ASSERT_TRUE(r1); + auto r2 = db.Exec(q); + ASSERT_TRUE(r2); + } + void SetDefaultWithNull() { tizen_base::Database db(TEST_DB, SQLITE_OPEN_READWRITE); auto q = tizen_base::Database::Sql(Q_INSERT) @@ -331,6 +346,23 @@ TEST_F(DatabaseTest, test_select4) { EXPECT_EQ(cnt, 0); } +TEST_F(DatabaseTest, test_select_i64) { + int64_t i64 = 123456789000LL; + SetInt64(); + using tizen_base::_; + tizen_base::Database db(TEST_DB, SQLITE_OPEN_READWRITE); + auto r = db.Exec({ Q_SELECT }); + + EXPECT_TRUE(r); + for (const auto& i : r) { + int64_t val = static_cast(i.Get(1)); + std::optional ov = i.Get(1); + EXPECT_EQ(val, i64); + EXPECT_TRUE(ov); + EXPECT_EQ(*ov, i64); + } +} + TEST_F(DatabaseTest, test_ToVector) { SetDefault(); diff --git a/tizen-database/database.hpp b/tizen-database/database.hpp index 5effc55..67d55fc 100644 --- a/tizen-database/database.hpp +++ b/tizen-database/database.hpp @@ -47,7 +47,7 @@ void for_(F func) { for_(func, std::make_index_sequence()); } -using DbType = std::optional>>; class DbException : public std::runtime_error { @@ -84,7 +84,13 @@ class AutoDbType { explicit operator int () { if (!db_type_) throw DbException("invalid type conversion from nullopt to int"); - return std::get(*db_type_); + return std::get(*db_type_); + } + + explicit operator int64_t () { + if (!db_type_) + throw DbException("invalid type conversion from nullopt to int"); + return std::get(*db_type_); } explicit operator std::string () { @@ -117,7 +123,13 @@ class AutoDbType { operator std::optional () { if (!db_type_) return std::nullopt; - return std::get(*db_type_); + return std::get(*db_type_); + } + + operator std::optional () { + if (!db_type_) + return std::nullopt; + return std::get(*db_type_); } operator std::optional () { @@ -206,6 +218,11 @@ class Database { } Sql& Bind(int val) { + bindings_.push_back(DbType(static_cast(val))); + return *this; + } + + Sql& Bind(int64_t val) { bindings_.push_back(DbType(val)); return *this; } @@ -237,6 +254,11 @@ class Database { } Sql& Bind(int pos, int val) { + binding_map_[pos] = DbType(static_cast(val)); + return *this; + } + + Sql& Bind(int pos, int64_t val) { binding_map_[pos] = DbType(val); return *this; } @@ -268,6 +290,11 @@ class Database { } Sql& Bind(std::string name, int val) { + binding_name_map_[std::move(name)] = DbType(static_cast(val)); + return *this; + } + + Sql& Bind(std::string name, int64_t val) { binding_name_map_[std::move(name)] = DbType(val); return *this; } @@ -378,7 +405,7 @@ class Database { dbt = DbType(reinterpret_cast( sqlite3_column_text(stmt, pos))); } else if (type == SQLITE_INTEGER) { - dbt = DbType(sqlite3_column_int(stmt, pos)); + dbt = DbType(sqlite3_column_int64(stmt, pos)); } else if (type == SQLITE_FLOAT) { dbt = DbType(sqlite3_column_double(stmt, pos)); } else if (type == SQLITE_BLOB) { @@ -698,8 +725,8 @@ class Database { if (const std::string* pstr = std::get_if(&(*type))) { r = sqlite3_bind_text(stmt, pos, (*pstr).c_str(), -1, SQLITE_TRANSIENT); - } else if (const int* pint = std::get_if(&(*type))) { - r = sqlite3_bind_int(stmt, pos, (*pint)); + } else if (const int64_t* pint = std::get_if(&(*type))) { + r = sqlite3_bind_int64(stmt, pos, (*pint)); } else if (const double* pdouble = std::get_if(&(*type))) { r = sqlite3_bind_double(stmt, pos, (*pdouble)); } else if (const std::vector* pvector =