forked from qt-creator/qt-creator
Sqlite: Cleanup table and column handling
Using values instead of pointers makes the handling much easier. We can remove ColumnDefinition too, and use SqliteColumn instead. Change-Id: I224db9cc569c4dfb6e2746179b02096904bfbccb Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
@@ -1,44 +0,0 @@
|
|||||||
/****************************************************************************
|
|
||||||
**
|
|
||||||
** 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 "columndefinition.h"
|
|
||||||
|
|
||||||
namespace Sqlite {
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} // namespace Sqlite
|
|
@@ -1,90 +0,0 @@
|
|||||||
/****************************************************************************
|
|
||||||
**
|
|
||||||
** 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 "sqliteglobal.h"
|
|
||||||
|
|
||||||
#include <utils/smallstring.h>
|
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
namespace Sqlite {
|
|
||||||
|
|
||||||
class ColumnDefinition
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
void setName(Utils::SmallString &&name)
|
|
||||||
{
|
|
||||||
m_name = std::move(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
const Utils::SmallString &name() const
|
|
||||||
{
|
|
||||||
return m_name;
|
|
||||||
}
|
|
||||||
|
|
||||||
void setType(ColumnType type)
|
|
||||||
{
|
|
||||||
m_type = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
ColumnType type() const
|
|
||||||
{
|
|
||||||
return m_type;
|
|
||||||
}
|
|
||||||
|
|
||||||
Utils::SmallString typeString() const
|
|
||||||
{
|
|
||||||
switch (m_type) {
|
|
||||||
case ColumnType::None: return {};
|
|
||||||
case ColumnType::Numeric: return "NUMERIC";
|
|
||||||
case ColumnType::Integer: return "INTEGER";
|
|
||||||
case ColumnType::Real: return "REAL";
|
|
||||||
case ColumnType::Text: return "TEXT";
|
|
||||||
}
|
|
||||||
|
|
||||||
Q_UNREACHABLE();
|
|
||||||
}
|
|
||||||
|
|
||||||
void setIsPrimaryKey(bool isPrimaryKey)
|
|
||||||
{
|
|
||||||
m_isPrimaryKey = isPrimaryKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool isPrimaryKey() const
|
|
||||||
{
|
|
||||||
return m_isPrimaryKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
Utils::SmallString m_name;
|
|
||||||
ColumnType m_type;
|
|
||||||
bool m_isPrimaryKey = false;
|
|
||||||
};
|
|
||||||
|
|
||||||
using ColumnDefinitions = std::vector<ColumnDefinition>;
|
|
||||||
|
|
||||||
} // namespace Sqlite
|
|
@@ -40,25 +40,20 @@ void CreateTableSqlStatementBuilder::setTable(Utils::SmallString &&tableName)
|
|||||||
this->m_tableName = std::move(tableName);
|
this->m_tableName = std::move(tableName);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CreateTableSqlStatementBuilder::addColumnDefinition(Utils::SmallString &&columnName,
|
void CreateTableSqlStatementBuilder::addColumn(Utils::SmallString &&columnName,
|
||||||
ColumnType columnType,
|
ColumnType columnType,
|
||||||
bool isPrimaryKey)
|
IsPrimaryKey isPrimaryKey)
|
||||||
{
|
{
|
||||||
m_sqlStatementBuilder.clear();
|
m_sqlStatementBuilder.clear();
|
||||||
|
|
||||||
ColumnDefinition columnDefinition;
|
m_columns.emplace_back(std::move(columnName), columnType, isPrimaryKey);
|
||||||
columnDefinition.setName(std::move(columnName));
|
|
||||||
columnDefinition.setType(columnType);
|
|
||||||
columnDefinition.setIsPrimaryKey(isPrimaryKey);
|
|
||||||
|
|
||||||
m_columnDefinitions.push_back(columnDefinition);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CreateTableSqlStatementBuilder::setColumnDefinitions(ColumnDefinitions &&columnDefinitions)
|
void CreateTableSqlStatementBuilder::setColumns(const SqliteColumns &columns)
|
||||||
{
|
{
|
||||||
m_sqlStatementBuilder.clear();
|
m_sqlStatementBuilder.clear();
|
||||||
|
|
||||||
m_columnDefinitions = std::move(columnDefinitions);
|
m_columns = std::move(columns);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CreateTableSqlStatementBuilder::setUseWithoutRowId(bool useWithoutRowId)
|
void CreateTableSqlStatementBuilder::setUseWithoutRowId(bool useWithoutRowId)
|
||||||
@@ -69,7 +64,7 @@ void CreateTableSqlStatementBuilder::setUseWithoutRowId(bool useWithoutRowId)
|
|||||||
void CreateTableSqlStatementBuilder::clear()
|
void CreateTableSqlStatementBuilder::clear()
|
||||||
{
|
{
|
||||||
m_sqlStatementBuilder.clear();
|
m_sqlStatementBuilder.clear();
|
||||||
m_columnDefinitions.clear();
|
m_columns.clear();
|
||||||
m_tableName.clear();
|
m_tableName.clear();
|
||||||
m_useWithoutRowId = false;
|
m_useWithoutRowId = false;
|
||||||
}
|
}
|
||||||
@@ -77,7 +72,7 @@ void CreateTableSqlStatementBuilder::clear()
|
|||||||
void CreateTableSqlStatementBuilder::clearColumns()
|
void CreateTableSqlStatementBuilder::clearColumns()
|
||||||
{
|
{
|
||||||
m_sqlStatementBuilder.clear();
|
m_sqlStatementBuilder.clear();
|
||||||
m_columnDefinitions.clear();
|
m_columns.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
Utils::SmallStringView CreateTableSqlStatementBuilder::sqlStatement() const
|
Utils::SmallStringView CreateTableSqlStatementBuilder::sqlStatement() const
|
||||||
@@ -90,17 +85,17 @@ Utils::SmallStringView CreateTableSqlStatementBuilder::sqlStatement() const
|
|||||||
|
|
||||||
bool CreateTableSqlStatementBuilder::isValid() const
|
bool CreateTableSqlStatementBuilder::isValid() const
|
||||||
{
|
{
|
||||||
return m_tableName.hasContent() && !m_columnDefinitions.empty();
|
return m_tableName.hasContent() && !m_columns.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
void CreateTableSqlStatementBuilder::bindColumnDefinitions() const
|
void CreateTableSqlStatementBuilder::bindColumnDefinitions() const
|
||||||
{
|
{
|
||||||
Utils::SmallStringVector columnDefinitionStrings;
|
Utils::SmallStringVector columnDefinitionStrings;
|
||||||
|
|
||||||
for (const ColumnDefinition &columnDefinition : m_columnDefinitions) {
|
for (const SqliteColumn &columns : m_columns) {
|
||||||
Utils::SmallString columnDefinitionString = {columnDefinition.name(), " ", columnDefinition.typeString()};
|
Utils::SmallString columnDefinitionString = {columns.name(), " ", columns.typeString()};
|
||||||
|
|
||||||
if (columnDefinition.isPrimaryKey())
|
if (columns.isPrimaryKey())
|
||||||
columnDefinitionString.append(" PRIMARY KEY");
|
columnDefinitionString.append(" PRIMARY KEY");
|
||||||
|
|
||||||
columnDefinitionStrings.push_back(columnDefinitionString);
|
columnDefinitionStrings.push_back(columnDefinitionString);
|
||||||
|
@@ -25,11 +25,9 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "columndefinition.h"
|
#include "sqlitecolumn.h"
|
||||||
#include "sqlstatementbuilder.h"
|
#include "sqlstatementbuilder.h"
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
namespace Sqlite {
|
namespace Sqlite {
|
||||||
|
|
||||||
class SQLITE_EXPORT CreateTableSqlStatementBuilder
|
class SQLITE_EXPORT CreateTableSqlStatementBuilder
|
||||||
@@ -38,8 +36,10 @@ public:
|
|||||||
CreateTableSqlStatementBuilder();
|
CreateTableSqlStatementBuilder();
|
||||||
|
|
||||||
void setTable(Utils::SmallString &&tableName);
|
void setTable(Utils::SmallString &&tableName);
|
||||||
void addColumnDefinition(Utils::SmallString &&columnName, ColumnType columnType, bool isPrimaryKey = false);
|
void addColumn(Utils::SmallString &&columnName,
|
||||||
void setColumnDefinitions(ColumnDefinitions &&columnDefinitions);
|
ColumnType columnType,
|
||||||
|
IsPrimaryKey isPrimaryKey = IsPrimaryKey::No);
|
||||||
|
void setColumns(const SqliteColumns &columns);
|
||||||
void setUseWithoutRowId(bool useWithoutRowId);
|
void setUseWithoutRowId(bool useWithoutRowId);
|
||||||
|
|
||||||
void clear();
|
void clear();
|
||||||
@@ -56,7 +56,7 @@ protected:
|
|||||||
private:
|
private:
|
||||||
mutable SqlStatementBuilder m_sqlStatementBuilder;
|
mutable SqlStatementBuilder m_sqlStatementBuilder;
|
||||||
Utils::SmallString m_tableName;
|
Utils::SmallString m_tableName;
|
||||||
ColumnDefinitions m_columnDefinitions;
|
SqliteColumns m_columns;
|
||||||
bool m_useWithoutRowId;
|
bool m_useWithoutRowId;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -11,7 +11,6 @@ unix:!bsd: LIBS += -ldl
|
|||||||
include(../3rdparty/sqlite/sqlite.pri)
|
include(../3rdparty/sqlite/sqlite.pri)
|
||||||
|
|
||||||
SOURCES += \
|
SOURCES += \
|
||||||
$$PWD/columndefinition.cpp \
|
|
||||||
$$PWD/createtablesqlstatementbuilder.cpp \
|
$$PWD/createtablesqlstatementbuilder.cpp \
|
||||||
$$PWD/sqlitedatabasebackend.cpp \
|
$$PWD/sqlitedatabasebackend.cpp \
|
||||||
$$PWD/sqliteexception.cpp \
|
$$PWD/sqliteexception.cpp \
|
||||||
@@ -29,7 +28,6 @@ SOURCES += \
|
|||||||
$$PWD/sqlitetable.cpp \
|
$$PWD/sqlitetable.cpp \
|
||||||
$$PWD/sqlitecolumn.cpp
|
$$PWD/sqlitecolumn.cpp
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
$$PWD/columndefinition.h \
|
|
||||||
$$PWD/createtablesqlstatementbuilder.h \
|
$$PWD/createtablesqlstatementbuilder.h \
|
||||||
$$PWD/sqlitedatabasebackend.h \
|
$$PWD/sqlitedatabasebackend.h \
|
||||||
$$PWD/sqliteexception.h \
|
$$PWD/sqliteexception.h \
|
||||||
|
@@ -25,28 +25,30 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "columndefinition.h"
|
#include "sqliteglobal.h"
|
||||||
#include "utf8string.h"
|
|
||||||
|
|
||||||
#include <QObject>
|
#include <utils/smallstring.h>
|
||||||
|
|
||||||
namespace Sqlite {
|
namespace Sqlite {
|
||||||
|
|
||||||
class SqliteColumn
|
class SqliteColumn
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
SqliteColumn()
|
SqliteColumn() = default;
|
||||||
: m_type(ColumnType::Numeric),
|
|
||||||
m_isPrimaryKey(false)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
SqliteColumn(Utils::SmallString &&name,
|
||||||
|
ColumnType type = ColumnType::Numeric,
|
||||||
|
IsPrimaryKey isPrimaryKey = IsPrimaryKey::No)
|
||||||
|
: m_name(std::move(name)),
|
||||||
|
m_type(type),
|
||||||
|
m_isPrimaryKey(isPrimaryKey)
|
||||||
|
{}
|
||||||
|
|
||||||
void clear()
|
void clear()
|
||||||
{
|
{
|
||||||
m_name.clear();
|
m_name.clear();
|
||||||
m_type = ColumnType::Numeric;
|
m_type = ColumnType::Numeric;
|
||||||
m_isPrimaryKey = false;
|
m_isPrimaryKey = IsPrimaryKey::No;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setName(Utils::SmallString &&newName)
|
void setName(Utils::SmallString &&newName)
|
||||||
@@ -69,31 +71,42 @@ public:
|
|||||||
return m_type;
|
return m_type;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setIsPrimaryKey(bool isPrimaryKey)
|
void setIsPrimaryKey(IsPrimaryKey isPrimaryKey)
|
||||||
{
|
{
|
||||||
m_isPrimaryKey = isPrimaryKey;
|
m_isPrimaryKey = isPrimaryKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isPrimaryKey() const
|
bool isPrimaryKey() const
|
||||||
{
|
{
|
||||||
return m_isPrimaryKey;
|
return m_isPrimaryKey == IsPrimaryKey::Yes;
|
||||||
}
|
}
|
||||||
|
|
||||||
ColumnDefinition columnDefintion() const
|
Utils::SmallString typeString() const
|
||||||
{
|
{
|
||||||
ColumnDefinition columnDefinition;
|
switch (m_type) {
|
||||||
|
case ColumnType::None: return {};
|
||||||
|
case ColumnType::Numeric: return "NUMERIC";
|
||||||
|
case ColumnType::Integer: return "INTEGER";
|
||||||
|
case ColumnType::Real: return "REAL";
|
||||||
|
case ColumnType::Text: return "TEXT";
|
||||||
|
}
|
||||||
|
|
||||||
columnDefinition.setName(m_name.clone());
|
Q_UNREACHABLE();
|
||||||
columnDefinition.setType(m_type);
|
}
|
||||||
columnDefinition.setIsPrimaryKey(m_isPrimaryKey);
|
|
||||||
|
|
||||||
return columnDefinition;
|
friend bool operator==(const SqliteColumn &first, const SqliteColumn &second)
|
||||||
|
{
|
||||||
|
return first.m_name == second.m_name
|
||||||
|
&& first.m_type == second.m_type
|
||||||
|
&& first.m_isPrimaryKey == second.m_isPrimaryKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Utils::SmallString m_name;
|
Utils::SmallString m_name;
|
||||||
ColumnType m_type;
|
ColumnType m_type = ColumnType::Numeric;
|
||||||
bool m_isPrimaryKey;
|
IsPrimaryKey m_isPrimaryKey = IsPrimaryKey::No;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
using SqliteColumns = std::vector<SqliteColumn>;
|
||||||
|
|
||||||
} // namespace Sqlite
|
} // namespace Sqlite
|
||||||
|
@@ -35,11 +35,6 @@ SqliteDatabase::SqliteDatabase()
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
SqliteDatabase::~SqliteDatabase()
|
|
||||||
{
|
|
||||||
qDeleteAll(m_sqliteTables);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SqliteDatabase::open()
|
void SqliteDatabase::open()
|
||||||
{
|
{
|
||||||
m_databaseBackend.open(m_databaseFilePath);
|
m_databaseBackend.open(m_databaseFilePath);
|
||||||
@@ -65,13 +60,14 @@ bool SqliteDatabase::isOpen() const
|
|||||||
return m_isOpen;
|
return m_isOpen;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SqliteDatabase::addTable(SqliteTable *newSqliteTable)
|
SqliteTable &SqliteDatabase::addTable()
|
||||||
{
|
{
|
||||||
newSqliteTable->setSqliteDatabase(this);
|
m_sqliteTables.emplace_back(*this);
|
||||||
m_sqliteTables.push_back(newSqliteTable);
|
|
||||||
|
return m_sqliteTables.back();
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::vector<SqliteTable *> &SqliteDatabase::tables() const
|
const std::vector<SqliteTable> &SqliteDatabase::tables() const
|
||||||
{
|
{
|
||||||
return m_sqliteTables;
|
return m_sqliteTables;
|
||||||
}
|
}
|
||||||
@@ -113,8 +109,8 @@ void SqliteDatabase::execute(Utils::SmallStringView sqlStatement)
|
|||||||
|
|
||||||
void SqliteDatabase::initializeTables()
|
void SqliteDatabase::initializeTables()
|
||||||
{
|
{
|
||||||
for (SqliteTable *table: tables())
|
for (SqliteTable &table : m_sqliteTables)
|
||||||
table->initialize();
|
table.initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
SqliteDatabaseBackend &SqliteDatabase::backend()
|
SqliteDatabaseBackend &SqliteDatabase::backend()
|
||||||
|
@@ -27,6 +27,7 @@
|
|||||||
|
|
||||||
#include "sqlitedatabasebackend.h"
|
#include "sqlitedatabasebackend.h"
|
||||||
#include "sqliteglobal.h"
|
#include "sqliteglobal.h"
|
||||||
|
#include "sqlitetable.h"
|
||||||
|
|
||||||
#include <utils/smallstring.h>
|
#include <utils/smallstring.h>
|
||||||
|
|
||||||
@@ -34,9 +35,6 @@
|
|||||||
|
|
||||||
namespace Sqlite {
|
namespace Sqlite {
|
||||||
|
|
||||||
class SqliteTable;
|
|
||||||
class SqliteDatabaseBackend;
|
|
||||||
|
|
||||||
class SQLITE_EXPORT SqliteDatabase
|
class SQLITE_EXPORT SqliteDatabase
|
||||||
{
|
{
|
||||||
friend class SqliteAbstractTransaction;
|
friend class SqliteAbstractTransaction;
|
||||||
@@ -45,7 +43,12 @@ class SQLITE_EXPORT SqliteDatabase
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
SqliteDatabase();
|
SqliteDatabase();
|
||||||
~SqliteDatabase();
|
|
||||||
|
SqliteDatabase(const SqliteDatabase &) = delete;
|
||||||
|
bool operator=(const SqliteDatabase &) = delete;
|
||||||
|
|
||||||
|
SqliteDatabase(SqliteDatabase &&) = delete;
|
||||||
|
bool operator=(SqliteDatabase &&) = delete;
|
||||||
|
|
||||||
void open();
|
void open();
|
||||||
void open(Utils::PathString &&databaseFilePath);
|
void open(Utils::PathString &&databaseFilePath);
|
||||||
@@ -53,8 +56,8 @@ public:
|
|||||||
|
|
||||||
bool isOpen() const;
|
bool isOpen() const;
|
||||||
|
|
||||||
void addTable(SqliteTable *newSqliteTable);
|
SqliteTable &addTable();
|
||||||
const std::vector<SqliteTable *> &tables() const;
|
const std::vector<SqliteTable> &tables() const;
|
||||||
|
|
||||||
void setDatabaseFilePath(Utils::PathString &&databaseFilePath);
|
void setDatabaseFilePath(Utils::PathString &&databaseFilePath);
|
||||||
const Utils::PathString &databaseFilePath() const;
|
const Utils::PathString &databaseFilePath() const;
|
||||||
@@ -75,7 +78,7 @@ private:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
SqliteDatabaseBackend m_databaseBackend;
|
SqliteDatabaseBackend m_databaseBackend;
|
||||||
std::vector<SqliteTable*> m_sqliteTables;
|
std::vector<SqliteTable> m_sqliteTables;
|
||||||
Utils::PathString m_databaseFilePath;
|
Utils::PathString m_databaseFilePath;
|
||||||
JournalMode m_journalMode;
|
JournalMode m_journalMode;
|
||||||
bool m_isOpen = false;
|
bool m_isOpen = false;
|
||||||
|
@@ -41,8 +41,11 @@ public:
|
|||||||
SqliteDatabaseBackend(SqliteDatabase &database);
|
SqliteDatabaseBackend(SqliteDatabase &database);
|
||||||
~SqliteDatabaseBackend();
|
~SqliteDatabaseBackend();
|
||||||
|
|
||||||
SqliteDatabaseBackend(const SqliteDatabase &database) = delete;
|
SqliteDatabaseBackend(const SqliteDatabase &) = delete;
|
||||||
SqliteDatabase &operator=(const SqliteDatabase &database) = delete;
|
SqliteDatabase &operator=(const SqliteDatabase &) = delete;
|
||||||
|
|
||||||
|
SqliteDatabaseBackend(SqliteDatabase &&) = delete;
|
||||||
|
SqliteDatabase &operator=(SqliteDatabase &&) = delete;
|
||||||
|
|
||||||
void setMmapSize(qint64 defaultSize, qint64 maximumSize);
|
void setMmapSize(qint64 defaultSize, qint64 maximumSize);
|
||||||
void activateMultiThreading();
|
void activateMultiThreading();
|
||||||
|
@@ -37,7 +37,8 @@
|
|||||||
# define SQLITE_EXPORT Q_DECL_IMPORT
|
# define SQLITE_EXPORT Q_DECL_IMPORT
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
enum class ColumnType {
|
enum class ColumnType : char
|
||||||
|
{
|
||||||
Numeric,
|
Numeric,
|
||||||
Integer,
|
Integer,
|
||||||
Real,
|
Real,
|
||||||
@@ -45,11 +46,19 @@ enum class ColumnType {
|
|||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class ColumnConstraint {
|
enum class IsPrimaryKey : char
|
||||||
|
{
|
||||||
|
No,
|
||||||
|
Yes
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class ColumnConstraint : char
|
||||||
|
{
|
||||||
PrimaryKey
|
PrimaryKey
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class JournalMode {
|
enum class JournalMode : char
|
||||||
|
{
|
||||||
Delete,
|
Delete,
|
||||||
Truncate,
|
Truncate,
|
||||||
Persist,
|
Persist,
|
||||||
@@ -57,7 +66,8 @@ enum class JournalMode {
|
|||||||
Wal
|
Wal
|
||||||
};
|
};
|
||||||
|
|
||||||
enum TextEncoding {
|
enum TextEncoding : char
|
||||||
|
{
|
||||||
Utf8,
|
Utf8,
|
||||||
Utf16le,
|
Utf16le,
|
||||||
Utf16be,
|
Utf16be,
|
||||||
|
@@ -33,52 +33,6 @@
|
|||||||
|
|
||||||
namespace Sqlite {
|
namespace Sqlite {
|
||||||
|
|
||||||
SqliteTable::SqliteTable()
|
|
||||||
: m_withoutRowId(false)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
SqliteTable::~SqliteTable()
|
|
||||||
{
|
|
||||||
qDeleteAll(m_sqliteColumns);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SqliteTable::setName(Utils::SmallString &&name)
|
|
||||||
{
|
|
||||||
m_tableName = std::move(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
Utils::SmallStringView SqliteTable::name() const
|
|
||||||
{
|
|
||||||
return m_tableName;
|
|
||||||
}
|
|
||||||
|
|
||||||
void SqliteTable::setUseWithoutRowId(bool useWithoutWorId)
|
|
||||||
{
|
|
||||||
m_withoutRowId = useWithoutWorId;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool SqliteTable::useWithoutRowId() const
|
|
||||||
{
|
|
||||||
return m_withoutRowId;
|
|
||||||
}
|
|
||||||
|
|
||||||
void SqliteTable::addColumn(SqliteColumn *newColumn)
|
|
||||||
{
|
|
||||||
m_sqliteColumns.push_back(newColumn);
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::vector<SqliteColumn *> &SqliteTable::columns() const
|
|
||||||
{
|
|
||||||
return m_sqliteColumns;
|
|
||||||
}
|
|
||||||
|
|
||||||
void SqliteTable::setSqliteDatabase(SqliteDatabase *database)
|
|
||||||
{
|
|
||||||
m_sqliteDatabase = database;
|
|
||||||
}
|
|
||||||
|
|
||||||
void SqliteTable::initialize()
|
void SqliteTable::initialize()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
@@ -86,10 +40,10 @@ void SqliteTable::initialize()
|
|||||||
|
|
||||||
createTableSqlStatementBuilder.setTable(m_tableName.clone());
|
createTableSqlStatementBuilder.setTable(m_tableName.clone());
|
||||||
createTableSqlStatementBuilder.setUseWithoutRowId(m_withoutRowId);
|
createTableSqlStatementBuilder.setUseWithoutRowId(m_withoutRowId);
|
||||||
createTableSqlStatementBuilder.setColumnDefinitions(createColumnDefintions());
|
createTableSqlStatementBuilder.setColumns(m_sqliteColumns);
|
||||||
|
|
||||||
SqliteImmediateTransaction transaction(*m_sqliteDatabase);
|
SqliteImmediateTransaction transaction(m_sqliteDatabase);
|
||||||
m_sqliteDatabase->execute(createTableSqlStatementBuilder.sqlStatement());
|
m_sqliteDatabase.execute(createTableSqlStatementBuilder.sqlStatement());
|
||||||
transaction.commit();
|
transaction.commit();
|
||||||
|
|
||||||
m_isReady = true;
|
m_isReady = true;
|
||||||
@@ -99,19 +53,4 @@ void SqliteTable::initialize()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SqliteTable::isReady() const
|
|
||||||
{
|
|
||||||
return m_isReady;
|
|
||||||
}
|
|
||||||
|
|
||||||
ColumnDefinitions SqliteTable::createColumnDefintions() const
|
|
||||||
{
|
|
||||||
ColumnDefinitions columnDefintions;
|
|
||||||
|
|
||||||
for (SqliteColumn *sqliteColumn : m_sqliteColumns)
|
|
||||||
columnDefintions.push_back(sqliteColumn->columnDefintion());
|
|
||||||
|
|
||||||
return columnDefintions;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace Sqlite
|
} // namespace Sqlite
|
||||||
|
@@ -26,47 +26,75 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "sqliteglobal.h"
|
#include "sqliteglobal.h"
|
||||||
#include "columndefinition.h"
|
#include "sqlitecolumn.h"
|
||||||
#include "utf8string.h"
|
|
||||||
|
|
||||||
#include <QObject>
|
|
||||||
#include <QVector>
|
|
||||||
|
|
||||||
namespace Sqlite {
|
namespace Sqlite {
|
||||||
|
|
||||||
class SqliteColumn;
|
|
||||||
class SqliteDatabase;
|
class SqliteDatabase;
|
||||||
|
|
||||||
class SQLITE_EXPORT SqliteTable
|
class SqliteTable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
SqliteTable();
|
SqliteTable(SqliteDatabase &m_sqliteDatabase)
|
||||||
~SqliteTable();
|
: m_sqliteDatabase(m_sqliteDatabase)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
void setName(Utils::SmallString &&name);
|
void setName(Utils::SmallString &&name)
|
||||||
Utils::SmallStringView name() const;
|
{
|
||||||
|
m_tableName = std::move(name);
|
||||||
|
}
|
||||||
|
|
||||||
void setUseWithoutRowId(bool useWithoutWorId);
|
Utils::SmallStringView name() const
|
||||||
bool useWithoutRowId() const;
|
{
|
||||||
|
return m_tableName;
|
||||||
|
}
|
||||||
|
|
||||||
void addColumn(SqliteColumn *newColumn);
|
void setUseWithoutRowId(bool useWithoutWorId)
|
||||||
const std::vector<SqliteColumn *> &columns() const;
|
{
|
||||||
|
m_withoutRowId = useWithoutWorId;
|
||||||
|
}
|
||||||
|
|
||||||
void setSqliteDatabase(SqliteDatabase *database);
|
bool useWithoutRowId() const
|
||||||
|
{
|
||||||
|
return m_withoutRowId;
|
||||||
|
}
|
||||||
|
|
||||||
|
SqliteColumn &addColumn(Utils::SmallString &&name,
|
||||||
|
ColumnType type = ColumnType::Numeric,
|
||||||
|
IsPrimaryKey isPrimaryKey = IsPrimaryKey::No)
|
||||||
|
{
|
||||||
|
m_sqliteColumns.emplace_back(std::move(name), type, isPrimaryKey);
|
||||||
|
|
||||||
|
return m_sqliteColumns.back();
|
||||||
|
}
|
||||||
|
|
||||||
|
const SqliteColumns &columns() const
|
||||||
|
{
|
||||||
|
return m_sqliteColumns;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isReady() const
|
||||||
|
{
|
||||||
|
return m_isReady;
|
||||||
|
}
|
||||||
|
|
||||||
void initialize();
|
void initialize();
|
||||||
|
|
||||||
bool isReady() const;
|
friend bool operator==(const SqliteTable &first, const SqliteTable &second)
|
||||||
|
{
|
||||||
|
return first.m_tableName == second.m_tableName
|
||||||
|
&& &first.m_sqliteDatabase == &second.m_sqliteDatabase
|
||||||
|
&& first.m_withoutRowId == second.m_withoutRowId
|
||||||
|
&& first.m_isReady == second.m_isReady
|
||||||
|
&& first.m_sqliteColumns == second.m_sqliteColumns;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ColumnDefinitions createColumnDefintions() const;
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::vector<SqliteColumn*> m_sqliteColumns;
|
|
||||||
Utils::SmallString m_tableName;
|
Utils::SmallString m_tableName;
|
||||||
SqliteDatabase *m_sqliteDatabase;
|
SqliteColumns m_sqliteColumns;
|
||||||
bool m_withoutRowId;
|
SqliteDatabase &m_sqliteDatabase;
|
||||||
|
bool m_withoutRowId = false;
|
||||||
bool m_isReady = false;
|
bool m_isReady = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -30,18 +30,17 @@
|
|||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
using Sqlite::ColumnDefinition;
|
using Sqlite::SqliteColumn;
|
||||||
using Sqlite::ColumnDefinitions;
|
using Sqlite::SqliteColumns;
|
||||||
|
|
||||||
using Sqlite::SqlStatementBuilderException;
|
using Sqlite::SqlStatementBuilderException;
|
||||||
|
|
||||||
class CreateTableSqlStatementBuilder : public ::testing::Test
|
class CreateTableSqlStatementBuilder : public ::testing::Test
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
void bindValues();
|
void bindValues();
|
||||||
static ColumnDefinitions createColumnDefintions();
|
static SqliteColumns createColumns();
|
||||||
static ColumnDefinition createColumnDefintion(Utils::SmallString name,
|
|
||||||
ColumnType type,
|
|
||||||
bool isPrimaryKey = false);
|
|
||||||
protected:
|
protected:
|
||||||
Sqlite::CreateTableSqlStatementBuilder builder;
|
Sqlite::CreateTableSqlStatementBuilder builder;
|
||||||
};
|
};
|
||||||
@@ -88,7 +87,7 @@ TEST_F(CreateTableSqlStatementBuilder, AddColumnToExistingColumns)
|
|||||||
{
|
{
|
||||||
bindValues();
|
bindValues();
|
||||||
|
|
||||||
builder.addColumnDefinition("number2", ColumnType::Real);
|
builder.addColumn("number2", ColumnType::Real);
|
||||||
|
|
||||||
ASSERT_THAT(builder.sqlStatement(),
|
ASSERT_THAT(builder.sqlStatement(),
|
||||||
"CREATE TABLE IF NOT EXISTS test(id INTEGER PRIMARY KEY, name TEXT, number NUMERIC, number2 REAL)");}
|
"CREATE TABLE IF NOT EXISTS test(id INTEGER PRIMARY KEY, name TEXT, number NUMERIC, number2 REAL)");}
|
||||||
@@ -118,8 +117,8 @@ TEST_F(CreateTableSqlStatementBuilder, ClearColumnsAndAddColumnNewColumns)
|
|||||||
bindValues();
|
bindValues();
|
||||||
builder.clearColumns();
|
builder.clearColumns();
|
||||||
|
|
||||||
builder.addColumnDefinition("name3", ColumnType::Text);
|
builder.addColumn("name3", ColumnType::Text);
|
||||||
builder.addColumnDefinition("number3", ColumnType::Real);
|
builder.addColumn("number3", ColumnType::Real);
|
||||||
|
|
||||||
ASSERT_THAT(builder.sqlStatement(),
|
ASSERT_THAT(builder.sqlStatement(),
|
||||||
"CREATE TABLE IF NOT EXISTS test(name3 TEXT, number3 REAL)");
|
"CREATE TABLE IF NOT EXISTS test(name3 TEXT, number3 REAL)");
|
||||||
@@ -140,7 +139,7 @@ TEST_F(CreateTableSqlStatementBuilder, SetColumnDefinitions)
|
|||||||
builder.clear();
|
builder.clear();
|
||||||
builder.setTable("test");
|
builder.setTable("test");
|
||||||
|
|
||||||
builder.setColumnDefinitions(createColumnDefintions());
|
builder.setColumns(createColumns());
|
||||||
|
|
||||||
ASSERT_THAT(builder.sqlStatement(),
|
ASSERT_THAT(builder.sqlStatement(),
|
||||||
"CREATE TABLE IF NOT EXISTS test(id INTEGER PRIMARY KEY, name TEXT, number NUMERIC)");
|
"CREATE TABLE IF NOT EXISTS test(id INTEGER PRIMARY KEY, name TEXT, number NUMERIC)");
|
||||||
@@ -150,30 +149,19 @@ void CreateTableSqlStatementBuilder::bindValues()
|
|||||||
{
|
{
|
||||||
builder.clear();
|
builder.clear();
|
||||||
builder.setTable("test");
|
builder.setTable("test");
|
||||||
builder.addColumnDefinition("id", ColumnType::Integer, true);
|
builder.addColumn("id", ColumnType::Integer, IsPrimaryKey::Yes);
|
||||||
builder.addColumnDefinition("name", ColumnType::Text);
|
builder.addColumn("name", ColumnType::Text);
|
||||||
builder.addColumnDefinition("number",ColumnType:: Numeric);
|
builder.addColumn("number",ColumnType:: Numeric);
|
||||||
}
|
}
|
||||||
|
|
||||||
ColumnDefinitions CreateTableSqlStatementBuilder::createColumnDefintions()
|
SqliteColumns CreateTableSqlStatementBuilder::createColumns()
|
||||||
{
|
{
|
||||||
ColumnDefinitions columnDefinitions;
|
SqliteColumns columns;
|
||||||
columnDefinitions.push_back(createColumnDefintion("id", ColumnType::Integer, true));
|
columns.emplace_back("id", ColumnType::Integer, IsPrimaryKey::Yes);
|
||||||
columnDefinitions.push_back(createColumnDefintion("name", ColumnType::Text));
|
columns.emplace_back("name", ColumnType::Text);
|
||||||
columnDefinitions.push_back(createColumnDefintion("number", ColumnType::Numeric));
|
columns.emplace_back("number", ColumnType::Numeric);
|
||||||
|
|
||||||
return columnDefinitions;
|
return columns;
|
||||||
}
|
|
||||||
|
|
||||||
ColumnDefinition CreateTableSqlStatementBuilder::createColumnDefintion(Utils::SmallString name, ColumnType type, bool isPrimaryKey)
|
|
||||||
{
|
|
||||||
ColumnDefinition columnDefinition;
|
|
||||||
|
|
||||||
columnDefinition.setName(std::move(name));
|
|
||||||
columnDefinition.setType(type);
|
|
||||||
columnDefinition.setIsPrimaryKey(isPrimaryKey);
|
|
||||||
|
|
||||||
return columnDefinition;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -29,7 +29,12 @@
|
|||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
using Sqlite::ColumnDefinition;
|
using testing::AllOf;
|
||||||
|
using testing::Contains;
|
||||||
|
using testing::Property;
|
||||||
|
|
||||||
|
using Column = Sqlite::SqliteColumn;
|
||||||
|
using Sqlite::SqliteColumns;
|
||||||
|
|
||||||
class SqliteColumn : public ::testing::Test
|
class SqliteColumn : public ::testing::Test
|
||||||
{
|
{
|
||||||
@@ -65,7 +70,7 @@ TEST_F(SqliteColumn, DefaultPrimaryKey)
|
|||||||
|
|
||||||
TEST_F(SqliteColumn, SetPrimaryKey)
|
TEST_F(SqliteColumn, SetPrimaryKey)
|
||||||
{
|
{
|
||||||
column.setIsPrimaryKey(true);
|
column.setIsPrimaryKey(IsPrimaryKey::Yes);
|
||||||
|
|
||||||
ASSERT_TRUE(column.isPrimaryKey());
|
ASSERT_TRUE(column.isPrimaryKey());
|
||||||
}
|
}
|
||||||
@@ -74,11 +79,11 @@ TEST_F(SqliteColumn, GetColumnDefinition)
|
|||||||
{
|
{
|
||||||
column.setName("Claudia");
|
column.setName("Claudia");
|
||||||
|
|
||||||
ColumnDefinition columnDefintion = column.columnDefintion();
|
ASSERT_THAT(column,
|
||||||
|
AllOf(
|
||||||
ASSERT_THAT(columnDefintion.name(), "Claudia");
|
Property(&Column::name, "Claudia"),
|
||||||
ASSERT_THAT(columnDefintion.type(), ColumnType::Numeric);
|
Property(&Column::type, ColumnType::Numeric),
|
||||||
ASSERT_FALSE(columnDefintion.isPrimaryKey());
|
Property(&Column::isPrimaryKey, false)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void SqliteColumn::SetUp()
|
void SqliteColumn::SetUp()
|
||||||
|
@@ -81,9 +81,7 @@ TEST_F(SqliteDatabase, CloseDatabase)
|
|||||||
|
|
||||||
TEST_F(SqliteDatabase, AddTable)
|
TEST_F(SqliteDatabase, AddTable)
|
||||||
{
|
{
|
||||||
SqliteTable *sqliteTable = new SqliteTable;
|
auto sqliteTable = database.addTable();
|
||||||
|
|
||||||
database.addTable(sqliteTable);
|
|
||||||
|
|
||||||
ASSERT_THAT(database.tables(), Contains(sqliteTable));
|
ASSERT_THAT(database.tables(), Contains(sqliteTable));
|
||||||
}
|
}
|
||||||
|
@@ -41,68 +41,55 @@ protected:
|
|||||||
void SetUp() override;
|
void SetUp() override;
|
||||||
void TearDown() override;
|
void TearDown() override;
|
||||||
|
|
||||||
SqliteColumn *addColumn(Utils::SmallString columnName);
|
protected:
|
||||||
|
|
||||||
SpyDummy spyDummy;
|
SpyDummy spyDummy;
|
||||||
SqliteDatabase database;
|
SqliteDatabase database;
|
||||||
Sqlite::SqliteTable *table = new Sqlite::SqliteTable;
|
Sqlite::SqliteTable &table = database.addTable();
|
||||||
Utils::SmallString tableName = "testTable";
|
Utils::SmallString tableName = "testTable";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
TEST_F(SqliteTable, ColumnIsAddedToTable)
|
TEST_F(SqliteTable, ColumnIsAddedToTable)
|
||||||
{
|
{
|
||||||
table->setUseWithoutRowId(true);
|
table.setUseWithoutRowId(true);
|
||||||
|
|
||||||
ASSERT_TRUE(table->useWithoutRowId());
|
ASSERT_TRUE(table.useWithoutRowId());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(SqliteTable, SetTableName)
|
TEST_F(SqliteTable, SetTableName)
|
||||||
{
|
{
|
||||||
table->setName(tableName.clone());
|
table.setName(tableName.clone());
|
||||||
|
|
||||||
ASSERT_THAT(table->name(), tableName);
|
ASSERT_THAT(table.name(), tableName);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(SqliteTable, SetUseWithoutRowid)
|
TEST_F(SqliteTable, SetUseWithoutRowid)
|
||||||
{
|
{
|
||||||
table->setUseWithoutRowId(true);
|
table.setUseWithoutRowId(true);
|
||||||
|
|
||||||
ASSERT_TRUE(table->useWithoutRowId());
|
ASSERT_TRUE(table.useWithoutRowId());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(SqliteTable, TableIsReadyAfterOpenDatabase)
|
TEST_F(SqliteTable, TableIsReadyAfterOpenDatabase)
|
||||||
{
|
{
|
||||||
table->setName(tableName.clone());
|
table.setName(tableName.clone());
|
||||||
addColumn("name");
|
table.addColumn("name");
|
||||||
|
|
||||||
database.open();
|
database.open();
|
||||||
|
|
||||||
ASSERT_TRUE(table->isReady());
|
ASSERT_TRUE(table.isReady());
|
||||||
}
|
}
|
||||||
|
|
||||||
void SqliteTable::SetUp()
|
void SqliteTable::SetUp()
|
||||||
{
|
{
|
||||||
database.setJournalMode(JournalMode::Memory);
|
database.setJournalMode(JournalMode::Memory);
|
||||||
database.setDatabaseFilePath( QStringLiteral(":memory:"));
|
database.setDatabaseFilePath( QStringLiteral(":memory:"));
|
||||||
database.addTable(table);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SqliteTable::TearDown()
|
void SqliteTable::TearDown()
|
||||||
{
|
{
|
||||||
if (database.isOpen())
|
if (database.isOpen())
|
||||||
database.close();
|
database.close();
|
||||||
table = nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SqliteColumn *SqliteTable::addColumn(Utils::SmallString columnName)
|
|
||||||
{
|
|
||||||
SqliteColumn *newSqliteColum = new SqliteColumn;
|
|
||||||
|
|
||||||
newSqliteColum->setName(std::move(columnName));
|
|
||||||
|
|
||||||
table->addColumn(newSqliteColum);
|
|
||||||
|
|
||||||
return newSqliteColum;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user