aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/sqlite/sqlitereadwritestatement.h
blob: 5f77973a3591dc2324e93bfb47b394a76c0a371b (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#pragma once

#include "sqlitebasestatement.h"

namespace Sqlite {

template<int ResultCount = 0, int BindParameterCount = 0>
class ReadWriteStatement final
    : protected StatementImplementation<BaseStatement, ResultCount, BindParameterCount>
{
    friend class DatabaseBackend;
    using Base = StatementImplementation<BaseStatement, ResultCount, BindParameterCount>;

public:
    ReadWriteStatement(Utils::SmallStringView sqlStatement,
                       Database &database,
                       const source_location &sourceLocation = source_location::current())
        : Base{sqlStatement, database, sourceLocation}
    {
        Base::checkBindingParameterCount(BindParameterCount, sourceLocation);
        Base::checkColumnCount(ResultCount, sourceLocation);
    }

    using Base::execute;
    using Base::optionalValue;
    using Base::readCallback;
    using Base::readTo;
    using Base::toValue;
    using Base::value;
    using Base::values;
    using Base::write;

    template<typename ResultType, typename... QueryTypes>
    auto valueWithTransaction(const source_location &sourceLocation, const QueryTypes &...queryValues)
    {
        return withImmediateTransaction(
            Base::database(),
            [&] { return Base::template value<ResultType>(sourceLocation, queryValues...); },
            sourceLocation);
    }

    template<typename ResultType, typename... QueryTypes>
    auto valueWithTransaction(const QueryTypes &...queryValues)
    {
        static constexpr auto sourceLocation = source_location::current();
        return valueWithTransaction<ResultType>(sourceLocation, queryValues...);
    }

    template<typename ResultType, typename... QueryTypes>
    auto optionalValueWithTransaction(const source_location &sourceLocation,
                                      const QueryTypes &...queryValues)
    {
        return withImmediateTransaction(
            Base::database(),
            [&] { return Base::template optionalValue<ResultType>(sourceLocation, queryValues...); },
            sourceLocation);
    }

    template<typename ResultType, typename... QueryTypes>
    auto optionalValueWithTransaction(const QueryTypes &...queryValues)
    {
        static constexpr auto sourceLocation = source_location::current();
        return optionalValueWithTransaction<ResultType>(sourceLocation, queryValues...);
    }

    template<typename ResultType, std::size_t capacity = 32, typename... QueryTypes>
    auto valuesWithTransaction(const source_location &sourceLocation, const QueryTypes &...queryValues)
    {
        return withImmediateTransaction(
            Base::database(),
            [&] {
                return Base::template values<ResultType, capacity>(sourceLocation, queryValues...);
            },
            sourceLocation);
    }

    template<typename ResultType, std::size_t capacity = 32, typename... QueryTypes>
    auto valuesWithTransaction(const QueryTypes &...queryValues)
    {
        static constexpr auto sourceLocation = source_location::current();
        return valuesWithTransaction<ResultType, capacity>(sourceLocation, queryValues...);
    }

    template<typename Callable, typename... QueryTypes>
    void readCallbackWithTransaction(Callable &&callable,
                                     const source_location &sourceLocation,
                                     const QueryTypes &...queryValues)
    {
        withImmediateTransaction(
            Base::database(),
            [&] {
                Base::readCallback(std::forward<Callable>(callable), sourceLocation, queryValues...);
            },
            sourceLocation);
    }

    template<typename Callable, typename... QueryTypes>
    void readCallbackWithTransaction(Callable &&callable, const QueryTypes &...queryValues)
    {
        static constexpr auto sourceLocation = source_location::current();
        readCallbackWithTransaction(callable, sourceLocation, queryValues...);
    }

    template<typename Container, typename... QueryTypes>
    void readToWithTransaction(Container &container,
                               const source_location &sourceLocation,
                               const QueryTypes &...queryValues)
    {
        withImmediateTransaction(
            Base::database(),
            [&] { Base::readTo(container, sourceLocation, queryValues...); },
            sourceLocation);
    }

    template<typename Container, typename... QueryTypes>
    void readToWithTransaction(Container &container, const QueryTypes &...queryValues)
    {
        static constexpr auto sourceLocation = source_location::current();
        readToWithTransaction(container, sourceLocation, queryValues...);
    }

    void executeWithTransaction(const source_location &sourceLocation = source_location::current())
    {
        withImmediateTransaction(Base::database(), [&] { Base::execute(sourceLocation); }, sourceLocation);
    }
};

} // namespace Sqlite