2015-06-01 18:51:55 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2015-06-01 18:51:55 +02:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:58:39 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2015-06-01 18:51:55 +02:00
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
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
|
|
|
|
2015-06-16 12:38:04 +02:00
|
|
|
#include "sqliteglobal.h"
|
|
|
|
|
|
|
|
|
|
#include "sqliteexception.h"
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
#include <utils/smallstringvector.h>
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-09-21 11:43:24 +02:00
|
|
|
#include <utils/optional.h>
|
|
|
|
|
|
2017-08-17 15:33:25 +02:00
|
|
|
#include <cstdint>
|
2015-06-16 12:38:04 +02:00
|
|
|
#include <memory>
|
2017-08-17 15:33:25 +02:00
|
|
|
#include <type_traits>
|
|
|
|
|
#include <tuple>
|
|
|
|
|
|
|
|
|
|
using std::int64_t;
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
struct sqlite3_stmt;
|
|
|
|
|
struct sqlite3;
|
|
|
|
|
|
2017-07-26 16:02:24 +02:00
|
|
|
namespace Sqlite {
|
|
|
|
|
|
2017-09-18 10:21:45 +02:00
|
|
|
class Database;
|
|
|
|
|
class DatabaseBackend;
|
2017-07-26 18:43:07 +02:00
|
|
|
|
2017-09-18 10:21:45 +02:00
|
|
|
class SQLITE_EXPORT Statement
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
protected:
|
2017-09-18 10:21:45 +02:00
|
|
|
explicit Statement(Utils::SmallStringView sqlStatement, Database &database);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-07-26 16:02:24 +02:00
|
|
|
static void deleteCompiledStatement(sqlite3_stmt *m_compiledStatement);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
bool next() const;
|
|
|
|
|
void step() const;
|
Sqlite: Add variadic bind and write functions
You can now write
SqliteWriteStatement statement("UPDATE test SET name=?, number=?
WHERE rowid=?", database);
statement.write("see", 7.23, 1);
and
SqliteWriteStatement statement("UPDATE test SET name=@name, number=@number
WHERE rowid=@id", database);
statement.writeNamed("@name", "see", "@number", 7.23, "@id", 1);
This is more type safe than using variants and performant too.
Change-Id: Ie1ed2a6d326b956be5c4ec056214f3f5b1531f45
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
2017-07-31 19:44:39 +02:00
|
|
|
void execute() const;
|
2015-06-01 18:51:55 +02:00
|
|
|
void reset() const;
|
|
|
|
|
|
2017-09-21 11:43:24 +02:00
|
|
|
int fetchIntValue(int column) const;
|
|
|
|
|
long fetchLongValue(int column) const;
|
|
|
|
|
long long fetchLongLongValue(int column) const;
|
|
|
|
|
double fetchDoubleValue(int column) const;
|
|
|
|
|
Utils::SmallString fetchSmallStringValue(int column) const;
|
|
|
|
|
Utils::PathString fetchPathStringValue(int column) const;
|
2015-06-01 18:51:55 +02:00
|
|
|
template<typename Type>
|
2017-09-21 11:43:24 +02:00
|
|
|
Type fetchValue(int column) const;
|
2017-07-27 15:59:54 +02:00
|
|
|
Utils::SmallString text(int column) const;
|
2015-06-01 18:51:55 +02:00
|
|
|
int columnCount() const;
|
2017-07-27 15:59:54 +02:00
|
|
|
Utils::SmallStringVector columnNames() const;
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-09-21 11:43:24 +02:00
|
|
|
void bind(int index, int fetchValue);
|
|
|
|
|
void bind(int index, long long fetchValue);
|
|
|
|
|
void bind(int index, double fetchValue);
|
|
|
|
|
void bind(int index, Utils::SmallStringView fetchValue);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-08-17 15:33:25 +02:00
|
|
|
void bind(int index, uint value)
|
|
|
|
|
{
|
|
|
|
|
bind(index, static_cast<long long>(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void bind(int index, long value)
|
|
|
|
|
{
|
|
|
|
|
bind(index, static_cast<long long>(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void bindValues()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-21 12:00:27 +02:00
|
|
|
template<typename... ValueType>
|
2017-09-18 15:53:51 +02:00
|
|
|
void bindValues(const ValueType&... values)
|
Sqlite: Add variadic bind and write functions
You can now write
SqliteWriteStatement statement("UPDATE test SET name=?, number=?
WHERE rowid=?", database);
statement.write("see", 7.23, 1);
and
SqliteWriteStatement statement("UPDATE test SET name=@name, number=@number
WHERE rowid=@id", database);
statement.writeNamed("@name", "see", "@number", 7.23, "@id", 1);
This is more type safe than using variants and performant too.
Change-Id: Ie1ed2a6d326b956be5c4ec056214f3f5b1531f45
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
2017-07-31 19:44:39 +02:00
|
|
|
{
|
|
|
|
|
bindValuesByIndex(1, values...);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-21 12:00:27 +02:00
|
|
|
template<typename... ValueType>
|
2017-09-18 15:53:51 +02:00
|
|
|
void write(const ValueType&... values)
|
Sqlite: Add variadic bind and write functions
You can now write
SqliteWriteStatement statement("UPDATE test SET name=?, number=?
WHERE rowid=?", database);
statement.write("see", 7.23, 1);
and
SqliteWriteStatement statement("UPDATE test SET name=@name, number=@number
WHERE rowid=@id", database);
statement.writeNamed("@name", "see", "@number", 7.23, "@id", 1);
This is more type safe than using variants and performant too.
Change-Id: Ie1ed2a6d326b956be5c4ec056214f3f5b1531f45
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
2017-07-31 19:44:39 +02:00
|
|
|
{
|
|
|
|
|
bindValuesByIndex(1, values...);
|
|
|
|
|
execute();
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-21 12:00:27 +02:00
|
|
|
template<typename... ValueType>
|
2017-09-18 15:53:51 +02:00
|
|
|
void bindNameValues(const ValueType&... values)
|
Sqlite: Add variadic bind and write functions
You can now write
SqliteWriteStatement statement("UPDATE test SET name=?, number=?
WHERE rowid=?", database);
statement.write("see", 7.23, 1);
and
SqliteWriteStatement statement("UPDATE test SET name=@name, number=@number
WHERE rowid=@id", database);
statement.writeNamed("@name", "see", "@number", 7.23, "@id", 1);
This is more type safe than using variants and performant too.
Change-Id: Ie1ed2a6d326b956be5c4ec056214f3f5b1531f45
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
2017-07-31 19:44:39 +02:00
|
|
|
{
|
|
|
|
|
bindValuesByName(values...);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-21 12:00:27 +02:00
|
|
|
template<typename... ValueType>
|
2017-09-18 15:53:51 +02:00
|
|
|
void writeNamed(const ValueType&... values)
|
Sqlite: Add variadic bind and write functions
You can now write
SqliteWriteStatement statement("UPDATE test SET name=?, number=?
WHERE rowid=?", database);
statement.write("see", 7.23, 1);
and
SqliteWriteStatement statement("UPDATE test SET name=@name, number=@number
WHERE rowid=@id", database);
statement.writeNamed("@name", "see", "@number", 7.23, "@id", 1);
This is more type safe than using variants and performant too.
Change-Id: Ie1ed2a6d326b956be5c4ec056214f3f5b1531f45
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
2017-07-31 19:44:39 +02:00
|
|
|
{
|
|
|
|
|
bindValuesByName(values...);
|
|
|
|
|
execute();
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-01 18:51:55 +02:00
|
|
|
template <typename Type>
|
2017-09-21 11:43:24 +02:00
|
|
|
void bind(Utils::SmallStringView name, Type fetchValue);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
Sqlite: Add variadic bind and write functions
You can now write
SqliteWriteStatement statement("UPDATE test SET name=?, number=?
WHERE rowid=?", database);
statement.write("see", 7.23, 1);
and
SqliteWriteStatement statement("UPDATE test SET name=@name, number=@number
WHERE rowid=@id", database);
statement.writeNamed("@name", "see", "@number", 7.23, "@id", 1);
This is more type safe than using variants and performant too.
Change-Id: Ie1ed2a6d326b956be5c4ec056214f3f5b1531f45
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
2017-07-31 19:44:39 +02:00
|
|
|
int bindingIndexForName(Utils::SmallStringView name) const;
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
void setBindingColumnNames(const Utils::SmallStringVector &bindingColumnNames);
|
|
|
|
|
const Utils::SmallStringVector &bindingColumnNames() const;
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-08-17 15:33:25 +02:00
|
|
|
template <typename ResultType,
|
2017-09-21 11:43:24 +02:00
|
|
|
int ResultTypeCount = 1>
|
|
|
|
|
std::vector<ResultType> values(std::size_t reserveSize)
|
2017-08-17 15:33:25 +02:00
|
|
|
{
|
2017-09-21 11:43:24 +02:00
|
|
|
std::vector<ResultType> resultValues;
|
2017-08-17 15:33:25 +02:00
|
|
|
resultValues.reserve(reserveSize);
|
|
|
|
|
|
|
|
|
|
while (next())
|
2017-09-21 11:43:24 +02:00
|
|
|
emplaceBackValues<ResultTypeCount>(resultValues);
|
2017-08-17 15:33:25 +02:00
|
|
|
|
|
|
|
|
reset();
|
|
|
|
|
|
|
|
|
|
return resultValues;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename ResultType,
|
2017-09-21 11:43:24 +02:00
|
|
|
int ResultTypeCount = 1,
|
2017-08-21 12:00:27 +02:00
|
|
|
typename... QueryTypes>
|
2017-09-21 11:43:24 +02:00
|
|
|
std::vector<ResultType> values(std::size_t reserveSize, const QueryTypes&... queryValues)
|
2017-08-17 15:33:25 +02:00
|
|
|
{
|
2017-09-21 11:43:24 +02:00
|
|
|
std::vector<ResultType> resultValues;
|
2017-08-17 15:33:25 +02:00
|
|
|
resultValues.reserve(reserveSize);
|
|
|
|
|
|
|
|
|
|
bindValues(queryValues...);
|
|
|
|
|
|
|
|
|
|
while (next())
|
2017-09-21 11:43:24 +02:00
|
|
|
emplaceBackValues<ResultTypeCount>(resultValues);
|
2017-08-17 15:33:25 +02:00
|
|
|
|
|
|
|
|
reset();
|
|
|
|
|
|
|
|
|
|
return resultValues;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename ResultType,
|
2017-09-21 11:43:24 +02:00
|
|
|
int ResultTypeCount = 1,
|
2017-08-17 15:33:25 +02:00
|
|
|
typename QueryElementType>
|
2017-09-21 11:43:24 +02:00
|
|
|
std::vector<ResultType> values(std::size_t reserveSize,
|
2017-08-17 15:33:25 +02:00
|
|
|
const std::vector<QueryElementType> &queryValues)
|
|
|
|
|
{
|
2017-09-21 11:43:24 +02:00
|
|
|
std::vector<ResultType> resultValues;
|
2017-08-17 15:33:25 +02:00
|
|
|
resultValues.reserve(reserveSize);
|
|
|
|
|
|
|
|
|
|
for (const QueryElementType &queryValue : queryValues) {
|
|
|
|
|
bindValues(queryValue);
|
|
|
|
|
|
|
|
|
|
while (next())
|
2017-09-21 11:43:24 +02:00
|
|
|
emplaceBackValues<ResultTypeCount>(resultValues);
|
2017-08-17 15:33:25 +02:00
|
|
|
|
|
|
|
|
reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resultValues;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename ResultType,
|
2017-09-21 11:43:24 +02:00
|
|
|
int ResultTypeCount = 1,
|
2017-08-21 12:00:27 +02:00
|
|
|
typename... QueryElementTypes>
|
2017-09-21 11:43:24 +02:00
|
|
|
std::vector<ResultType> values(std::size_t reserveSize,
|
2017-08-21 12:00:27 +02:00
|
|
|
const std::vector<std::tuple<QueryElementTypes...>> &queryTuples)
|
2017-08-17 15:33:25 +02:00
|
|
|
{
|
|
|
|
|
using Container = std::vector<ResultType>;
|
|
|
|
|
Container resultValues;
|
|
|
|
|
resultValues.reserve(reserveSize);
|
|
|
|
|
|
|
|
|
|
for (const auto &queryTuple : queryTuples) {
|
|
|
|
|
bindTupleValues(queryTuple);
|
|
|
|
|
|
|
|
|
|
while (next())
|
2017-09-21 11:43:24 +02:00
|
|
|
emplaceBackValues<ResultTypeCount>(resultValues);
|
2017-08-17 15:33:25 +02:00
|
|
|
|
|
|
|
|
reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resultValues;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename ResultType,
|
2017-09-21 11:43:24 +02:00
|
|
|
int ResultTypeCount = 1,
|
2017-08-21 12:00:27 +02:00
|
|
|
typename... QueryTypes>
|
2017-09-21 11:43:24 +02:00
|
|
|
Utils::optional<ResultType> value( const QueryTypes&... queryValues)
|
2017-08-17 15:33:25 +02:00
|
|
|
{
|
2017-09-21 11:43:24 +02:00
|
|
|
Utils::optional<ResultType> resultValue;
|
2017-08-17 15:33:25 +02:00
|
|
|
|
|
|
|
|
bindValues(queryValues...);
|
|
|
|
|
|
2017-09-21 11:43:24 +02:00
|
|
|
if (next())
|
|
|
|
|
resultValue = assignValue<Utils::optional<ResultType>, ResultTypeCount>();
|
2017-08-17 15:33:25 +02:00
|
|
|
|
|
|
|
|
reset();
|
|
|
|
|
|
2017-09-21 11:43:24 +02:00
|
|
|
return resultValue;
|
2017-08-17 15:33:25 +02:00
|
|
|
}
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
template <typename Type>
|
2017-09-18 10:21:45 +02:00
|
|
|
static Type toValue(Utils::SmallStringView sqlStatement, Database &database);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
void prepare(Utils::SmallStringView sqlStatement);
|
2015-06-01 18:51:55 +02:00
|
|
|
void waitForUnlockNotify() const;
|
|
|
|
|
|
2017-07-26 18:43:07 +02:00
|
|
|
sqlite3 *sqliteDatabaseHandle() const;
|
|
|
|
|
TextEncoding databaseTextEncoding();
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
bool checkForStepError(int resultCode) const;
|
|
|
|
|
void checkForPrepareError(int resultCode) const;
|
2017-09-18 11:23:09 +02:00
|
|
|
void checkForBindingError(int resultCode) const;
|
2015-06-01 18:51:55 +02:00
|
|
|
void setIfIsReadyToFetchValues(int resultCode) const;
|
|
|
|
|
void checkIfIsReadyToFetchValues() const;
|
2017-07-27 15:59:54 +02:00
|
|
|
void checkColumnsAreValid(const std::vector<int> &columns) const;
|
2015-06-01 18:51:55 +02:00
|
|
|
void checkColumnIsValid(int column) const;
|
|
|
|
|
void checkBindingName(int index) const;
|
|
|
|
|
void setBindingParameterCount();
|
|
|
|
|
void setBindingColumnNamesFromStatement();
|
|
|
|
|
void setColumnCount();
|
|
|
|
|
bool isReadOnlyStatement() const;
|
2017-09-18 11:23:09 +02:00
|
|
|
[[noreturn]] void throwStatementIsBusy(const char *whatHasHappened) const;
|
|
|
|
|
[[noreturn]] void throwStatementHasError(const char *whatHasHappened) const;
|
|
|
|
|
[[noreturn]] void throwStatementIsMisused(const char *whatHasHappened) const;
|
|
|
|
|
[[noreturn]] void throwConstraintPreventsModification(const char *whatHasHappened) const;
|
|
|
|
|
[[noreturn]] void throwNoValuesToFetch(const char *whatHasHappened) const;
|
|
|
|
|
[[noreturn]] void throwInvalidColumnFetched(const char *whatHasHappened) const;
|
|
|
|
|
[[noreturn]] void throwBindingIndexIsOutOfRange(const char *whatHasHappened) const;
|
|
|
|
|
[[noreturn]] void throwWrongBingingName(const char *whatHasHappened) const;
|
|
|
|
|
[[noreturn]] void throwUnknowError(const char *whatHasHappened) const;
|
|
|
|
|
[[noreturn]] void throwBingingTooBig(const char *whatHasHappened) const;
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
template <typename ContainerType>
|
2017-07-27 15:59:54 +02:00
|
|
|
ContainerType columnValues(const std::vector<int> &columnIndices) const;
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
QString columnName(int column) const;
|
|
|
|
|
|
2017-09-18 10:21:45 +02:00
|
|
|
Database &database() const;
|
Sqlite: Add variadic bind and write functions
You can now write
SqliteWriteStatement statement("UPDATE test SET name=?, number=?
WHERE rowid=?", database);
statement.write("see", 7.23, 1);
and
SqliteWriteStatement statement("UPDATE test SET name=@name, number=@number
WHERE rowid=@id", database);
statement.writeNamed("@name", "see", "@number", 7.23, "@id", 1);
This is more type safe than using variants and performant too.
Change-Id: Ie1ed2a6d326b956be5c4ec056214f3f5b1531f45
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
2017-07-31 19:44:39 +02:00
|
|
|
|
2017-07-26 18:43:07 +02:00
|
|
|
protected:
|
2017-09-18 10:21:45 +02:00
|
|
|
explicit Statement(Utils::SmallStringView sqlStatement,
|
|
|
|
|
DatabaseBackend &databaseBackend);
|
2017-07-26 18:43:07 +02:00
|
|
|
|
Sqlite: Add variadic bind and write functions
You can now write
SqliteWriteStatement statement("UPDATE test SET name=?, number=?
WHERE rowid=?", database);
statement.write("see", 7.23, 1);
and
SqliteWriteStatement statement("UPDATE test SET name=@name, number=@number
WHERE rowid=@id", database);
statement.writeNamed("@name", "see", "@number", 7.23, "@id", 1);
This is more type safe than using variants and performant too.
Change-Id: Ie1ed2a6d326b956be5c4ec056214f3f5b1531f45
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
2017-07-31 19:44:39 +02:00
|
|
|
private:
|
2017-09-21 11:43:24 +02:00
|
|
|
class ValueGetter
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
ValueGetter(Statement &statement, int column)
|
|
|
|
|
: statement(statement),
|
|
|
|
|
column(column)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
operator int()
|
|
|
|
|
{
|
|
|
|
|
return statement.fetchIntValue(column);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
operator long()
|
|
|
|
|
{
|
|
|
|
|
return statement.fetchLongValue(column);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
operator long long()
|
|
|
|
|
{
|
|
|
|
|
return statement.fetchLongLongValue(column);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
operator double()
|
|
|
|
|
{
|
|
|
|
|
return statement.fetchDoubleValue(column);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
operator Utils::SmallString()
|
|
|
|
|
{
|
|
|
|
|
return statement.fetchSmallStringValue(column);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
operator Utils::PathString()
|
|
|
|
|
{
|
|
|
|
|
return statement.fetchPathStringValue(column);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Statement &statement;
|
|
|
|
|
int column;
|
|
|
|
|
};
|
|
|
|
|
|
2017-08-21 12:00:27 +02:00
|
|
|
template <typename ContainerType,
|
|
|
|
|
int... ColumnIndices>
|
2017-09-21 11:43:24 +02:00
|
|
|
void emplaceBackValues(ContainerType &container, std::integer_sequence<int, ColumnIndices...>)
|
2017-08-17 15:33:25 +02:00
|
|
|
{
|
2017-09-21 11:43:24 +02:00
|
|
|
container.emplace_back(ValueGetter(*this, ColumnIndices)...);
|
2017-08-17 15:33:25 +02:00
|
|
|
}
|
|
|
|
|
|
2017-09-21 11:43:24 +02:00
|
|
|
template <int ResultTypeCount,
|
|
|
|
|
typename ContainerType>
|
|
|
|
|
void emplaceBackValues(ContainerType &container)
|
2017-08-17 15:33:25 +02:00
|
|
|
{
|
2017-09-21 11:43:24 +02:00
|
|
|
emplaceBackValues(container, std::make_integer_sequence<int, ResultTypeCount>{});
|
2017-08-17 15:33:25 +02:00
|
|
|
}
|
|
|
|
|
|
2017-09-21 11:43:24 +02:00
|
|
|
template <typename ResultOptionalType,
|
2017-08-21 12:00:27 +02:00
|
|
|
int... ColumnIndices>
|
2017-09-21 11:43:24 +02:00
|
|
|
ResultOptionalType assignValue(std::integer_sequence<int, ColumnIndices...>)
|
2017-08-17 15:33:25 +02:00
|
|
|
{
|
2017-09-21 11:43:24 +02:00
|
|
|
return ResultOptionalType(Utils::in_place, ValueGetter(*this, ColumnIndices)...);
|
2017-08-17 15:33:25 +02:00
|
|
|
}
|
|
|
|
|
|
2017-09-21 11:43:24 +02:00
|
|
|
template <typename ResultOptionalType,
|
|
|
|
|
int ResultTypeCount>
|
|
|
|
|
ResultOptionalType assignValue()
|
2017-08-17 15:33:25 +02:00
|
|
|
{
|
2017-09-21 11:43:24 +02:00
|
|
|
return assignValue<ResultOptionalType>(std::make_integer_sequence<int, ResultTypeCount>{});
|
2017-08-17 15:33:25 +02:00
|
|
|
}
|
|
|
|
|
|
2017-08-21 12:00:27 +02:00
|
|
|
template<typename ValueType>
|
2017-09-18 15:53:51 +02:00
|
|
|
void bindValuesByIndex(int index, const ValueType &value)
|
Sqlite: Add variadic bind and write functions
You can now write
SqliteWriteStatement statement("UPDATE test SET name=?, number=?
WHERE rowid=?", database);
statement.write("see", 7.23, 1);
and
SqliteWriteStatement statement("UPDATE test SET name=@name, number=@number
WHERE rowid=@id", database);
statement.writeNamed("@name", "see", "@number", 7.23, "@id", 1);
This is more type safe than using variants and performant too.
Change-Id: Ie1ed2a6d326b956be5c4ec056214f3f5b1531f45
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
2017-07-31 19:44:39 +02:00
|
|
|
{
|
|
|
|
|
bind(index, value);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-21 12:00:27 +02:00
|
|
|
template<typename ValueType, typename... ValueTypes>
|
2017-09-18 15:53:51 +02:00
|
|
|
void bindValuesByIndex(int index, const ValueType &value, const ValueTypes&... values)
|
Sqlite: Add variadic bind and write functions
You can now write
SqliteWriteStatement statement("UPDATE test SET name=?, number=?
WHERE rowid=?", database);
statement.write("see", 7.23, 1);
and
SqliteWriteStatement statement("UPDATE test SET name=@name, number=@number
WHERE rowid=@id", database);
statement.writeNamed("@name", "see", "@number", 7.23, "@id", 1);
This is more type safe than using variants and performant too.
Change-Id: Ie1ed2a6d326b956be5c4ec056214f3f5b1531f45
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
2017-07-31 19:44:39 +02:00
|
|
|
{
|
|
|
|
|
bind(index, value);
|
|
|
|
|
bindValuesByIndex(index + 1, values...);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-21 12:00:27 +02:00
|
|
|
template<typename ValueType>
|
2017-09-18 15:53:51 +02:00
|
|
|
void bindValuesByName(Utils::SmallStringView name, const ValueType &value)
|
Sqlite: Add variadic bind and write functions
You can now write
SqliteWriteStatement statement("UPDATE test SET name=?, number=?
WHERE rowid=?", database);
statement.write("see", 7.23, 1);
and
SqliteWriteStatement statement("UPDATE test SET name=@name, number=@number
WHERE rowid=@id", database);
statement.writeNamed("@name", "see", "@number", 7.23, "@id", 1);
This is more type safe than using variants and performant too.
Change-Id: Ie1ed2a6d326b956be5c4ec056214f3f5b1531f45
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
2017-07-31 19:44:39 +02:00
|
|
|
{
|
|
|
|
|
bind(bindingIndexForName(name), value);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-21 12:00:27 +02:00
|
|
|
template<typename ValueType, typename... ValueTypes>
|
2017-09-18 15:53:51 +02:00
|
|
|
void bindValuesByName(Utils::SmallStringView name, const ValueType &value, const ValueTypes&... values)
|
Sqlite: Add variadic bind and write functions
You can now write
SqliteWriteStatement statement("UPDATE test SET name=?, number=?
WHERE rowid=?", database);
statement.write("see", 7.23, 1);
and
SqliteWriteStatement statement("UPDATE test SET name=@name, number=@number
WHERE rowid=@id", database);
statement.writeNamed("@name", "see", "@number", 7.23, "@id", 1);
This is more type safe than using variants and performant too.
Change-Id: Ie1ed2a6d326b956be5c4ec056214f3f5b1531f45
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
2017-07-31 19:44:39 +02:00
|
|
|
{
|
|
|
|
|
bind(bindingIndexForName(name), value);
|
|
|
|
|
bindValuesByName(values...);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-21 12:00:27 +02:00
|
|
|
template <typename TupleType, std::size_t... ColumnIndices>
|
|
|
|
|
void bindTupleValuesElement(const TupleType &tuple, std::index_sequence<ColumnIndices...>)
|
2017-08-17 15:33:25 +02:00
|
|
|
{
|
2017-08-21 12:00:27 +02:00
|
|
|
bindValues(std::get<ColumnIndices>(tuple)...);
|
2017-08-17 15:33:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename TupleType,
|
2017-08-21 12:00:27 +02:00
|
|
|
typename ColumnIndices = std::make_index_sequence<std::tuple_size<TupleType>::value>>
|
2017-08-17 15:33:25 +02:00
|
|
|
void bindTupleValues(const TupleType &element)
|
|
|
|
|
{
|
2017-08-21 12:00:27 +02:00
|
|
|
bindTupleValuesElement(element, ColumnIndices());
|
2017-08-17 15:33:25 +02:00
|
|
|
}
|
|
|
|
|
|
2015-06-01 18:51:55 +02:00
|
|
|
private:
|
2017-07-26 16:02:24 +02:00
|
|
|
std::unique_ptr<sqlite3_stmt, void (*)(sqlite3_stmt*)> m_compiledStatement;
|
2017-07-27 15:59:54 +02:00
|
|
|
Utils::SmallStringVector m_bindingColumnNames;
|
2017-09-18 10:21:45 +02:00
|
|
|
Database &m_database;
|
2017-07-26 16:02:24 +02:00
|
|
|
int m_bindingParameterCount;
|
|
|
|
|
int m_columnCount;
|
|
|
|
|
mutable bool m_isReadyToFetchValues;
|
2015-06-01 18:51:55 +02:00
|
|
|
};
|
2017-07-26 16:02:24 +02:00
|
|
|
|
2017-09-18 10:21:45 +02:00
|
|
|
extern template SQLITE_EXPORT void Statement::bind(Utils::SmallStringView name, int value);
|
|
|
|
|
extern template SQLITE_EXPORT void Statement::bind(Utils::SmallStringView name, long value);
|
|
|
|
|
extern template SQLITE_EXPORT void Statement::bind(Utils::SmallStringView name, long long value);
|
|
|
|
|
extern template SQLITE_EXPORT void Statement::bind(Utils::SmallStringView name, double value);
|
|
|
|
|
extern template SQLITE_EXPORT void Statement::bind(Utils::SmallStringView name, Utils::SmallStringView text);
|
|
|
|
|
|
|
|
|
|
extern template SQLITE_EXPORT int Statement::toValue<int>(Utils::SmallStringView sqlStatement, Database &database);
|
|
|
|
|
extern template SQLITE_EXPORT long long Statement::toValue<long long>(Utils::SmallStringView sqlStatement, Database &database);
|
|
|
|
|
extern template SQLITE_EXPORT double Statement::toValue<double>(Utils::SmallStringView sqlStatement, Database &database);
|
|
|
|
|
extern template SQLITE_EXPORT Utils::SmallString Statement::toValue<Utils::SmallString>(Utils::SmallStringView sqlStatement, Database &database);
|
|
|
|
|
|
2017-09-21 11:43:24 +02:00
|
|
|
template <> SQLITE_EXPORT int Statement::fetchValue<int>(int column) const;
|
|
|
|
|
template <> SQLITE_EXPORT long Statement::fetchValue<long>(int column) const;
|
|
|
|
|
template <> SQLITE_EXPORT long long Statement::fetchValue<long long>(int column) const;
|
|
|
|
|
template <> SQLITE_EXPORT double Statement::fetchValue<double>(int column) const;
|
|
|
|
|
extern template SQLITE_EXPORT Utils::SmallString Statement::fetchValue<Utils::SmallString>(int column) const;
|
|
|
|
|
extern template SQLITE_EXPORT Utils::PathString Statement::fetchValue<Utils::PathString>(int column) const;
|
2017-07-26 16:02:24 +02:00
|
|
|
} // namespace Sqlite
|