aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/sqlite/sqlitereadstatement.h
blob: 04e4a37cf70614d8ad39f61c3c1ecf91cbe9cea1 (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
// 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, int BindParameterCount = 0>
class ReadStatement final
    : protected StatementImplementation<BaseStatement, ResultCount, BindParameterCount>
{
    using Base = StatementImplementation<BaseStatement, ResultCount, BindParameterCount>;

public:
    ReadStatement(Utils::SmallStringView sqlStatement, Database &database)
        : Base{sqlStatement, database}
    {
        checkIsReadOnlyStatement();
        Base::checkBindingParameterCount(BindParameterCount);
        Base::checkColumnCount(ResultCount);
    }

    using Base::optionalValue;
    using Base::range;
    using Base::rangeWithTransaction;
    using Base::readCallback;
    using Base::readTo;
    using Base::toValue;
    using Base::value;
    using Base::values;

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

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

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

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

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

protected:
    void checkIsReadOnlyStatement()
    {
        if (!Base::isReadOnlyStatement())
            throw NotReadOnlySqlStatement();
    }
};

template<int ResultCount>
ReadStatement(ReadStatement<ResultCount> &) -> ReadStatement<ResultCount>;
template<int ResultCount>
ReadStatement(const ReadStatement<ResultCount> &) -> ReadStatement<ResultCount>;

} // namespace Sqlite