1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtSql module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QSQLDATABASE_H
41#define QSQLDATABASE_H
42
43#include <QtSql/qtsqlglobal.h>
44#include <QtCore/qstring.h>
45
46QT_BEGIN_NAMESPACE
47
48
49class QSqlError;
50class QSqlDriver;
51class QSqlIndex;
52class QSqlRecord;
53class QSqlQuery;
54class QSqlDatabasePrivate;
55
56class Q_SQL_EXPORT QSqlDriverCreatorBase
57{
58public:
59 virtual ~QSqlDriverCreatorBase() {}
60 virtual QSqlDriver *createObject() const = 0;
61};
62
63template <class T>
64class QSqlDriverCreator : public QSqlDriverCreatorBase
65{
66public:
67 QSqlDriver *createObject() const override { return new T; }
68};
69
70class Q_SQL_EXPORT QSqlDatabase
71{
72public:
73 QSqlDatabase();
74 QSqlDatabase(const QSqlDatabase &other);
75 ~QSqlDatabase();
76
77 QSqlDatabase &operator=(const QSqlDatabase &other);
78
79 bool open();
80 bool open(const QString& user, const QString& password);
81 void close();
82 bool isOpen() const;
83 bool isOpenError() const;
84 QStringList tables(QSql::TableType type = QSql::Tables) const;
85 QSqlIndex primaryIndex(const QString& tablename) const;
86 QSqlRecord record(const QString& tablename) const;
87 QSqlQuery exec(const QString& query = QString()) const;
88 QSqlError lastError() const;
89 bool isValid() const;
90
91 bool transaction();
92 bool commit();
93 bool rollback();
94
95 void setDatabaseName(const QString& name);
96 void setUserName(const QString& name);
97 void setPassword(const QString& password);
98 void setHostName(const QString& host);
99 void setPort(int p);
100 void setConnectOptions(const QString& options = QString());
101 QString databaseName() const;
102 QString userName() const;
103 QString password() const;
104 QString hostName() const;
105 QString driverName() const;
106 int port() const;
107 QString connectOptions() const;
108 QString connectionName() const;
109 void setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy precisionPolicy);
110 QSql::NumericalPrecisionPolicy numericalPrecisionPolicy() const;
111
112 QSqlDriver* driver() const;
113
114 static const char *defaultConnection;
115
116 static QSqlDatabase addDatabase(const QString& type,
117 const QString& connectionName = QLatin1String(defaultConnection));
118 static QSqlDatabase addDatabase(QSqlDriver* driver,
119 const QString& connectionName = QLatin1String(defaultConnection));
120 static QSqlDatabase cloneDatabase(const QSqlDatabase &other, const QString& connectionName);
121 static QSqlDatabase cloneDatabase(const QString &other, const QString& connectionName);
122 static QSqlDatabase database(const QString& connectionName = QLatin1String(defaultConnection),
123 bool open = true);
124 static void removeDatabase(const QString& connectionName);
125 static bool contains(const QString& connectionName = QLatin1String(defaultConnection));
126 static QStringList drivers();
127 static QStringList connectionNames();
128 static void registerSqlDriver(const QString &name, QSqlDriverCreatorBase *creator);
129 static bool isDriverAvailable(const QString &name);
130
131protected:
132 explicit QSqlDatabase(const QString& type);
133 explicit QSqlDatabase(QSqlDriver* driver);
134
135private:
136 friend class QSqlDatabasePrivate;
137 QSqlDatabasePrivate *d;
138};
139
140#ifndef QT_NO_DEBUG_STREAM
141Q_SQL_EXPORT QDebug operator<<(QDebug, const QSqlDatabase &);
142#endif
143
144QT_END_NAMESPACE
145
146#endif // QSQLDATABASE_H
147