diff options
author | Marco Bubke <[email protected]> | 2023-02-20 17:24:19 +0100 |
---|---|---|
committer | Marco Bubke <[email protected]> | 2023-02-22 10:48:05 +0000 |
commit | 18157d0b245a8eb9beeb7a595154cafc5deefe36 (patch) | |
tree | 25fa28763dd143022c2438154fd601528b83094d /src/libs/sqlite/sqlitefunctionregistry.cpp | |
parent | b4ab1e173be812dc9e4f701bc1b24542dd733b1d (diff) |
Sqlite: Add pathExists function
The 'pathExists' function can be registered to the database connection
and is then callable in Sql.
Task-number: QDS-9217
Change-Id: I21afc5cd38765854daa0b1058cc5e8946b551924
Reviewed-by: Vikas Pachdha <[email protected]>
Diffstat (limited to 'src/libs/sqlite/sqlitefunctionregistry.cpp')
-rw-r--r-- | src/libs/sqlite/sqlitefunctionregistry.cpp | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/libs/sqlite/sqlitefunctionregistry.cpp b/src/libs/sqlite/sqlitefunctionregistry.cpp new file mode 100644 index 00000000000..c2cb6a696a1 --- /dev/null +++ b/src/libs/sqlite/sqlitefunctionregistry.cpp @@ -0,0 +1,50 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0 + +#include "sqlitefunctionregistry.h" + +#include "sqlite.h" + +#include <QFileInfo> + +namespace { +extern "C" { +void pathExists(sqlite3_context *context, int, sqlite3_value **arguments) +{ + auto argument = arguments[0]; + + auto errorText = "pathExists only accepts text"; + + if (sqlite3_value_type(argument) != SQLITE_TEXT) { + sqlite3_result_error(context, errorText, int(std::char_traits<char>::length(errorText))); + return; + } + + auto size = sqlite3_value_bytes(argument); + + auto content = QByteArrayView{sqlite3_value_text(argument), size}; + + QString path = QString::fromUtf8(content); + + bool exists = QFileInfo::exists(path); + + sqlite3_result_int(context, exists); +} +} +} // namespace + +namespace Sqlite::FunctionRegistry { + +void registerPathExists(Sqlite::Database &database) +{ + sqlite3_create_function(database.backend().sqliteDatabaseHandle(), + "pathExists", + 1, + SQLITE_UTF8 | SQLITE_INNOCUOUS, + nullptr, + pathExists, + nullptr, + nullptr); +} + +} // namespace Sqlite::FunctionRegistry |