1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QSQLDATABASE_H
5#define QSQLDATABASE_H
6
7#include <QtSql/qtsqlglobal.h>
8#include <QtCore/qstring.h>
9
10QT_BEGIN_NAMESPACE
11
12
13class QSqlError;
14class QSqlDriver;
15class QSqlIndex;
16class QSqlRecord;
17class QSqlQuery;
18class QSqlDatabasePrivate;
19
20class Q_SQL_EXPORT QSqlDriverCreatorBase
21{
22public:
23 virtual ~QSqlDriverCreatorBase() {}
24 virtual QSqlDriver *createObject() const = 0;
25};
26
27template <class T>
28class QSqlDriverCreator : public QSqlDriverCreatorBase
29{
30public:
31 QSqlDriver *createObject() const override { return new T; }
32};
33
34class Q_SQL_EXPORT QSqlDatabase
35{
36public:
37 QSqlDatabase();
38 QSqlDatabase(const QSqlDatabase &other);
39 ~QSqlDatabase();
40
41 QSqlDatabase &operator=(const QSqlDatabase &other);
42
43 bool open();
44 bool open(const QString& user, const QString& password);
45 void close();
46 bool isOpen() const;
47 bool isOpenError() const;
48 QStringList tables(QSql::TableType type = QSql::Tables) const;
49 QSqlIndex primaryIndex(const QString& tablename) const;
50 QSqlRecord record(const QString& tablename) const;
51#if QT_DEPRECATED_SINCE(6, 6)
52 QT_DEPRECATED_VERSION_X_6_6("Use QSqlQuery::exec() instead.")
53 QSqlQuery exec(const QString& query = QString()) const;
54#endif
55 QSqlError lastError() const;
56 bool isValid() const;
57
58 bool transaction();
59 bool commit();
60 bool rollback();
61
62 void setDatabaseName(const QString& name);
63 void setUserName(const QString& name);
64 void setPassword(const QString& password);
65 void setHostName(const QString& host);
66 void setPort(int p);
67 void setConnectOptions(const QString& options = QString());
68 QString databaseName() const;
69 QString userName() const;
70 QString password() const;
71 QString hostName() const;
72 QString driverName() const;
73 int port() const;
74 QString connectOptions() const;
75 QString connectionName() const;
76 void setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy precisionPolicy);
77 QSql::NumericalPrecisionPolicy numericalPrecisionPolicy() const;
78
79 QSqlDriver* driver() const;
80
81 static const char *defaultConnection;
82
83 static QSqlDatabase addDatabase(const QString& type,
84 const QString& connectionName = QLatin1StringView(defaultConnection));
85 static QSqlDatabase addDatabase(QSqlDriver* driver,
86 const QString& connectionName = QLatin1StringView(defaultConnection));
87 static QSqlDatabase cloneDatabase(const QSqlDatabase &other, const QString& connectionName);
88 static QSqlDatabase cloneDatabase(const QString &other, const QString& connectionName);
89 static QSqlDatabase database(const QString& connectionName = QLatin1StringView(defaultConnection),
90 bool open = true);
91 static void removeDatabase(const QString& connectionName);
92 static bool contains(const QString& connectionName = QLatin1StringView(defaultConnection));
93 static QStringList drivers();
94 static QStringList connectionNames();
95 static void registerSqlDriver(const QString &name, QSqlDriverCreatorBase *creator);
96 static bool isDriverAvailable(const QString &name);
97
98protected:
99 explicit QSqlDatabase(const QString& type);
100 explicit QSqlDatabase(QSqlDriver* driver);
101
102private:
103 friend class QSqlDatabasePrivate;
104 QSqlDatabasePrivate *d;
105};
106
107#ifndef QT_NO_DEBUG_STREAM
108Q_SQL_EXPORT QDebug operator<<(QDebug, const QSqlDatabase &);
109#endif
110
111QT_END_NAMESPACE
112
113#endif // QSQLDATABASE_H
114

source code of qtbase/src/sql/kernel/qsqldatabase.h