aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/sqlite/sqlitefunctionregistry.cpp
blob: a370248c1f6a9308b45a1d0c76f369df0ed415c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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, const source_location &sourceLocation)
{
    sqlite3_create_function(database.backend().sqliteDatabaseHandle(sourceLocation),
                            "pathExists",
                            1,
                            SQLITE_UTF8 | SQLITE_INNOCUOUS,
                            nullptr,
                            pathExists,
                            nullptr,
                            nullptr);
}

} // namespace Sqlite::FunctionRegistry