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
|
|
|
|
2015-06-16 12:38:04 +02:00
|
|
|
#include <type_traits>
|
|
|
|
|
#include <memory>
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
struct sqlite3_stmt;
|
|
|
|
|
struct sqlite3;
|
|
|
|
|
|
2017-07-26 16:02:24 +02:00
|
|
|
namespace Sqlite {
|
|
|
|
|
|
2017-07-26 18:43:07 +02:00
|
|
|
class SqliteDatabase;
|
|
|
|
|
class SqliteDatabaseBackend;
|
|
|
|
|
|
2015-06-01 18:51:55 +02:00
|
|
|
class SQLITE_EXPORT SqliteStatement
|
|
|
|
|
{
|
|
|
|
|
protected:
|
2017-07-27 15:59:54 +02:00
|
|
|
explicit SqliteStatement(Utils::SmallStringView sqlStatement, SqliteDatabase &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;
|
|
|
|
|
void reset() const;
|
|
|
|
|
|
|
|
|
|
template<typename Type>
|
|
|
|
|
Type value(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
|
|
|
|
|
|
|
|
void bind(int index, int value);
|
|
|
|
|
void bind(int index, qint64 value);
|
|
|
|
|
void bind(int index, double value);
|
2017-07-27 15:59:54 +02:00
|
|
|
void bind(int index, Utils::SmallStringView value);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
template <typename Type>
|
2017-07-27 15:59:54 +02:00
|
|
|
void bind(Utils::SmallStringView name, Type value);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
int bindingIndexForName(Utils::SmallStringView name);
|
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
|
|
|
|
|
|
|
|
template <typename ContainerType>
|
2017-07-27 15:59:54 +02:00
|
|
|
ContainerType values(const std::vector<int> &columns, int size = 0) const;
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
template <typename ContainerType>
|
|
|
|
|
ContainerType values(int column = 0) const;
|
|
|
|
|
|
|
|
|
|
template <typename Type>
|
2017-07-27 15:59:54 +02:00
|
|
|
static Type toValue(Utils::SmallStringView sqlStatement, SqliteDatabase &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;
|
|
|
|
|
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 checkBindingIndex(int index) const;
|
|
|
|
|
void checkBindingName(int index) const;
|
|
|
|
|
void setBindingParameterCount();
|
|
|
|
|
void setBindingColumnNamesFromStatement();
|
|
|
|
|
void setColumnCount();
|
|
|
|
|
bool isReadOnlyStatement() const;
|
2017-07-26 18:43:07 +02:00
|
|
|
Q_NORETURN void throwException(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-07-26 18:43:07 +02:00
|
|
|
protected:
|
2017-07-27 15:59:54 +02:00
|
|
|
explicit SqliteStatement(Utils::SmallStringView sqlStatement,
|
2017-07-26 18:43:07 +02:00
|
|
|
SqliteDatabaseBackend &databaseBackend);
|
|
|
|
|
|
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-07-26 18:43:07 +02:00
|
|
|
SqliteDatabaseBackend &m_databaseBackend;
|
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
|
|
|
|
|
|
|
|
} // namespace Sqlite
|