Commit 3dcce060 authored by Marco Bubke's avatar Marco Bubke

Sqlite: Cleanup Sqlite

We don't need the threading anymore, so we removed it. The indexer
will be run in its thread anyway, so an extra thread makes the code
only more complicated. And we added namespaces.

Change-Id: Ibcba306324763285cf653c28bb08122345e5f8da
Reviewed-by: Tim Jenssen's avatarTim Jenssen <tim.jenssen@qt.io>
parent 94ba1b8b
......@@ -25,49 +25,20 @@
#include "columndefinition.h"
namespace Internal {
void ColumnDefinition::setName(const Utf8String &name)
{
name_ = name;
}
const Utf8String &ColumnDefinition::name() const
{
return name_;
}
void ColumnDefinition::setType(ColumnType type)
{
type_ = type;
}
ColumnType ColumnDefinition::type() const
{
return type_;
}
Utf8String ColumnDefinition::typeString() const
{
switch (type_) {
case ColumnType::None: return Utf8String();
case ColumnType::Numeric: return Utf8StringLiteral("NUMERIC");
case ColumnType::Integer: return Utf8StringLiteral("INTEGER");
case ColumnType::Real: return Utf8StringLiteral("REAL");
case ColumnType::Text: return Utf8StringLiteral("TEXT");
}
Q_UNREACHABLE();
}
void ColumnDefinition::setIsPrimaryKey(bool isPrimaryKey)
{
isPrimaryKey_ = isPrimaryKey;
}
bool ColumnDefinition::isPrimaryKey() const
{
return isPrimaryKey_;
}
}
namespace Sqlite {
} // namespace Sqlite
......@@ -28,25 +28,58 @@
#include "sqliteglobal.h"
#include "utf8string.h"
namespace Internal {
namespace Sqlite {
class ColumnDefinition
{
public:
void setName(const Utf8String &name);
const Utf8String &name() const;
void setName(const Utf8String &name)
{
m_name = name;
}
void setType(ColumnType type);
ColumnType type() const;
Utf8String typeString() const;
const Utf8String &name() const
{
return m_name;
}
void setIsPrimaryKey(bool isPrimaryKey);
bool isPrimaryKey() const;
void setType(ColumnType type)
{
m_type = type;
}
ColumnType type() const
{
return m_type;
}
Utf8String typeString() const
{
switch (m_type) {
case ColumnType::None: return Utf8String();
case ColumnType::Numeric: return Utf8StringLiteral("NUMERIC");
case ColumnType::Integer: return Utf8StringLiteral("INTEGER");
case ColumnType::Real: return Utf8StringLiteral("REAL");
case ColumnType::Text: return Utf8StringLiteral("TEXT");
}
Q_UNREACHABLE();
}
void setIsPrimaryKey(bool isPrimaryKey)
{
m_isPrimaryKey = isPrimaryKey;
}
bool isPrimaryKey() const
{
return m_isPrimaryKey;
}
private:
Utf8String name_;
ColumnType type_;
bool isPrimaryKey_ = false;
Utf8String m_name;
ColumnType m_type;
bool m_isPrimaryKey = false;
};
}
} // namespace Sqlite
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** 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
** 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.
**
** 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.
**
****************************************************************************/
#include "createtablecommand.h"
namespace Internal {
void CreateTableCommand::registerType()
{
qRegisterMetaType<CreateTableCommand>("CreateTableCommand");
}
} // namespace Internal
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** 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
** 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.
**
** 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.
**
****************************************************************************/
#pragma once
#include "columndefinition.h"
#include "utf8string.h"
#include <QMetaType>
#include <QVector>
namespace Internal {
class CreateTableCommand
{
public:
QVector<ColumnDefinition> definitions;
Utf8String tableName;
bool useWithoutRowId;
static void registerType();
};
} // namespace Internal
Q_DECLARE_METATYPE(Internal::CreateTableCommand)
......@@ -27,79 +27,79 @@
#include "utf8stringvector.h"
namespace Internal {
namespace Sqlite {
CreateTableSqlStatementBuilder::CreateTableSqlStatementBuilder()
: sqlStatementBuilder(Utf8StringLiteral("CREATE TABLE IF NOT EXISTS $table($columnDefinitions)$withoutRowId")),
useWithoutRowId(false)
: m_sqlStatementBuilder(Utf8StringLiteral("CREATE TABLE IF NOT EXISTS $table($columnDefinitions)$withoutRowId")),
m_useWithoutRowId(false)
{
}
void CreateTableSqlStatementBuilder::setTable(const Utf8String &tableName)
{
sqlStatementBuilder.clear();
m_sqlStatementBuilder.clear();
this->tableName = tableName;
this->m_tableName = tableName;
}
void CreateTableSqlStatementBuilder::addColumnDefinition(const Utf8String &columnName,
ColumnType columnType,
bool isPrimaryKey)
{
sqlStatementBuilder.clear();
m_sqlStatementBuilder.clear();
ColumnDefinition columnDefinition;
columnDefinition.setName(columnName);
columnDefinition.setType(columnType);
columnDefinition.setIsPrimaryKey(isPrimaryKey);
columnDefinitions.append(columnDefinition);
m_columnDefinitions.append(columnDefinition);
}
void CreateTableSqlStatementBuilder::setColumnDefinitions(const QVector<ColumnDefinition> &columnDefinitions)
{
sqlStatementBuilder.clear();
m_sqlStatementBuilder.clear();
this->columnDefinitions = columnDefinitions;
this->m_columnDefinitions = columnDefinitions;
}
void CreateTableSqlStatementBuilder::setUseWithoutRowId(bool useWithoutRowId)
{
this->useWithoutRowId = useWithoutRowId;
this->m_useWithoutRowId = useWithoutRowId;
}
void CreateTableSqlStatementBuilder::clear()
{
sqlStatementBuilder.clear();
columnDefinitions.clear();
tableName.clear();
useWithoutRowId = false;
m_sqlStatementBuilder.clear();
m_columnDefinitions.clear();
m_tableName.clear();
m_useWithoutRowId = false;
}
void CreateTableSqlStatementBuilder::clearColumns()
{
sqlStatementBuilder.clear();
columnDefinitions.clear();
m_sqlStatementBuilder.clear();
m_columnDefinitions.clear();
}
Utf8String CreateTableSqlStatementBuilder::sqlStatement() const
{
if (!sqlStatementBuilder.isBuild())
if (!m_sqlStatementBuilder.isBuild())
bindAll();
return sqlStatementBuilder.sqlStatement();
return m_sqlStatementBuilder.sqlStatement();
}
bool CreateTableSqlStatementBuilder::isValid() const
{
return tableName.hasContent() && !columnDefinitions.isEmpty();
return m_tableName.hasContent() && !m_columnDefinitions.isEmpty();
}
void CreateTableSqlStatementBuilder::bindColumnDefinitions() const
{
Utf8StringVector columnDefinitionStrings;
foreach (const ColumnDefinition &columnDefinition, columnDefinitions) {
foreach (const ColumnDefinition &columnDefinition, m_columnDefinitions) {
Utf8String columnDefinitionString = columnDefinition.name() + Utf8StringLiteral(" ") + columnDefinition.typeString();
if (columnDefinition.isPrimaryKey())
......@@ -108,19 +108,19 @@ void CreateTableSqlStatementBuilder::bindColumnDefinitions() const
columnDefinitionStrings.append(columnDefinitionString);
}
sqlStatementBuilder.bind(Utf8StringLiteral("$columnDefinitions"), columnDefinitionStrings);
m_sqlStatementBuilder.bind(Utf8StringLiteral("$columnDefinitions"), columnDefinitionStrings);
}
void CreateTableSqlStatementBuilder::bindAll() const
{
sqlStatementBuilder.bind(Utf8StringLiteral("$table"), tableName);
m_sqlStatementBuilder.bind(Utf8StringLiteral("$table"), m_tableName);
bindColumnDefinitions();
if (useWithoutRowId)
sqlStatementBuilder.bind(Utf8StringLiteral("$withoutRowId"), Utf8StringLiteral(" WITHOUT ROWID"));
if (m_useWithoutRowId)
m_sqlStatementBuilder.bind(Utf8StringLiteral("$withoutRowId"), Utf8StringLiteral(" WITHOUT ROWID"));
else
sqlStatementBuilder.bindEmptyText(Utf8StringLiteral("$withoutRowId"));
m_sqlStatementBuilder.bindEmptyText(Utf8StringLiteral("$withoutRowId"));
}
}
} // namespace Sqlite
......@@ -30,17 +30,17 @@
#include <QVector>
namespace Internal {
namespace Sqlite {
class SQLITE_EXPORT CreateTableSqlStatementBuilder
{
public:
CreateTableSqlStatementBuilder();
void setTable(const Utf8String &tableName);
void setTable(const Utf8String &m_tableName);
void addColumnDefinition(const Utf8String &columnName, ColumnType columnType, bool isPrimaryKey = false);
void setColumnDefinitions(const QVector<ColumnDefinition> & columnDefinitions);
void setUseWithoutRowId(bool useWithoutRowId);
void setColumnDefinitions(const QVector<ColumnDefinition> & m_columnDefinitions);
void setUseWithoutRowId(bool m_useWithoutRowId);
void clear();
void clearColumns();
......@@ -54,10 +54,10 @@ protected:
void bindAll() const;
private:
mutable SqlStatementBuilder sqlStatementBuilder;
Utf8String tableName;
QVector<ColumnDefinition> columnDefinitions;
bool useWithoutRowId;
mutable SqlStatementBuilder m_sqlStatementBuilder;
Utf8String m_tableName;
QVector<ColumnDefinition> m_columnDefinitions;
bool m_useWithoutRowId;
};
}
} // namespace Sqlite
......@@ -12,18 +12,14 @@ include(../3rdparty/sqlite/sqlite.pri)
SOURCES += \
$$PWD/columndefinition.cpp \
$$PWD/createtablecommand.cpp \
$$PWD/createtablesqlstatementbuilder.cpp \
$$PWD/sqlitedatabasebackend.cpp \
$$PWD/sqlitedatabaseconnection.cpp \
$$PWD/sqlitedatabaseconnectionproxy.cpp \
$$PWD/sqliteexception.cpp \
$$PWD/sqliteglobal.cpp \
$$PWD/sqlitereadstatement.cpp \
$$PWD/sqlitereadwritestatement.cpp \
$$PWD/sqlitestatement.cpp \
$$PWD/sqlitetransaction.cpp \
$$PWD/sqliteworkerthread.cpp \
$$PWD/sqlitewritestatement.cpp \
$$PWD/sqlstatementbuilder.cpp \
$$PWD/sqlstatementbuilderexception.cpp \
......@@ -31,22 +27,17 @@ SOURCES += \
$$PWD/utf8stringvector.cpp \
$$PWD/sqlitedatabase.cpp \
$$PWD/sqlitetable.cpp \
$$PWD/sqlitecolumn.cpp \
$$PWD/tablewriteworker.cpp \
$$PWD/tablewriteworkerproxy.cpp
$$PWD/sqlitecolumn.cpp
HEADERS += \
$$PWD/columndefinition.h \
$$PWD/createtablesqlstatementbuilder.h \
$$PWD/sqlitedatabasebackend.h \
$$PWD/sqlitedatabaseconnection.h \
$$PWD/sqlitedatabaseconnectionproxy.h \
$$PWD/sqliteexception.h \
$$PWD/sqliteglobal.h \
$$PWD/sqlitereadstatement.h \
$$PWD/sqlitereadwritestatement.h \
$$PWD/sqlitestatement.h \
$$PWD/sqlitetransaction.h \
$$PWD/sqliteworkerthread.h \
$$PWD/sqlitewritestatement.h \
$$PWD/sqlstatementbuilder.h \
$$PWD/sqlstatementbuilderexception.h \
......@@ -54,10 +45,7 @@ HEADERS += \
$$PWD/utf8stringvector.h \
$$PWD/sqlitedatabase.h \
$$PWD/sqlitetable.h \
$$PWD/sqlitecolumn.h \
$$PWD/tablewriteworker.h \
$$PWD/tablewriteworkerproxy.h \
$$PWD/createtablecommand.h
$$PWD/sqlitecolumn.h
DEFINES += SQLITE_THREADSAFE=2 SQLITE_ENABLE_FTS4 SQLITE_ENABLE_FTS3_PARENTHESIS SQLITE_ENABLE_UNLOCK_NOTIFY SQLITE_ENABLE_COLUMN_METADATA
......
......@@ -25,57 +25,7 @@
#include "sqlitecolumn.h"
SqliteColumn::SqliteColumn()
: type_(ColumnType::Numeric),
isPrimaryKey_(false)
{
namespace Sqlite {
}
void SqliteColumn::clear()
{
name_.clear();
type_ = ColumnType::Numeric;
isPrimaryKey_ = false;
}
void SqliteColumn::setName(const Utf8String &newName)
{
name_ = newName;
}
const Utf8String &SqliteColumn::name() const
{
return name_;
}
void SqliteColumn::setType(ColumnType newType)
{
type_ = newType;
}
ColumnType SqliteColumn::type() const
{
return type_;
}
void SqliteColumn::setIsPrimaryKey(bool isPrimaryKey)
{
isPrimaryKey_ = isPrimaryKey;
}
bool SqliteColumn::isPrimaryKey() const
{
return isPrimaryKey_;
}
Internal::ColumnDefinition SqliteColumn::columnDefintion() const
{
Internal::ColumnDefinition columnDefinition;
columnDefinition.setName(name_);
columnDefinition.setType(type_);
columnDefinition.setIsPrimaryKey(isPrimaryKey_);
return columnDefinition;
}
} // namespace Sqlite
......@@ -30,27 +30,70 @@
#include <QObject>
class SQLITE_EXPORT SqliteColumn : public QObject
namespace Sqlite {
class SqliteColumn
{
Q_OBJECT
public:
SqliteColumn();
SqliteColumn()
: m_type(ColumnType::Numeric),
m_isPrimaryKey(false)
{
}
void clear()
{
m_name.clear();
m_type = ColumnType::Numeric;
m_isPrimaryKey = false;
}
void setName(const Utf8String &newName)
{
m_name = newName;
}
void clear();
const Utf8String &name() const
{
return m_name;
}
void setName(const Utf8String &newName);
const Utf8String &name() const;
void setType(ColumnType newType)
{
m_type = newType;
}
void setType(ColumnType newType);
ColumnType type() const;
ColumnType type() const
{
return m_type;
}
void setIsPrimaryKey(bool isPrimaryKey);
bool isPrimaryKey() const;
void setIsPrimaryKey(bool isPrimaryKey)
{
m_isPrimaryKey = isPrimaryKey;
}
Internal::ColumnDefinition columnDefintion() const;
bool isPrimaryKey() const
{
return m_isPrimaryKey;
}
ColumnDefinition columnDefintion() const
{
ColumnDefinition columnDefinition;
columnDefinition.setName(m_name);
columnDefinition.setType(m_type);
columnDefinition.setIsPrimaryKey(m_isPrimaryKey);
return columnDefinition;
}
private:
Utf8String name_;
ColumnType type_;
bool isPrimaryKey_;
Utf8String m_name;
ColumnType m_type;
bool m_isPrimaryKey;
};
} // namespace Sqlite
......@@ -27,103 +27,66 @@
#include "sqlitetable.h"
namespace Sqlite {
SqliteDatabase::SqliteDatabase()
: readDatabaseConnection(QStringLiteral("ReadWorker")),
writeDatabaseConnection(QStringLiteral("WriterWorker")),
journalMode_(JournalMode::Wal)
: m_journalMode(JournalMode::Wal)
{
connect(&readDatabaseConnection, &SqliteDatabaseConnectionProxy::connectionIsOpened, this, &SqliteDatabase::handleReadDatabaseConnectionIsOpened);
connect(&writeDatabaseConnection, &SqliteDatabaseConnectionProxy::connectionIsOpened, this, &SqliteDatabase::handleWriteDatabaseConnectionIsOpened);
connect(&readDatabaseConnection, &SqliteDatabaseConnectionProxy::connectionIsClosed, this, &SqliteDatabase::handleReadDatabaseConnectionIsClosed);
connect(&writeDatabaseConnection, &SqliteDatabaseConnectionProxy::connectionIsClosed, this, &SqliteDatabase::handleWriteDatabaseConnectionIsClosed);
}
SqliteDatabase::~SqliteDatabase()
{
qDeleteAll(sqliteTables);
qDeleteAll(m_sqliteTables);
}
void SqliteDatabase::open()
{
writeDatabaseConnection.setDatabaseFilePath(databaseFilePath());
writeDatabaseConnection.setJournalMode(journalMode());
m_sqliteDatabaseBackEnd.open(m_databaseFilePath);
m_sqliteDatabaseBackEnd.setJournalMode(journalMode());
initializeTables();
m_isOpen = true;
}
void SqliteDatabase::close()
{
writeDatabaseConnection.close();
m_isOpen = false;
m_sqliteDatabaseBackEnd.close();
}
bool SqliteDatabase::isOpen() const
{
return readDatabaseConnection.isOpen() && writeDatabaseConnection.isOpen();
return m_isOpen;
}
void SqliteDatabase::addTable(SqliteTable *newSqliteTable)
{
newSqliteTable->setSqliteDatabase(this);
sqliteTables.append(newSqliteTable);
m_sqliteTables.append(newSqliteTable);
}
const QVector<SqliteTable *> &SqliteDatabase::tables() const
{
return sqliteTables;
return m_sqliteTables;
}
void SqliteDatabase::setDatabaseFilePath(const QString &databaseFilePath)
{
databaseFilePath_ = databaseFilePath;
m_databaseFilePath = databaseFilePath;
}
const QString &SqliteDatabase::databaseFilePath() const
{
return databaseFilePath_;
return m_databaseFilePath;
}
void SqliteDatabase::setJournalMode(JournalMode journalMode)
{
journalMode_ = journalMode;
m_journalMode = journalMode;
}
JournalMode SqliteDatabase::journalMode() const
{
return journalMode_;
}
QThread *SqliteDatabase::writeWorkerThread() const
{
return writeDatabaseConnection.connectionThread();
}
QThread *SqliteDatabase::readWorkerThread() const
{
return readDatabaseConnection.connectionThread();
}
void SqliteDatabase::handleReadDatabaseConnectionIsOpened()
{
if (writeDatabaseConnection.isOpen() && readDatabaseConnection.isOpen()) {
initializeTables();
emit databaseIsOpened();
}
}
void SqliteDatabase::handleWriteDatabaseConnectionIsOpened()
{
readDatabaseConnection.setDatabaseFilePath(databaseFilePath());
}
void SqliteDatabase::handleReadDatabaseConnectionIsClosed()
{
if (!writeDatabaseConnection.isOpen() && !readDatabaseConnection.isOpen()) {
shutdownTables();
emit databaseIsClosed();
}
}
void SqliteDatabase::handleWriteDatabaseConnectionIsClosed()
{
readDatabaseConnection.close();
return m_journalMode;
}
void SqliteDatabase::initializeTables()
......@@ -132,10 +95,4 @@ void SqliteDatabase::initializeTables()
table->initialize();
}
void SqliteDatabase::shutdownTables()
{
for (SqliteTable *table: tables())
table->shutdown();
}