1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Log SQL queries

This commit is contained in:
Laurent Cozic 2017-01-02 15:17:03 +01:00
parent 629fe42c59
commit 9cd3554e4a
6 changed files with 32 additions and 31 deletions

View File

@ -22,12 +22,12 @@ void Database::initialize(const QString &path) {
upgrade();
}
QSqlQuery Database::query(const QString &sql) const {
QSqlQuery output(db_);
output.prepare(sql);
log(sql);
return output;
}
//QSqlQuery Database::query(const QString &sql) const {
// QSqlQuery output(db_);
// output.prepare(sql);
// //log(sql);
// return output;
//}
QSqlDatabase &Database::database() {
return db_;
@ -86,7 +86,7 @@ QSqlQuery Database::buildSqlQuery(Database::QueryType type, const QString &table
}
}
log(sql, query);
//log(sql, query);
// qDebug() <<"SQL:"<<sql;
@ -144,14 +144,16 @@ bool Database::commit() {
return true;
}
void Database::log(const QString &sql, const QSqlQuery &query) const {
qDebug() <<"SQL:"<<sql;
bool Database::execQuery(QSqlQuery &query) {
qDebug().noquote() << "SQL:" << query.lastQuery();
QMapIterator<QString, QVariant> i(query.boundValues());
while (i.hasNext()) {
i.next();
qDebug() << i.key() << ":" << i.value().toString();
qDebug().noquote() << "SQL:" << i.key() << "=" << i.value().toString();
}
return query.exec();
}
int Database::version() const {

View File

@ -15,18 +15,16 @@ public:
Database();
void initialize(const QString& path);
QSqlQuery query(const QString& sql) const;
QSqlDatabase& database();
QSqlQuery buildSqlQuery(Database::QueryType type, const QString& tableName, const QStringList& fields, const VariantVector& values, const QString& whereCondition = "");
QSqlQuery buildSqlQuery(Database::QueryType type, const QString& tableName, const QMap<QString, QVariant>& values, const QString& whereCondition = "");
bool errorCheck(const QSqlQuery& query);
bool transaction();
bool commit();
bool execQuery(QSqlQuery &query);
private:
void log(const QString& sql, const QSqlQuery& query = QSqlQuery()) const;
QSqlDatabase db_;
void upgrade();
int version() const;

View File

@ -25,8 +25,8 @@ int BaseModel::count(Table table) {
QVariant r = BaseModel::cacheGet(k);
if (r.isValid()) return r.toInt();
QSqlQuery q = jop::db().query("SELECT count(*) AS row_count FROM " + t);
q.exec();
QSqlQuery q("SELECT count(*) AS row_count FROM " + t);
jop::db().execQuery(q);
q.next();
int output = q.value(0).toInt();
BaseModel::cacheSet(k, QVariant(output));
@ -82,12 +82,12 @@ bool BaseModel::save() {
if (isNew) {
QSqlQuery q = jop::db().buildSqlQuery(Database::Insert, tableName, values);
q.exec();
jop::db().execQuery(q);
output = jop::db().errorCheck(q);
if (output) setValue("id", values["id"]);
} else {
QSqlQuery q = jop::db().buildSqlQuery(Database::Update, tableName, values, QString("%1 = '%2'").arg(primaryKey()).arg(value("id").toString()));
q.exec();
jop::db().execQuery(q);
output = jop::db().errorCheck(q);
}
@ -120,7 +120,7 @@ bool BaseModel::dispose() {
QSqlQuery q(jop::db().database());
q.prepare("DELETE FROM " + tableName + " WHERE " + primaryKey() + " = :id");
q.bindValue(":id", id().toString());
q.exec();
jop::db().execQuery(q);
bool output = jop::db().errorCheck(q);
@ -313,7 +313,7 @@ BaseModel::Value::Value(const QVariant &v) {
} else if (type_ == QMetaType::Int) {
intValue_ = v.toInt();
} else {
// Creates an invalid Value
// Creates an invalid Value, which is what we want
}
}

View File

@ -24,8 +24,8 @@ int Folder::count() {
}
QVector<Folder> Folder::all(const QString &orderBy) {
QSqlQuery q = jop::db().query("SELECT " + BaseModel::tableFieldNames(jop::FoldersTable).join(",") + " FROM folders ORDER BY " + orderBy);
q.exec();
QSqlQuery q("SELECT " + BaseModel::tableFieldNames(jop::FoldersTable).join(",") + " FROM folders ORDER BY " + orderBy);
jop::db().execQuery(q);
QVector<Folder> output;

View File

@ -46,13 +46,14 @@ Note NoteCollection::at(int index) const {
// TODO: cache result
int NoteCollection::count() const {
if (parentId_ == "") return 0;
return 0;
// if (parentId_ == "") return 0;
QSqlQuery q = db_.query("SELECT count(*) as row_count FROM notes WHERE parent_id = :parent_id");
q.bindValue(":parent_id", parentId_);
q.exec();
q.next();
return q.value(0).toInt();
// QSqlQuery q = db_.query("SELECT count(*) as row_count FROM notes WHERE parent_id = :parent_id");
// q.bindValue(":parent_id", parentId_);
// q.exec();
// q.next();
// return q.value(0).toInt();
}
Note NoteCollection::byId(const QString& id) const {

View File

@ -70,10 +70,10 @@ void Synchronizer::api_requestDone(const QJsonObject& response, const QString& t
}
if (action == "putFolder") {
qDebug() << "Synced folder" << id;
query = db_.query("UPDATE folders SET synced = 1 WHERE id = ?");
query.addBindValue(id);
query.exec();
// qDebug() << "Synced folder" << id;
// query = db_.query("UPDATE folders SET synced = 1 WHERE id = ?");
// query.addBindValue(id);
// query.exec();
}
if (action == "putNote") {