From f98765a18a9d35121c7539500f76f7f146420bbc Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Fri, 20 Sep 2024 14:33:11 +0500 Subject: [PATCH] UI: Programs: Sort "Name" column case-insentively --- src/ui/3rdparty/sqlite/sqlite.pri | 2 ++ src/ui/3rdparty/sqlite/sqlitedb.cpp | 9 +++++- src/ui/3rdparty/sqlite/sqlitedbext.cpp | 44 ++++++++++++++++++++++++++ src/ui/3rdparty/sqlite/sqlitedbext.h | 14 ++++++++ src/ui/conf/confmanager.cpp | 2 +- src/ui/conf/migrations/1.sql | 2 +- src/ui/model/applistmodel.cpp | 2 +- 7 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 src/ui/3rdparty/sqlite/sqlitedbext.cpp create mode 100644 src/ui/3rdparty/sqlite/sqlitedbext.h diff --git a/src/ui/3rdparty/sqlite/sqlite.pri b/src/ui/3rdparty/sqlite/sqlite.pri index b2ce70c4..24c41ace 100644 --- a/src/ui/3rdparty/sqlite/sqlite.pri +++ b/src/ui/3rdparty/sqlite/sqlite.pri @@ -16,6 +16,7 @@ SOURCES += \ $$PWD/dbutil.cpp \ $$PWD/dbvar.cpp \ $$PWD/sqlitedb.cpp \ + $$PWD/sqlitedbext.cpp \ $$PWD/sqlitestmt.cpp HEADERS += \ @@ -27,4 +28,5 @@ HEADERS += \ $$PWD/dbvar.h \ $$PWD/sqlite_types.h \ $$PWD/sqlitedb.h \ + $$PWD/sqlitedbext.h \ $$PWD/sqlitestmt.h diff --git a/src/ui/3rdparty/sqlite/sqlitedb.cpp b/src/ui/3rdparty/sqlite/sqlitedb.cpp index c5530751..03aa9281 100644 --- a/src/ui/3rdparty/sqlite/sqlitedb.cpp +++ b/src/ui/3rdparty/sqlite/sqlitedb.cpp @@ -9,6 +9,7 @@ #include #include "dbquery.h" +#include "sqlitedbext.h" #include "sqlitestmt.h" namespace { @@ -84,7 +85,13 @@ bool SqliteDb::open() { const auto filePathUtf8 = m_filePath.toUtf8(); - return sqlite3_open_v2(filePathUtf8.data(), &m_db, m_openFlags, nullptr) == SQLITE_OK; + const bool ok = sqlite3_open_v2(filePathUtf8.data(), &m_db, m_openFlags, nullptr) == SQLITE_OK; + + if (ok) { + SqliteDbExt::registerExtensions(this); + } + + return ok; } void SqliteDb::close() diff --git a/src/ui/3rdparty/sqlite/sqlitedbext.cpp b/src/ui/3rdparty/sqlite/sqlitedbext.cpp new file mode 100644 index 00000000..7f234d92 --- /dev/null +++ b/src/ui/3rdparty/sqlite/sqlitedbext.cpp @@ -0,0 +1,44 @@ +#include "sqlitedbext.h" + +#include + +#include + +#include "sqlitedb.h" + +namespace { + +const QLoggingCategory LC("dbExt"); + +void extLower(sqlite3_context *ctx, int argc, sqlite3_value **argv) +{ + Q_ASSERT(argc == 1); + + const unsigned char *textUtf8 = sqlite3_value_text(argv[0]); + const auto text = QString::fromUtf8(textUtf8); + const auto textLower = text.toLower(); + const auto result = textLower.toUtf8(); + + sqlite3_result_text(ctx, result.data(), result.size(), SQLITE_TRANSIENT); +} + +bool createExtLower(struct sqlite3 *db) +{ + return sqlite3_create_function(db, "EXT_LOWER", + /*nArg=*/1, + /*eTextRep=*/(SQLITE_DETERMINISTIC | SQLITE_UTF8), + /*pApp=*/nullptr, + /*xFunc=*/&extLower, + /*xStep=*/nullptr, + /*xFinal=*/nullptr) + == SQLITE_OK; +} + +} + +void SqliteDbExt::registerExtensions(SqliteDb *sqliteDb) +{ + struct sqlite3 *db = sqliteDb->db(); + + createExtLower(db); +} diff --git a/src/ui/3rdparty/sqlite/sqlitedbext.h b/src/ui/3rdparty/sqlite/sqlitedbext.h new file mode 100644 index 00000000..3806e441 --- /dev/null +++ b/src/ui/3rdparty/sqlite/sqlitedbext.h @@ -0,0 +1,14 @@ +#ifndef SQLITEDBEXT_H +#define SQLITEDBEXT_H + +#include + +class SqliteDb; + +class SqliteDbExt +{ +public: + static void registerExtensions(SqliteDb *sqliteDb); +}; + +#endif // SQLITEDBEXT_H diff --git a/src/ui/conf/confmanager.cpp b/src/ui/conf/confmanager.cpp index 81b5aeee..f5eeb90a 100644 --- a/src/ui/conf/confmanager.cpp +++ b/src/ui/conf/confmanager.cpp @@ -34,7 +34,7 @@ namespace { const QLoggingCategory LC("conf"); -constexpr int DATABASE_USER_VERSION = 43; +constexpr int DATABASE_USER_VERSION = 44; const char *const sqlSelectAddressGroups = "SELECT addr_group_id, include_all, exclude_all," " include_zones, exclude_zones," diff --git a/src/ui/conf/migrations/1.sql b/src/ui/conf/migrations/1.sql index 6c62bc0e..b215a5a7 100644 --- a/src/ui/conf/migrations/1.sql +++ b/src/ui/conf/migrations/1.sql @@ -80,7 +80,7 @@ CREATE TABLE app( CREATE INDEX app_app_group_id_idx ON app(app_group_id); CREATE UNIQUE INDEX app_path_uk ON app(path); -CREATE INDEX app_name_idx ON app(name); +CREATE INDEX app_name_idx ON app(EXT_LOWER(name)); CREATE INDEX app_rule_idx ON app(rule_id); CREATE INDEX app_end_time_idx ON app(end_time); diff --git a/src/ui/model/applistmodel.cpp b/src/ui/model/applistmodel.cpp index 8f8e6e97..e9fbcd02 100644 --- a/src/ui/model/applistmodel.cpp +++ b/src/ui/model/applistmodel.cpp @@ -320,7 +320,7 @@ QString AppListModel::sqlWhereFts() const QString AppListModel::sqlOrderColumn() const { - static const QString nameColumn = "t.name"; + static const QString nameColumn = "EXT_LOWER(t.name)"; static const QString pathColumn = "t.path"; static const QStringList orderColumns = {