UI: Simplify the query bindings

This commit is contained in:
Nodir Temirkhodjaev 2023-08-14 18:28:39 +05:00
parent e70954b80d
commit 274e8f377a
6 changed files with 25 additions and 42 deletions

View File

@ -120,15 +120,14 @@ bool SqliteDb::executeStr(const QString &sql)
return execute(sqlUtf8.data()); return execute(sqlUtf8.data());
} }
QVariant SqliteDb::executeEx(const char *sql, const QVariantList &vars, const QVariantMap &varsMap, QVariant SqliteDb::executeEx(const char *sql, const QVariantList &vars, int resultCount, bool *ok)
int resultCount, bool *ok)
{ {
QVariantList list; QVariantList list;
SqliteStmt stmt; SqliteStmt stmt;
bool success = false; bool success = false;
if (prepare(stmt, sql, vars, varsMap)) { if (prepare(stmt, sql, vars)) {
const auto stepRes = stmt.step(); const auto stepRes = stmt.step();
success = (stepRes != SqliteStmt::StepError); success = (stepRes != SqliteStmt::StepError);
@ -149,13 +148,7 @@ QVariant SqliteDb::executeEx(const char *sql, const QVariantList &vars, const QV
return (listSize == 0) ? QVariant() : (listSize == 1 ? list.at(0) : list); return (listSize == 0) ? QVariant() : (listSize == 1 ? list.at(0) : list);
} }
QVariant SqliteDb::executeEx(const char *sql, const QVariantList &vars, int resultCount, bool *ok) bool SqliteDb::prepare(SqliteStmt &stmt, const char *sql, const QVariantList &vars)
{
return executeEx(sql, vars, {}, resultCount, ok);
}
bool SqliteDb::prepare(
SqliteStmt &stmt, const char *sql, const QVariantList &vars, const QVariantMap &varsMap)
{ {
if (!stmt.prepare(db(), sql)) if (!stmt.prepare(db(), sql))
return false; return false;
@ -163,17 +156,13 @@ bool SqliteDb::prepare(
if (!(vars.isEmpty() || stmt.bindVars(vars))) if (!(vars.isEmpty() || stmt.bindVars(vars)))
return false; return false;
if (!(varsMap.isEmpty() || stmt.bindVarsMap(varsMap)))
return false;
return true; return true;
} }
bool SqliteDb::prepare( bool SqliteDb::prepare(SqliteStmt &stmt, const QString &sql, const QVariantList &vars)
SqliteStmt &stmt, const QString &sql, const QVariantList &vars, const QVariantMap &varsMap)
{ {
const auto sqlUtf8 = sql.toUtf8(); const auto sqlUtf8 = sql.toUtf8();
return prepare(stmt, sqlUtf8.constData(), vars, varsMap); return prepare(stmt, sqlUtf8.constData(), vars);
} }
bool SqliteDb::done(SqliteStmt *stmt) bool SqliteDb::done(SqliteStmt *stmt)

View File

@ -76,15 +76,11 @@ public:
bool execute(const char *sql); bool execute(const char *sql);
bool executeStr(const QString &sql); bool executeStr(const QString &sql);
QVariant executeEx(const char *sql, const QVariantList &vars, const QVariantMap &varsMap,
int resultCount = 1, bool *ok = nullptr);
QVariant executeEx(const char *sql, const QVariantList &vars = {}, int resultCount = 1, QVariant executeEx(const char *sql, const QVariantList &vars = {}, int resultCount = 1,
bool *ok = nullptr); bool *ok = nullptr);
bool prepare(SqliteStmt &stmt, const char *sql, const QVariantList &vars = {}, bool prepare(SqliteStmt &stmt, const char *sql, const QVariantList &vars = {});
const QVariantMap &varsMap = {}); bool prepare(SqliteStmt &stmt, const QString &sql, const QVariantList &vars = {});
bool prepare(SqliteStmt &stmt, const QString &sql, const QVariantList &vars = {},
const QVariantMap &varsMap = {});
bool done(SqliteStmt *stmt); bool done(SqliteStmt *stmt);
qint64 lastInsertRowid() const; qint64 lastInsertRowid() const;

View File

@ -297,11 +297,10 @@ QIcon AppListModel::appStateIcon(const AppRow &appRow)
return IconCache::icon(appStateIconPath(appRow)); return IconCache::icon(appStateIconPath(appRow));
} }
bool AppListModel::updateAppRow(const QString &sql, const QVariantList &vars, bool AppListModel::updateAppRow(const QString &sql, const QVariantList &vars, AppRow &appRow) const
const QVariantMap &varsMap, AppRow &appRow) const
{ {
SqliteStmt stmt; SqliteStmt stmt;
if (!(sqliteDb()->prepare(stmt, sql, vars, varsMap) && stmt.step() == SqliteStmt::StepRow)) { if (!(sqliteDb()->prepare(stmt, sql, vars) && stmt.step() == SqliteStmt::StepRow)) {
appRow.invalidate(); appRow.invalidate();
return false; return false;
} }
@ -335,7 +334,7 @@ const AppRow &AppListModel::appRowAt(int row) const
AppRow AppListModel::appRowById(qint64 appId) const AppRow AppListModel::appRowById(qint64 appId) const
{ {
AppRow appRow; AppRow appRow;
updateAppRow(sqlBase() + " WHERE t.app_id = ?1;", { appId }, {}, appRow); updateAppRow(sqlBase() + " WHERE t.app_id = ?1;", { appId }, appRow);
return appRow; return appRow;
} }
@ -344,7 +343,7 @@ AppRow AppListModel::appRowByPath(const QString &appPath) const
const QString normPath = FileUtil::normalizePath(appPath); const QString normPath = FileUtil::normalizePath(appPath);
AppRow appRow; AppRow appRow;
if (!updateAppRow(sqlBase() + " WHERE t.path = ?1;", { normPath }, {}, appRow)) { if (!updateAppRow(sqlBase() + " WHERE t.path = ?1;", { normPath }, appRow)) {
appRow.appOriginPath = appPath; appRow.appOriginPath = appPath;
appRow.appPath = normPath; appRow.appPath = normPath;
} }
@ -353,17 +352,17 @@ AppRow AppListModel::appRowByPath(const QString &appPath) const
bool AppListModel::updateTableRow(int row) const bool AppListModel::updateTableRow(int row) const
{ {
QVariantMap varsMap; QVariantList vars;
varsMap.insert(":row", row); fillSqlVars(vars);
fillSqlVars(varsMap); vars.append(row); // must be a last one!
return updateAppRow(sql(), {}, varsMap, m_appRow); return updateAppRow(sql(), vars, m_appRow);
} }
void AppListModel::fillSqlVars(QVariantMap &varsMap) const void AppListModel::fillSqlVars(QVariantList &vars) const
{ {
if (!ftsFilterMatch().isEmpty()) { if (!ftsFilterMatch().isEmpty()) {
varsMap.insert(":match", ftsFilterMatch()); vars.append(ftsFilterMatch());
} }
} }

View File

@ -49,7 +49,7 @@ protected:
bool updateTableRow(int row) const override; bool updateTableRow(int row) const override;
TableRow &tableRow() const override { return m_appRow; } TableRow &tableRow() const override { return m_appRow; }
void fillSqlVars(QVariantMap &varsMap) const override; void fillSqlVars(QVariantList &vars) const override;
QString sqlBase() const override; QString sqlBase() const override;
QString sqlWhere() const override; QString sqlWhere() const override;
@ -71,8 +71,7 @@ private:
static QColor appStateColor(const AppRow &appRow); static QColor appStateColor(const AppRow &appRow);
static QIcon appStateIcon(const AppRow &appRow); static QIcon appStateIcon(const AppRow &appRow);
bool updateAppRow(const QString &sql, const QVariantList &vars, const QVariantMap &varsMap, bool updateAppRow(const QString &sql, const QVariantList &vars, AppRow &appRow) const;
AppRow &appRow) const;
private: private:
QString m_ftsFilter; QString m_ftsFilter;

View File

@ -33,19 +33,19 @@ void TableSqlModel::invalidateRowCache()
emit modelChanged(); emit modelChanged();
} }
void TableSqlModel::fillSqlVars(QVariantMap &varsMap) const void TableSqlModel::fillSqlVars(QVariantList &vars) const
{ {
Q_UNUSED(varsMap); Q_UNUSED(vars);
} }
int TableSqlModel::doSqlCount() const int TableSqlModel::doSqlCount() const
{ {
QVariantMap varsMap; QVariantList vars;
fillSqlVars(varsMap); fillSqlVars(vars);
const auto sqlUtf8 = sqlCount().toUtf8(); const auto sqlUtf8 = sqlCount().toUtf8();
return sqliteDb()->executeEx(sqlUtf8, {}, varsMap).toInt(); return sqliteDb()->executeEx(sqlUtf8, vars).toInt();
} }
QString TableSqlModel::sqlCount() const QString TableSqlModel::sqlCount() const

View File

@ -24,7 +24,7 @@ signals:
protected: protected:
void invalidateRowCache() override; void invalidateRowCache() override;
virtual void fillSqlVars(QVariantMap &varsMap) const; virtual void fillSqlVars(QVariantList &vars) const;
virtual int doSqlCount() const; virtual int doSqlCount() const;
virtual QString sqlCount() const; virtual QString sqlCount() const;