File indexing completed on 2024-05-05 16:47:14

0001 /* This file is part of the KDE project
0002    Copyright (C) 2003 Adam Pigg <adam@piggz.co.uk>
0003    Copyright (C) 2010-2016 Jarosław Staniek <staniek@kde.org>
0004 
0005    This program is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This program is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this program; see the file COPYING.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #ifndef KDB_POSTGRESQLCONNECTION_H
0022 #define KDB_POSTGRESQLCONNECTION_H
0023 
0024 #include "KDbConnection.h"
0025 #include "KDbTransactionData.h"
0026 
0027 #include <libpq-fe.h>
0028 
0029 class PostgresqlConnectionInternal;
0030 
0031 //! @internal
0032 class PostgresqlTransactionData : public KDbTransactionData
0033 {
0034 public:
0035     explicit PostgresqlTransactionData(KDbConnection *conn);
0036     ~PostgresqlTransactionData();
0037 private:
0038     Q_DISABLE_COPY(PostgresqlTransactionData)
0039 };
0040 
0041 class PostgresqlConnection : public KDbConnection
0042 {
0043     Q_DECLARE_TR_FUNCTIONS(PostgresqlConnection)
0044 public:
0045     ~PostgresqlConnection() override;
0046 
0047     //! @return a new query based on a query statement
0048     Q_REQUIRED_RESULT KDbCursor *prepareQuery(const KDbEscapedString &sql,
0049                                               KDbCursor::Options options
0050                                               = KDbCursor::Option::None) override;
0051 
0052     //! @return a new query based on a query object
0053     Q_REQUIRED_RESULT KDbCursor *prepareQuery(KDbQuerySchema *query,
0054                                               KDbCursor::Options options
0055                                               = KDbCursor::Option::None) override;
0056 
0057     Q_REQUIRED_RESULT KDbPreparedStatementInterface *prepareStatementInternal() override;
0058 
0059     /*! Connection-specific string escaping.  */
0060     KDbEscapedString escapeString(const QString& str) const override;
0061     virtual KDbEscapedString escapeString(const QByteArray& str) const;
0062 
0063 private:
0064     /*! Used by driver */
0065     PostgresqlConnection(KDbDriver *driver, const KDbConnectionData& connData,
0066                          const KDbConnectionOptions &options);
0067 
0068     //! @return true if currently connected to a database, ignoring the m_is_connected flag.
0069     bool drv_isDatabaseUsed() const override;
0070     //! Noop: we tell we are connected, but we wont actually connect until we use a database.
0071     bool drv_connect() override;
0072     bool drv_getServerVersion(KDbServerVersionInfo* version) override;
0073     //! Noop: we tell we have disconnected, but it is actually handled by closeDatabase.
0074     bool drv_disconnect() override;
0075     //! @return a list of database names
0076     bool drv_getDatabasesList(QStringList* list) override;
0077     //! Create a new database
0078     bool drv_createDatabase(const QString &dbName = QString()) override;
0079     //! Uses database. Note that if data().localSocketFileName() is not empty,
0080     //! only directory path is used for connecting; the local socket's filename stays ".s.PGSQL.5432".
0081     bool drv_useDatabase(const QString &dbName = QString(), bool *cancelled = nullptr,
0082                                  KDbMessageHandler* msgHandler = nullptr) override;
0083     //! Close the database connection
0084     bool drv_closeDatabase() override;
0085     //! Drops the given database
0086     bool drv_dropDatabase(const QString &dbName = QString()) override;
0087     //! Executes an SQL statement
0088     Q_REQUIRED_RESULT KDbSqlResult *drv_prepareSql(const KDbEscapedString &sql) override;
0089     bool drv_executeSql(const KDbEscapedString& sql) override;
0090 
0091     //! Implemented for KDbResultable
0092     QString serverResultName() const override;
0093 
0094 //! @todo move this somewhere to low level class (MIGRATION?)
0095     tristate drv_containsTable(const QString &tableName) override;
0096 
0097     void storeResult(PGresult *pgResult, ExecStatusType execStatus);
0098 
0099     PostgresqlConnectionInternal * const d;
0100 
0101     friend class PostgresqlDriver;
0102     friend class PostgresqlCursorData;
0103     friend class PostgresqlTransactionData;
0104     friend class PostgresqlSqlResult;
0105     Q_DISABLE_COPY(PostgresqlConnection)
0106 };
0107 
0108 #endif