File indexing completed on 2024-10-13 10:08:50

0001 /* This file is part of the KDE project
0002    Copyright (C) 2005 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_CONNECTION_P_H
0021 #define KDB_CONNECTION_P_H
0022 
0023 #include "KDbConnectionData.h"
0024 #include "KDbConnection.h"
0025 #include "KDbConnectionOptions.h"
0026 #include "kdb_export.h"
0027 #include "KDbParser.h"
0028 #include "KDbProperties.h"
0029 #include "KDbQuerySchema_p.h"
0030 #include "KDbVersionInfo.h"
0031 
0032 //! Interface for accessing connection's internal result, for use by drivers.
0033 class KDB_EXPORT KDbConnectionInternal
0034 {
0035 public:
0036     explicit KDbConnectionInternal(KDbConnection *conn);
0037     KDbConnection* const connection;
0038 private:
0039     Q_DISABLE_COPY(KDbConnectionInternal)
0040 };
0041 
0042 class KDbConnectionPrivate
0043 {
0044     Q_DECLARE_TR_FUNCTIONS(KDbConnectionPrivate)
0045 public:
0046     KDbConnectionPrivate(KDbConnection* const conn, KDbDriver *drv,
0047                          const KDbConnectionData& _connData,
0048                          const KDbConnectionOptions &_options);
0049 
0050     ~KDbConnectionPrivate();
0051 
0052     void deleteAllCursors();
0053 
0054     void errorInvalidDBContents(const QString& details);
0055 
0056     QString strItIsASystemObject() const;
0057 
0058     inline KDbParser *parser() {
0059         return m_parser ? m_parser : (m_parser = new KDbParser(conn));
0060     }
0061 
0062     inline KDbTableSchema* table(const QString& name) const {
0063         return m_tablesByName.value(name);
0064     }
0065 
0066     inline KDbTableSchema* table(int id) const {
0067         return m_tables.value(id);
0068     }
0069 
0070     //! used just for removing system KDbTableSchema objects on db close.
0071     inline QSet<KDbInternalTableSchema*> internalKDbTables() const {
0072         return m_internalKDbTables;
0073     }
0074 
0075     /*! Allocates all needed table KDb system objects for kexi__* KDb library's
0076      system tables schema.
0077      These objects are used internally in this connection and are added to list of tables
0078      (by name,      not by id because these have no ids).
0079     */
0080     void setupKDbSystemSchema();
0081 
0082     void insertTable(KDbTableSchema* tableSchema);
0083 
0084     /*! Removes table schema having identifier @a id from internal structures and destroys it.
0085      Does not make any change at the backend. */
0086     void removeTable(int id);
0087 
0088     void takeTable(KDbTableSchema* tableSchema);
0089 
0090     void renameTable(KDbTableSchema* tableSchema, const QString& newName);
0091 
0092     void changeTableId(KDbTableSchema* tableSchema, int newId);
0093 
0094     void clearTables();
0095 
0096     inline KDbQuerySchema* query(const QString& name) const {
0097         return m_queriesByName.value(name);
0098     }
0099 
0100     inline KDbQuerySchema* query(int id) const {
0101         return m_queries.value(id);
0102     }
0103 
0104     void insertQuery(KDbQuerySchema* query);
0105 
0106     /*! Removes @a querySchema from internal structures and
0107      destroys it. Does not make any change at the backend. */
0108     void removeQuery(KDbQuerySchema* querySchema);
0109 
0110     void setQueryObsolete(KDbQuerySchema* query);
0111 
0112     void clearQueries();
0113 
0114     /*! @return a full table schema for a table retrieved using 'kexi__*' system tables.
0115      Connection keeps ownership of the returned object.
0116      Used internally by tableSchema() methods.
0117      On failure deletes @a table and returns @c nullptr. */
0118     Q_REQUIRED_RESULT KDbTableSchema *setupTableSchema(KDbTableSchema *table);
0119 
0120     /*! @return a full query schema for a query using 'kexi__*' system tables.
0121      Connection keeps ownership of the returned object.
0122      Used internally by querySchema() methods.
0123      On failure deletes @a query and returns @c nullptr. */
0124     Q_REQUIRED_RESULT KDbQuerySchema *setupQuerySchema(KDbQuerySchema *query);
0125 
0126     //! @return cached fields expanded information for @a query
0127     KDbQuerySchemaFieldsExpanded *fieldsExpanded(const KDbQuerySchema *query);
0128 
0129     //! Inserts cached fields expanded information for @a query
0130     void insertFieldsExpanded(const KDbQuerySchema *query, KDbQuerySchemaFieldsExpanded *cache);
0131 
0132     //! Removes cached fields expanded information for @a query
0133     void removeFieldsExpanded(const KDbQuerySchema *query);
0134 
0135     KDbConnection* const conn; //!< The @a KDbConnection instance this @a KDbConnectionPrivate belongs to.
0136     KDbConnectionData connData; //!< the @a KDbConnectionData used within that connection.
0137 
0138     //! True for read only connection. Used especially for file-based drivers.
0139     KDbConnectionOptions options;
0140 
0141     //!< The driver this @a KDbConnection instance uses.
0142     KDbDriver * const driver;
0143 
0144     /*! Default transaction handle.
0145     If transactions are supported: Any operation on database (e.g. inserts)
0146     that is started without specifying transaction context, will be performed
0147     in the context of this transaction. */
0148     KDbTransaction default_trans;
0149     QList<KDbTransaction> transactions;
0150 
0151     QHash<const KDbTableSchema*, QSet<KDbTableSchemaChangeListener*>* > tableSchemaChangeListeners;
0152 
0153     QHash<const KDbQuerySchema*, QSet<KDbTableSchemaChangeListener*>* > queryTableSchemaChangeListeners;
0154 
0155     //! Used in KDbConnection::setQuerySchemaObsolete( const QString& queryName )
0156     //! to collect obsolete queries. THese are deleted on connection deleting.
0157     QSet<KDbQuerySchema*> obsoleteQueries;
0158 
0159     //! server version information for this connection.
0160     KDbServerVersionInfo serverVersion;
0161 
0162     //! Database version information for this connection.
0163     KDbVersionInfo databaseVersion;
0164 
0165     KDbParser *m_parser = nullptr;
0166 
0167     //! cursors created for this connection
0168     QSet<KDbCursor*> cursors;
0169 
0170     //! Database properties
0171     KDbProperties dbProperties;
0172 
0173     QString availableDatabaseName; //!< used by anyAvailableDatabaseName()
0174     QString usedDatabase; //!< database name that is opened now (the currentDatabase() name)
0175 
0176     //! true if rollbackTransaction() and commitTransaction() shouldn't remove
0177     //! the transaction object from 'transactions' list; used by closeDatabase()
0178     bool dontRemoveTransactions = false;
0179 
0180     //! used to avoid endless recursion between useDatabase() and databaseExists()
0181     //! when useTemporaryDatabaseIfNeeded() works
0182     bool skipDatabaseExistsCheckInUseDatabase = false;
0183 
0184     /*! Used when single transactions are only supported (KDbDriver::SingleTransactions).
0185      True value means default KDbTransaction has been started inside connection object
0186      (by beginAutoCommitTransaction()), otherwise default transaction has been started outside
0187      of the object (e.g. before createTable()), so we shouldn't autocommit the transaction
0188      in commitAutoCommitTransaction(). Also, beginAutoCommitTransaction() doesn't restarts
0189      transaction if default_trans_started_inside is false. Such behavior allows user to
0190      execute a sequence of actions like CREATE TABLE...; INSERT DATA...; within a single transaction
0191      and commit it or rollback by hand. */
0192     bool defaultTransactionStartedInside = false;
0193 
0194     bool isConnected = false;
0195 
0196     bool autoCommit = true;
0197 
0198     bool insideCloseDatabase = false; //!< helper: true while closeDatabase() is executed
0199 
0200 private:
0201     //! Table schemas retrieved on demand with tableSchema()
0202     QHash<int, KDbTableSchema*> m_tables;
0203     QHash<QString, KDbTableSchema*> m_tablesByName;
0204     //! used just for removing system KDbTableSchema objects on db close.
0205     QSet<KDbInternalTableSchema*> m_internalKDbTables;
0206     //! Query schemas retrieved on demand with querySchema()
0207     QHash<int, KDbQuerySchema*> m_queries;
0208     QHash<QString, KDbQuerySchema*> m_queriesByName;
0209     KDbUtils::AutodeletedHash<const KDbQuerySchema*, KDbQuerySchemaFieldsExpanded*> m_fieldsExpandedCache;
0210     Q_DISABLE_COPY(KDbConnectionPrivate)
0211 };
0212 
0213 #endif