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
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "sqlitestatement.h"
|
|
|
|
|
|
2017-07-26 18:43:07 +02:00
|
|
|
#include "sqlitedatabase.h"
|
2015-06-16 12:38:04 +02:00
|
|
|
#include "sqlitedatabasebackend.h"
|
|
|
|
|
#include "sqliteexception.h"
|
|
|
|
|
|
2015-06-01 18:51:55 +02:00
|
|
|
#include "sqlite3.h"
|
|
|
|
|
|
2017-08-17 15:33:25 +02:00
|
|
|
#include <condition_variable>
|
|
|
|
|
#include <mutex>
|
|
|
|
|
|
2015-06-01 18:51:55 +02:00
|
|
|
#if defined(__GNUC__)
|
|
|
|
|
# pragma GCC diagnostic ignored "-Wignored-qualifiers"
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-07-26 16:02:24 +02:00
|
|
|
namespace Sqlite {
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
SqliteStatement::SqliteStatement(Utils::SmallStringView sqlStatement, SqliteDatabase &database)
|
2017-07-26 16:02:24 +02:00
|
|
|
: m_compiledStatement(nullptr, deleteCompiledStatement),
|
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
|
|
|
m_database(database),
|
2017-07-26 16:02:24 +02:00
|
|
|
m_bindingParameterCount(0),
|
|
|
|
|
m_columnCount(0),
|
|
|
|
|
m_isReadyToFetchValues(false)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
prepare(sqlStatement);
|
2015-06-01 18:51:55 +02:00
|
|
|
setBindingParameterCount();
|
|
|
|
|
setBindingColumnNamesFromStatement();
|
|
|
|
|
setColumnCount();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqliteStatement::deleteCompiledStatement(sqlite3_stmt *compiledStatement)
|
|
|
|
|
{
|
|
|
|
|
if (compiledStatement)
|
|
|
|
|
sqlite3_finalize(compiledStatement);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-17 15:33:25 +02:00
|
|
|
class UnlockNotification
|
|
|
|
|
{
|
2015-06-01 18:51:55 +02:00
|
|
|
public:
|
|
|
|
|
static void unlockNotifyCallBack(void **arguments, int argumentCount)
|
|
|
|
|
{
|
|
|
|
|
for (int index = 0; index < argumentCount; index++) {
|
|
|
|
|
UnlockNotification *unlockNotification = static_cast<UnlockNotification *>(arguments[index]);
|
|
|
|
|
unlockNotification->wakeupWaitCondition();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void wakeupWaitCondition()
|
|
|
|
|
{
|
2017-08-17 15:33:25 +02:00
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
|
|
|
m_fired = 1;
|
|
|
|
|
}
|
|
|
|
|
m_waitCondition.notify_all();
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void wait()
|
|
|
|
|
{
|
2017-08-17 15:33:25 +02:00
|
|
|
std::unique_lock<std::mutex> lock(m_mutex);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-08-17 15:33:25 +02:00
|
|
|
m_waitCondition.wait(lock, [&] () { return m_fired; });
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2017-08-17 15:33:25 +02:00
|
|
|
bool m_fired = false;
|
|
|
|
|
std::condition_variable m_waitCondition;
|
|
|
|
|
std::mutex m_mutex;
|
2015-06-01 18:51:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void SqliteStatement::waitForUnlockNotify() const
|
|
|
|
|
{
|
|
|
|
|
UnlockNotification unlockNotification;
|
|
|
|
|
int resultCode = sqlite3_unlock_notify(sqliteDatabaseHandle(), UnlockNotification::unlockNotifyCallBack, &unlockNotification);
|
|
|
|
|
|
|
|
|
|
if (resultCode == SQLITE_OK)
|
|
|
|
|
unlockNotification.wait();
|
|
|
|
|
else
|
|
|
|
|
throwException("SqliteStatement::waitForUnlockNotify: database is in a dead lock!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqliteStatement::reset() const
|
|
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
int resultCode = sqlite3_reset(m_compiledStatement.get());
|
2015-06-01 18:51:55 +02:00
|
|
|
if (resultCode != SQLITE_OK)
|
|
|
|
|
throwException("SqliteStatement::reset: can't reset statement!");
|
|
|
|
|
|
2017-07-26 16:02:24 +02:00
|
|
|
m_isReadyToFetchValues = false;
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SqliteStatement::next() const
|
|
|
|
|
{
|
|
|
|
|
int resultCode;
|
|
|
|
|
|
|
|
|
|
do {
|
2017-07-26 16:02:24 +02:00
|
|
|
resultCode = sqlite3_step(m_compiledStatement.get());
|
2015-06-01 18:51:55 +02:00
|
|
|
if (resultCode == SQLITE_LOCKED) {
|
|
|
|
|
waitForUnlockNotify();
|
2017-07-26 16:02:24 +02:00
|
|
|
sqlite3_reset(m_compiledStatement.get());
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} while (resultCode == SQLITE_LOCKED);
|
|
|
|
|
|
|
|
|
|
setIfIsReadyToFetchValues(resultCode);
|
|
|
|
|
|
|
|
|
|
return checkForStepError(resultCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqliteStatement::step() const
|
|
|
|
|
{
|
|
|
|
|
next();
|
|
|
|
|
}
|
|
|
|
|
|
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 SqliteStatement::execute() const
|
|
|
|
|
{
|
|
|
|
|
next();
|
2017-08-17 15:33:25 +02:00
|
|
|
reset();
|
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
|
|
|
}
|
|
|
|
|
|
2015-06-01 18:51:55 +02:00
|
|
|
int SqliteStatement::columnCount() const
|
|
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
return m_columnCount;
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
Utils::SmallStringVector SqliteStatement::columnNames() const
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
Utils::SmallStringVector columnNames;
|
2015-06-01 18:51:55 +02:00
|
|
|
int columnCount = SqliteStatement::columnCount();
|
2017-07-27 15:59:54 +02:00
|
|
|
columnNames.reserve(std::size_t(columnCount));
|
2015-06-01 18:51:55 +02:00
|
|
|
for (int columnIndex = 0; columnIndex < columnCount; columnIndex++)
|
2017-07-27 15:59:54 +02:00
|
|
|
columnNames.emplace_back(sqlite3_column_origin_name(m_compiledStatement.get(), columnIndex));
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
return columnNames;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqliteStatement::bind(int index, int value)
|
|
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
int resultCode = sqlite3_bind_int(m_compiledStatement.get(), index, value);
|
2015-06-01 18:51:55 +02:00
|
|
|
if (resultCode != SQLITE_OK)
|
|
|
|
|
throwException("SqliteStatement::bind: cant' bind 32 bit integer!");
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-17 15:33:25 +02:00
|
|
|
void SqliteStatement::bind(int index, long long value)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
int resultCode = sqlite3_bind_int64(m_compiledStatement.get(), index, value);
|
2015-06-01 18:51:55 +02:00
|
|
|
if (resultCode != SQLITE_OK)
|
|
|
|
|
throwException("SqliteStatement::bind: cant' bind 64 bit integer!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqliteStatement::bind(int index, double value)
|
|
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
int resultCode = sqlite3_bind_double(m_compiledStatement.get(), index, value);
|
2015-06-01 18:51:55 +02:00
|
|
|
if (resultCode != SQLITE_OK)
|
|
|
|
|
throwException("SqliteStatement::bind: cant' bind double!");
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
void SqliteStatement::bind(int index, Utils::SmallStringView text)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
int resultCode = sqlite3_bind_text(m_compiledStatement.get(), index, text.data(), int(text.size()), SQLITE_TRANSIENT);
|
2015-06-01 18:51:55 +02:00
|
|
|
if (resultCode != SQLITE_OK)
|
2017-07-27 15:59:54 +02:00
|
|
|
throwException("SqliteStatement::bind: cant' bind double!");
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename Type>
|
2017-07-27 15:59:54 +02:00
|
|
|
void SqliteStatement::bind(Utils::SmallStringView name, Type value)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
int index = bindingIndexForName(name);
|
|
|
|
|
checkBindingName(index);
|
|
|
|
|
bind(index, value);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
template SQLITE_EXPORT void SqliteStatement::bind(Utils::SmallStringView name, int value);
|
2017-08-17 15:33:25 +02:00
|
|
|
template SQLITE_EXPORT void SqliteStatement::bind(Utils::SmallStringView name, long value);
|
|
|
|
|
template SQLITE_EXPORT void SqliteStatement::bind(Utils::SmallStringView name, long long value);
|
2017-07-27 15:59:54 +02:00
|
|
|
template SQLITE_EXPORT void SqliteStatement::bind(Utils::SmallStringView name, double value);
|
|
|
|
|
template SQLITE_EXPORT void SqliteStatement::bind(Utils::SmallStringView name, Utils::SmallStringView text);
|
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 SqliteStatement::bindingIndexForName(Utils::SmallStringView name) const
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
return sqlite3_bind_parameter_index(m_compiledStatement.get(), name.data());
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
void SqliteStatement::setBindingColumnNames(const Utils::SmallStringVector &bindingColumnNames)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
m_bindingColumnNames = bindingColumnNames;
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
const Utils::SmallStringVector &SqliteStatement::bindingColumnNames() const
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
return m_bindingColumnNames;
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
void SqliteStatement::prepare(Utils::SmallStringView sqlStatement)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
int resultCode;
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
sqlite3_stmt *sqliteStatement = nullptr;
|
2017-07-27 15:59:54 +02:00
|
|
|
resultCode = sqlite3_prepare_v2(sqliteDatabaseHandle(),
|
|
|
|
|
sqlStatement.data(),
|
|
|
|
|
int(sqlStatement.size()),
|
|
|
|
|
&sqliteStatement,
|
|
|
|
|
nullptr);
|
2017-07-26 16:02:24 +02:00
|
|
|
m_compiledStatement.reset(sqliteStatement);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
if (resultCode == SQLITE_LOCKED)
|
|
|
|
|
waitForUnlockNotify();
|
|
|
|
|
|
|
|
|
|
} while (resultCode == SQLITE_LOCKED);
|
|
|
|
|
|
|
|
|
|
checkForPrepareError(resultCode);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-26 18:43:07 +02:00
|
|
|
sqlite3 *SqliteStatement::sqliteDatabaseHandle() const
|
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
|
|
|
return m_database.backend().sqliteDatabaseHandle();
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextEncoding SqliteStatement::databaseTextEncoding()
|
|
|
|
|
{
|
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
|
|
|
return m_database.backend().textEncoding();
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SqliteStatement::checkForStepError(int resultCode) const
|
|
|
|
|
{
|
|
|
|
|
switch (resultCode) {
|
|
|
|
|
case SQLITE_ROW: return true;
|
|
|
|
|
case SQLITE_DONE: return false;
|
|
|
|
|
case SQLITE_BUSY: throwException("SqliteStatement::stepStatement: database engine was unable to acquire the database locks!");
|
|
|
|
|
case SQLITE_ERROR : throwException("SqliteStatement::stepStatement: run-time error (such as a constraint violation) has occurred!");
|
|
|
|
|
case SQLITE_MISUSE: throwException("SqliteStatement::stepStatement: was called inappropriately!");
|
|
|
|
|
case SQLITE_CONSTRAINT: throwException("SqliteStatement::stepStatement: contraint prevent insert or update!");
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-04 10:42:39 +01:00
|
|
|
throwException("SqliteStatement::stepStatement: unknown error has happened");
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
Q_UNREACHABLE();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqliteStatement::checkForPrepareError(int resultCode) const
|
|
|
|
|
{
|
|
|
|
|
switch (resultCode) {
|
|
|
|
|
case SQLITE_OK: return;
|
|
|
|
|
case SQLITE_BUSY: throwException("SqliteStatement::prepareStatement: database engine was unable to acquire the database locks!");
|
|
|
|
|
case SQLITE_ERROR : throwException("SqliteStatement::prepareStatement: run-time error (such as a constraint violation) has occurred!");
|
|
|
|
|
case SQLITE_MISUSE: throwException("SqliteStatement::prepareStatement: was called inappropriately!");
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-04 10:42:39 +01:00
|
|
|
throwException("SqliteStatement::prepareStatement: unknown error has happened");
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqliteStatement::setIfIsReadyToFetchValues(int resultCode) const
|
|
|
|
|
{
|
|
|
|
|
if (resultCode == SQLITE_ROW)
|
2017-07-26 16:02:24 +02:00
|
|
|
m_isReadyToFetchValues = true;
|
2015-06-01 18:51:55 +02:00
|
|
|
else
|
2017-07-26 16:02:24 +02:00
|
|
|
m_isReadyToFetchValues = false;
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqliteStatement::checkIfIsReadyToFetchValues() const
|
|
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
if (!m_isReadyToFetchValues)
|
2015-06-01 18:51:55 +02:00
|
|
|
throwException("SqliteStatement::value: there are no values to fetch!");
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
void SqliteStatement::checkColumnsAreValid(const std::vector<int> &columns) const
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
for (int column : columns) {
|
2017-07-26 16:02:24 +02:00
|
|
|
if (column < 0 || column >= m_columnCount)
|
2015-06-01 18:51:55 +02:00
|
|
|
throwException("SqliteStatement::values: column index out of bound!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqliteStatement::checkColumnIsValid(int column) const
|
|
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
if (column < 0 || column >= m_columnCount)
|
2015-06-01 18:51:55 +02:00
|
|
|
throwException("SqliteStatement::values: column index out of bound!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqliteStatement::checkBindingIndex(int index) const
|
|
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
if (index <= 0 || index > m_bindingParameterCount)
|
2015-06-01 18:51:55 +02:00
|
|
|
throwException("SqliteStatement::bind: binding index is out of bound!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqliteStatement::checkBindingName(int index) const
|
|
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
if (index <= 0 || index > m_bindingParameterCount)
|
2015-06-01 18:51:55 +02:00
|
|
|
throwException("SqliteStatement::bind: binding name are not exists in this statement!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqliteStatement::setBindingParameterCount()
|
|
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
m_bindingParameterCount = sqlite3_bind_parameter_count(m_compiledStatement.get());
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
Utils::SmallStringView chopFirstLetter(const char *rawBindingName)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
if (rawBindingName != nullptr)
|
|
|
|
|
return Utils::SmallStringView(++rawBindingName);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
return Utils::SmallStringView("");
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqliteStatement::setBindingColumnNamesFromStatement()
|
|
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
for (int index = 1; index <= m_bindingParameterCount; index++) {
|
2017-07-27 15:59:54 +02:00
|
|
|
Utils::SmallStringView bindingName = chopFirstLetter(sqlite3_bind_parameter_name(m_compiledStatement.get(), index));
|
|
|
|
|
m_bindingColumnNames.push_back(Utils::SmallString(bindingName));
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqliteStatement::setColumnCount()
|
|
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
m_columnCount = sqlite3_column_count(m_compiledStatement.get());
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SqliteStatement::isReadOnlyStatement() const
|
|
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
return sqlite3_stmt_readonly(m_compiledStatement.get());
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2017-07-26 18:43:07 +02:00
|
|
|
void SqliteStatement::throwException(const char *whatHasHappened) const
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
throw SqliteException(whatHasHappened, sqlite3_errmsg(sqliteDatabaseHandle()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString SqliteStatement::columnName(int column) const
|
|
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
return QString::fromUtf8(sqlite3_column_name(m_compiledStatement.get(), column));
|
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
|
|
|
SqliteDatabase &SqliteStatement::database() const
|
|
|
|
|
{
|
|
|
|
|
return m_database;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-17 15:33:25 +02:00
|
|
|
template <typename StringType>
|
|
|
|
|
static StringType textForColumn(sqlite3_stmt *sqlStatment, int column)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
const char *text = reinterpret_cast<const char*>(sqlite3_column_text(sqlStatment, column));
|
2017-07-27 15:59:54 +02:00
|
|
|
std::size_t size = std::size_t(sqlite3_column_bytes(sqlStatment, column));
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-08-17 15:33:25 +02:00
|
|
|
return StringType(text, size);
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2017-08-17 15:33:25 +02:00
|
|
|
template <typename StringType>
|
|
|
|
|
static StringType convertToTextForColumn(sqlite3_stmt *sqlStatment, int column)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
int dataType = sqlite3_column_type(sqlStatment, column);
|
|
|
|
|
switch (dataType) {
|
2017-07-27 15:59:54 +02:00
|
|
|
case SQLITE_INTEGER:
|
|
|
|
|
case SQLITE_FLOAT:
|
2017-08-17 15:33:25 +02:00
|
|
|
case SQLITE3_TEXT: return textForColumn<StringType>(sqlStatment, column);
|
2017-07-27 15:59:54 +02:00
|
|
|
case SQLITE_BLOB:
|
|
|
|
|
case SQLITE_NULL: return {};
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Q_UNREACHABLE();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
int SqliteStatement::value<int>(int column) const
|
|
|
|
|
{
|
|
|
|
|
checkIfIsReadyToFetchValues();
|
|
|
|
|
checkColumnIsValid(column);
|
2017-07-26 16:02:24 +02:00
|
|
|
return sqlite3_column_int(m_compiledStatement.get(), column);
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<>
|
2017-08-17 15:33:25 +02:00
|
|
|
long SqliteStatement::value<long>(int column) const
|
|
|
|
|
{
|
|
|
|
|
return long(value<long long>(column));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
long long SqliteStatement::value<long long>(int column) const
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
checkIfIsReadyToFetchValues();
|
|
|
|
|
checkColumnIsValid(column);
|
2017-07-26 16:02:24 +02:00
|
|
|
return sqlite3_column_int64(m_compiledStatement.get(), column);
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
double SqliteStatement::value<double>(int column) const
|
|
|
|
|
{
|
|
|
|
|
checkIfIsReadyToFetchValues();
|
|
|
|
|
checkColumnIsValid(column);
|
2017-07-26 16:02:24 +02:00
|
|
|
return sqlite3_column_double(m_compiledStatement.get(), column);
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2017-08-17 15:33:25 +02:00
|
|
|
template<typename StringType>
|
|
|
|
|
StringType SqliteStatement::value(int column) const
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
checkIfIsReadyToFetchValues();
|
|
|
|
|
checkColumnIsValid(column);
|
2017-08-17 15:33:25 +02:00
|
|
|
return convertToTextForColumn<StringType>(m_compiledStatement.get(), column);
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2017-08-17 15:33:25 +02:00
|
|
|
template SQLITE_EXPORT Utils::SmallString SqliteStatement::value<Utils::SmallString>(int column) const;
|
|
|
|
|
template SQLITE_EXPORT Utils::PathString SqliteStatement::value<Utils::PathString>(int column) const;
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
Utils::SmallString SqliteStatement::text(int column) const
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
return value<Utils::SmallString>(column);
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename ContainerType>
|
2017-07-27 15:59:54 +02:00
|
|
|
ContainerType SqliteStatement::columnValues(const std::vector<int> &columnIndices) const
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
using ElementType = typename ContainerType::value_type;
|
2015-06-01 18:51:55 +02:00
|
|
|
ContainerType valueContainer;
|
2017-07-27 15:59:54 +02:00
|
|
|
valueContainer.reserve(columnIndices.size());
|
2015-06-01 18:51:55 +02:00
|
|
|
for (int columnIndex : columnIndices)
|
2017-07-27 15:59:54 +02:00
|
|
|
valueContainer.push_back(value<ElementType>(columnIndex));
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
return valueContainer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename Type>
|
2017-07-27 15:59:54 +02:00
|
|
|
Type SqliteStatement::toValue(Utils::SmallStringView sqlStatement, SqliteDatabase &database)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
SqliteStatement statement(sqlStatement, database);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
statement.next();
|
|
|
|
|
|
|
|
|
|
return statement.value<Type>(0);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
template SQLITE_EXPORT int SqliteStatement::toValue<int>(Utils::SmallStringView sqlStatement, SqliteDatabase &database);
|
2017-08-17 15:33:25 +02:00
|
|
|
template SQLITE_EXPORT long long SqliteStatement::toValue<long long>(Utils::SmallStringView sqlStatement, SqliteDatabase &database);
|
2017-07-27 15:59:54 +02:00
|
|
|
template SQLITE_EXPORT double SqliteStatement::toValue<double>(Utils::SmallStringView sqlStatement, SqliteDatabase &database);
|
|
|
|
|
template SQLITE_EXPORT Utils::SmallString SqliteStatement::toValue<Utils::SmallString>(Utils::SmallStringView sqlStatement, SqliteDatabase &database);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-07-26 16:02:24 +02:00
|
|
|
} // namespace Sqlite
|