Changeset View
Changeset View
Standalone View
Standalone View
runners/bookmarks/fetchsqlite.cpp
Show All 23 Lines | |||||
24 | #include "bookmarks_debug.h" | 24 | #include "bookmarks_debug.h" | ||
25 | #include "bookmarksrunner_defs.h" | 25 | #include "bookmarksrunner_defs.h" | ||
26 | #include <QSqlQuery> | 26 | #include <QSqlQuery> | ||
27 | #include <QSqlError> | 27 | #include <QSqlError> | ||
28 | #include <QSqlRecord> | 28 | #include <QSqlRecord> | ||
29 | #include <QMutexLocker> | 29 | #include <QMutexLocker> | ||
30 | #include <thread> | 30 | #include <thread> | ||
31 | #include <sstream> | 31 | #include <sstream> | ||
32 | #include <QtCore/QFileInfo> | ||||
meven: Is this used here ? | |||||
32 | 33 | | |||
33 | FetchSqlite::FetchSqlite(const QString &databaseFile, QObject *parent) : | 34 | FetchSqlite::FetchSqlite(const QString &databaseFile, QObject *parent) : | ||
34 | QObject(parent), m_databaseFile(databaseFile) | 35 | QObject(parent), m_databaseFile(databaseFile) | ||
35 | { | 36 | { | ||
36 | } | 37 | } | ||
37 | 38 | | |||
38 | FetchSqlite::~FetchSqlite() | 39 | FetchSqlite::~FetchSqlite() | ||
39 | { | 40 | { | ||
Show All 35 Lines | 60 | static QSqlDatabase openDbConnection(const QString& databaseFile) { | |||
75 | db.setHostName(QStringLiteral("localhost")); | 76 | db.setHostName(QStringLiteral("localhost")); | ||
76 | db.setDatabaseName(databaseFile); | 77 | db.setDatabaseName(databaseFile); | ||
77 | db.open(); | 78 | db.open(); | ||
78 | qCDebug(RUNNER_BOOKMARKS) << "Opened connection" << connection; | 79 | qCDebug(RUNNER_BOOKMARKS) << "Opened connection" << connection; | ||
79 | 80 | | |||
80 | return db; | 81 | return db; | ||
81 | } | 82 | } | ||
82 | 83 | | |||
83 | QList<QVariantMap> FetchSqlite::query(const QString &sql, QMap<QString, QVariant> bindObjects) | 84 | QList<QVariantMap> FetchSqlite::query(const QString &sql, const QMap<QString, QVariant> &bindObjects) | ||
84 | { | 85 | { | ||
85 | QMutexLocker lock(&m_mutex); | 86 | QMutexLocker lock(&m_mutex); | ||
86 | 87 | | |||
87 | auto db = openDbConnection(m_databaseFile); | 88 | auto db = openDbConnection(m_databaseFile); | ||
88 | if (!db.isValid()) { | 89 | if (!db.isValid()) { | ||
90 | qCWarning(RUNNER_BOOKMARKS()) << "Invalid Database"; | ||||
89 | return QList<QVariantMap>(); | 91 | return QList<QVariantMap>(); | ||
90 | } | 92 | } | ||
91 | 93 | | |||
92 | //qDebug() << "query: " << sql; | | |||
93 | QSqlQuery query(db); | 94 | QSqlQuery query(db); | ||
94 | query.prepare(sql); | 95 | query.prepare(sql); | ||
95 | for (auto entry = bindObjects.constKeyValueBegin(); entry != bindObjects.constKeyValueEnd(); ++entry) { | 96 | for (auto entry = bindObjects.constKeyValueBegin(); entry != bindObjects.constKeyValueEnd(); ++entry) { | ||
96 | query.bindValue((*entry).first, (*entry).second); | 97 | query.bindValue((*entry).first, (*entry).second); | ||
97 | //qDebug() << "* Bound " << variableName << " to " << query.boundValue(variableName); | | |||
98 | } | 98 | } | ||
99 | 99 | | |||
100 | if(!query.exec()) { | 100 | if(!query.exec()) { | ||
101 | QSqlError error = db.lastError(); | 101 | qCWarning(RUNNER_BOOKMARKS()) << "Error when executing statement" << query.lastQuery() | ||
102 | //qDebug() << "query failed: " << error.text() << " (" << error.type() << ", " << error.number() << ")"; | 102 | << bindObjects << query.lastError(); | ||
103 | //qDebug() << query.lastQuery(); | 103 | return QList<QVariantMap>(); | ||
104 | } | 104 | } | ||
105 | 105 | | |||
106 | QList<QVariantMap> result; | 106 | QList<QVariantMap> result; | ||
107 | while(query.next()) { | 107 | while(query.next()) { | ||
108 | QVariantMap recordValues; | 108 | QVariantMap recordValues; | ||
109 | QSqlRecord record = query.record(); | 109 | QSqlRecord record = query.record(); | ||
110 | for(int field=0; field<record.count(); field++) { | 110 | for(int field=0; field<record.count(); field++) { | ||
111 | QVariant value = record.value(field); | 111 | QVariant value = record.value(field); | ||
Show All 15 Lines |
Is this used here ?