Skip to content

Commit

Permalink
added remove convience function which executes a sql delete command
Browse files Browse the repository at this point in the history
  • Loading branch information
washcycle committed Sep 23, 2015
1 parent a9b401e commit ec273bf
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 17 deletions.
79 changes: 62 additions & 17 deletions src/SQLiteDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
Expand Down Expand Up @@ -317,17 +318,18 @@ int SQLiteDatabase::insert(const std::string& table, const std::vector<std::stri
// 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;
}

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 statement " + getSQLite3ErrorMessage());
Expand All @@ -349,7 +351,7 @@ int SQLiteDatabase::insert(const std::string& table, const std::vector<std::stri

int SQLiteDatabase::update(const std::string& table, const std::vector<std::string>& columns, const std::vector<std::string>& values,
const std::string& selection, const std::vector<std::string>& selectionArgs) {
long result;
int result;

if(columns.size() == 0){
throw new SQLiteDatabaseException("columns vector must has at least one item");
Expand Down Expand Up @@ -378,17 +380,18 @@ int SQLiteDatabase::update(const std::string& table, const std::vector<std::stri
// 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;
}

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());
Expand All @@ -410,6 +413,48 @@ int SQLiteDatabase::update(const std::string& table, const std::vector<std::stri

int SQLiteDatabase::remove(const std::string& table, const std::string& selection,
const std::vector<std::string>& 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 */
39 changes: 39 additions & 0 deletions test/src/unittest_SQLiteDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -187,9 +189,46 @@ TEST_F(SQLiteDatabaseTestFixture, update_test) {

EXPECT_EQ(updated_rows, 2);

db.insert(table, std::vector<std::string>{"mpg", "weight"}, std::vector<std::string>{"20", "1500"}, "", std::vector<std::string>{});
updated_rows = db.update(table, std::vector<std::string>{"mpg", "weight"},
std::vector<std::string>{"2", "2"}, "mpg = ? AND weight = ?",
std::vector<std::string>{"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<std::string>{"mpg", "weight"}, std::vector<std::string>{"34", "2000"}, "", std::vector<std::string>{});
long id2 = db.insert(table, std::vector<std::string>{"mpg", "weight"}, std::vector<std::string>{"20", "1500"}, "", std::vector<std::string>{});

int deleted_rows = db.remove(table, "mpg = ? AND weight = ?", std::vector<std::string>{"34", "2000"});

EXPECT_EQ(deleted_rows, 1);

db.close();
}
catch (const std::exception & e){
std::cout << e.what() << std::endl;
}
}

0 comments on commit ec273bf

Please sign in to comment.