Akonadi

dbinitializer.h
1 /***************************************************************************
2  * SPDX-FileCopyrightText: 2006 Tobias Koenig <[email protected]> *
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 
17 class DbInitializerTest;
18 
19 namespace Akonadi
20 {
21 namespace Server
22 {
23 class Schema;
24 class DbUpdater;
25 
26 class TestInterface
27 {
28 public:
29  virtual ~TestInterface() = default;
30 
31  virtual void execStatement(const QString &statement) = 0;
32 
33 protected:
34  explicit TestInterface() = default;
35 
36 private:
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 
51 public:
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  * Returns whether the database has working and complete foreign keys.
80  * This information can be used for query optimizations.
81  * @note Result is invalid before run() has been called.
82  */
83  virtual bool hasForeignKeyConstraints() const = 0;
84 
85  /**
86  * Checks and creates missing indexes.
87  *
88  * This method is run after DbUpdater to ensure that data in new columns
89  * are populated and creation of indexes and foreign keys does not fail.
90  */
92 
93  /**
94  * Returns a backend-specific CREATE TABLE SQL query describing given table
95  */
96  virtual QString buildCreateTableStatement(const TableDescription &tableDescription) const = 0;
97 
98 protected:
99  /**
100  * Creates a new database initializer.
101  *
102  * @param database The reference to the database.
103  */
104  DbInitializer(const QSqlDatabase &database);
105 
106  /**
107  * Overwrite in backend-specific sub-classes to return the SQL type for a given C++ type.
108  * @param type Name of the C++ type.
109  * @param size Optional size hint for the column, if -1 use the default SQL type for @p type.
110  */
111  virtual QString sqlType(const ColumnDescription &col, int size) const;
112  /** Overwrite in backend-specific sub-classes to return the SQL value for a given C++ value. */
113  virtual QString sqlValue(const ColumnDescription &col, const QString &value) const;
114 
115  virtual QString buildColumnStatement(const ColumnDescription &columnDescription, const TableDescription &tableDescription) const = 0;
116  virtual QString buildAddColumnStatement(const TableDescription &tableDescription, const ColumnDescription &columnDescription) const;
117  virtual QString buildCreateIndexStatement(const TableDescription &tableDescription, const IndexDescription &indexDescription) const;
118  virtual QString buildInsertValuesStatement(const TableDescription &tableDescription, const DataDescription &dataDescription) const = 0;
119 
120  /**
121  * Returns an SQL statements to add a foreign key constraint to an existing column @p column.
122  * The default implementation returns an empty string, so any backend supporting foreign key constraints
123  * must reimplement this.
124  */
126 
127  /**
128  * Returns an SQL statements to remove the foreign key constraint @p fk from table @p table.
129  * The default implementation returns an empty string, so any backend supporting foreign key constraints
130  * must reimplement this.
131  */
133 
134  static QString buildReferentialAction(ColumnDescription::ReferentialAction onUpdate, ColumnDescription::ReferentialAction onDelete);
135  /// Use for multi-column primary keys during table creation
137 
138 private:
139  friend class ::DbInitializerTest;
140  Q_DISABLE_COPY_MOVE(DbInitializer)
141 
142  /**
143  * Sets the debug @p interface that shall be used on unit test run.
144  */
145  void setTestInterface(TestInterface *interface);
146 
147  /**
148  * Sets a different DbIntrospector. This allows unit tests to simulate certain
149  * states of the database.
150  */
151  void setIntrospector(const DbIntrospector::Ptr &introspector);
152 
153  /** Helper method for executing a query.
154  * If a debug interface is set for testing, that gets the queries instead.
155  * @throws DbException if something went wrong.
156  */
157  void execQuery(const QString &queryString);
158 
159  bool checkTable(const TableDescription &tableDescription);
160  /**
161  * Checks foreign key constraints on table @p tableDescription and fixes them if necessary.
162  */
163  void checkForeignKeys(const TableDescription &tableDescription);
164  void checkIndexes(const TableDescription &tableDescription);
165  bool checkRelation(const RelationDescription &relationDescription);
166 
167  static QString referentialActionToString(ColumnDescription::ReferentialAction action);
168 
169  void execPendingQueries(const QStringList &queries);
170 
171  QSqlDatabase mDatabase;
172  Schema *mSchema = nullptr;
173  QString mErrorMsg;
174  TestInterface *mTestInterface = nullptr;
175  DbIntrospector::Ptr m_introspector;
176  QStringList m_pendingIndexes;
177  QStringList m_pendingForeignKeys;
178  QStringList m_removedForeignKeys;
179 };
180 
181 } // namespace Server
182 } // namespace Akonadi
183 
Updates the database schema.
Definition: dbupdater.h:45
A helper class that describes a column of a table for the DbInitializer.
Definition: schematypes.h:32
A helper class that describes the relation between two tables for the DbInitializer.
Definition: schematypes.h:107
A helper class that describes indexes of a table for the DbInitializer.
Definition: schematypes.h:64
QString errorMsg() const
Returns the textual description of an occurred error.
A structure describing an existing foreign key.
static QString buildPrimaryKeyStatement(const TableDescription &table)
Use for multi-column primary keys during table creation.
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.
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.
A helper class that describes a table for the DbInitializer.
Definition: schematypes.h:92
virtual ~DbInitializer()
Destroys the database initializer.
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.
virtual bool hasForeignKeyConstraints() const =0
Returns whether the database has working and complete foreign keys.
bool run()
Starts the initialization process.
DbInitializer(const QSqlDatabase &database)
Creates a new database initializer.
virtual QString buildCreateTableStatement(const TableDescription &tableDescription) const =0
Returns a backend-specific CREATE TABLE SQL query describing given table.
Methods to access the desired database schema (.
Definition: schema.h:26
bool updateIndexesAndConstraints()
Checks and creates missing indexes.
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.
A helper class that describes the predefined data of a table for the DbInitializer.
Definition: schematypes.h:78
A helper class which takes a reference to a database object and the file name of a template file and ...
Definition: dbinitializer.h:47
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Mon Jun 27 2022 04:01:06 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.