Akonadi

dbintrospector.h
1 /*
2  SPDX-FileCopyrightText: 2006 Tobias Koenig <[email protected]>
3  SPDX-FileCopyrightText: 2012 Volker Krause <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 #pragma once
8 
9 #include <QHash>
10 #include <QSharedPointer>
11 #include <QSqlDatabase>
12 #include <QStringList>
13 
14 class DbIntrospectorTest;
15 
16 namespace Akonadi
17 {
18 namespace Server
19 {
20 /**
21  * Methods for introspecting the current state of a database schema.
22  * I.e. this is about the structure of a database, not its content.
23  */
25 {
26 public:
28 
29  /** A structure describing an existing foreign key. */
30  class ForeignKey
31  {
32  public:
33  QString name;
34  QString column;
35  QString refTable;
36  QString refColumn;
37  QString onUpdate; // TODO use same enum as DbInitializer
38  QString onDelete; // dito
39  };
40 
41  /**
42  * Returns an introspector instance for a given database.
43  */
44  static DbIntrospector::Ptr createInstance(const QSqlDatabase &database);
45 
46  virtual ~DbIntrospector();
47 
48  /**
49  * Returns @c true if table @p tableName exists.
50  * The default implementation relies on QSqlDatabase::tables(). Usually this
51  * does not need to be reimplemented.
52  */
53  virtual bool hasTable(const QString &tableName);
54 
55  /**
56  * Returns @c true of the given table has an index with the given name.
57  * The default implementation performs the query returned by hasIndexQuery().
58  * @see hasIndexQuery()
59  * @throws DbException on database errors.
60  */
61  virtual bool hasIndex(const QString &tableName, const QString &indexName);
62 
63  /**
64  * Check whether table @p tableName has a column named @p columnName.
65  * The default implementation should work with all backends.
66  */
67  virtual bool hasColumn(const QString &tableName, const QString &columnName);
68 
69  /**
70  * Check whether table @p tableName is empty, ie. does not contain any rows.
71  * The default implementation should work for all backends.
72  * @throws DbException on database errors.
73  */
74  virtual bool isTableEmpty(const QString &tableName);
75 
76  /**
77  * Returns the foreign key constraints on table @p tableName.
78  * The default implementation returns an empty list, so any backend supporting
79  * referential integrity should reimplement this.
80  */
81  virtual QVector<ForeignKey> foreignKeyConstraints(const QString &tableName);
82 
83 protected:
84  /**
85  * Creates a new database introspector, call from subclass.
86  *
87  * @param database The database to introspect.
88  */
89  DbIntrospector(const QSqlDatabase &database);
90 
91  /**
92  * Returns a query string to determine if @p tableName has an index @p indexName.
93  * The query is expected to have one boolean result row/column.
94  * This is used by the default implementation of hasIndex() only, thus reimplementation
95  * is not necessary if you reimplement hasIndex()
96  * The default implementation asserts.
97  */
98  virtual QString hasIndexQuery(const QString &tableName, const QString &indexName);
99 
100  /** The database connection we are introspecting. */
102 
103 private:
104  Q_DISABLE_COPY_MOVE(DbIntrospector)
105 
106  friend class ::DbIntrospectorTest;
107  QHash<QString, QStringList> m_columnCache; // avoids extra db roundtrips
108 };
109 
110 } // namespace Server
111 } // namespace Akonadi
112 
virtual bool hasTable(const QString &tableName)
Returns true if table tableName exists.
A structure describing an existing foreign key.
virtual bool isTableEmpty(const QString &tableName)
Check whether table tableName is empty, ie.
virtual QVector< ForeignKey > foreignKeyConstraints(const QString &tableName)
Returns the foreign key constraints on table tableName.
virtual QString hasIndexQuery(const QString &tableName, const QString &indexName)
Returns a query string to determine if tableName has an index indexName.
static DbIntrospector::Ptr createInstance(const QSqlDatabase &database)
Returns an introspector instance for a given database.
QSqlDatabase m_database
The database connection we are introspecting.
DbIntrospector(const QSqlDatabase &database)
Creates a new database introspector, call from subclass.
virtual bool hasColumn(const QString &tableName, const QString &columnName)
Check whether table tableName has a column named columnName.
Methods for introspecting the current state of a database schema.
Helper integration between Akonadi and Qt.
virtual bool hasIndex(const QString &tableName, const QString &indexName)
Returns true of the given table has an index with the given name.
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.