Akonadi

dbintrospector.h
1/*
2 SPDX-FileCopyrightText: 2006 Tobias Koenig <tokoe@kde.org>
3 SPDX-FileCopyrightText: 2012 Volker Krause <vkrause@kde.org>
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
14class DbIntrospectorTest;
15
16namespace Akonadi
17{
18namespace 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{
26public:
28
29 /** A structure describing an existing foreign key. */
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 QList<ForeignKey> foreignKeyConstraints(const QString &tableName);
82
83 /**
84 * Returns query to retrieve the next autoincrement value for @p tableName.@p columnName.
85 */
86 virtual QString getAutoIncrementValueQuery(const QString &tableName, const QString &columnName) = 0;
87
88 /**
89 * Returns query to update the next autoincrement value for @p tableName.@p columnName to value @p value.
90 */
91 virtual QString updateAutoIncrementValueQuery(const QString &tableName, const QString &columnName, qint64 value) = 0;
92
93protected:
94 /**
95 * Creates a new database introspector, call from subclass.
96 *
97 * @param database The database to introspect.
98 */
99 DbIntrospector(const QSqlDatabase &database);
100
101 /**
102 * Returns a query string to determine if @p tableName has an index @p indexName.
103 * The query is expected to have one boolean result row/column.
104 * This is used by the default implementation of hasIndex() only, thus reimplementation
105 * is not necessary if you reimplement hasIndex()
106 * The default implementation asserts.
107 */
108 virtual QString hasIndexQuery(const QString &tableName, const QString &indexName);
109
110 /** The database connection we are introspecting. */
112
113private:
114 Q_DISABLE_COPY_MOVE(DbIntrospector)
115
116 friend class ::DbIntrospectorTest;
117 QHash<QString, QStringList> m_columnCache; // avoids extra db roundtrips
118};
119
120} // namespace Server
121} // namespace Akonadi
A structure describing an existing foreign key.
Methods for introspecting the current state of a database schema.
virtual QString getAutoIncrementValueQuery(const QString &tableName, const QString &columnName)=0
Returns query to retrieve the next autoincrement value for tableName.
virtual bool hasIndex(const QString &tableName, const QString &indexName)
Returns true of the given table has an index with the given name.
QSqlDatabase m_database
The database connection we are introspecting.
virtual bool hasTable(const QString &tableName)
Returns true if table tableName exists.
virtual bool isTableEmpty(const QString &tableName)
Check whether table tableName is empty, ie.
virtual QString updateAutoIncrementValueQuery(const QString &tableName, const QString &columnName, qint64 value)=0
Returns query to update the next autoincrement value for tableName.
virtual bool hasColumn(const QString &tableName, const QString &columnName)
Check whether table tableName has a column named columnName.
DbIntrospector(const QSqlDatabase &database)
Creates a new database introspector, call from subclass.
virtual QString hasIndexQuery(const QString &tableName, const QString &indexName)
Returns a query string to determine if tableName has an index indexName.
virtual QList< ForeignKey > foreignKeyConstraints(const QString &tableName)
Returns the foreign key constraints on table tableName.
static DbIntrospector::Ptr createInstance(const QSqlDatabase &database)
Returns an introspector instance for a given database.
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:13:38 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.