File indexing completed on 2024-12-08 07:18:19

0001 /* This file is part of the KDE project
0002    Copyright (C) 2016 Jarosław Staniek <staniek@kde.org>
0003 
0004    This program is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This program is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this program; see the file COPYING.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #ifndef KDB_CONNECTIONPROXY_H
0021 #define KDB_CONNECTIONPROXY_H
0022 
0023 #include <KDbConnection>
0024 
0025 //! The KDbConnectionProxy class gives access to protected (low-level) API of KDbConnection
0026 /**
0027  * The connection object specified in constructor of the proxy is called a parent connection.
0028  * All inherited methods of the KDbConnection API call equivalent methods of the parent
0029  * connection. The KDbConnectionProxy class also provides non-virtual methods that are
0030  * equivalent to KDbConnection ones. These KDbConnection's equivalent methods are called
0031  * by the proxy too.
0032  *
0033  * Example use of this class is Kexi's database database migration plugins when the source
0034  * database is only accessibly using low-level routines.
0035  */
0036 class KDB_EXPORT KDbConnectionProxy : protected KDbConnection
0037 {
0038 public:
0039     //! Creates a proxy object for parent @a connection.
0040     //! @a connection must not be @c nullptr.
0041     //! It is owned by this proxy unless setConnectionIsOwned(false) is called.
0042     explicit KDbConnectionProxy(KDbConnection *connection);
0043 
0044     //! Deletes this proxy. Owned connection is closed and destroyed.
0045     ~KDbConnectionProxy() override;
0046 
0047     //! @return parent connection for this proxy
0048     KDbConnection *parentConnection();
0049 
0050     //! @overload KDbConnection *parentConnection()
0051     const KDbConnection *parentConnection() const;
0052 
0053     //! Control owhership of parent connection that is assigned to this proxy.
0054     //! Owned connection is closed and destroyed upon destruction of the KDbConnectionProxy
0055     //! object.
0056     void setParentConnectionIsOwned(bool set);
0057 
0058     KDbConnectionData data() const;
0059 
0060     KDbDriver* driver() const;
0061 
0062     bool connect();
0063 
0064     bool isConnected() const;
0065 
0066     bool isDatabaseUsed() const;
0067 
0068     KDbConnectionOptions *options();
0069 
0070     void clearResult();
0071 
0072     KDbResult result() const;
0073 
0074     KDbResultable resultable() const;
0075 
0076     bool disconnect();
0077 
0078     QStringList databaseNames(bool also_system_db = false);
0079 
0080     bool databaseExists(const QString &dbName, bool ignoreErrors = true);
0081 
0082     bool createDatabase(const QString &dbName);
0083 
0084     bool useDatabase(const QString &dbName = QString(), bool kexiCompatible = true, bool *cancelled = nullptr,
0085                      KDbMessageHandler* msgHandler = nullptr);
0086 
0087     bool closeDatabase();
0088 
0089     QString currentDatabase() const;
0090 
0091     bool dropDatabase(const QString &dbName = QString());
0092 
0093     QStringList objectNames(int objectType = KDb::AnyObjectType, bool* ok = nullptr);
0094 
0095     QStringList tableNames(bool alsoSystemTables = false, bool* ok = nullptr);
0096 
0097     tristate containsTable(const QString &tableName);
0098 
0099     KDbServerVersionInfo serverVersion() const;
0100 
0101     KDbVersionInfo databaseVersion() const;
0102 
0103     KDbProperties databaseProperties() const;
0104 
0105     QList<int> tableIds(bool* ok = nullptr);
0106 
0107     QList<int> queryIds(bool* ok = nullptr);
0108 
0109     QList<int> objectIds(int objectType, bool* ok = nullptr);
0110 
0111     KDbTransaction beginTransaction();
0112 
0113     bool commitTransaction(KDbTransaction trans = KDbTransaction(),
0114                            KDbTransaction::CommitOptions options = KDbTransaction::CommitOptions());
0115 
0116     bool rollbackTransaction(KDbTransaction trans = KDbTransaction(),
0117                              KDbTransaction::CommitOptions options = KDbTransaction::CommitOptions());
0118 
0119     KDbTransaction defaultTransaction() const;
0120 
0121     void setDefaultTransaction(const KDbTransaction& trans);
0122 
0123     QList<KDbTransaction> transactions();
0124 
0125     bool autoCommit() const;
0126 
0127     bool setAutoCommit(bool on);
0128 
0129     KDbEscapedString escapeString(const QString& str) const override;
0130 
0131     KDbCursor *prepareQuery(const KDbEscapedString &sql,
0132                             KDbCursor::Options options = KDbCursor::Option::None) override;
0133 
0134     KDbCursor *prepareQuery(KDbQuerySchema *query,
0135                             KDbCursor::Options options = KDbCursor::Option::None) override;
0136 
0137     KDbCursor *prepareQuery(KDbTableSchema *table,
0138                             KDbCursor::Options options = KDbCursor::Option::None);
0139 
0140     KDbCursor *executeQuery(const KDbEscapedString &sql,
0141                             KDbCursor::Options options = KDbCursor::Option::None);
0142 
0143     KDbCursor* executeQuery(KDbQuerySchema* query, const QList<QVariant>& params,
0144                             KDbCursor::Options options = KDbCursor::Option::None);
0145 
0146     KDbCursor* executeQuery(KDbQuerySchema* query, KDbCursor::Options options = KDbCursor::Option::None);
0147 
0148     KDbCursor* executeQuery(KDbTableSchema* table, KDbCursor::Options options = KDbCursor::Option::None);
0149 
0150     bool deleteCursor(KDbCursor *cursor);
0151 
0152     KDbTableSchema* tableSchema(int tableId);
0153 
0154     KDbTableSchema* tableSchema(const QString& tableName);
0155 
0156     KDbQuerySchema* querySchema(int queryId);
0157 
0158     KDbQuerySchema* querySchema(const QString& queryName);
0159 
0160     bool setQuerySchemaObsolete(const QString& queryName);
0161 
0162     tristate querySingleRecord(const KDbEscapedString& sql, KDbRecordData* data,
0163                                QueryRecordOptions options = QueryRecordOption::Default);
0164 
0165     tristate querySingleRecord(KDbQuerySchema* query, KDbRecordData* data,
0166                                QueryRecordOptions options = QueryRecordOption::Default);
0167 
0168     tristate querySingleRecord(KDbQuerySchema* query, KDbRecordData* data,
0169                                const QList<QVariant>& params,
0170                                QueryRecordOptions options = QueryRecordOption::Default);
0171 
0172     tristate querySingleString(const KDbEscapedString& sql, QString* value, int column = 0,
0173                                QueryRecordOptions options = QueryRecordOption::Default);
0174 
0175     tristate querySingleString(KDbQuerySchema* query, QString* value, int column = 0,
0176                                QueryRecordOptions options = QueryRecordOption::Default);
0177 
0178     tristate querySingleString(KDbQuerySchema* query, QString* value,
0179                                const QList<QVariant>& params, int column = 0,
0180                                QueryRecordOptions options = QueryRecordOption::Default);
0181 
0182     tristate querySingleNumber(const KDbEscapedString& sql, int* number, int column = 0,
0183                                QueryRecordOptions options = QueryRecordOption::Default);
0184 
0185      tristate querySingleNumber(KDbQuerySchema* query, int* number, int column = 0,
0186                                 QueryRecordOptions options = QueryRecordOption::Default);
0187 
0188     tristate querySingleNumber(KDbQuerySchema* query, int* number,
0189                                const QList<QVariant>& params, int column = 0,
0190                                QueryRecordOptions options = QueryRecordOption::Default);
0191 
0192     bool queryStringList(const KDbEscapedString& sql, QStringList* list, int column = 0);
0193 
0194     bool queryStringList(KDbQuerySchema* query, QStringList* list, int column = 0);
0195 
0196     bool queryStringList(KDbQuerySchema* query, QStringList* list,
0197                          const QList<QVariant>& params, int column = 0);
0198 
0199     tristate resultExists(const KDbEscapedString &sql, QueryRecordOptions options
0200                           = QueryRecordOption::Default);
0201 
0202     tristate isEmpty(KDbTableSchema* table);
0203 
0204     KDbEscapedString recentSqlString() const override;
0205 
0206     //PROTOTYPE:
0207 #define A , const QVariant&
0208 #define H_INS_REC(args) bool insertRecord(KDbTableSchema* tableSchema args)
0209 #define H_INS_REC_ALL \
0210     H_INS_REC(A); \
0211     H_INS_REC(A A); \
0212     H_INS_REC(A A A); \
0213     H_INS_REC(A A A A); \
0214     H_INS_REC(A A A A A); \
0215     H_INS_REC(A A A A A A); \
0216     H_INS_REC(A A A A A A A); \
0217     H_INS_REC(A A A A A A A A)
0218     H_INS_REC_ALL;
0219 
0220 #undef H_INS_REC
0221 #define H_INS_REC(args) bool insertRecord(KDbFieldList* fields args)
0222 
0223     H_INS_REC_ALL;
0224 #undef H_INS_REC_ALL
0225 #undef H_INS_REC
0226 #undef A
0227 
0228     bool insertRecord(KDbTableSchema* tableSchema, const QList<QVariant>& values);
0229 
0230     bool insertRecord(KDbFieldList* fields, const QList<QVariant>& values);
0231 
0232     bool createTable(KDbTableSchema *tableSchema,
0233                      CreateTableOptions options = CreateTableOption::Default);
0234 
0235     KDbTableSchema *copyTable(const KDbTableSchema &tableSchema, const KDbObject &newData);
0236 
0237     KDbTableSchema *copyTable(const QString& tableName, const KDbObject &newData);
0238 
0239     tristate dropTable(KDbTableSchema* tableSchema);
0240 
0241     tristate dropTable(const QString& tableName);
0242 
0243     tristate alterTable(KDbTableSchema* tableSchema, KDbTableSchema* newTableSchema);
0244 
0245     bool alterTableName(KDbTableSchema* tableSchema, const QString& newName,
0246                         AlterTableNameOptions options = AlterTableNameOption::Default);
0247 
0248     bool dropQuery(KDbQuerySchema* querySchema);
0249 
0250     bool dropQuery(const QString& queryName);
0251 
0252     bool removeObject(int objId);
0253 
0254     KDbField* findSystemFieldName(const KDbFieldList& fieldlist);
0255 
0256     QString anyAvailableDatabaseName() override;
0257 
0258     void setAvailableDatabaseName(const QString& dbName);
0259 
0260     bool useTemporaryDatabaseIfNeeded(QString* name);
0261 
0262     Q_REQUIRED_RESULT QSharedPointer<KDbSqlResult> prepareSql(const KDbEscapedString& sql);
0263 
0264     bool executeSql(const KDbEscapedString& sql);
0265 
0266     bool storeObjectData(KDbObject* object);
0267 
0268     bool storeNewObjectData(KDbObject* object);
0269 
0270     //! @since 3.1
0271     tristate loadObjectData(int type, int id, KDbObject* object);
0272 
0273     tristate loadObjectData(int type, const QString& name, KDbObject* object);
0274 
0275     tristate loadDataBlock(int objectID, QString* dataString, const QString& dataID = QString());
0276 
0277     bool storeDataBlock(int objectID, const QString &dataString,
0278                         const QString& dataID = QString());
0279 
0280     bool copyDataBlock(int sourceObjectID, int destObjectID, const QString& dataID = QString());
0281 
0282     bool removeDataBlock(int objectID, const QString& dataID = QString());
0283 
0284     KDbPreparedStatement prepareStatement(KDbPreparedStatement::Type type,
0285         KDbFieldList* fields, const QStringList& whereFieldNames = QStringList());
0286 
0287     bool isInternalTableSchema(const QString& tableName);
0288 
0289     QString escapeIdentifier(const QString& id) const override;
0290 
0291     bool drv_connect() override;
0292 
0293     bool drv_disconnect() override;
0294 
0295     bool drv_getServerVersion(KDbServerVersionInfo* version) override;
0296 
0297     /**
0298      * @since 3.2
0299      */
0300     QStringList drv_getTableNames(bool *ok) override;
0301 
0302     tristate drv_containsTable(const QString &tableName) override;
0303 
0304     bool drv_createTable(const KDbTableSchema& tableSchema) override;
0305 
0306     bool drv_alterTableName(KDbTableSchema* tableSchema, const QString& newName) override;
0307 
0308     bool drv_copyTableData(const KDbTableSchema &tableSchema,
0309                            const KDbTableSchema &destinationTableSchema) override;
0310 
0311     bool drv_dropTable(const QString& tableName) override;
0312 
0313     tristate dropTableInternal(KDbTableSchema* tableSchema, bool alsoRemoveSchema);
0314 
0315     bool setupObjectData(const KDbRecordData& data, KDbObject* object);
0316 
0317     KDbField* setupField(const KDbRecordData& data);
0318 
0319     Q_REQUIRED_RESULT KDbSqlResult *drv_prepareSql(const KDbEscapedString &sql) override;
0320 
0321     bool drv_executeSql(const KDbEscapedString& sql) override;
0322 
0323     bool drv_getDatabasesList(QStringList* list) override;
0324 
0325     bool drv_databaseExists(const QString &dbName, bool ignoreErrors = true) override;
0326 
0327     bool drv_createDatabase(const QString &dbName = QString()) override;
0328 
0329     bool drv_useDatabase(const QString &dbName = QString(), bool *cancelled = nullptr,
0330                          KDbMessageHandler* msgHandler = nullptr) override;
0331 
0332     bool drv_closeDatabase() override;
0333 
0334     bool drv_isDatabaseUsed() const override;
0335 
0336     bool drv_dropDatabase(const QString &dbName = QString()) override;
0337 
0338     bool drv_createTable(const QString& tableName) override;
0339 
0340     KDbTransactionData* drv_beginTransaction() override;
0341 
0342     bool drv_commitTransaction(KDbTransactionData* trans) override;
0343 
0344     bool drv_rollbackTransaction(KDbTransactionData* trans) override;
0345 
0346     bool drv_beforeInsert(const QString& tableName, KDbFieldList* fields) override;
0347 
0348     bool drv_afterInsert(const QString& tableName, KDbFieldList* fields) override;
0349 
0350     bool drv_beforeUpdate(const QString& tableName, KDbFieldList* fields) override;
0351 
0352     bool drv_afterUpdate(const QString& tableName, KDbFieldList* fields) override;
0353 
0354     bool drv_setAutoCommit(bool on) override;
0355 
0356     KDbPreparedStatementInterface* prepareStatementInternal() override;
0357 
0358     bool beginAutoCommitTransaction(KDbTransactionGuard* tg);
0359 
0360     bool commitAutoCommitTransaction(const KDbTransaction& trans);
0361 
0362     bool rollbackAutoCommitTransaction(const KDbTransaction& trans);
0363 
0364     bool checkConnected();
0365 
0366     bool checkIsDatabaseUsed();
0367 
0368     bool updateRecord(KDbQuerySchema* query, KDbRecordData* data, KDbRecordEditBuffer* buf, bool useRecordId = false);
0369 
0370     bool insertRecord(KDbQuerySchema* query, KDbRecordData* data, KDbRecordEditBuffer* buf, bool getRecordId = false);
0371 
0372     bool deleteRecord(KDbQuerySchema* query, KDbRecordData* data, bool useRecordId = false);
0373 
0374     bool deleteAllRecords(KDbQuerySchema* query);
0375 
0376     bool checkIfColumnExists(KDbCursor *cursor, int column);
0377 
0378     tristate querySingleRecordInternal(KDbRecordData* data, const KDbEscapedString* sql,
0379                                        KDbQuerySchema* query, const QList<QVariant>* params,
0380                                        QueryRecordOptions options);
0381 
0382     tristate querySingleStringInternal(const KDbEscapedString* sql, QString* value,
0383                                        KDbQuerySchema* query, const QList<QVariant>* params,
0384                                        int column, QueryRecordOptions options);
0385 
0386     tristate querySingleNumberInternal(const KDbEscapedString* sql, int* number,
0387                                        KDbQuerySchema* query, const QList<QVariant>* params,
0388                                        int column, QueryRecordOptions options);
0389 
0390     bool queryStringListInternal(const KDbEscapedString *sql, QStringList* list,
0391                                  KDbQuerySchema* query, const QList<QVariant>* params,
0392                                  int column, bool (*filterFunction)(const QString&));
0393 
0394     KDbCursor* executeQueryInternal(const KDbEscapedString& sql, KDbQuerySchema* query,
0395                                     const QList<QVariant>* params);
0396 
0397     bool loadExtendedTableSchemaData(KDbTableSchema* tableSchema);
0398 
0399     bool storeExtendedTableSchemaData(KDbTableSchema* tableSchema);
0400 
0401     bool storeMainFieldSchema(KDbField *field);
0402 
0403 private:
0404     Q_DISABLE_COPY(KDbConnectionProxy)
0405     class Private;
0406     Private * const d;
0407 };
0408 
0409 #endif