Skip to content

Commit

Permalink
Replace all double-quoted string literals by single quotes in unit te…
Browse files Browse the repository at this point in the history
…st (#483)

Fix #481 23 test fails under FreeBSD 14.1 builtin SQLite library 3.45.1

The SQLITE_DQS compile-time option activates or deactivates the legacy
double-quoted string literal misfeature.
Legacy behavior is that double-quoted string literals are allowed
anywhere.
  • Loading branch information
SRombauts authored Aug 18, 2024
2 parents 00d9dba + 553361b commit 58d9867
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 68 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ else (SQLITECPP_INTERNAL_SQLITE)
endif()
else()
find_package (SQLite3 REQUIRED)
message(STATUS "Link to sqlite3 system library")
message(STATUS "Link to sqlite3 system library ${SQLite3_VERSION}")
target_link_libraries(SQLiteCpp PUBLIC SQLite::SQLite3)
if(SQLite3_VERSION VERSION_LESS "3.19")
set_target_properties(SQLiteCpp PROPERTIES COMPILE_FLAGS "-DSQLITECPP_HAS_MEM_STRUCT")
Expand Down
20 changes: 10 additions & 10 deletions examples/example1/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,16 +298,16 @@ int main()
db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)");

// first row
int nb = db.exec("INSERT INTO test VALUES (NULL, \"test\")");
std::cout << "INSERT INTO test VALUES (NULL, \"test\")\", returned " << nb << std::endl;
int nb = db.exec("INSERT INTO test VALUES (NULL, 'test')");
std::cout << "INSERT INTO test VALUES (NULL, 'test')\", returned " << nb << std::endl;

// second row
nb = db.exec("INSERT INTO test VALUES (NULL, \"second\")");
std::cout << "INSERT INTO test VALUES (NULL, \"second\")\", returned " << nb << std::endl;
nb = db.exec("INSERT INTO test VALUES (NULL, 'second')");
std::cout << "INSERT INTO test VALUES (NULL, 'second')\", returned " << nb << std::endl;

// update the second row
nb = db.exec("UPDATE test SET value=\"second-updated\" WHERE id='2'");
std::cout << "UPDATE test SET value=\"second-updated\" WHERE id='2', returned " << nb << std::endl;
nb = db.exec("UPDATE test SET value='second-updated' WHERE id='2'");
std::cout << "UPDATE test SET value='second-updated' WHERE id='2', returned " << nb << std::endl;

nb = db.getTotalChanges();
std::cout << "Nb of total changes since connection: " << nb << std::endl;
Expand Down Expand Up @@ -347,8 +347,8 @@ int main()

db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)");

int nb = db.exec("INSERT INTO test VALUES (NULL, \"test\")");
std::cout << "INSERT INTO test VALUES (NULL, \"test\")\", returned " << nb << std::endl;
int nb = db.exec("INSERT INTO test VALUES (NULL, 'test')");
std::cout << "INSERT INTO test VALUES (NULL, 'test')\", returned " << nb << std::endl;

// Commit transaction
transaction.commit();
Expand All @@ -365,8 +365,8 @@ int main()
// Begin transaction
SQLite::Transaction transaction(db);

int nb = db.exec("INSERT INTO test VALUES (NULL, \"second\")");
std::cout << "INSERT INTO test VALUES (NULL, \"second\")\", returned " << nb << std::endl;
int nb = db.exec("INSERT INTO test VALUES (NULL, 'second')");
std::cout << "INSERT INTO test VALUES (NULL, 'second')\", returned " << nb << std::endl;

nb = db.exec("INSERT INTO test ObviousError");
std::cout << "INSERT INTO test \"error\", returned " << nb << std::endl;
Expand Down
16 changes: 8 additions & 8 deletions tests/Backup_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ TEST(Backup, initException)
{
SQLite::Database srcDB("backup_test.db3", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE);
srcDB.exec("CREATE TABLE backup_test (id INTEGER PRIMARY KEY, value TEXT)");
ASSERT_EQ(1, srcDB.exec("INSERT INTO backup_test VALUES (1, \"first\")"));
ASSERT_EQ(1, srcDB.exec("INSERT INTO backup_test VALUES (2, \"second\")"));
ASSERT_EQ(1, srcDB.exec("INSERT INTO backup_test VALUES (1, 'first')"));
ASSERT_EQ(1, srcDB.exec("INSERT INTO backup_test VALUES (2, 'second')"));
EXPECT_THROW(SQLite::Backup backup(srcDB, srcDB), SQLite::Exception);
EXPECT_THROW(SQLite::Backup backup(srcDB, "main", srcDB, "main"), SQLite::Exception);
const std::string name("main");
Expand All @@ -44,8 +44,8 @@ TEST(Backup, executeStepOne)
{
SQLite::Database srcDB("backup_test.db3", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE);
srcDB.exec("CREATE TABLE backup_test (id INTEGER PRIMARY KEY, value TEXT)");
ASSERT_EQ(1, srcDB.exec("INSERT INTO backup_test VALUES (1, \"first\")"));
ASSERT_EQ(1, srcDB.exec("INSERT INTO backup_test VALUES (2, \"second\")"));
ASSERT_EQ(1, srcDB.exec("INSERT INTO backup_test VALUES (1, 'first')"));
ASSERT_EQ(1, srcDB.exec("INSERT INTO backup_test VALUES (2, 'second')"));

SQLite::Database destDB("backup_test.db3.backup", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE);
SQLite::Backup backup(destDB, "main", srcDB, "main");
Expand Down Expand Up @@ -79,8 +79,8 @@ TEST(Backup, executeStepAll)
{
SQLite::Database srcDB("backup_test.db3", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE);
srcDB.exec("CREATE TABLE backup_test (id INTEGER PRIMARY KEY, value TEXT)");
ASSERT_EQ(1, srcDB.exec("INSERT INTO backup_test VALUES (1, \"first\")"));
ASSERT_EQ(1, srcDB.exec("INSERT INTO backup_test VALUES (2, \"second\")"));
ASSERT_EQ(1, srcDB.exec("INSERT INTO backup_test VALUES (1, 'first')"));
ASSERT_EQ(1, srcDB.exec("INSERT INTO backup_test VALUES (2, 'second')"));

SQLite::Database destDB("backup_test.db3.backup", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE);
SQLite::Backup backup(destDB, srcDB);
Expand Down Expand Up @@ -110,8 +110,8 @@ TEST(Backup, executeStepException)
{
SQLite::Database srcDB("backup_test.db3", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE);
srcDB.exec("CREATE TABLE backup_test (id INTEGER PRIMARY KEY, value TEXT)");
ASSERT_EQ(1, srcDB.exec("INSERT INTO backup_test VALUES (1, \"first\")"));
ASSERT_EQ(1, srcDB.exec("INSERT INTO backup_test VALUES (2, \"second\")"));
ASSERT_EQ(1, srcDB.exec("INSERT INTO backup_test VALUES (1, 'first')"));
ASSERT_EQ(1, srcDB.exec("INSERT INTO backup_test VALUES (2, 'second')"));
{
SQLite::Database destDB("backup_test.db3.backup", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE);
(void)destDB;
Expand Down
6 changes: 3 additions & 3 deletions tests/Column_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static void test_column_basis(bool utf16)
EXPECT_EQ(0, db.getLastInsertRowid());

// Create a first row (autoid: 1) with all kind of data and a null value
SQLite::Statement insert(db, "INSERT INTO test VALUES (NULL, \"first\", -123, 0.123, ?, NULL)");
SQLite::Statement insert(db, "INSERT INTO test VALUES (NULL, 'first', -123, 0.123, ?, NULL)");
// Bind the blob value to the first parameter of the SQL query
const char buffer[] = {'b', 'l', '\0', 'b'}; // "bl\0b" : 4 char, with a null byte inside
const int size = sizeof(buffer); // size = 4
Expand Down Expand Up @@ -214,7 +214,7 @@ TEST(Column, getName)
// Create a new database
SQLite::Database db(":memory:", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE);
EXPECT_EQ(0, db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, msg TEXT)"));
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, \"first\")"));
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, 'first')"));

// Compile a SQL query, using the "id" column name as-is, but aliasing the "msg" column with new name "value"
SQLite::Statement query(db, "SELECT id, msg as value FROM test");
Expand Down Expand Up @@ -267,7 +267,7 @@ TEST(Column, shared_ptr)
// Create a new database
SQLite::Database db(":memory:", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE);
EXPECT_EQ(0, db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, msg TEXT)"));
EXPECT_EQ(1, db.exec(R"(INSERT INTO test VALUES (42, "fortytwo"))"));
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (42, 'fortytwo')"));
const char* query_str = "SELECT id, msg FROM test";

std::unique_ptr<SQLite::Statement> query{ new SQLite::Statement(db, query_str) };
Expand Down
54 changes: 27 additions & 27 deletions tests/Database_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,26 +219,26 @@ TEST(Database, exec)
EXPECT_EQ(0, db.getTotalChanges());

// first row : insert the "first" text value into new row of id 1
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, \"first\")"));
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, 'first')"));
EXPECT_EQ(1, db.getChanges());
EXPECT_EQ(1, db.getLastInsertRowid());
EXPECT_EQ(1, db.getTotalChanges());

// second row : insert the "second" text value into new row of id 2
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, \"second\")"));
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, 'second')"));
EXPECT_EQ(1, db.getChanges());
EXPECT_EQ(2, db.getLastInsertRowid());
EXPECT_EQ(2, db.getTotalChanges());

// third row : insert the "third" text value into new row of id 3
const std::string insert("INSERT INTO test VALUES (NULL, \"third\")");
const std::string insert("INSERT INTO test VALUES (NULL, 'third')");
EXPECT_EQ(1, db.exec(insert));
EXPECT_EQ(1, db.getChanges());
EXPECT_EQ(3, db.getLastInsertRowid());
EXPECT_EQ(3, db.getTotalChanges());

// update the second row : update text value to "second_updated"
EXPECT_EQ(1, db.exec("UPDATE test SET value=\"second-updated\" WHERE id='2'"));
EXPECT_EQ(1, db.exec("UPDATE test SET value='second-updated' WHERE id='2'"));
EXPECT_EQ(1, db.getChanges());
EXPECT_EQ(3, db.getLastInsertRowid()); // last inserted row ID is still 3
EXPECT_EQ(4, db.getTotalChanges());
Expand All @@ -261,14 +261,14 @@ TEST(Database, exec)
EXPECT_EQ(5, db.getTotalChanges());

// insert two rows with two *different* statements => returns only 1, ie. for the second INSERT statement
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, \"first\");INSERT INTO test VALUES (NULL, \"second\");"));
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, 'first');INSERT INTO test VALUES (NULL, 'second');"));
EXPECT_EQ(1, db.getChanges());
EXPECT_EQ(2, db.getLastInsertRowid());
EXPECT_EQ(7, db.getTotalChanges());

#if (SQLITE_VERSION_NUMBER >= 3007011)
// insert two rows with only one statement (starting with SQLite 3.7.11) => returns 2
EXPECT_EQ(2, db.exec("INSERT INTO test VALUES (NULL, \"third\"), (NULL, \"fourth\");"));
EXPECT_EQ(2, db.exec("INSERT INTO test VALUES (NULL, 'third'), (NULL, 'fourth');"));
EXPECT_EQ(2, db.getChanges());
EXPECT_EQ(4, db.getLastInsertRowid());
EXPECT_EQ(9, db.getTotalChanges());
Expand All @@ -287,26 +287,26 @@ TEST(Database, tryExec)
EXPECT_EQ(0, db.getTotalChanges());

// first row : insert the "first" text value into new row of id 1
EXPECT_EQ(SQLite::OK, db.tryExec("INSERT INTO test VALUES (NULL, \"first\")"));
EXPECT_EQ(SQLite::OK, db.tryExec("INSERT INTO test VALUES (NULL, 'first')"));
EXPECT_EQ(1, db.getChanges());
EXPECT_EQ(1, db.getLastInsertRowid());
EXPECT_EQ(1, db.getTotalChanges());

// second row : insert the "second" text value into new row of id 2
EXPECT_EQ(SQLite::OK, db.tryExec("INSERT INTO test VALUES (NULL, \"second\")"));
EXPECT_EQ(SQLite::OK, db.tryExec("INSERT INTO test VALUES (NULL, 'second')"));
EXPECT_EQ(1, db.getChanges());
EXPECT_EQ(2, db.getLastInsertRowid());
EXPECT_EQ(2, db.getTotalChanges());

// third row : insert the "third" text value into new row of id 3
const std::string insert("INSERT INTO test VALUES (NULL, \"third\")");
const std::string insert("INSERT INTO test VALUES (NULL, 'third')");
EXPECT_EQ(SQLite::OK, db.tryExec(insert));
EXPECT_EQ(1, db.getChanges());
EXPECT_EQ(3, db.getLastInsertRowid());
EXPECT_EQ(3, db.getTotalChanges());

// update the second row : update text value to "second_updated"
EXPECT_EQ(SQLite::OK, db.tryExec("UPDATE test SET value=\"second-updated\" WHERE id='2'"));
EXPECT_EQ(SQLite::OK, db.tryExec("UPDATE test SET value='second-updated' WHERE id='2'"));
EXPECT_EQ(1, db.getChanges());
EXPECT_EQ(3, db.getLastInsertRowid()); // last inserted row ID is still 3
EXPECT_EQ(4, db.getTotalChanges());
Expand All @@ -327,14 +327,14 @@ TEST(Database, tryExec)
EXPECT_EQ(5, db.getTotalChanges());

// insert two rows with two *different* statements => only 1 change, ie. for the second INSERT statement
EXPECT_EQ(SQLite::OK, db.tryExec("INSERT INTO test VALUES (NULL, \"first\");INSERT INTO test VALUES (NULL, \"second\");"));
EXPECT_EQ(SQLite::OK, db.tryExec("INSERT INTO test VALUES (NULL, 'first');INSERT INTO test VALUES (NULL, 'second');"));
EXPECT_EQ(1, db.getChanges());
EXPECT_EQ(2, db.getLastInsertRowid());
EXPECT_EQ(7, db.getTotalChanges());

#if (SQLITE_VERSION_NUMBER >= 3007011)
// insert two rows with only one statement (starting with SQLite 3.7.11)
EXPECT_EQ(SQLite::OK, db.tryExec("INSERT INTO test VALUES (NULL, \"third\"), (NULL, \"fourth\");"));
EXPECT_EQ(SQLite::OK, db.tryExec("INSERT INTO test VALUES (NULL, 'third'), (NULL, 'fourth');"));
EXPECT_EQ(2, db.getChanges());
EXPECT_EQ(4, db.getLastInsertRowid());
EXPECT_EQ(9, db.getTotalChanges());
Expand All @@ -350,14 +350,14 @@ TEST(Database, execAndGet)
db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT, weight INTEGER)");

// insert a few rows
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, \"first\", 3)"));
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, \"second\", 5)"));
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, \"third\", 7)"));
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, 'first', 3)"));
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, 'second', 5)"));
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, 'third', 7)"));

// Get a single value result with an easy to use shortcut
EXPECT_STREQ("second", db.execAndGet("SELECT value FROM test WHERE id=2"));
EXPECT_STREQ("third", db.execAndGet("SELECT value FROM test WHERE weight=7"));
const std::string query("SELECT weight FROM test WHERE value=\"first\"");
const std::string query("SELECT weight FROM test WHERE value='first'");
EXPECT_EQ(3, db.execAndGet(query).getInt());
}

Expand All @@ -369,7 +369,7 @@ TEST(Database, execException)
EXPECT_EQ(SQLite::OK, db.getExtendedErrorCode());

// exception with SQL error: "no such table"
EXPECT_THROW(db.exec("INSERT INTO test VALUES (NULL, \"first\", 3)"), SQLite::Exception);
EXPECT_THROW(db.exec("INSERT INTO test VALUES (NULL, 'first', 3)"), SQLite::Exception);
EXPECT_EQ(SQLITE_ERROR, db.getErrorCode());
EXPECT_EQ(SQLITE_ERROR, db.getExtendedErrorCode());
EXPECT_STREQ("no such table: test", db.getErrorMsg());
Expand All @@ -387,14 +387,14 @@ TEST(Database, execException)
EXPECT_STREQ("table test has 3 columns but 2 values were supplied", db.getErrorMsg());

// exception with SQL error: "No row to get a column from"
EXPECT_THROW(db.execAndGet("SELECT weight FROM test WHERE value=\"first\""), SQLite::Exception);
EXPECT_THROW(db.execAndGet("SELECT weight FROM test WHERE value='first'"), SQLite::Exception);

EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, \"first\", 3)"));
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, 'first', 3)"));
// exception with SQL error: "No row to get a column from"
EXPECT_THROW(db.execAndGet("SELECT weight FROM test WHERE value=\"second\""), SQLite::Exception);
EXPECT_THROW(db.execAndGet("SELECT weight FROM test WHERE value='second'"), SQLite::Exception);

// Add a row with more values than columns in the table: "table test has 3 columns but 4 values were supplied"
EXPECT_THROW(db.exec("INSERT INTO test VALUES (NULL, \"first\", 123, 0.123)"), SQLite::Exception);
EXPECT_THROW(db.exec("INSERT INTO test VALUES (NULL, 'first', 123, 0.123)"), SQLite::Exception);
EXPECT_EQ(SQLITE_ERROR, db.getErrorCode());
EXPECT_EQ(SQLITE_ERROR, db.getExtendedErrorCode());
EXPECT_STREQ("table test has 3 columns but 4 values were supplied", db.getErrorMsg());
Expand All @@ -408,7 +408,7 @@ TEST(Database, tryExecError)
EXPECT_EQ(SQLite::OK, db.getExtendedErrorCode());

// Insert into nonexistent table: "no such table"
EXPECT_EQ(SQLITE_ERROR, db.tryExec("INSERT INTO test VALUES (NULL, \"first\", 3)"));
EXPECT_EQ(SQLITE_ERROR, db.tryExec("INSERT INTO test VALUES (NULL, 'first', 3)"));
EXPECT_EQ(SQLITE_ERROR, db.getErrorCode());
EXPECT_EQ(SQLITE_ERROR, db.getExtendedErrorCode());
EXPECT_STREQ("no such table: test", db.getErrorMsg());
Expand All @@ -426,17 +426,17 @@ TEST(Database, tryExecError)
EXPECT_STREQ("table test has 3 columns but 2 values were supplied", db.getErrorMsg());

// Add a row with more values than columns in the table: "table test has 3 columns but 4 values were supplied"
EXPECT_EQ(SQLITE_ERROR, db.tryExec("INSERT INTO test VALUES (NULL, \"first\", 123, 0.123)"));
EXPECT_EQ(SQLITE_ERROR, db.tryExec("INSERT INTO test VALUES (NULL, 'first', 123, 0.123)"));
EXPECT_EQ(SQLITE_ERROR, db.getErrorCode());
EXPECT_EQ(SQLITE_ERROR, db.getExtendedErrorCode());
EXPECT_STREQ("table test has 3 columns but 4 values were supplied", db.getErrorMsg());

// Create a first row
EXPECT_EQ(SQLite::OK, db.tryExec("INSERT INTO test VALUES (NULL, \"first\", 3)"));
EXPECT_EQ(SQLite::OK, db.tryExec("INSERT INTO test VALUES (NULL, 'first', 3)"));
EXPECT_EQ(1, db.getLastInsertRowid());

// Try to insert a new row with the same PRIMARY KEY: "UNIQUE constraint failed: test.id"
EXPECT_EQ(SQLITE_CONSTRAINT, db.tryExec("INSERT INTO test VALUES (1, \"impossible\", 456)"));
EXPECT_EQ(SQLITE_CONSTRAINT, db.tryExec("INSERT INTO test VALUES (1, 'impossible', 456)"));
EXPECT_EQ(SQLITE_CONSTRAINT, db.getErrorCode());
EXPECT_EQ(SQLITE_CONSTRAINT_PRIMARYKEY, db.getExtendedErrorCode());
EXPECT_STREQ("UNIQUE constraint failed: test.id", db.getErrorMsg());
Expand Down Expand Up @@ -464,8 +464,8 @@ TEST(Database, createFunction)
SQLite::Database db(":memory:", SQLite::OPEN_READWRITE);
db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)");

EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, \"first\")"));
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, \"second\")"));
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, 'first')"));
EXPECT_EQ(1, db.exec("INSERT INTO test VALUES (NULL, 'second')"));

// exception with SQL error: "no such function: firstchar"
EXPECT_THROW(db.exec("SELECT firstchar(value) FROM test WHERE id=1"), SQLite::Exception);
Expand Down
Loading

0 comments on commit 58d9867

Please sign in to comment.