forked from qt-creator/qt-creator
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 <vikas.pachdha@qt.io>
This commit is contained in:
@@ -32,6 +32,7 @@ add_qtc_library(Sqlite
|
|||||||
sqlitedatabaseinterface.h
|
sqlitedatabaseinterface.h
|
||||||
sqliteexception.cpp sqliteexception.h
|
sqliteexception.cpp sqliteexception.h
|
||||||
sqliteglobal.cpp sqliteglobal.h
|
sqliteglobal.cpp sqliteglobal.h
|
||||||
|
sqlitefunctionregistry.cpp sqlitefunctionregistry.h
|
||||||
sqliteindex.h
|
sqliteindex.h
|
||||||
sqliteprogresshandler.h
|
sqliteprogresshandler.h
|
||||||
sqlitereadstatement.h
|
sqlitereadstatement.h
|
||||||
|
50
src/libs/sqlite/sqlitefunctionregistry.cpp
Normal file
50
src/libs/sqlite/sqlitefunctionregistry.cpp
Normal file
@@ -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
|
12
src/libs/sqlite/sqlitefunctionregistry.h
Normal file
12
src/libs/sqlite/sqlitefunctionregistry.h
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
// 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
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <sqlitedatabase.h>
|
||||||
|
|
||||||
|
namespace Sqlite::FunctionRegistry {
|
||||||
|
|
||||||
|
SQLITE_EXPORT void registerPathExists(Sqlite::Database &database);
|
||||||
|
|
||||||
|
} // namespace Sqlite::FunctionRegistry
|
@@ -73,6 +73,7 @@ add_qtc_test(unittest GTEST
|
|||||||
sqlitecolumn-test.cpp
|
sqlitecolumn-test.cpp
|
||||||
sqlitedatabasebackend-test.cpp
|
sqlitedatabasebackend-test.cpp
|
||||||
sqlitedatabase-test.cpp
|
sqlitedatabase-test.cpp
|
||||||
|
sqlitefunctionregistry-test.cpp
|
||||||
sqlitesessions-test.cpp
|
sqlitesessions-test.cpp
|
||||||
sqlitestatement-test.cpp
|
sqlitestatement-test.cpp
|
||||||
sqlitetable-test.cpp
|
sqlitetable-test.cpp
|
||||||
|
41
tests/unit/unittest/sqlitefunctionregistry-test.cpp
Normal file
41
tests/unit/unittest/sqlitefunctionregistry-test.cpp
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
// 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 "googletest.h"
|
||||||
|
|
||||||
|
#include <sqlitefunctionregistry.h>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
class SqliteFunctionRegistry : public testing::Test
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
SqliteFunctionRegistry() { Sqlite::FunctionRegistry::registerPathExists(database); }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
Sqlite::Database database{":memory:", Sqlite::JournalMode::Memory};
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_F(SqliteFunctionRegistry, PathExists)
|
||||||
|
{
|
||||||
|
std::lock_guard lock{database};
|
||||||
|
Sqlite::ReadStatement<1> statement{"SELECT pathExists('" TESTDATA_DIR "/sqlite_database.db')",
|
||||||
|
database};
|
||||||
|
|
||||||
|
auto pathExists = statement.value<bool>();
|
||||||
|
|
||||||
|
ASSERT_TRUE(pathExists);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(SqliteFunctionRegistry, PathDoesntExists)
|
||||||
|
{
|
||||||
|
std::lock_guard lock{database};
|
||||||
|
Sqlite::ReadStatement<1> statement{"SELECT pathExists('" TESTDATA_DIR "/sqlite_database2.db')",
|
||||||
|
database};
|
||||||
|
|
||||||
|
auto pathExists = statement.value<bool>();
|
||||||
|
|
||||||
|
ASSERT_FALSE(pathExists);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
Reference in New Issue
Block a user