Changeset View
Changeset View
Standalone View
Standalone View
runners/bookmarks/fetchsqlite.cpp
Show All 19 Lines | |||||
20 | 20 | | |||
21 | #include "fetchsqlite.h" | 21 | #include "fetchsqlite.h" | ||
22 | #include <QFile> | 22 | #include <QFile> | ||
23 | #include <QDebug> | 23 | #include <QDebug> | ||
24 | #include "bookmarksrunner_defs.h" | 24 | #include "bookmarksrunner_defs.h" | ||
25 | #include <QSqlQuery> | 25 | #include <QSqlQuery> | ||
26 | #include <QSqlError> | 26 | #include <QSqlError> | ||
27 | #include <QSqlRecord> | 27 | #include <QSqlRecord> | ||
28 | #include <QMutexLocker> | ||||
28 | 29 | | |||
29 | FetchSqlite::FetchSqlite(const QString &originalFilePath, const QString ©To, QObject *parent) : | 30 | FetchSqlite::FetchSqlite(const QString &originalFilePath, const QString ©To, QObject *parent) : | ||
30 | QObject(parent), m_databaseFile(copyTo) | 31 | QObject(parent), m_databaseFile(copyTo) | ||
31 | { | 32 | { | ||
32 | m_db = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"), originalFilePath); | 33 | m_db = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"), originalFilePath); | ||
33 | m_db.setHostName(QStringLiteral("localhost")); | 34 | m_db.setHostName(QStringLiteral("localhost")); | ||
34 | 35 | | |||
35 | QFile originalFile(originalFilePath); | 36 | QFile originalFile(originalFilePath); | ||
36 | QFile(copyTo).remove(); | 37 | QFile(copyTo).remove(); | ||
37 | bool couldCopy = originalFile.copy(copyTo); | 38 | bool couldCopy = originalFile.copy(copyTo); | ||
38 | if(!couldCopy) { | 39 | if(!couldCopy) { | ||
39 | //qDebug() << "error copying favicon database from " << originalFile.fileName() << " to " << copyTo; | 40 | //qDebug() << "error copying favicon database from " << originalFile.fileName() << " to " << copyTo; | ||
40 | //qDebug() << originalFile.errorString(); | 41 | //qDebug() << originalFile.errorString(); | ||
41 | } | 42 | } | ||
42 | } | 43 | } | ||
43 | 44 | | |||
44 | FetchSqlite::~FetchSqlite() | 45 | FetchSqlite::~FetchSqlite() | ||
45 | { | 46 | { | ||
46 | if(m_db.isOpen()) m_db.close(); | 47 | if(m_db.isOpen()) m_db.close(); | ||
47 | QFile(m_databaseFile).remove(); | 48 | QFile(m_databaseFile).remove(); | ||
48 | } | 49 | } | ||
49 | 50 | | |||
50 | void FetchSqlite::prepare() | 51 | void FetchSqlite::prepare() | ||
51 | { | 52 | { | ||
53 | QMutexLocker lock(&m_mutex); | ||||
52 | m_db.setDatabaseName(m_databaseFile); | 54 | m_db.setDatabaseName(m_databaseFile); | ||
53 | bool ok = m_db.open(); | 55 | bool ok = m_db.open(); | ||
54 | //qDebug() << "Sqlite Database " << m_databaseFile << " was opened: " << ok; | 56 | //qDebug() << "Sqlite Database " << m_databaseFile << " was opened: " << ok; | ||
55 | if(!ok) { | 57 | if(!ok) { | ||
56 | //qDebug() << "Error: " << m_db.lastError().text(); | 58 | //qDebug() << "Error: " << m_db.lastError().text(); | ||
57 | } | 59 | } | ||
58 | } | 60 | } | ||
59 | 61 | | |||
60 | void FetchSqlite::teardown() | 62 | void FetchSqlite::teardown() | ||
61 | { | 63 | { | ||
64 | QMutexLocker lock(&m_mutex); | ||||
62 | m_db.close(); | 65 | m_db.close(); | ||
63 | } | 66 | } | ||
64 | 67 | | |||
65 | QList<QVariantMap> FetchSqlite::query(BuildQuery *buildQuery, QMap<QString, QVariant> bindObjects) | 68 | QList<QVariantMap> FetchSqlite::query(BuildQuery *buildQuery, QMap<QString, QVariant> bindObjects) | ||
66 | { | 69 | { | ||
67 | return query(buildQuery->query(&m_db), bindObjects); | 70 | return query(buildQuery->query(&m_db), bindObjects); | ||
68 | } | 71 | } | ||
69 | 72 | | |||
70 | QList<QVariantMap> FetchSqlite::query(const QString &sql, QMap<QString, QVariant> bindObjects) | 73 | QList<QVariantMap> FetchSqlite::query(const QString &sql, QMap<QString, QVariant> bindObjects) | ||
71 | { | 74 | { | ||
75 | QMutexLocker lock(&m_mutex); | ||||
76 | | ||||
72 | //qDebug() << "query: " << sql; | 77 | //qDebug() << "query: " << sql; | ||
73 | QSqlQuery query(m_db); | 78 | QSqlQuery query(m_db); | ||
74 | query.prepare(sql); | 79 | query.prepare(sql); | ||
75 | foreach(const QString &variableName, bindObjects.keys()) { | 80 | foreach(const QString &variableName, bindObjects.keys()) { | ||
76 | query.bindValue(variableName, bindObjects.value(variableName)); | 81 | query.bindValue(variableName, bindObjects.value(variableName)); | ||
77 | //qDebug() << "* Bound " << variableName << " to " << query.boundValue(variableName); | 82 | //qDebug() << "* Bound " << variableName << " to " << query.boundValue(variableName); | ||
78 | } | 83 | } | ||
79 | 84 | | |||
Show All 18 Lines |