File indexing completed on 2025-01-18 10:09:18
0001
0002
0003
0004 #ifndef QSQLDATABASE_H
0005 #define QSQLDATABASE_H
0006
0007 #include <QtSql/qtsqlglobal.h>
0008 #include <QtCore/qstring.h>
0009
0010 QT_BEGIN_NAMESPACE
0011
0012
0013 class QSqlError;
0014 class QSqlDriver;
0015 class QSqlIndex;
0016 class QSqlRecord;
0017 class QSqlQuery;
0018 class QSqlDatabasePrivate;
0019
0020 class Q_SQL_EXPORT QSqlDriverCreatorBase
0021 {
0022 public:
0023 virtual ~QSqlDriverCreatorBase() {}
0024 virtual QSqlDriver *createObject() const = 0;
0025 };
0026
0027 template <class T>
0028 class QSqlDriverCreator : public QSqlDriverCreatorBase
0029 {
0030 public:
0031 QSqlDriver *createObject() const override { return new T; }
0032 };
0033
0034 class Q_SQL_EXPORT QSqlDatabase
0035 {
0036 public:
0037 QSqlDatabase();
0038 QSqlDatabase(const QSqlDatabase &other);
0039 ~QSqlDatabase();
0040
0041 QSqlDatabase &operator=(const QSqlDatabase &other);
0042
0043 bool open();
0044 bool open(const QString& user, const QString& password);
0045 void close();
0046 bool isOpen() const;
0047 bool isOpenError() const;
0048 QStringList tables(QSql::TableType type = QSql::Tables) const;
0049 QSqlIndex primaryIndex(const QString& tablename) const;
0050 QSqlRecord record(const QString& tablename) const;
0051 #if QT_DEPRECATED_SINCE(6, 6)
0052 QT_DEPRECATED_VERSION_X_6_6("Use QSqlQuery::exec() instead.")
0053 QSqlQuery exec(const QString& query = QString()) const;
0054 #endif
0055 QSqlError lastError() const;
0056 bool isValid() const;
0057
0058 bool transaction();
0059 bool commit();
0060 bool rollback();
0061
0062 void setDatabaseName(const QString& name);
0063 void setUserName(const QString& name);
0064 void setPassword(const QString& password);
0065 void setHostName(const QString& host);
0066 void setPort(int p);
0067 void setConnectOptions(const QString& options = QString());
0068 QString databaseName() const;
0069 QString userName() const;
0070 QString password() const;
0071 QString hostName() const;
0072 QString driverName() const;
0073 int port() const;
0074 QString connectOptions() const;
0075 QString connectionName() const;
0076 void setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy precisionPolicy);
0077 QSql::NumericalPrecisionPolicy numericalPrecisionPolicy() const;
0078
0079 QSqlDriver* driver() const;
0080
0081 static const char *defaultConnection;
0082
0083 static QSqlDatabase addDatabase(const QString& type,
0084 const QString& connectionName = QLatin1StringView(defaultConnection));
0085 static QSqlDatabase addDatabase(QSqlDriver* driver,
0086 const QString& connectionName = QLatin1StringView(defaultConnection));
0087 static QSqlDatabase cloneDatabase(const QSqlDatabase &other, const QString& connectionName);
0088 static QSqlDatabase cloneDatabase(const QString &other, const QString& connectionName);
0089 static QSqlDatabase database(const QString& connectionName = QLatin1StringView(defaultConnection),
0090 bool open = true);
0091 static void removeDatabase(const QString& connectionName);
0092 static bool contains(const QString& connectionName = QLatin1StringView(defaultConnection));
0093 static QStringList drivers();
0094 static QStringList connectionNames();
0095 static void registerSqlDriver(const QString &name, QSqlDriverCreatorBase *creator);
0096 static bool isDriverAvailable(const QString &name);
0097
0098 protected:
0099 explicit QSqlDatabase(const QString& type);
0100 explicit QSqlDatabase(QSqlDriver* driver);
0101
0102 private:
0103 friend class QSqlDatabasePrivate;
0104 QSqlDatabasePrivate *d;
0105 };
0106
0107 #ifndef QT_NO_DEBUG_STREAM
0108 Q_SQL_EXPORT QDebug operator<<(QDebug, const QSqlDatabase &);
0109 #endif
0110
0111 QT_END_NAMESPACE
0112
0113 #endif