Skip to content

Commit

Permalink
UI: ConfAppManager: Simplify SQL vars binding
Browse files Browse the repository at this point in the history
  • Loading branch information
tnodir committed Jan 16, 2024
1 parent 7aad887 commit 03ccedf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
5 changes: 1 addition & 4 deletions src/ui/3rdparty/sqlite/sqlitedb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,7 @@ bool SqliteDb::prepare(SqliteStmt &stmt, const char *sql, const QVariantList &va
if (!stmt.prepare(db(), sql))
return false;

if (!(vars.isEmpty() || stmt.bindVars(vars)))
return false;

return true;
return stmt.bindVars(vars);
}

bool SqliteDb::prepare(SqliteStmt &stmt, const QString &sql, const QVariantList &vars)
Expand Down
25 changes: 17 additions & 8 deletions src/ui/3rdparty/sqlite/sqlitestmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@

#include <sqlite.h>

namespace {

bool checkBindResult(int res)
{
return res == SQLITE_OK || res == SQLITE_RANGE;
}

}

SqliteStmt::SqliteStmt() = default;

SqliteStmt::~SqliteStmt()
Expand Down Expand Up @@ -41,22 +50,22 @@ int SqliteStmt::bindParameterIndex(const QString &name) const

bool SqliteStmt::bindInt(int index, qint32 number)
{
return sqlite3_bind_int(m_stmt, index, number) == SQLITE_OK;
return checkBindResult(sqlite3_bind_int(m_stmt, index, number));
}

bool SqliteStmt::bindInt64(int index, qint64 number)
{
return sqlite3_bind_int64(m_stmt, index, number) == SQLITE_OK;
return checkBindResult(sqlite3_bind_int64(m_stmt, index, number));
}

bool SqliteStmt::bindDouble(int index, double number)
{
return sqlite3_bind_double(m_stmt, index, number) == SQLITE_OK;
return checkBindResult(sqlite3_bind_double(m_stmt, index, number));
}

bool SqliteStmt::bindNull(int index)
{
return sqlite3_bind_null(m_stmt, index) == SQLITE_OK;
return checkBindResult(sqlite3_bind_null(m_stmt, index));
}

bool SqliteStmt::bindText(int index, const QString &text)
Expand All @@ -66,8 +75,8 @@ bool SqliteStmt::bindText(int index, const QString &text)

m_bindObjects.insert(index, textUtf8);

return sqlite3_bind_text(m_stmt, index, textUtf8.data(), bytesCount, SQLITE_STATIC)
== SQLITE_OK;
return checkBindResult(
sqlite3_bind_text(m_stmt, index, textUtf8.data(), bytesCount, SQLITE_STATIC));
}

bool SqliteStmt::bindDateTime(int index, const QDateTime &dateTime)
Expand All @@ -82,8 +91,8 @@ bool SqliteStmt::bindBlob(int index, const QByteArray &data)

m_bindObjects.insert(index, data);

return sqlite3_bind_blob(m_stmt, index, data.constData(), bytesCount, SQLITE_STATIC)
== SQLITE_OK;
return checkBindResult(
sqlite3_bind_blob(m_stmt, index, data.constData(), bytesCount, SQLITE_STATIC));
}

bool SqliteStmt::bindVarBlob(int index, const QVariant &v)
Expand Down
4 changes: 2 additions & 2 deletions src/ui/conf/confappmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const char *const sqlUpsertApp = "INSERT INTO app(app_group_id, origin_path, pat
" parked = ?11, log_blocked = ?12, log_conn = ?13,"
" blocked = ?14, kill_process = ?15,"
" accept_zones = ?16, reject_zones = ?17, end_time = ?18"
" RETURNING app_id, ?1;";
" RETURNING app_id;";

const char *const sqlUpdateApp = "UPDATE app"
" SET app_group_id = ?2, origin_path = ?3, path = ?4,"
Expand All @@ -93,7 +93,7 @@ const char *const sqlUpdateApp = "UPDATE app"
" blocked = ?14, kill_process = ?15,"
" accept_zones = ?16, reject_zones = ?17, end_time = ?18"
" WHERE app_id = ?1"
" RETURNING app_id, ?19;";
" RETURNING app_id;";

const char *const sqlUpdateAppName = "UPDATE app SET name = ?2 WHERE app_id = ?1;";

Expand Down

0 comments on commit 03ccedf

Please sign in to comment.