Commit c846e79e authored by Lukas Appelhans's avatar Lukas Appelhans
Browse files

Use proper bindings instead of faked ones and lowercase keys

parent 68f1ffa4
...@@ -44,18 +44,39 @@ SQLiteConnection::~SQLiteConnection() ...@@ -44,18 +44,39 @@ SQLiteConnection::~SQLiteConnection()
SQLiteResource SQLiteConnection::query(QString q) SQLiteResource SQLiteConnection::query(QString q)
{ {
rows.clear(); rows.clear();
QString bound = translateBindings(q); /* translates back to their original value */
bindings.clear();
/* /*
* Prepares the statement. _v2 assures us a better error checking * Prepares the statement. _v2 assures us a better error checking
* To future developers: don't fall in the temptation of calling bound.toStdString().c_str() to make things shorter. * To future developers: don't fall in the temptation of calling bound.toStdString().c_str() to make things shorter.
* If you do that, then good luck finding out why akabei is suddenly full of syntax errors :D * If you do that, then good luck finding out why akabei is suddenly full of syntax errors :D
*/ */
std::string tmp = bound.toStdString(); std::string tmp = q.toStdString();
const char* query(tmp.c_str()); const char* query(tmp.c_str());
int retvalue = sqlite3_prepare_v2(dbHandle, query, -1, &statement, NULL); int retvalue = sqlite3_prepare_v2(dbHandle, query, -1, &statement, NULL);
checkForErrors(retvalue); checkForErrors(retvalue);
{
QMap<QString, int>::iterator it = intBindings.begin();
QMap<QString, int>::iterator end = intBindings.end();
for ( ; it != end; it++) {
std::string key = it.key().toStdString();
int ret = sqlite3_bind_int64(statement, sqlite3_bind_parameter_index(statement, key.c_str()), it.value());
checkForErrors(ret);
}
intBindings.clear();
}
{
QMap<QString, QString>::iterator it = stringBindings.begin();
QMap<QString, QString>::iterator end = stringBindings.end();
for ( ; it != end; it++) {
std::string key = it.key().toStdString();
int ret = sqlite3_bind_text16(statement, sqlite3_bind_parameter_index(statement, key.c_str()), it.value().utf16(),
(it.value().size() + 1) * sizeof(QChar), SQLITE_TRANSIENT);
checkForErrors(ret);
}
stringBindings.clear();
}
while ((retvalue = sqlite3_step(statement)) == SQLITE_ROW) { while ((retvalue = sqlite3_step(statement)) == SQLITE_ROW) {
rows.append(getRowFromStatement(statement)); rows.append(getRowFromStatement(statement));
...@@ -130,48 +151,32 @@ qint64 SQLiteConnection::getLastRowId() ...@@ -130,48 +151,32 @@ qint64 SQLiteConnection::getLastRowId()
*/ */
void SQLiteConnection::bind(const QString& name, int value) void SQLiteConnection::bind(const QString& name, int value)
{ {
if (bindings.contains(name)) if (intBindings.contains(name) || stringBindings.contains(name))
{ {
return; return;
} }
QVariant v(value); intBindings.insert(name, value);
bindings.insert(name, v);
} }
void SQLiteConnection::bind(const QString& name, QString value) void SQLiteConnection::bind(const QString& name, QString value)
{ {
if (bindings.contains(name)) if (intBindings.contains(name) || stringBindings.contains(name))
{ {
return; return;
} }
QVariant v("'" + value + "'"); stringBindings.insert(name, value);
bindings.insert(name, v);
} }
void SQLiteConnection::bind(const QString& name, QByteArray value) void SQLiteConnection::bind(const QString& name, QByteArray value)
{ {
if (bindings.contains(name)) if (intBindings.contains(name) || stringBindings.contains(name))
{ {
return; return;
} }
QVariant v("'" + value + "'"); stringBindings.insert(name, value);
bindings.insert(name, v);
}
QString SQLiteConnection::translateBindings(QString query)
{
QList<QString> keys = bindings.keys();
for (QList<QString>::iterator it = keys.begin(); it < keys.end(); it++)
{
QString key = (*it);
query.replace(key, bindings[key].toString());
}
return query;
} }
void SQLiteConnection::checkForErrors(int r) void SQLiteConnection::checkForErrors(int r)
......
...@@ -56,10 +56,10 @@ class SQLiteConnection ...@@ -56,10 +56,10 @@ class SQLiteConnection
sqlite3 *dbHandle; sqlite3 *dbHandle;
sqlite3_stmt *statement; sqlite3_stmt *statement;
QList<Row> rows; QList<Row> rows;
QVariantMap bindings; QMap<QString, int> intBindings;
QMap<QString, QString> stringBindings;
void checkForErrors(int); void checkForErrors(int);
QString translateBindings(QString);
Row getRowFromStatement(sqlite3_stmt*); Row getRowFromStatement(sqlite3_stmt*);
// Disable copy constructor and default constructor // Disable copy constructor and default constructor
...@@ -70,7 +70,7 @@ class SQLiteConnection ...@@ -70,7 +70,7 @@ class SQLiteConnection
SQLiteConnection(QString const&, bool); SQLiteConnection(QString const&, bool);
~SQLiteConnection(); ~SQLiteConnection();
SQLiteResource query(QString);; SQLiteResource query(QString);
qint64 getLastRowId(); qint64 getLastRowId();
void bind(QString const&, int); void bind(QString const&, int);
......
...@@ -1091,7 +1091,7 @@ int QueryPerformer::insertScriptlet(Database *db, Package* p) ...@@ -1091,7 +1091,7 @@ int QueryPerformer::insertScriptlet(Database *db, Package* p)
int QueryPerformer::insertScriptlet(SQLiteConnection &dbConnection, Package* p) int QueryPerformer::insertScriptlet(SQLiteConnection &dbConnection, Package* p)
{ {
QString sqlQuery = "INSERT INTO scriptlets ('Package', 'Scriptlet') VALUES (:Package, :Scriptlet)"; QString sqlQuery = "INSERT INTO scriptlets (`package`, `scriptlet`) VALUES (:Package, :Scriptlet)";
try { try {
dbConnection.bind(":Package", p->databaseId()); dbConnection.bind(":Package", p->databaseId());
...@@ -1112,7 +1112,7 @@ int QueryPerformer::updateScriptlet(Database *db, Package* from, Package *to) ...@@ -1112,7 +1112,7 @@ int QueryPerformer::updateScriptlet(Database *db, Package* from, Package *to)
int QueryPerformer::updateScriptlet(SQLiteConnection &dbConnection, Package* from, Package* to) int QueryPerformer::updateScriptlet(SQLiteConnection &dbConnection, Package* from, Package* to)
{ {
QString sqlQuery = "UPDATE scriptlets SET Scriptlet=:Scriptlet WHERE Package=:Package"; QString sqlQuery = "UPDATE scriptlets SET scriptlet=:Scriptlet WHERE package=:Package";
try { try {
dbConnection.bind(":Package", from->databaseId()); dbConnection.bind(":Package", from->databaseId());
...@@ -1133,7 +1133,7 @@ int QueryPerformer::removeScriptlet(Database *db, Package* p) ...@@ -1133,7 +1133,7 @@ int QueryPerformer::removeScriptlet(Database *db, Package* p)
int QueryPerformer::removeScriptlet(SQLiteConnection &dbConnection, Package* p) int QueryPerformer::removeScriptlet(SQLiteConnection &dbConnection, Package* p)
{ {
QString sqlQuery = "DELETE FROM scriptlets WHERE Package = :Package"; QString sqlQuery = "DELETE FROM scriptlets WHERE package = :Package";
try { try {
dbConnection.bind(":Package", p->databaseId()); dbConnection.bind(":Package", p->databaseId());
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment