Akonadi

dbinitializer.h
1 /***************************************************************************
2  * Copyright (C) 2006 by Tobias Koenig <[email protected]> *
3  * *
4  * This program is free software; you can redistribute it and/or modify *
5  * it under the terms of the GNU Library General Public License as *
6  * published by the Free Software Foundation; either version 2 of the *
7  * License, or (at your option) any later version. *
8  * *
9  * This program is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU Library General Public *
15  * License along with this program; if not, write to the *
16  * Free Software Foundation, Inc., *
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18  ***************************************************************************/
19 
20 #ifndef DBINITIALIZER_H
21 #define DBINITIALIZER_H
22 
23 #include "dbintrospector.h"
24 #include "schematypes.h"
25 
26 #include <QHash>
27 #include <QStringList>
28 #include <QSharedPointer>
29 #include <QSqlDatabase>
30 
31 class DbInitializerTest;
32 
33 namespace Akonadi
34 {
35 namespace Server
36 {
37 
38 class Schema;
39 class DbUpdater;
40 
41 class TestInterface
42 {
43 public:
44  virtual ~TestInterface()
45  {
46  }
47 
48  virtual void execStatement(const QString &statement) = 0;
49 };
50 
59 {
60  friend class DbUpdater;
61 public:
63 
67  static DbInitializer::Ptr createInstance(const QSqlDatabase &database, Schema *schema = nullptr);
68 
72  virtual ~DbInitializer();
73 
81  bool run();
82 
86  QString errorMsg() const;
87 
93  virtual bool hasForeignKeyConstraints() const = 0;
94 
101  bool updateIndexesAndConstraints();
102 
106  virtual QString buildCreateTableStatement(const TableDescription &tableDescription) const = 0;
107 
108 protected:
114  DbInitializer(const QSqlDatabase &database);
115 
121  virtual QString sqlType(const ColumnDescription &col, int size) const;
123  virtual QString sqlValue(const ColumnDescription &col, const QString &value) const;
124 
125  virtual QString buildColumnStatement(const ColumnDescription &columnDescription, const TableDescription &tableDescription) const = 0;
126  virtual QString buildAddColumnStatement(const TableDescription &tableDescription, const ColumnDescription &columnDescription) const;
127  virtual QString buildCreateIndexStatement(const TableDescription &tableDescription, const IndexDescription &indexDescription) const;
128  virtual QString buildInsertValuesStatement(const TableDescription &tableDescription, const DataDescription &dataDescription) const = 0;
129 
135  virtual QStringList buildAddForeignKeyConstraintStatements(const TableDescription &table, const ColumnDescription &column) const;
136 
142  virtual QStringList buildRemoveForeignKeyConstraintStatements(const DbIntrospector::ForeignKey &fk, const TableDescription &table) const;
143 
144  static QString buildReferentialAction(ColumnDescription::ReferentialAction onUpdate, ColumnDescription::ReferentialAction onDelete);
146  static QString buildPrimaryKeyStatement(const TableDescription &table);
147 
148 private:
149  friend class ::DbInitializerTest;
150 
154  void setTestInterface(TestInterface *interface);
155 
160  void setIntrospector(const DbIntrospector::Ptr &introspector);
161 
166  void execQuery(const QString &queryString);
167 
168  bool checkTable(const TableDescription &tableDescription);
172  void checkForeignKeys(const TableDescription &tableDescription);
173  void checkIndexes(const TableDescription &tableDescription);
174  bool checkRelation(const RelationDescription &relationDescription);
175 
176  static QString referentialActionToString(ColumnDescription::ReferentialAction action);
177 
178  void execPendingQueries(const QStringList &queries);
179 
180  QSqlDatabase mDatabase;
181  Schema *mSchema = nullptr;
182  QString mErrorMsg;
183  TestInterface *mTestInterface = nullptr;
184  DbIntrospector::Ptr m_introspector;
185  QStringList m_pendingIndexes;
186  QStringList m_pendingForeignKeys;
187  QStringList m_removedForeignKeys;
188 };
189 
190 } // namespace Server
191 } // namespace Akonadi
192 
193 #endif
A structure describing an existing foreign key.
A helper class that describes the predefined data of a table for the DbInitializer.
Definition: schematypes.h:83
A helper class that describes indexes of a table for the DbInitializer.
Definition: schematypes.h:69
A helper class that describes a table for the DbInitializer.
Definition: schematypes.h:97
Updates the database schema.
Definition: dbupdater.h:60
A helper class that describes a column of a table for the DbInitializer.
Definition: schematypes.h:37
A helper class that describes the relation between two tables for the DbInitializer.
Definition: schematypes.h:112
Methods to access the desired database schema (.
Definition: schema.h:33
Helper integration between Akonadi and Qt.
A helper class which takes a reference to a database object and the file name of a template file and ...
Definition: dbinitializer.h:58
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed May 27 2020 22:43:37 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.