From 7b7de751e3239b9e082d0606b4ddb57e088faaa9 Mon Sep 17 00:00:00 2001 From: Oliver Giles Date: Sat, 23 Jul 2016 18:07:33 +0300 Subject: [PATCH] fix for new non-COW std::strings --- src/database.cpp | 6 +++--- src/database.h | 11 +++++++---- src/laminar.cpp | 3 ++- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/database.cpp b/src/database.cpp index e1865cb..8673541 100644 --- a/src/database.cpp +++ b/src/database.cpp @@ -50,14 +50,14 @@ void Database::Statement::bindValue(int i, const char* e) { sqlite3_bind_text(stmt, i, e, -1, NULL); } -void Database::Statement::bindValue(int i, std::string e) { - sqlite3_bind_blob(stmt, i, e.data(), e.size(), NULL); +void Database::Statement::bindValue(int i, const std::string& e) { + sqlite3_bind_text(stmt, i, e.data(), e.size(), NULL); } template<> std::string Database::Statement::fetchColumn(int col) { int sz = sqlite3_column_bytes(stmt, col); std::string res(sz, '\0'); - memcpy(&res[0], sqlite3_column_blob(stmt, col), sz); + memcpy(&res[0], sqlite3_column_text(stmt, col), sz); return res; } diff --git a/src/database.h b/src/database.h index a4c0c99..519af60 100644 --- a/src/database.h +++ b/src/database.h @@ -55,9 +55,12 @@ private: ~Statement(); // Bind several parameters in a single call. They are bound - // by index in the order passed into this function + // by index in the order passed into this function. Must be + // passed by reference because arguments may be std::strings, + // which must be passed by reference because sqlite requires + // the bound string's lifetime to exist until sqlite3_step template - Statement& bind(Args...args) { + Statement& bind(const Args&...args) { return bindRecursive(1, args...); } // Fetch columns. Supply a callback that will be executed for @@ -100,7 +103,7 @@ private: bool row(); template - Statement& bindRecursive(int i, T v, Args...args) { + Statement& bindRecursive(int i, const T& v, const Args&...args) { bindValue(i, v); // specialization must exist for T return bindRecursive(i + 1, args...); } @@ -112,7 +115,7 @@ private: // Bind value specializations void bindValue(int i, int e); void bindValue(int i, const char* e); - void bindValue(int i, std::string e); + void bindValue(int i, const std::string& e); // Declaration for fetch column interface, // intentionally missing definition diff --git a/src/laminar.cpp b/src/laminar.cpp index 3848cdc..5aef5a4 100644 --- a/src/laminar.cpp +++ b/src/laminar.cpp @@ -669,9 +669,10 @@ void Laminar::runFinished(Run * r) { (unsigned char*)&r->log[0], logsize); zipped.resize(zippedSize); + std::string reason = r->reason(); db->stmt("INSERT INTO builds VALUES(?,?,?,?,?,?,?,?,?,?,?,?)") .bind(r->name, r->build, node->name, r->queuedAt, r->startedAt, completedAt, int(r->result), - zipped, logsize, r->parentName, r->parentBuild, r->reason()) + zipped, logsize, r->parentName, r->parentBuild, reason) .exec(); // notify clients