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());
}
QVariant SqliteDb::executeEx(const char *sql, const QVariantList &vars, const QVariantMap &varsMap,
int resultCount, bool *ok)
QVariant SqliteDb::executeEx(const char *sql, const QVariantList &vars, int resultCount, bool *ok)
{
QVariantList list;
SqliteStmt stmt;
bool success = false;
if (prepare(stmt, sql, vars, varsMap)) {
if (prepare(stmt, sql, vars)) {
const auto stepRes = stmt.step();
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);
}
QVariant SqliteDb::executeEx(const char *sql, const QVariantList &vars, int resultCount, bool *ok)
{
return executeEx(sql, vars, {}, resultCount, ok);
}
bool SqliteDb::prepare(
SqliteStmt &stmt, const char *sql, const QVariantList &vars, const QVariantMap &varsMap)
bool SqliteDb::prepare(SqliteStmt &stmt, const char *sql, const QVariantList &vars)
{
if (!stmt.prepare(db(), sql))
return false;
@ -163,17 +156,13 @@ bool SqliteDb::prepare(
if (!(vars.isEmpty() || stmt.bindVars(vars)))
return false;
if (!(varsMap.isEmpty() || stmt.bindVarsMap(varsMap)))
return false;
return true;
}
bool SqliteDb::prepare(
SqliteStmt &stmt, const QString &sql, const QVariantList &vars, const QVariantMap &varsMap)
bool SqliteDb::prepare(SqliteStmt &stmt, const QString &sql, const QVariantList &vars)
{
const auto sqlUtf8 = sql.toUtf8();
return prepare(stmt, sqlUtf8.constData(), vars, varsMap);
return prepare(stmt, sqlUtf8.constData(), vars);
}
bool SqliteDb::done(SqliteStmt *stmt)

View File

@ -76,15 +76,11 @@ public:
bool execute(const char *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,
bool *ok = nullptr);
bool prepare(SqliteStmt &stmt, const char *sql, const QVariantList &vars = {},
const QVariantMap &varsMap = {});
bool prepare(SqliteStmt &stmt, const QString &sql, const QVariantList &vars = {},
const QVariantMap &varsMap = {});
bool prepare(SqliteStmt &stmt, const char *sql, const QVariantList &vars = {});
bool prepare(SqliteStmt &stmt, const QString &sql, const QVariantList &vars = {});
bool done(SqliteStmt *stmt);
qint64 lastInsertRowid() const;

View File

@ -297,11 +297,10 @@ QIcon AppListModel::appStateIcon(const AppRow &appRow)
return IconCache::icon(appStateIconPath(appRow));
}
bool AppListModel::updateAppRow(const QString &sql, const QVariantList &vars,
const QVariantMap &varsMap, AppRow &appRow) const
bool AppListModel::updateAppRow(const QString &sql, const QVariantList &vars, AppRow &appRow) const
{
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();
return false;
}
@ -335,7 +334,7 @@ const AppRow &AppListModel::appRowAt(int row) const
AppRow AppListModel::appRowById(qint64 appId) const
{
AppRow appRow;
updateAppRow(sqlBase() + " WHERE t.app_id = ?1;", { appId }, {}, appRow);
updateAppRow(sqlBase() + " WHERE t.app_id = ?1;", { appId }, appRow);
return appRow;
}
@ -344,7 +343,7 @@ AppRow AppListModel::appRowByPath(const QString &appPath) const
const QString normPath = FileUtil::normalizePath(appPath);
AppRow appRow;
if (!updateAppRow(sqlBase() + " WHERE t.path = ?1;", { normPath }, {}, appRow)) {
if (!updateAppRow(sqlBase() + " WHERE t.path = ?1;", { normPath }, appRow)) {
appRow.appOriginPath = appPath;
appRow.appPath = normPath;
}
@ -353,17 +352,17 @@ AppRow AppListModel::appRowByPath(const QString &appPath) const
bool AppListModel::updateTableRow(int row) const
{
QVariantMap varsMap;
varsMap.insert(":row", row);
fillSqlVars(varsMap);
QVariantList vars;
fillSqlVars(vars);
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()) {
varsMap.insert(":match", ftsFilterMatch());
vars.append(ftsFilterMatch());
}
}

View File

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

View File

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

View File

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