From ec273bfd73b9e6b24dd3a5565432ca985062faf8 Mon Sep 17 00:00:00 2001 From: Matthew Landowski Date: Tue, 22 Sep 2015 21:37:04 -0500 Subject: [PATCH] added remove convience function which executes a sql delete command --- src/SQLiteDatabase.cpp | 79 ++++++++++++++++++++++------ test/src/unittest_SQLiteDatabase.cpp | 39 ++++++++++++++ 2 files changed, 101 insertions(+), 17 deletions(-) diff --git a/src/SQLiteDatabase.cpp b/src/SQLiteDatabase.cpp index 70574af..8e4f1fb 100644 --- a/src/SQLiteDatabase.cpp +++ b/src/SQLiteDatabase.cpp @@ -228,11 +228,7 @@ Cursor SQLiteDatabase::query(bool distinct, const std::string& table, const std: // Add where clause if needed if(!selection.empty()){ sql += " WHERE "; - - // add selections - for(auto ii = 0; ii < selection.size(); ii++){ - sql += selection[ii] + (ii < selection.size() - 1 ? ", " : ""); - } + sql += selection; } // Add where clause if needed @@ -254,6 +250,11 @@ Cursor SQLiteDatabase::query(bool distinct, const std::string& table, const std: auto rc = sqlite3_prepare_v2(db_, sql.c_str(), -1, &stmt, nullptr); + // Bind arguments + for(auto ii = 0; ii < selectionArgs.size(); ii++) { + sqlite3_bind_text(stmt, ii + 1, selectionArgs[ii].c_str(), -1, SQLITE_TRANSIENT); + } + if(rc){ sqlite3_finalize(stmt); throw SQLiteDatabaseException("Error preparing statment" + getSQLite3ErrorMessage()); @@ -317,17 +318,18 @@ int SQLiteDatabase::insert(const std::string& table, const std::vector& columns, const std::vector& values, const std::string& selection, const std::vector& selectionArgs) { - long result; + int result; if(columns.size() == 0){ throw new SQLiteDatabaseException("columns vector must has at least one item"); @@ -378,17 +380,18 @@ int SQLiteDatabase::update(const std::string& table, const std::vector& selectionArgs) { - return 0; + long result; + + if(selection.size() == 0){ + throw new SQLiteDatabaseException("selection must has at least one column name"); + } + + if(selection.size() == selectionArgs.size()){ + throw new SQLiteDatabaseException("selection and selectionArgs must be the same size"); + } + + std::string sql = "DELETE FROM "; + + // Add table + sql += table; + sql += " WHERE "; + sql += selection; + + sqlite3_stmt *stmt; + + auto rc = sqlite3_prepare_v2(db_, sql.c_str(), -1, &stmt, nullptr); + + // Bind arguments + for(auto ii = 0; ii < selectionArgs.size(); ii++) { + sqlite3_bind_text(stmt, ii + 1, selectionArgs[ii].c_str(), -1, SQLITE_TRANSIENT); + } + + if(rc){ + sqlite3_finalize(stmt); + throw SQLiteDatabaseException("Error preparing update statement " + getSQLite3ErrorMessage()); + } + + // Step through all rows in the result set + // building the cursor result set + if(sqlite3_step(stmt) != SQLITE_DONE){ + throw SQLiteDatabaseException("Error executing update statement " + getSQLite3ErrorMessage()); + } + + // Get number of rows modified + result = sqlite3_changes(db_); + + sqlite3_finalize(stmt); + + return result; } } /* namespace sqlite */ \ No newline at end of file diff --git a/test/src/unittest_SQLiteDatabase.cpp b/test/src/unittest_SQLiteDatabase.cpp index 85f7703..fc4a8ce 100644 --- a/test/src/unittest_SQLiteDatabase.cpp +++ b/test/src/unittest_SQLiteDatabase.cpp @@ -74,6 +74,8 @@ TEST_F(SQLiteDatabaseTestFixture, get_readonly_database_test) { try{ db.open(test_database_filename_, SQLITE_OPEN_READONLY); + int expected_version = 2; + EXPECT_ANY_THROW(db.setVersion(expected_version)); db.close(); @@ -187,9 +189,46 @@ TEST_F(SQLiteDatabaseTestFixture, update_test) { EXPECT_EQ(updated_rows, 2); + db.insert(table, std::vector{"mpg", "weight"}, std::vector{"20", "1500"}, "", std::vector{}); + updated_rows = db.update(table, std::vector{"mpg", "weight"}, + std::vector{"2", "2"}, "mpg = ? AND weight = ?", + std::vector{"20", "1500"}); + + EXPECT_EQ(updated_rows, 1); + db.close(); } catch (const std::exception & e){ std::cout << e.what() << std::endl; } } + +TEST_F(SQLiteDatabaseTestFixture, remove_test) { + + sqlite::SQLiteDatabase db; + + try{ + db.open(test_database_filename_, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE); + + // Create Cars table test data + const std::string kCreateTable = + "CREATE TABLE IF NOT EXISTS cars " + "(mpg text, " + "weight text)"; + + db.execQuery(kCreateTable); + + const std::string table = "cars"; + long id = db.insert(table, std::vector{"mpg", "weight"}, std::vector{"34", "2000"}, "", std::vector{}); + long id2 = db.insert(table, std::vector{"mpg", "weight"}, std::vector{"20", "1500"}, "", std::vector{}); + + int deleted_rows = db.remove(table, "mpg = ? AND weight = ?", std::vector{"34", "2000"}); + + EXPECT_EQ(deleted_rows, 1); + + db.close(); + } + catch (const std::exception & e){ + std::cout << e.what() << std::endl; + } +} \ No newline at end of file