Commit 2fed425c authored by Marco Bubke's avatar Marco Bubke

Clang: Cleanup string interface of sqlite

We only will need the utf8 optimized SmallString for the indexer database.
So we remove all other string bindings, QByteArray and QVariant.

Change-Id: I4a77901a80b26bf292fc85df535c2ccf3a8ab52f
Reviewed-by: Tim Jenssen's avatarTim Jenssen <tim.jenssen@qt.io>
parent 1727e643
...@@ -26,19 +26,22 @@ ...@@ -26,19 +26,22 @@
#pragma once #pragma once
#include "sqliteglobal.h" #include "sqliteglobal.h"
#include "utf8string.h"
#include <utils/smallstring.h>
#include <vector>
namespace Sqlite { namespace Sqlite {
class ColumnDefinition class ColumnDefinition
{ {
public: public:
void setName(const Utf8String &name) void setName(Utils::SmallString &&name)
{ {
m_name = name; m_name = std::move(name);
} }
const Utf8String &name() const const Utils::SmallString &name() const
{ {
return m_name; return m_name;
} }
...@@ -53,14 +56,14 @@ public: ...@@ -53,14 +56,14 @@ public:
return m_type; return m_type;
} }
Utf8String typeString() const Utils::SmallString typeString() const
{ {
switch (m_type) { switch (m_type) {
case ColumnType::None: return Utf8String(); case ColumnType::None: return {};
case ColumnType::Numeric: return Utf8StringLiteral("NUMERIC"); case ColumnType::Numeric: return "NUMERIC";
case ColumnType::Integer: return Utf8StringLiteral("INTEGER"); case ColumnType::Integer: return "INTEGER";
case ColumnType::Real: return Utf8StringLiteral("REAL"); case ColumnType::Real: return "REAL";
case ColumnType::Text: return Utf8StringLiteral("TEXT"); case ColumnType::Text: return "TEXT";
} }
Q_UNREACHABLE(); Q_UNREACHABLE();
...@@ -77,9 +80,11 @@ public: ...@@ -77,9 +80,11 @@ public:
} }
private: private:
Utf8String m_name; Utils::SmallString m_name;
ColumnType m_type; ColumnType m_type;
bool m_isPrimaryKey = false; bool m_isPrimaryKey = false;
}; };
using ColumnDefinitions = std::vector<ColumnDefinition>;
} // namespace Sqlite } // namespace Sqlite
...@@ -25,47 +25,45 @@ ...@@ -25,47 +25,45 @@
#include "createtablesqlstatementbuilder.h" #include "createtablesqlstatementbuilder.h"
#include "utf8stringvector.h"
namespace Sqlite { namespace Sqlite {
CreateTableSqlStatementBuilder::CreateTableSqlStatementBuilder() CreateTableSqlStatementBuilder::CreateTableSqlStatementBuilder()
: m_sqlStatementBuilder(Utf8StringLiteral("CREATE TABLE IF NOT EXISTS $table($columnDefinitions)$withoutRowId")), : m_sqlStatementBuilder("CREATE TABLE IF NOT EXISTS $table($columnDefinitions)$withoutRowId"),
m_useWithoutRowId(false) m_useWithoutRowId(false)
{ {
} }
void CreateTableSqlStatementBuilder::setTable(const Utf8String &tableName) void CreateTableSqlStatementBuilder::setTable(Utils::SmallString &&tableName)
{ {
m_sqlStatementBuilder.clear(); m_sqlStatementBuilder.clear();
this->m_tableName = tableName; this->m_tableName = std::move(tableName);
} }
void CreateTableSqlStatementBuilder::addColumnDefinition(const Utf8String &columnName, void CreateTableSqlStatementBuilder::addColumnDefinition(Utils::SmallString &&columnName,
ColumnType columnType, ColumnType columnType,
bool isPrimaryKey) bool isPrimaryKey)
{ {
m_sqlStatementBuilder.clear(); m_sqlStatementBuilder.clear();
ColumnDefinition columnDefinition; ColumnDefinition columnDefinition;
columnDefinition.setName(columnName); columnDefinition.setName(std::move(columnName));
columnDefinition.setType(columnType); columnDefinition.setType(columnType);
columnDefinition.setIsPrimaryKey(isPrimaryKey); columnDefinition.setIsPrimaryKey(isPrimaryKey);
m_columnDefinitions.append(columnDefinition); m_columnDefinitions.push_back(columnDefinition);
} }
void CreateTableSqlStatementBuilder::setColumnDefinitions(const QVector<ColumnDefinition> &columnDefinitions) void CreateTableSqlStatementBuilder::setColumnDefinitions(ColumnDefinitions &&columnDefinitions)
{ {
m_sqlStatementBuilder.clear(); m_sqlStatementBuilder.clear();
this->m_columnDefinitions = columnDefinitions; m_columnDefinitions = std::move(columnDefinitions);
} }
void CreateTableSqlStatementBuilder::setUseWithoutRowId(bool useWithoutRowId) void CreateTableSqlStatementBuilder::setUseWithoutRowId(bool useWithoutRowId)
{ {
this->m_useWithoutRowId = useWithoutRowId; m_useWithoutRowId = useWithoutRowId;
} }
void CreateTableSqlStatementBuilder::clear() void CreateTableSqlStatementBuilder::clear()
...@@ -82,7 +80,7 @@ void CreateTableSqlStatementBuilder::clearColumns() ...@@ -82,7 +80,7 @@ void CreateTableSqlStatementBuilder::clearColumns()
m_columnDefinitions.clear(); m_columnDefinitions.clear();
} }
Utf8String CreateTableSqlStatementBuilder::sqlStatement() const Utils::SmallStringView CreateTableSqlStatementBuilder::sqlStatement() const
{ {
if (!m_sqlStatementBuilder.isBuild()) if (!m_sqlStatementBuilder.isBuild())
bindAll(); bindAll();
...@@ -92,35 +90,35 @@ Utf8String CreateTableSqlStatementBuilder::sqlStatement() const ...@@ -92,35 +90,35 @@ Utf8String CreateTableSqlStatementBuilder::sqlStatement() const
bool CreateTableSqlStatementBuilder::isValid() const bool CreateTableSqlStatementBuilder::isValid() const
{ {
return m_tableName.hasContent() && !m_columnDefinitions.isEmpty(); return m_tableName.hasContent() && !m_columnDefinitions.empty();
} }
void CreateTableSqlStatementBuilder::bindColumnDefinitions() const void CreateTableSqlStatementBuilder::bindColumnDefinitions() const
{ {
Utf8StringVector columnDefinitionStrings; Utils::SmallStringVector columnDefinitionStrings;
foreach (const ColumnDefinition &columnDefinition, m_columnDefinitions) { for (const ColumnDefinition &columnDefinition : m_columnDefinitions) {
Utf8String columnDefinitionString = columnDefinition.name() + Utf8StringLiteral(" ") + columnDefinition.typeString(); Utils::SmallString columnDefinitionString = {columnDefinition.name(), " ", columnDefinition.typeString()};
if (columnDefinition.isPrimaryKey()) if (columnDefinition.isPrimaryKey())
columnDefinitionString.append(Utf8StringLiteral(" PRIMARY KEY")); columnDefinitionString.append(" PRIMARY KEY");
columnDefinitionStrings.append(columnDefinitionString); columnDefinitionStrings.push_back(columnDefinitionString);
} }
m_sqlStatementBuilder.bind(Utf8StringLiteral("$columnDefinitions"), columnDefinitionStrings); m_sqlStatementBuilder.bind("$columnDefinitions", columnDefinitionStrings);
} }
void CreateTableSqlStatementBuilder::bindAll() const void CreateTableSqlStatementBuilder::bindAll() const
{ {
m_sqlStatementBuilder.bind(Utf8StringLiteral("$table"), m_tableName); m_sqlStatementBuilder.bind("$table", m_tableName.clone());
bindColumnDefinitions(); bindColumnDefinitions();
if (m_useWithoutRowId) if (m_useWithoutRowId)
m_sqlStatementBuilder.bind(Utf8StringLiteral("$withoutRowId"), Utf8StringLiteral(" WITHOUT ROWID")); m_sqlStatementBuilder.bind("$withoutRowId", " WITHOUT ROWID");
else else
m_sqlStatementBuilder.bindEmptyText(Utf8StringLiteral("$withoutRowId")); m_sqlStatementBuilder.bindEmptyText("$withoutRowId");
} }
} // namespace Sqlite } // namespace Sqlite
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
#include "columndefinition.h" #include "columndefinition.h"
#include "sqlstatementbuilder.h" #include "sqlstatementbuilder.h"
#include <QVector> #include <vector>
namespace Sqlite { namespace Sqlite {
...@@ -37,15 +37,15 @@ class SQLITE_EXPORT CreateTableSqlStatementBuilder ...@@ -37,15 +37,15 @@ class SQLITE_EXPORT CreateTableSqlStatementBuilder
public: public:
CreateTableSqlStatementBuilder(); CreateTableSqlStatementBuilder();
void setTable(const Utf8String &m_tableName); void setTable(Utils::SmallString &&tableName);
void addColumnDefinition(const Utf8String &columnName, ColumnType columnType, bool isPrimaryKey = false); void addColumnDefinition(Utils::SmallString &&columnName, ColumnType columnType, bool isPrimaryKey = false);
void setColumnDefinitions(const QVector<ColumnDefinition> & m_columnDefinitions); void setColumnDefinitions(ColumnDefinitions &&columnDefinitions);
void setUseWithoutRowId(bool m_useWithoutRowId); void setUseWithoutRowId(bool useWithoutRowId);
void clear(); void clear();
void clearColumns(); void clearColumns();
Utf8String sqlStatement() const; Utils::SmallStringView sqlStatement() const;
bool isValid() const; bool isValid() const;
...@@ -55,8 +55,8 @@ protected: ...@@ -55,8 +55,8 @@ protected:
private: private:
mutable SqlStatementBuilder m_sqlStatementBuilder; mutable SqlStatementBuilder m_sqlStatementBuilder;
Utf8String m_tableName; Utils::SmallString m_tableName;
QVector<ColumnDefinition> m_columnDefinitions; ColumnDefinitions m_columnDefinitions;
bool m_useWithoutRowId; bool m_useWithoutRowId;
}; };
......
...@@ -49,12 +49,12 @@ public: ...@@ -49,12 +49,12 @@ public:
m_isPrimaryKey = false; m_isPrimaryKey = false;
} }
void setName(const Utf8String &newName) void setName(Utils::SmallString &&newName)
{ {
m_name = newName; m_name = newName;
} }
const Utf8String &name() const const Utils::SmallString &name() const
{ {
return m_name; return m_name;
} }
...@@ -83,7 +83,7 @@ public: ...@@ -83,7 +83,7 @@ public:
{ {
ColumnDefinition columnDefinition; ColumnDefinition columnDefinition;
columnDefinition.setName(m_name); columnDefinition.setName(m_name.clone());
columnDefinition.setType(m_type); columnDefinition.setType(m_type);
columnDefinition.setIsPrimaryKey(m_isPrimaryKey); columnDefinition.setIsPrimaryKey(m_isPrimaryKey);
...@@ -91,7 +91,7 @@ public: ...@@ -91,7 +91,7 @@ public:
} }
private: private:
Utf8String m_name; Utils::SmallString m_name;
ColumnType m_type; ColumnType m_type;
bool m_isPrimaryKey; bool m_isPrimaryKey;
}; };
......
...@@ -47,9 +47,9 @@ void SqliteDatabase::open() ...@@ -47,9 +47,9 @@ void SqliteDatabase::open()
m_isOpen = true; m_isOpen = true;
} }
void SqliteDatabase::open(const QString &databaseFilePath) void SqliteDatabase::open(Utils::PathString &&databaseFilePath)
{ {
setDatabaseFilePath(databaseFilePath); setDatabaseFilePath(std::move(databaseFilePath));
open(); open();
} }
...@@ -67,20 +67,20 @@ bool SqliteDatabase::isOpen() const ...@@ -67,20 +67,20 @@ bool SqliteDatabase::isOpen() const
void SqliteDatabase::addTable(SqliteTable *newSqliteTable) void SqliteDatabase::addTable(SqliteTable *newSqliteTable)
{ {
newSqliteTable->setSqliteDatabase(this); newSqliteTable->setSqliteDatabase(this);
m_sqliteTables.append(newSqliteTable); m_sqliteTables.push_back(newSqliteTable);
} }
const QVector<SqliteTable *> &SqliteDatabase::tables() const const std::vector<SqliteTable *> &SqliteDatabase::tables() const
{ {
return m_sqliteTables; return m_sqliteTables;
} }
void SqliteDatabase::setDatabaseFilePath(const QString &databaseFilePath) void SqliteDatabase::setDatabaseFilePath(Utils::PathString &&databaseFilePath)
{ {
m_databaseFilePath = databaseFilePath; m_databaseFilePath = std::move(databaseFilePath);
} }
const QString &SqliteDatabase::databaseFilePath() const const Utils::PathString &SqliteDatabase::databaseFilePath() const
{ {
return m_databaseFilePath; return m_databaseFilePath;
} }
......
...@@ -28,8 +28,9 @@ ...@@ -28,8 +28,9 @@
#include "sqlitedatabasebackend.h" #include "sqlitedatabasebackend.h"
#include "sqliteglobal.h" #include "sqliteglobal.h"
#include <QString> #include <utils/smallstring.h>
#include <QVector>
#include <vector>
namespace Sqlite { namespace Sqlite {
...@@ -46,16 +47,16 @@ public: ...@@ -46,16 +47,16 @@ public:
~SqliteDatabase(); ~SqliteDatabase();
void open(); void open();
void open(const QString &databaseFilePath); void open(Utils::PathString &&databaseFilePath);
void close(); void close();
bool isOpen() const; bool isOpen() const;
void addTable(SqliteTable *newSqliteTable); void addTable(SqliteTable *newSqliteTable);
const QVector<SqliteTable *> &tables() const; const std::vector<SqliteTable *> &tables() const;
void setDatabaseFilePath(const QString &databaseFilePath); void setDatabaseFilePath(Utils::PathString &&databaseFilePath);
const QString &databaseFilePath() const; const Utils::PathString &databaseFilePath() const;
void setJournalMode(JournalMode journalMode); void setJournalMode(JournalMode journalMode);
JournalMode journalMode() const; JournalMode journalMode() const;
...@@ -70,8 +71,8 @@ private: ...@@ -70,8 +71,8 @@ private:
private: private:
SqliteDatabaseBackend m_databaseBackend; SqliteDatabaseBackend m_databaseBackend;
QVector<SqliteTable*> m_sqliteTables; std::vector<SqliteTable*> m_sqliteTables;
QString m_databaseFilePath; Utils::PathString m_databaseFilePath;
JournalMode m_journalMode; JournalMode m_journalMode;
bool m_isOpen = false; bool m_isOpen = false;
}; };
......
...@@ -100,15 +100,14 @@ void SqliteDatabaseBackend::checkpointFullWalLog() ...@@ -100,15 +100,14 @@ void SqliteDatabaseBackend::checkpointFullWalLog()
checkIfLogCouldBeCheckpointed(resultCode); checkIfLogCouldBeCheckpointed(resultCode);
} }
void SqliteDatabaseBackend::open(const QString &databaseFilePath) void SqliteDatabaseBackend::open(Utils::SmallStringView databaseFilePath)
{ {
checkCanOpenDatabase(databaseFilePath); checkCanOpenDatabase(databaseFilePath);
QByteArray databaseUtf8Path = databaseFilePath.toUtf8(); int resultCode = sqlite3_open_v2(databaseFilePath.data(),
int resultCode = sqlite3_open_v2(databaseUtf8Path.data(), &m_databaseHandle,
&m_databaseHandle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
NULL);
checkDatabaseCouldBeOpened(resultCode); checkDatabaseCouldBeOpened(resultCode);
...@@ -123,33 +122,33 @@ sqlite3 *SqliteDatabaseBackend::sqliteDatabaseHandle() ...@@ -123,33 +122,33 @@ sqlite3 *SqliteDatabaseBackend::sqliteDatabaseHandle()
return m_databaseHandle; return m_databaseHandle;
} }
void SqliteDatabaseBackend::setPragmaValue(const Utf8String &pragmaKey, const Utf8String &newPragmaValue) void SqliteDatabaseBackend::setPragmaValue(Utils::SmallStringView pragmaKey, Utils::SmallStringView newPragmaValue)
{ {
SqliteReadWriteStatement statement(Utf8StringLiteral("PRAGMA ") + pragmaKey + Utf8StringLiteral("='") + newPragmaValue + Utf8StringLiteral("'"), *this); SqliteReadWriteStatement statement(Utils::SmallString{"PRAGMA ", pragmaKey, "='", newPragmaValue, "'"}, *this);
statement.step(); statement.step();
Utf8String pragmeValueInDatabase = toValue<Utf8String>(Utf8StringLiteral("PRAGMA ") + pragmaKey); Utils::SmallString pragmeValueInDatabase = toValue<Utils::SmallString>("PRAGMA " + pragmaKey);
checkPragmaValue(pragmeValueInDatabase, newPragmaValue); checkPragmaValue(pragmeValueInDatabase, newPragmaValue);
} }
Utf8String SqliteDatabaseBackend::pragmaValue(const Utf8String &pragma) Utils::SmallString SqliteDatabaseBackend::pragmaValue(Utils::SmallStringView pragma)
{ {
return toValue<Utf8String>(Utf8StringLiteral("PRAGMA ") + pragma); return toValue<Utils::SmallString>("PRAGMA " + pragma);
} }
void SqliteDatabaseBackend::setJournalMode(JournalMode journalMode) void SqliteDatabaseBackend::setJournalMode(JournalMode journalMode)
{ {
setPragmaValue(Utf8StringLiteral("journal_mode"), journalModeToPragma(journalMode)); setPragmaValue("journal_mode", journalModeToPragma(journalMode));
} }
JournalMode SqliteDatabaseBackend::journalMode() JournalMode SqliteDatabaseBackend::journalMode()
{ {
return pragmaToJournalMode(pragmaValue(Utf8StringLiteral("journal_mode"))); return pragmaToJournalMode(pragmaValue("journal_mode"));
} }
void SqliteDatabaseBackend::setTextEncoding(TextEncoding textEncoding) void SqliteDatabaseBackend::setTextEncoding(TextEncoding textEncoding)
{ {
setPragmaValue(Utf8StringLiteral("encoding"), textEncodingToPragma(textEncoding)); setPragmaValue("encoding", textEncodingToPragma(textEncoding));
cacheTextEncoding(); cacheTextEncoding();
} }
...@@ -159,9 +158,9 @@ TextEncoding SqliteDatabaseBackend::textEncoding() ...@@ -159,9 +158,9 @@ TextEncoding SqliteDatabaseBackend::textEncoding()
} }
Utf8StringVector SqliteDatabaseBackend::columnNames(const Utf8String &tableName) Utils::SmallStringVector SqliteDatabaseBackend::columnNames(Utils::SmallStringView tableName)
{ {
SqliteReadWriteStatement statement(Utf8StringLiteral("SELECT * FROM ") + tableName, *this); SqliteReadWriteStatement statement("SELECT * FROM " + tableName, *this);
return statement.columnNames(); return statement.columnNames();
} }
...@@ -175,9 +174,9 @@ int SqliteDatabaseBackend::totalChangesCount() ...@@ -175,9 +174,9 @@ int SqliteDatabaseBackend::totalChangesCount()
return sqlite3_total_changes(sqliteDatabaseHandle()); return sqlite3_total_changes(sqliteDatabaseHandle());
} }
void SqliteDatabaseBackend::execute(const Utf8String &sqlStatementUtf8) void SqliteDatabaseBackend::execute(Utils::SmallStringView sqlStatement)
{ {
SqliteReadWriteStatement statement(sqlStatementUtf8, *this); SqliteReadWriteStatement statement(sqlStatement, *this);
statement.step(); statement.step();
} }
...@@ -233,7 +232,7 @@ int SqliteDatabaseBackend::busyHandlerCallback(void *, int counter) ...@@ -233,7 +232,7 @@ int SqliteDatabaseBackend::busyHandlerCallback(void *, int counter)
void SqliteDatabaseBackend::cacheTextEncoding() void SqliteDatabaseBackend::cacheTextEncoding()
{ {
m_cachedTextEncoding = pragmaToTextEncoding(pragmaValue(Utf8StringLiteral("encoding"))); m_cachedTextEncoding = pragmaToTextEncoding(pragmaValue("encoding"));
} }