mirror of https://github.com/laurent22/joplin.git
Log SQL queries
parent
629fe42c59
commit
9cd3554e4a
|
@ -22,12 +22,12 @@ void Database::initialize(const QString &path) {
|
||||||
upgrade();
|
upgrade();
|
||||||
}
|
}
|
||||||
|
|
||||||
QSqlQuery Database::query(const QString &sql) const {
|
//QSqlQuery Database::query(const QString &sql) const {
|
||||||
QSqlQuery output(db_);
|
// QSqlQuery output(db_);
|
||||||
output.prepare(sql);
|
// output.prepare(sql);
|
||||||
log(sql);
|
// //log(sql);
|
||||||
return output;
|
// return output;
|
||||||
}
|
//}
|
||||||
|
|
||||||
QSqlDatabase &Database::database() {
|
QSqlDatabase &Database::database() {
|
||||||
return db_;
|
return db_;
|
||||||
|
@ -86,7 +86,7 @@ QSqlQuery Database::buildSqlQuery(Database::QueryType type, const QString &table
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log(sql, query);
|
//log(sql, query);
|
||||||
|
|
||||||
// qDebug() <<"SQL:"<<sql;
|
// qDebug() <<"SQL:"<<sql;
|
||||||
|
|
||||||
|
@ -144,14 +144,16 @@ bool Database::commit() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Database::log(const QString &sql, const QSqlQuery &query) const {
|
bool Database::execQuery(QSqlQuery &query) {
|
||||||
qDebug() <<"SQL:"<<sql;
|
qDebug().noquote() << "SQL:" << query.lastQuery();
|
||||||
|
|
||||||
QMapIterator<QString, QVariant> i(query.boundValues());
|
QMapIterator<QString, QVariant> i(query.boundValues());
|
||||||
while (i.hasNext()) {
|
while (i.hasNext()) {
|
||||||
i.next();
|
i.next();
|
||||||
qDebug() << i.key() << ":" << i.value().toString();
|
qDebug().noquote() << "SQL:" << i.key() << "=" << i.value().toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return query.exec();
|
||||||
}
|
}
|
||||||
|
|
||||||
int Database::version() const {
|
int Database::version() const {
|
||||||
|
|
|
@ -15,18 +15,16 @@ public:
|
||||||
|
|
||||||
Database();
|
Database();
|
||||||
void initialize(const QString& path);
|
void initialize(const QString& path);
|
||||||
QSqlQuery query(const QString& sql) const;
|
|
||||||
QSqlDatabase& database();
|
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 QStringList& fields, const VariantVector& values, const QString& whereCondition = "");
|
||||||
QSqlQuery buildSqlQuery(Database::QueryType type, const QString& tableName, const QMap<QString, QVariant>& 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 errorCheck(const QSqlQuery& query);
|
||||||
bool transaction();
|
bool transaction();
|
||||||
bool commit();
|
bool commit();
|
||||||
|
bool execQuery(QSqlQuery &query);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
void log(const QString& sql, const QSqlQuery& query = QSqlQuery()) const;
|
|
||||||
|
|
||||||
QSqlDatabase db_;
|
QSqlDatabase db_;
|
||||||
void upgrade();
|
void upgrade();
|
||||||
int version() const;
|
int version() const;
|
||||||
|
|
|
@ -25,8 +25,8 @@ int BaseModel::count(Table table) {
|
||||||
QVariant r = BaseModel::cacheGet(k);
|
QVariant r = BaseModel::cacheGet(k);
|
||||||
if (r.isValid()) return r.toInt();
|
if (r.isValid()) return r.toInt();
|
||||||
|
|
||||||
QSqlQuery q = jop::db().query("SELECT count(*) AS row_count FROM " + t);
|
QSqlQuery q("SELECT count(*) AS row_count FROM " + t);
|
||||||
q.exec();
|
jop::db().execQuery(q);
|
||||||
q.next();
|
q.next();
|
||||||
int output = q.value(0).toInt();
|
int output = q.value(0).toInt();
|
||||||
BaseModel::cacheSet(k, QVariant(output));
|
BaseModel::cacheSet(k, QVariant(output));
|
||||||
|
@ -82,12 +82,12 @@ bool BaseModel::save() {
|
||||||
|
|
||||||
if (isNew) {
|
if (isNew) {
|
||||||
QSqlQuery q = jop::db().buildSqlQuery(Database::Insert, tableName, values);
|
QSqlQuery q = jop::db().buildSqlQuery(Database::Insert, tableName, values);
|
||||||
q.exec();
|
jop::db().execQuery(q);
|
||||||
output = jop::db().errorCheck(q);
|
output = jop::db().errorCheck(q);
|
||||||
if (output) setValue("id", values["id"]);
|
if (output) setValue("id", values["id"]);
|
||||||
} else {
|
} else {
|
||||||
QSqlQuery q = jop::db().buildSqlQuery(Database::Update, tableName, values, QString("%1 = '%2'").arg(primaryKey()).arg(value("id").toString()));
|
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);
|
output = jop::db().errorCheck(q);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,7 +120,7 @@ bool BaseModel::dispose() {
|
||||||
QSqlQuery q(jop::db().database());
|
QSqlQuery q(jop::db().database());
|
||||||
q.prepare("DELETE FROM " + tableName + " WHERE " + primaryKey() + " = :id");
|
q.prepare("DELETE FROM " + tableName + " WHERE " + primaryKey() + " = :id");
|
||||||
q.bindValue(":id", id().toString());
|
q.bindValue(":id", id().toString());
|
||||||
q.exec();
|
jop::db().execQuery(q);
|
||||||
|
|
||||||
bool output = jop::db().errorCheck(q);
|
bool output = jop::db().errorCheck(q);
|
||||||
|
|
||||||
|
@ -313,7 +313,7 @@ BaseModel::Value::Value(const QVariant &v) {
|
||||||
} else if (type_ == QMetaType::Int) {
|
} else if (type_ == QMetaType::Int) {
|
||||||
intValue_ = v.toInt();
|
intValue_ = v.toInt();
|
||||||
} else {
|
} else {
|
||||||
// Creates an invalid Value
|
// Creates an invalid Value, which is what we want
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,8 +24,8 @@ int Folder::count() {
|
||||||
}
|
}
|
||||||
|
|
||||||
QVector<Folder> Folder::all(const QString &orderBy) {
|
QVector<Folder> Folder::all(const QString &orderBy) {
|
||||||
QSqlQuery q = jop::db().query("SELECT " + BaseModel::tableFieldNames(jop::FoldersTable).join(",") + " FROM folders ORDER BY " + orderBy);
|
QSqlQuery q("SELECT " + BaseModel::tableFieldNames(jop::FoldersTable).join(",") + " FROM folders ORDER BY " + orderBy);
|
||||||
q.exec();
|
jop::db().execQuery(q);
|
||||||
|
|
||||||
QVector<Folder> output;
|
QVector<Folder> output;
|
||||||
|
|
||||||
|
|
|
@ -46,13 +46,14 @@ Note NoteCollection::at(int index) const {
|
||||||
|
|
||||||
// TODO: cache result
|
// TODO: cache result
|
||||||
int NoteCollection::count() const {
|
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");
|
// QSqlQuery q = db_.query("SELECT count(*) as row_count FROM notes WHERE parent_id = :parent_id");
|
||||||
q.bindValue(":parent_id", parentId_);
|
// q.bindValue(":parent_id", parentId_);
|
||||||
q.exec();
|
// q.exec();
|
||||||
q.next();
|
// q.next();
|
||||||
return q.value(0).toInt();
|
// return q.value(0).toInt();
|
||||||
}
|
}
|
||||||
|
|
||||||
Note NoteCollection::byId(const QString& id) const {
|
Note NoteCollection::byId(const QString& id) const {
|
||||||
|
|
|
@ -70,10 +70,10 @@ void Synchronizer::api_requestDone(const QJsonObject& response, const QString& t
|
||||||
}
|
}
|
||||||
|
|
||||||
if (action == "putFolder") {
|
if (action == "putFolder") {
|
||||||
qDebug() << "Synced folder" << id;
|
// qDebug() << "Synced folder" << id;
|
||||||
query = db_.query("UPDATE folders SET synced = 1 WHERE id = ?");
|
// query = db_.query("UPDATE folders SET synced = 1 WHERE id = ?");
|
||||||
query.addBindValue(id);
|
// query.addBindValue(id);
|
||||||
query.exec();
|
// query.exec();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (action == "putNote") {
|
if (action == "putNote") {
|
||||||
|
|
Loading…
Reference in New Issue