2014-10-14 13:14:57 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Klaas Freitag <freitag@owncloud.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2016-10-25 12:00:07 +03:00
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2014-10-14 13:14:57 +04:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <QDateTime>
|
2017-05-09 15:24:11 +03:00
|
|
|
#include <QLoggingCategory>
|
2014-10-14 13:14:57 +04:00
|
|
|
#include <QString>
|
2014-12-04 15:55:58 +03:00
|
|
|
#include <QFile>
|
2017-04-26 11:12:26 +03:00
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QDir>
|
2014-10-14 13:14:57 +04:00
|
|
|
|
|
|
|
#include "ownsql.h"
|
2017-08-16 09:36:52 +03:00
|
|
|
#include "common/utility.h"
|
2017-02-07 15:52:15 +03:00
|
|
|
#include "asserts.h"
|
2014-10-18 18:16:29 +04:00
|
|
|
|
|
|
|
#define SQLITE_SLEEP_TIME_USEC 100000
|
|
|
|
#define SQLITE_REPEAT_COUNT 20
|
2014-10-14 13:14:57 +04:00
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
#define SQLITE_DO(A) \
|
|
|
|
if (1) { \
|
|
|
|
_errId = (A); \
|
|
|
|
if (_errId != SQLITE_OK) { \
|
|
|
|
_error = QString::fromUtf8(sqlite3_errmsg(_db)); \
|
|
|
|
} \
|
|
|
|
}
|
2014-10-14 13:14:57 +04:00
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
namespace OCC {
|
2014-10-14 22:51:51 +04:00
|
|
|
|
2017-05-09 15:24:11 +03:00
|
|
|
Q_LOGGING_CATEGORY(lcSql, "sync.database.sql", QtInfoMsg)
|
|
|
|
|
2014-10-14 13:14:57 +04:00
|
|
|
SqlDatabase::SqlDatabase()
|
2017-05-17 11:55:42 +03:00
|
|
|
: _db(0)
|
|
|
|
, _errId(0)
|
2014-10-14 13:14:57 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SqlDatabase::isOpen()
|
|
|
|
{
|
2014-10-15 18:46:39 +04:00
|
|
|
return _db != 0;
|
2014-10-14 13:14:57 +04:00
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
bool SqlDatabase::openHelper(const QString &filename, int sqliteFlags)
|
2014-10-14 13:14:57 +04:00
|
|
|
{
|
2017-05-17 11:55:42 +03:00
|
|
|
if (isOpen()) {
|
2014-10-14 14:18:33 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-04 15:55:58 +03:00
|
|
|
sqliteFlags |= SQLITE_OPEN_NOMUTEX;
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
SQLITE_DO(sqlite3_open_v2(filename.toUtf8().constData(), &_db, sqliteFlags, 0));
|
2014-10-14 13:14:57 +04:00
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
if (_errId != SQLITE_OK) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcSql) << "Error:" << _error << "for" << filename;
|
2017-06-06 13:59:57 +03:00
|
|
|
if (_errId == SQLITE_CANTOPEN) {
|
|
|
|
qCWarning(lcSql) << "CANTOPEN extended errcode: " << sqlite3_extended_errcode(_db);
|
2017-06-06 15:45:27 +03:00
|
|
|
#if SQLITE_VERSION_NUMBER >= 3012000
|
2017-06-06 13:59:57 +03:00
|
|
|
qCWarning(lcSql) << "CANTOPEN system errno: " << sqlite3_system_errno(_db);
|
2017-06-06 15:45:27 +03:00
|
|
|
#endif
|
2017-06-06 13:59:57 +03:00
|
|
|
}
|
2014-10-28 23:46:30 +03:00
|
|
|
close();
|
2014-12-04 15:55:58 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
if (!_db) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcSql) << "Error: no database for" << filename;
|
2014-12-04 15:55:58 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlite3_busy_timeout(_db, 5000);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SqlDatabase::checkDb()
|
|
|
|
{
|
2016-12-21 13:09:24 +03:00
|
|
|
// quick_check can fail with a disk IO error when diskspace is low
|
|
|
|
SqlQuery quick_check(*this);
|
|
|
|
quick_check.prepare("PRAGMA quick_check;", /*allow_failure=*/true);
|
2017-05-17 11:55:42 +03:00
|
|
|
if (!quick_check.exec()) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcSql) << "Error running quick_check on database";
|
2014-12-04 15:55:58 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
quick_check.next();
|
|
|
|
QString result = quick_check.stringValue(0);
|
2017-05-17 11:55:42 +03:00
|
|
|
if (result != "ok") {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcSql) << "quick_check returned failure:" << result;
|
2014-12-04 15:55:58 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
bool SqlDatabase::openOrCreateReadWrite(const QString &filename)
|
2014-12-04 15:55:58 +03:00
|
|
|
{
|
2017-05-17 11:55:42 +03:00
|
|
|
if (isOpen()) {
|
2014-12-04 15:55:58 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
if (!openHelper(filename, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE)) {
|
2014-12-04 15:55:58 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
if (!checkDb()) {
|
2016-12-13 15:56:36 +03:00
|
|
|
// When disk space is low, checking the db may fail even though it's fine.
|
2017-04-26 11:12:26 +03:00
|
|
|
qint64 freeSpace = Utility::freeDiskSpace(QFileInfo(filename).dir().absolutePath());
|
|
|
|
if (freeSpace != -1 && freeSpace < 1000000) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcSql) << "Consistency check failed, disk space is low, aborting" << freeSpace;
|
2016-12-13 15:56:36 +03:00
|
|
|
close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-30 14:46:20 +03:00
|
|
|
qCCritical(lcSql) << "Consistency check failed, removing broken db" << filename;
|
2014-12-04 15:55:58 +03:00
|
|
|
close();
|
|
|
|
QFile::remove(filename);
|
|
|
|
|
|
|
|
return openHelper(filename, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
bool SqlDatabase::openReadOnly(const QString &filename)
|
2014-12-04 15:55:58 +03:00
|
|
|
{
|
2017-05-17 11:55:42 +03:00
|
|
|
if (isOpen()) {
|
2014-12-04 15:55:58 +03:00
|
|
|
return true;
|
2014-10-14 13:14:57 +04:00
|
|
|
}
|
2014-10-20 19:26:25 +04:00
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
if (!openHelper(filename, SQLITE_OPEN_READONLY)) {
|
2014-12-04 15:55:58 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
if (!checkDb()) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcSql) << "Consistency check failed in readonly mode, giving up" << filename;
|
2014-12-04 15:55:58 +03:00
|
|
|
close();
|
|
|
|
return false;
|
2014-10-28 23:46:30 +03:00
|
|
|
}
|
2014-10-20 19:26:25 +04:00
|
|
|
|
2014-12-04 15:55:58 +03:00
|
|
|
return true;
|
2014-10-14 13:14:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
QString SqlDatabase::error() const
|
|
|
|
{
|
|
|
|
const QString err(_error);
|
|
|
|
// _error.clear();
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SqlDatabase::close()
|
|
|
|
{
|
2017-05-17 11:55:42 +03:00
|
|
|
if (_db) {
|
|
|
|
SQLITE_DO(sqlite3_close(_db));
|
2017-02-07 15:52:15 +03:00
|
|
|
// Fatal because reopening an unclosed db might be problematic.
|
|
|
|
ENFORCE(_errId == SQLITE_OK, "Error when closing DB");
|
2014-10-15 18:46:39 +04:00
|
|
|
_db = 0;
|
2014-10-14 13:14:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SqlDatabase::transaction()
|
|
|
|
{
|
2017-05-17 11:55:42 +03:00
|
|
|
if (!_db) {
|
2014-10-15 21:47:25 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
SQLITE_DO(sqlite3_exec(_db, "BEGIN", 0, 0, 0));
|
|
|
|
return _errId == SQLITE_OK;
|
2014-10-14 13:14:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SqlDatabase::commit()
|
|
|
|
{
|
2017-05-17 11:55:42 +03:00
|
|
|
if (!_db) {
|
2014-10-15 21:47:25 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
SQLITE_DO(sqlite3_exec(_db, "COMMIT", 0, 0, 0));
|
|
|
|
return _errId == SQLITE_OK;
|
2014-10-14 13:14:57 +04:00
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
sqlite3 *SqlDatabase::sqliteDb()
|
2014-10-14 13:14:57 +04:00
|
|
|
{
|
|
|
|
return _db;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* =========================================================================================== */
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
SqlQuery::SqlQuery(SqlDatabase &db)
|
|
|
|
: _db(db.sqliteDb())
|
|
|
|
, _stmt(0)
|
|
|
|
, _errId(0)
|
2014-10-14 13:14:57 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SqlQuery::~SqlQuery()
|
|
|
|
{
|
2017-05-17 11:55:42 +03:00
|
|
|
if (_stmt) {
|
2014-10-17 15:39:48 +04:00
|
|
|
finish();
|
2014-10-14 13:14:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
SqlQuery::SqlQuery(const QString &sql, SqlDatabase &db)
|
|
|
|
: _db(db.sqliteDb())
|
|
|
|
, _stmt(0)
|
|
|
|
, _errId(0)
|
2014-10-14 13:14:57 +04:00
|
|
|
{
|
|
|
|
prepare(sql);
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
int SqlQuery::prepare(const QString &sql, bool allow_failure)
|
2014-10-14 13:14:57 +04:00
|
|
|
{
|
2014-10-14 22:51:51 +04:00
|
|
|
QString s(sql);
|
|
|
|
_sql = s.trimmed();
|
2017-05-17 11:55:42 +03:00
|
|
|
if (_stmt) {
|
2014-10-14 22:51:51 +04:00
|
|
|
finish();
|
|
|
|
}
|
2017-05-17 11:55:42 +03:00
|
|
|
if (!_sql.isEmpty()) {
|
2014-10-18 18:16:29 +04:00
|
|
|
int n = 0;
|
|
|
|
int rc;
|
|
|
|
do {
|
|
|
|
rc = sqlite3_prepare_v2(_db, _sql.toUtf8().constData(), -1, &_stmt, 0);
|
2017-05-17 11:55:42 +03:00
|
|
|
if ((rc == SQLITE_BUSY) || (rc == SQLITE_LOCKED)) {
|
2014-10-18 18:16:29 +04:00
|
|
|
n++;
|
2014-11-10 00:34:07 +03:00
|
|
|
OCC::Utility::usleep(SQLITE_SLEEP_TIME_USEC);
|
2014-10-18 18:16:29 +04:00
|
|
|
}
|
2017-05-17 11:55:42 +03:00
|
|
|
} while ((n < SQLITE_REPEAT_COUNT) && ((rc == SQLITE_BUSY) || (rc == SQLITE_LOCKED)));
|
2014-10-18 18:16:29 +04:00
|
|
|
_errId = rc;
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
if (_errId != SQLITE_OK) {
|
2015-01-16 11:12:25 +03:00
|
|
|
_error = QString::fromUtf8(sqlite3_errmsg(_db));
|
2017-05-17 11:55:42 +03:00
|
|
|
qCWarning(lcSql) << "Sqlite prepare statement error:" << _error << "in" << _sql;
|
2017-02-07 15:52:15 +03:00
|
|
|
ENFORCE(allow_failure, "SQLITE Prepare error");
|
2014-10-14 22:51:51 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return _errId;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SqlQuery::isSelect()
|
|
|
|
{
|
|
|
|
return (!_sql.isEmpty() && _sql.startsWith("SELECT", Qt::CaseInsensitive));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SqlQuery::isPragma()
|
|
|
|
{
|
|
|
|
return (!_sql.isEmpty() && _sql.startsWith("PRAGMA", Qt::CaseInsensitive));
|
2014-10-14 13:14:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SqlQuery::exec()
|
|
|
|
{
|
2017-03-30 14:46:20 +03:00
|
|
|
qCDebug(lcSql) << "SQL exec" << _sql;
|
|
|
|
|
2016-09-22 12:25:27 +03:00
|
|
|
if (!_stmt) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcSql) << "Can't exec query, statement unprepared.";
|
2016-09-22 12:25:27 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-14 22:51:51 +04:00
|
|
|
// Don't do anything for selects, that is how we use the lib :-|
|
2017-05-17 11:55:42 +03:00
|
|
|
if (!isSelect() && !isPragma()) {
|
2014-10-18 18:16:29 +04:00
|
|
|
int rc, n = 0;
|
|
|
|
do {
|
|
|
|
rc = sqlite3_step(_stmt);
|
2017-05-17 11:55:42 +03:00
|
|
|
if (rc == SQLITE_LOCKED) {
|
2014-10-18 18:16:29 +04:00
|
|
|
rc = sqlite3_reset(_stmt); /* This will also return SQLITE_LOCKED */
|
|
|
|
n++;
|
2014-11-10 00:34:07 +03:00
|
|
|
OCC::Utility::usleep(SQLITE_SLEEP_TIME_USEC);
|
2017-05-17 11:55:42 +03:00
|
|
|
} else if (rc == SQLITE_BUSY) {
|
2014-11-10 00:34:07 +03:00
|
|
|
OCC::Utility::usleep(SQLITE_SLEEP_TIME_USEC);
|
2014-10-18 18:16:29 +04:00
|
|
|
n++;
|
|
|
|
}
|
2017-05-17 11:55:42 +03:00
|
|
|
} while ((n < SQLITE_REPEAT_COUNT) && ((rc == SQLITE_BUSY) || (rc == SQLITE_LOCKED)));
|
2014-10-18 18:16:29 +04:00
|
|
|
_errId = rc;
|
|
|
|
|
2015-01-29 11:47:07 +03:00
|
|
|
if (_errId != SQLITE_DONE && _errId != SQLITE_ROW) {
|
|
|
|
_error = QString::fromUtf8(sqlite3_errmsg(_db));
|
2017-05-17 11:55:42 +03:00
|
|
|
qCWarning(lcSql) << "Sqlite exec statement error:" << _errId << _error << "in" << _sql;
|
2017-03-30 14:46:20 +03:00
|
|
|
} else {
|
|
|
|
qCDebug(lcSql) << "Last exec affected" << numRowsAffected() << "rows.";
|
2015-01-29 11:47:07 +03:00
|
|
|
}
|
2014-10-15 15:29:25 +04:00
|
|
|
return (_errId == SQLITE_DONE); // either SQLITE_ROW or SQLITE_DONE
|
2014-10-14 22:51:51 +04:00
|
|
|
}
|
2014-10-14 13:14:57 +04:00
|
|
|
|
2014-10-14 22:51:51 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SqlQuery::next()
|
|
|
|
{
|
|
|
|
SQLITE_DO(sqlite3_step(_stmt));
|
|
|
|
return _errId == SQLITE_ROW;
|
2014-10-14 13:14:57 +04:00
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
void SqlQuery::bindValue(int pos, const QVariant &value)
|
2014-10-14 13:14:57 +04:00
|
|
|
{
|
2017-03-30 14:46:20 +03:00
|
|
|
qCDebug(lcSql) << "SQL bind" << pos << value;
|
|
|
|
|
2014-10-16 13:20:21 +04:00
|
|
|
int res = -1;
|
2017-02-07 15:52:15 +03:00
|
|
|
if (!_stmt) {
|
|
|
|
ASSERT(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (value.type()) {
|
|
|
|
case QVariant::Int:
|
|
|
|
case QVariant::Bool:
|
|
|
|
res = sqlite3_bind_int(_stmt, pos, value.toInt());
|
|
|
|
break;
|
|
|
|
case QVariant::Double:
|
|
|
|
res = sqlite3_bind_double(_stmt, pos, value.toDouble());
|
|
|
|
break;
|
|
|
|
case QVariant::UInt:
|
|
|
|
case QVariant::LongLong:
|
|
|
|
res = sqlite3_bind_int64(_stmt, pos, value.toLongLong());
|
|
|
|
break;
|
|
|
|
case QVariant::DateTime: {
|
|
|
|
const QDateTime dateTime = value.toDateTime();
|
|
|
|
const QString str = dateTime.toString(QLatin1String("yyyy-MM-ddThh:mm:ss.zzz"));
|
|
|
|
res = sqlite3_bind_text16(_stmt, pos, str.utf16(),
|
2017-05-17 11:55:42 +03:00
|
|
|
str.size() * sizeof(ushort), SQLITE_TRANSIENT);
|
2017-02-07 15:52:15 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QVariant::Time: {
|
|
|
|
const QTime time = value.toTime();
|
|
|
|
const QString str = time.toString(QLatin1String("hh:mm:ss.zzz"));
|
|
|
|
res = sqlite3_bind_text16(_stmt, pos, str.utf16(),
|
2017-05-17 11:55:42 +03:00
|
|
|
str.size() * sizeof(ushort), SQLITE_TRANSIENT);
|
2017-02-07 15:52:15 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QVariant::String: {
|
2017-05-17 11:55:42 +03:00
|
|
|
if (!value.toString().isNull()) {
|
2017-02-07 15:52:15 +03:00
|
|
|
// lifetime of string == lifetime of its qvariant
|
2017-05-17 11:55:42 +03:00
|
|
|
const QString *str = static_cast<const QString *>(value.constData());
|
2017-02-07 15:52:15 +03:00
|
|
|
res = sqlite3_bind_text16(_stmt, pos, str->utf16(),
|
2017-05-17 11:55:42 +03:00
|
|
|
(str->size()) * sizeof(QChar), SQLITE_TRANSIENT);
|
2017-02-07 15:52:15 +03:00
|
|
|
} else {
|
|
|
|
res = sqlite3_bind_null(_stmt, pos);
|
2014-10-14 13:14:57 +04:00
|
|
|
}
|
2017-05-17 11:55:42 +03:00
|
|
|
break;
|
|
|
|
}
|
2017-02-07 15:52:15 +03:00
|
|
|
case QVariant::ByteArray: {
|
|
|
|
auto ba = value.toByteArray();
|
|
|
|
res = sqlite3_bind_text(_stmt, pos, ba.constData(), ba.size(), SQLITE_TRANSIENT);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
QString str = value.toString();
|
|
|
|
// SQLITE_TRANSIENT makes sure that sqlite buffers the data
|
|
|
|
res = sqlite3_bind_text16(_stmt, pos, str.utf16(),
|
2017-05-17 11:55:42 +03:00
|
|
|
(str.size()) * sizeof(QChar), SQLITE_TRANSIENT);
|
|
|
|
break;
|
|
|
|
}
|
2014-10-14 13:14:57 +04:00
|
|
|
}
|
2014-10-21 18:37:51 +04:00
|
|
|
if (res != SQLITE_OK) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcSql) << "ERROR binding SQL value:" << value << "error:" << res;
|
2014-10-21 18:37:51 +04:00
|
|
|
}
|
2017-05-17 11:55:42 +03:00
|
|
|
ASSERT(res == SQLITE_OK);
|
2014-10-14 13:14:57 +04:00
|
|
|
}
|
|
|
|
|
2015-10-28 13:00:03 +03:00
|
|
|
bool SqlQuery::nullValue(int index)
|
|
|
|
{
|
|
|
|
return sqlite3_column_type(_stmt, index) == SQLITE_NULL;
|
|
|
|
}
|
|
|
|
|
2014-10-14 13:14:57 +04:00
|
|
|
QString SqlQuery::stringValue(int index)
|
|
|
|
{
|
2017-05-17 11:55:42 +03:00
|
|
|
return QString::fromUtf16(static_cast<const ushort *>(sqlite3_column_text16(_stmt, index)));
|
2014-10-14 13:14:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int SqlQuery::intValue(int index)
|
|
|
|
{
|
|
|
|
return sqlite3_column_int(_stmt, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
quint64 SqlQuery::int64Value(int index)
|
|
|
|
{
|
|
|
|
return sqlite3_column_int64(_stmt, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray SqlQuery::baValue(int index)
|
|
|
|
{
|
2017-05-17 11:55:42 +03:00
|
|
|
return QByteArray(static_cast<const char *>(sqlite3_column_blob(_stmt, index)),
|
|
|
|
sqlite3_column_bytes(_stmt, index));
|
2014-10-14 13:14:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
QString SqlQuery::error() const
|
|
|
|
{
|
2014-10-14 22:51:51 +04:00
|
|
|
return _error;
|
2014-10-14 13:14:57 +04:00
|
|
|
}
|
|
|
|
|
2016-04-11 18:34:59 +03:00
|
|
|
int SqlQuery::errorId() const
|
|
|
|
{
|
|
|
|
return _errId;
|
|
|
|
}
|
|
|
|
|
2014-10-14 13:14:57 +04:00
|
|
|
QString SqlQuery::lastQuery() const
|
|
|
|
{
|
2014-10-14 22:51:51 +04:00
|
|
|
return _sql;
|
2014-10-14 13:14:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int SqlQuery::numRowsAffected()
|
|
|
|
{
|
2015-05-13 13:54:03 +03:00
|
|
|
return sqlite3_changes(_db);
|
2014-10-14 13:14:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SqlQuery::finish()
|
|
|
|
{
|
|
|
|
SQLITE_DO(sqlite3_finalize(_stmt));
|
2014-10-15 18:46:39 +04:00
|
|
|
_stmt = 0;
|
2014-10-14 13:14:57 +04:00
|
|
|
}
|
2014-10-14 22:51:51 +04:00
|
|
|
|
2016-03-15 16:32:09 +03:00
|
|
|
void SqlQuery::reset_and_clear_bindings()
|
2014-10-14 22:51:51 +04:00
|
|
|
{
|
2016-09-22 12:25:27 +03:00
|
|
|
if (_stmt) {
|
|
|
|
SQLITE_DO(sqlite3_reset(_stmt));
|
|
|
|
SQLITE_DO(sqlite3_clear_bindings(_stmt));
|
|
|
|
}
|
2014-10-14 22:51:51 +04:00
|
|
|
}
|
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
} // namespace OCC
|