commit be23ec87e25b8d7e320ddafb812c514586abf995
parent 9707ac792ef7823d47644223cd1488528dbc52a7
Author: cfillion <cfillion@users.noreply.github.com>
Date: Tue, 19 Jan 2016 12:02:09 -0500
don't assume strings will outlive a query
this fix weird "\x5" characters from being inserted in the DB as file
paths randomly
Diffstat:
2 files changed, 24 insertions(+), 2 deletions(-)
diff --git a/src/database.cpp b/src/database.cpp
@@ -110,7 +110,7 @@ Statement::~Statement()
void Statement::bind(const int index, const string &text)
{
- if(sqlite3_bind_text(m_stmt, index, text.c_str(), -1, SQLITE_STATIC))
+ if(sqlite3_bind_text(m_stmt, index, text.c_str(), -1, SQLITE_TRANSIENT))
throw m_db->lastError();
}
diff --git a/test/database.cpp b/test/database.cpp
@@ -76,7 +76,7 @@ TEST_CASE("get rows from prepared statement", M) {
}
}
-TEST_CASE("bing values and clear", M) {
+TEST_CASE("bind values and clear", M) {
Database db;
db.exec("CREATE TABLE test (value TEXT NOT NULL)");
@@ -138,3 +138,25 @@ TEST_CASE("last insert id", M) {
insert->exec();
REQUIRE(db.lastInsertId() == 2);
}
+
+TEST_CASE("bind temporary strings", M) {
+ Database db;
+ db.exec("CREATE TABLE a(text TEXT NOT NULL)");
+
+ Statement *insert = db.prepare("INSERT INTO a VALUES(?)");
+
+ string str("hello");
+ insert->bind(1, str);
+ str = "world";
+
+ insert->exec();
+
+ string got;
+ Statement *select = db.prepare("SELECT text FROM a LIMIT 1");
+ select->exec([&] {
+ got = select->stringColumn(0);
+ return false;
+ });
+
+ REQUIRE(got == "hello");
+}