Support 64bit integer in tizen-database 96/285596/3
authorjh9216.park <jh9216.park@samsung.com>
Thu, 15 Dec 2022 06:25:12 +0000 (01:25 -0500)
committerjh9216.park <jh9216.park@samsung.com>
Thu, 15 Dec 2022 07:54:35 +0000 (02:54 -0500)
- Bind method for 'int64_t' type was added in class 'Sql'

Change-Id: I7bbafd18505cbe2594296edd754e264c94074ea6
Signed-off-by: jh9216.park <jh9216.park@samsung.com>
tests/tizen-database_unittests/src/test_database.cc
tizen-database/database.hpp

index 897dbd6..f524af9 100644 (file)
@@ -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<unsigned char> {'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<int64_t>(i.Get(1));
+    std::optional<int64_t> ov = i.Get(1);
+    EXPECT_EQ(val, i64);
+    EXPECT_TRUE(ov);
+    EXPECT_EQ(*ov, i64);
+  }
+}
+
 TEST_F(DatabaseTest, test_ToVector) {
   SetDefault();
 
index 5effc55..67d55fc 100644 (file)
@@ -47,7 +47,7 @@ void for_(F func) {
   for_(func, std::make_index_sequence<N>());
 }
 
-using DbType = std::optional<std::variant<int, double, std::string,
+using DbType = std::optional<std::variant<int64_t, double, std::string,
     std::vector<unsigned char>>>;
 
 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<int>(*db_type_);
+    return std::get<int64_t>(*db_type_);
+  }
+
+  explicit operator int64_t () {
+    if (!db_type_)
+      throw DbException("invalid type conversion from nullopt to int");
+    return std::get<int64_t>(*db_type_);
   }
 
   explicit operator std::string () {
@@ -117,7 +123,13 @@ class AutoDbType {
   operator std::optional<int> () {
     if (!db_type_)
       return std::nullopt;
-    return std::get<int>(*db_type_);
+    return std::get<int64_t>(*db_type_);
+  }
+
+  operator std::optional<int64_t> () {
+    if (!db_type_)
+      return std::nullopt;
+    return std::get<int64_t>(*db_type_);
   }
 
   operator std::optional<std::string> () {
@@ -206,6 +218,11 @@ class Database {
     }
 
     Sql& Bind(int val) {
+      bindings_.push_back(DbType(static_cast<int64_t>(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<int64_t>(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<int64_t>(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<const char*>(
               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<std::string>(&(*type))) {
       r = sqlite3_bind_text(stmt, pos, (*pstr).c_str(), -1,
           SQLITE_TRANSIENT);
-    } else if (const int* pint = std::get_if<int>(&(*type))) {
-      r = sqlite3_bind_int(stmt, pos, (*pint));
+    } else if (const int64_t* pint = std::get_if<int64_t>(&(*type))) {
+      r = sqlite3_bind_int64(stmt, pos, (*pint));
     } else if (const double* pdouble = std::get_if<double>(&(*type))) {
       r = sqlite3_bind_double(stmt, pos, (*pdouble));
     } else if (const std::vector<unsigned char>* pvector =