Return StepResult instead of boolean value in SQLStatement::Step()
authorSangyoon Jang <jeremy.jang@samsung.com>
Thu, 17 May 2018 08:32:06 +0000 (17:32 +0900)
committer장상윤/Tizen Platform Lab(SR)/Engineer/삼성전자 <jeremy.jang@samsung.com>
Thu, 24 May 2018 08:14:52 +0000 (17:14 +0900)
There are some return codes of sqlite3_step(), such as SQLITE_ROW,
SQLITE_DONE. In current implementation, Step() just returns true
or false, it cannot represent various result of step.

Change-Id: I2e218bc9ee9db79036f2ea6396011a23b7c0f06b
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
src/common/sql_statement.h
src/common/sqlite_statement.cc
src/common/sqlite_statement.h
src/unit_tests/sql_test/sql_test.cc

index 3d01bb6526c04df984b8f3dc3685356c56514f95..35b09f200a08b207fa12a8af26142a14a83ed691 100644 (file)
@@ -13,10 +13,16 @@ class SQLConnection;
 
 class SQLStatement {
  public:
+  enum class StepResult : int {
+    DONE,
+    ROW,
+    ERROR,
+  };
+
   explicit SQLStatement(SQLConnection* sql_conn) : sql_conn_(sql_conn) {}
   virtual ~SQLStatement() = default;
 
-  virtual bool Step() = 0;
+  virtual StepResult Step() = 0;
 
   virtual bool BindInt(int pos, int val) = 0;
   virtual bool BindString(int pos, const std::string& val) = 0;
index 40ce41d895b4a9db5ad1fb16815742c70fcd3d7b..c94267c0f1eef20d30f305fc61d801a6f4e151e9 100644 (file)
@@ -23,14 +23,23 @@ SQLiteStatement::~SQLiteStatement() {
     LOG(ERROR) << "sqlite3_finalize() failed: " << GetErrorMessage();
 }
 
-bool SQLiteStatement::Step() {
+SQLStatement::StepResult SQLiteStatement::Step() {
   int r = sqlite3_step(stmt_);
   if (r != SQLITE_ROW && r != SQLITE_DONE) {
     LOG(ERROR) << "sqlite3_step() failed: " << GetErrorMessage();
     sql_conn_->SetErrorCode(r);
-    return false;
+    return SQLStatement::StepResult::ERROR;
   }
-  return true;
+  SQLStatement::StepResult res;
+  if (r == SQLITE_ROW) {
+    res = SQLStatement::StepResult::ROW;
+  } else if (r == SQLITE_DONE) {
+    res = SQLStatement::StepResult::DONE;
+  } else {
+    LOG(ERROR) << "Unexpected sqlite3 result code: " << r;
+    res = SQLStatement::StepResult::ERROR;
+  }
+  return res;
 }
 
 bool SQLiteStatement::BindInt(int pos, int val) {
index a9e54ed575c2e32b2832e8dd42051c02e7411d6a..f80e0de3cabdbb372b1607521b8679b50391db21 100644 (file)
@@ -24,7 +24,7 @@ class SQLiteStatement final : public SQLStatement {
   SQLiteStatement(const SQLiteStatement&) = delete;
   SQLiteStatement& operator=(const SQLiteStatement&) = delete;
 
-  bool Step() override;
+  SQLStatement::StepResult Step() override;
 
   bool BindInt(int pos, int val) override;
   bool BindString(int pos, const std::string& val) override;
index 263f70d16c546d5e870d8f9a2e7c9a1f2dc4ff9b..bebac707cc16e6a4263f98b518034cb7be9a1d64 100644 (file)
@@ -65,7 +65,7 @@ TEST_F(SQLTest, RollbackTest) {
       "SELECT COUNT(name) FROM capability WHERE pkgid='org.tizen.rollbacktest'";
   std::shared_ptr<SQLStatement> stmt =
       sql_conn->PrepareStatement(kQueryRollbackValidation);
-  ASSERT_TRUE(stmt->Step());
+  ASSERT_EQ(stmt->Step(), SQLStatement::StepResult::DONE);
   int count = stmt->GetColumnInt(0);
   ASSERT_EQ(count, 0);
 }