2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2016-03-18 07:55:01 +01:00
|
|
|
#pragma once
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
#include "sqlitebasestatement.h"
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-07-26 16:02:24 +02:00
|
|
|
namespace Sqlite {
|
|
|
|
|
|
2021-12-07 17:28:38 +01:00
|
|
|
template<int ResultCount, int BindParameterCount = 0>
|
|
|
|
|
class ReadStatement final
|
|
|
|
|
: protected StatementImplementation<BaseStatement, ResultCount, BindParameterCount>
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2021-12-07 17:28:38 +01:00
|
|
|
using Base = StatementImplementation<BaseStatement, ResultCount, BindParameterCount>;
|
2021-03-24 18:10:55 +01:00
|
|
|
|
2015-06-01 18:51:55 +02:00
|
|
|
public:
|
2021-03-24 18:10:55 +01:00
|
|
|
ReadStatement(Utils::SmallStringView sqlStatement, Database &database)
|
|
|
|
|
: Base{sqlStatement, database}
|
|
|
|
|
{
|
|
|
|
|
checkIsReadOnlyStatement();
|
2021-12-07 17:28:38 +01:00
|
|
|
Base::checkBindingParameterCount(BindParameterCount);
|
2021-03-24 18:10:55 +01:00
|
|
|
Base::checkColumnCount(ResultCount);
|
|
|
|
|
}
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2021-04-29 19:33:07 +02:00
|
|
|
using Base::optionalValue;
|
2021-04-21 14:31:36 +02:00
|
|
|
using Base::range;
|
|
|
|
|
using Base::rangeWithTransaction;
|
2021-03-24 18:10:55 +01:00
|
|
|
using Base::readCallback;
|
|
|
|
|
using Base::readTo;
|
|
|
|
|
using Base::toValue;
|
|
|
|
|
using Base::value;
|
|
|
|
|
using Base::values;
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2021-04-29 18:50:48 +02:00
|
|
|
template<typename ResultType, typename... QueryTypes>
|
|
|
|
|
auto valueWithTransaction(const QueryTypes &...queryValues)
|
|
|
|
|
{
|
|
|
|
|
DeferredTransaction transaction{Base::database()};
|
|
|
|
|
|
|
|
|
|
auto resultValue = Base::template value<ResultType>(queryValues...);
|
|
|
|
|
|
|
|
|
|
transaction.commit();
|
|
|
|
|
|
|
|
|
|
return resultValue;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 19:33:07 +02:00
|
|
|
template<typename ResultType, typename... QueryTypes>
|
|
|
|
|
auto optionalValueWithTransaction(const QueryTypes &...queryValues)
|
|
|
|
|
{
|
|
|
|
|
DeferredTransaction transaction{Base::database()};
|
|
|
|
|
|
|
|
|
|
auto resultValue = Base::template optionalValue<ResultType>(queryValues...);
|
|
|
|
|
|
|
|
|
|
transaction.commit();
|
|
|
|
|
|
|
|
|
|
return resultValue;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 18:50:48 +02:00
|
|
|
template<typename ResultType, typename... QueryTypes>
|
|
|
|
|
auto valuesWithTransaction(std::size_t reserveSize, const QueryTypes &...queryValues)
|
|
|
|
|
{
|
|
|
|
|
DeferredTransaction transaction{Base::database()};
|
|
|
|
|
|
|
|
|
|
auto resultValues = Base::template values<ResultType>(reserveSize, queryValues...);
|
|
|
|
|
|
|
|
|
|
transaction.commit();
|
|
|
|
|
|
|
|
|
|
return resultValues;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename Callable, typename... QueryTypes>
|
|
|
|
|
void readCallbackWithTransaction(Callable &&callable, const QueryTypes &...queryValues)
|
|
|
|
|
{
|
|
|
|
|
DeferredTransaction transaction{Base::database()};
|
|
|
|
|
|
|
|
|
|
Base::readCallback(std::forward<Callable>(callable), queryValues...);
|
|
|
|
|
|
|
|
|
|
transaction.commit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename Container, typename... QueryTypes>
|
|
|
|
|
void readToWithTransaction(Container &container, const QueryTypes &...queryValues)
|
|
|
|
|
{
|
|
|
|
|
DeferredTransaction transaction{Base::database()};
|
|
|
|
|
|
|
|
|
|
Base::readTo(container, queryValues...);
|
|
|
|
|
|
|
|
|
|
transaction.commit();
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-01 18:51:55 +02:00
|
|
|
protected:
|
2021-03-24 18:10:55 +01:00
|
|
|
void checkIsReadOnlyStatement()
|
|
|
|
|
{
|
|
|
|
|
if (!Base::isReadOnlyStatement())
|
|
|
|
|
throw NotReadOnlySqlStatement(
|
|
|
|
|
"SqliteStatement::SqliteReadStatement: is not read only statement!");
|
|
|
|
|
}
|
2015-06-01 18:51:55 +02:00
|
|
|
};
|
2017-07-26 16:02:24 +02:00
|
|
|
|
2021-03-24 18:10:55 +01:00
|
|
|
template<int ResultCount>
|
|
|
|
|
ReadStatement(ReadStatement<ResultCount> &) -> ReadStatement<ResultCount>;
|
|
|
|
|
template<int ResultCount>
|
|
|
|
|
ReadStatement(const ReadStatement<ResultCount> &) -> ReadStatement<ResultCount>;
|
|
|
|
|
|
2017-07-26 16:02:24 +02:00
|
|
|
} // namespace Sqlite
|