Akonadi

dbinitializer.h
1/***************************************************************************
2 * SPDX-FileCopyrightText: 2006 Tobias Koenig <tokoe@kde.org> *
3 * *
4 * SPDX-License-Identifier: LGPL-2.0-or-later *
5 ***************************************************************************/
6
7#pragma once
8
9#include "dbintrospector.h"
10#include "schematypes.h"
11
12#include <QHash>
13#include <QSharedPointer>
14#include <QSqlDatabase>
15#include <QStringList>
16
17class DbInitializerTest;
18
19namespace Akonadi
20{
21namespace Server
22{
23class Schema;
24class DbUpdater;
25
26class TestInterface
27{
28public:
29 virtual ~TestInterface() = default;
30
31 virtual void execStatement(const QString &statement) = 0;
32
33protected:
34 explicit TestInterface() = default;
35
36private:
37 Q_DISABLE_COPY_MOVE(TestInterface)
38};
39
40/**
41 * A helper class which takes a reference to a database object and
42 * the file name of a template file and initializes the database
43 * according to the rules in the template file.
44 *
45 * TODO: Refactor this to be easily reusable for updater too
46 */
48{
49 friend class DbUpdater;
50
51public:
53
54 /**
55 Returns an initializer instance for a given backend.
56 */
57 static DbInitializer::Ptr createInstance(const QSqlDatabase &database, Schema *schema = nullptr);
58
59 /**
60 * Destroys the database initializer.
61 */
62 virtual ~DbInitializer();
63
64 /**
65 * Starts the initialization process.
66 * On success true is returned, false otherwise.
67 *
68 * If something went wrong @see errorMsg() can be used to retrieve more
69 * information.
70 */
71 bool run();
72
73 /**
74 * Returns the textual description of an occurred error.
75 */
76 QString errorMsg() const;
77
78 /**
79 * Checks and creates missing indexes.
80 *
81 * This method is run after DbUpdater to ensure that data in new columns
82 * are populated and creation of indexes and foreign keys does not fail.
83 */
85
86 /**
87 * Returns a backend-specific CREATE TABLE SQL query describing given table
88 */
89 virtual QString buildCreateTableStatement(const TableDescription &tableDescription) const = 0;
90
91protected:
92 /**
93 * Creates a new database initializer.
94 *
95 * @param database The reference to the database.
96 */
97 DbInitializer(const QSqlDatabase &database);
98
99 /**
100 * Overwrite in backend-specific sub-classes to return the SQL type for a given C++ type.
101 * @param type Name of the C++ type.
102 * @param size Optional size hint for the column, if -1 use the default SQL type for @p type.
103 */
104 virtual QString sqlType(const ColumnDescription &col, int size) const;
105 /** Overwrite in backend-specific sub-classes to return the SQL value for a given C++ value. */
106 virtual QString sqlValue(const ColumnDescription &col, const QString &value) const;
107
108 virtual QString buildColumnStatement(const ColumnDescription &columnDescription, const TableDescription &tableDescription) const = 0;
109 virtual QString buildAddColumnStatement(const TableDescription &tableDescription, const ColumnDescription &columnDescription) const;
110 virtual QString buildCreateIndexStatement(const TableDescription &tableDescription, const IndexDescription &indexDescription) const;
111 virtual QString buildInsertValuesStatement(const TableDescription &tableDescription, const DataDescription &dataDescription) const = 0;
112
113 /**
114 * Returns an SQL statements to add a foreign key constraint to an existing column @p column.
115 * The default implementation returns an empty string, so any backend supporting foreign key constraints
116 * must reimplement this.
117 */
119
120 /**
121 * Returns an SQL statements to remove the foreign key constraint @p fk from table @p table.
122 * The default implementation returns an empty string, so any backend supporting foreign key constraints
123 * must reimplement this.
124 */
126
127 static QString buildReferentialAction(ColumnDescription::ReferentialAction onUpdate, ColumnDescription::ReferentialAction onDelete);
128 /// Use for multi-column primary keys during table creation
130
131private:
132 friend class ::DbInitializerTest;
133 Q_DISABLE_COPY_MOVE(DbInitializer)
134
135 /**
136 * Sets the debug @p interface that shall be used on unit test run.
137 */
138 void setTestInterface(TestInterface *interface);
139
140 /**
141 * Sets a different DbIntrospector. This allows unit tests to simulate certain
142 * states of the database.
143 */
144 void setIntrospector(const DbIntrospector::Ptr &introspector);
145
146 /** Helper method for executing a query.
147 * If a debug interface is set for testing, that gets the queries instead.
148 * @throws DbException if something went wrong.
149 */
150 void execQuery(const QString &queryString);
151
152 bool checkTable(const TableDescription &tableDescription);
153 /**
154 * Checks foreign key constraints on table @p tableDescription and fixes them if necessary.
155 */
156 void checkForeignKeys(const TableDescription &tableDescription);
157 void checkIndexes(const TableDescription &tableDescription);
158 bool checkRelation(const RelationDescription &relationDescription);
159
160 static QString referentialActionToString(ColumnDescription::ReferentialAction action);
161
162 void execPendingQueries(const QStringList &queries);
163
164 QSqlDatabase mDatabase;
165 Schema *mSchema = nullptr;
166 QString mErrorMsg;
167 TestInterface *mTestInterface = nullptr;
168 DbIntrospector::Ptr m_introspector;
169 QStringList m_pendingIndexes;
170 QStringList m_pendingForeignKeys;
171 QStringList m_removedForeignKeys;
172};
173
174} // namespace Server
175} // namespace Akonadi
A helper class that describes a column of a table for the DbInitializer.
Definition schematypes.h:23
A helper class that describes the predefined data of a table for the DbInitializer.
Definition schematypes.h:65
A helper class which takes a reference to a database object and the file name of a template file and ...
static DbInitializer::Ptr createInstance(const QSqlDatabase &database, Schema *schema=nullptr)
Returns an initializer instance for a given backend.
virtual QStringList buildRemoveForeignKeyConstraintStatements(const DbIntrospector::ForeignKey &fk, const TableDescription &table) const
Returns an SQL statements to remove the foreign key constraint fk from table table.
virtual ~DbInitializer()
Destroys the database initializer.
virtual QString sqlValue(const ColumnDescription &col, const QString &value) const
Overwrite in backend-specific sub-classes to return the SQL value for a given C++ value.
DbInitializer(const QSqlDatabase &database)
Creates a new database initializer.
bool run()
Starts the initialization process.
virtual QString sqlType(const ColumnDescription &col, int size) const
Overwrite in backend-specific sub-classes to return the SQL type for a given C++ type.
QString errorMsg() const
Returns the textual description of an occurred error.
virtual QStringList buildAddForeignKeyConstraintStatements(const TableDescription &table, const ColumnDescription &column) const
Returns an SQL statements to add a foreign key constraint to an existing column column.
bool updateIndexesAndConstraints()
Checks and creates missing indexes.
virtual QString buildCreateTableStatement(const TableDescription &tableDescription) const =0
Returns a backend-specific CREATE TABLE SQL query describing given table.
static QString buildPrimaryKeyStatement(const TableDescription &table)
Use for multi-column primary keys during table creation.
A structure describing an existing foreign key.
Updates the database schema.
Definition dbupdater.h:46
A helper class that describes indexes of a table for the DbInitializer.
Definition schematypes.h:53
A helper class that describes the relation between two tables for the DbInitializer.
Definition schematypes.h:91
Methods to access the desired database schema (.
Definition schema.h:19
A helper class that describes a table for the DbInitializer.
Definition schematypes.h:77
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:11:39 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.