kexi

connection.h

00001 /* This file is part of the KDE project
00002    Copyright (C) 2003-2006 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This program is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this program; see the file COPYING.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #ifndef KEXIDB_CONNECTION_H
00021 #define KEXIDB_CONNECTION_H
00022 
00023 #include <qobject.h>
00024 #include <qstringlist.h>
00025 #include <qintdict.h>
00026 #include <qdict.h>
00027 #include <qptrdict.h>
00028 #include <qvaluevector.h>
00029 #include <qvaluelist.h>
00030 #include <qvariant.h>
00031 #include <qguardedptr.h>
00032 
00033 #include <kexidb/object.h>
00034 #include <kexidb/connectiondata.h>
00035 #include <kexidb/tableschema.h>
00036 #include <kexidb/queryschema.h>
00037 #include <kexidb/transaction.h>
00038 #include <kexidb/driver.h>
00039 #include <kexidb/preparedstatement.h>
00040 
00041 #include <kexiutils/tristate.h>
00042 
00043 namespace KexiDB {
00044 
00046 typedef QValueVector<QVariant> RowData; 
00047 
00048 class Cursor;
00049 class ConnectionPrivate;
00050 class RowEditBuffer;
00051 class DatabaseProperties;
00052 
00059 class KEXI_DB_EXPORT Connection : public QObject, public KexiDB::Object
00060 {
00061     Q_OBJECT
00062 
00063     public:
00064 
00069         virtual ~Connection();
00070 
00072         ConnectionData* data() const { return m_data; }
00073 
00075         Driver* driver() const { return m_driver; }
00076 
00080         bool connect();
00081 
00083         bool isConnected() const;
00084 
00088         bool isDatabaseUsed() const;
00089 
00093         virtual bool isReadOnly() const;
00094 
00097         virtual void clearError();
00098 
00104         bool disconnect();
00105 
00108         QStringList databaseNames(bool also_system_db = false);
00109 
00115         bool databaseExists( const QString &dbName, bool ignoreErrors = true );
00116 
00128         bool createDatabase( const QString &dbName );
00129 
00140         bool useDatabase( const QString &dbName, bool kexiCompatible = true, bool *cancelled = 0, 
00141             MessageHandler* msgHandler = 0 );
00142 
00148         bool closeDatabase();
00149 
00154         QString currentDatabase() { return m_usedDatabase; }
00155 
00161         bool dropDatabase( const QString &dbName = QString::null );
00162 
00168         QStringList objectNames(int objType = KexiDB::AnyObjectType, bool* ok = 0);
00169 
00174         QStringList tableNames(bool also_system_tables = false);
00175 
00185         static const QStringList& kexiDBSystemTableNames();
00186 
00189         int versionMajor() const;
00190         int versionMinor() const;
00191 
00194         DatabaseProperties& databaseProperties();
00195 
00202         QValueList<int> tableIds();
00203 
00207         QValueList<int> queryIds();
00208 
00211         QValueList<int> objectIds(int objType);
00212 
00224         Transaction beginTransaction();
00225 
00238         bool commitTransaction( Transaction trans = Transaction::null,
00239             bool ignore_inactive = false );
00240         
00252         bool rollbackTransaction( Transaction trans = Transaction::null,
00253             bool ignore_inactive = false );
00254         
00267         Transaction& defaultTransaction() const;
00268 
00271         void setDefaultTransaction(const Transaction& trans);
00272         
00279         const QValueList<Transaction>& transactions();
00280 
00308         bool autoCommit() const;
00309 
00313         bool setAutoCommit(bool on);
00314 
00316 //js: MOVED TO Driver       virtual QString escapeString(const QString& str) const = 0;
00317 //      virtual QCString escapeString(const QCString& str) const = 0;
00318         
00330         virtual Cursor* prepareQuery( const QString& statement, uint cursor_options = 0) = 0;
00331 
00340         virtual Cursor* prepareQuery( QuerySchema& query, uint cursor_options = 0 ) = 0;
00341 
00345 //      Cursor* prepareQuery( QuerySchema& query, uint cursor_options = 0);
00346 
00350         Cursor* prepareQuery( TableSchema& table, uint cursor_options = 0);
00351 
00360         Cursor* executeQuery( const QString& statement, uint cursor_options = 0 );
00361 
00365         Cursor* executeQuery( QuerySchema& query, uint cursor_options = 0 );
00366 
00370         Cursor* executeQuery( TableSchema& table, uint cursor_options = 0 );
00371 
00377         bool deleteCursor(Cursor *cursor);
00378 
00382         TableSchema* tableSchema( int tableId );
00383         
00387         TableSchema* tableSchema( const QString& tableName );
00388         
00392         QuerySchema* querySchema( int queryId );
00393 
00396         QuerySchema* querySchema( const QString& queryName );
00397 
00407         bool setQuerySchemaObsolete( const QString& queryName );
00408 
00409 //js: MOVED TO Driver       QString valueToSQL( const Field::Type ftype, const QVariant& v ) const;
00410 //      QString valueToSQL( const Field *field, const QVariant& v ) const;
00411 
00418         tristate querySingleRecord(const QString& sql, RowData &data);
00419 
00422         tristate querySingleRecord(QuerySchema& query, RowData &data);
00423 
00431         tristate querySingleString(const QString& sql, QString &value, uint column = 0);
00432 
00438         tristate querySingleNumber(const QString& sql, int &number, uint column = 0);
00439 
00447         bool queryStringList(const QString& sql, QStringList& list, uint column = 0);
00448 
00454         bool resultExists(const QString& sql, bool &success);
00455 
00457         bool isEmpty( TableSchema& table, bool &success );
00458 
00460 
00464         int resultCount(const QString& sql);
00465 
00466         //PROTOTYPE:
00467         #define A , const QVariant&
00468         #define H_INS_REC(args) bool insertRecord(TableSchema &tableSchema args)
00469         #define H_INS_REC_ALL \
00470         H_INS_REC(A); \
00471         H_INS_REC(A A); \
00472         H_INS_REC(A A A); \
00473         H_INS_REC(A A A A); \
00474         H_INS_REC(A A A A A); \
00475         H_INS_REC(A A A A A A); \
00476         H_INS_REC(A A A A A A A); \
00477         H_INS_REC(A A A A A A A A)
00478         H_INS_REC_ALL;
00479         
00480         #undef H_INS_REC
00481         #define H_INS_REC(args) bool insertRecord(FieldList& fields args)
00482 
00483         H_INS_REC_ALL;
00484         #undef H_INS_REC_ALL
00485         #undef H_INS_REC
00486         #undef A
00487         
00488         bool insertRecord(TableSchema &tableSchema, QValueList<QVariant>& values);
00489         
00490         bool insertRecord(FieldList& fields, QValueList<QVariant>& values);
00491 
00512         bool createTable( TableSchema* tableSchema, bool replaceExisting = false );
00513 
00520 
00521         tristate dropTable( TableSchema* tableSchema );
00522 
00525         tristate dropTable( const QString& table );
00526 
00529 
00530 
00531         tristate alterTable( TableSchema& tableSchema, TableSchema& newTableSchema);
00532 
00539         bool alterTableName(TableSchema& tableSchema, const QString& newName, bool replace = false);
00540 
00545         bool dropQuery( QuerySchema* querySchema );
00546 
00549         bool dropQuery( const QString& query );
00550 
00554         bool removeObject( uint objId );
00555 
00560         Field* findSystemFieldName(FieldList *fieldlist);
00561 
00584         virtual QString anyAvailableDatabaseName();
00585         
00596         void setAvailableDatabaseName(const QString& dbName);
00597 
00613         bool useTemporaryDatabaseIfNeeded(QString &tmpdbName);
00614 
00627         Q_ULLONG lastInsertedAutoIncValue(const QString& aiFieldName, const QString& tableName, 
00628             Q_ULLONG* ROWID = 0);
00629         
00632         Q_ULLONG lastInsertedAutoIncValue(const QString& aiFieldName, 
00633             const TableSchema& table, Q_ULLONG* ROWID = 0);
00634 
00638         bool executeSQL( const QString& statement );
00639 
00646         inline QString selectStatement( QuerySchema& querySchema, 
00647             int idEscaping = Driver::EscapeDriver|Driver::EscapeAsNecessary ) const
00648         {
00649             return selectStatement(querySchema, false, idEscaping);
00650         }
00651 
00661         bool storeObjectSchemaData( SchemaData &sdata, bool newObject );
00662 
00666         tristate loadObjectSchemaData( int objectID, SchemaData &sdata );
00667 
00671         tristate loadObjectSchemaData( int objectType, const QString& objectName, SchemaData &sdata );
00672 
00677         bool loadDataBlock( int objectID, QString &dataString, const QString& dataID );
00678 
00685         bool storeDataBlock( int objectID, const QString &dataString, const QString& dataID = QString::null );
00686 
00692         bool removeDataBlock( int objectID, const QString& dataID = QString::null);
00693 
00694         class KEXI_DB_EXPORT TableSchemaChangeListenerInterface
00695         {
00696             public:
00697                 TableSchemaChangeListenerInterface() {}
00698                 virtual ~TableSchemaChangeListenerInterface() {}
00701                 virtual tristate closeListener() = 0;
00702 
00705                 QString listenerInfoString;
00706         };
00707 //TMP// TODO: will be more generic
00710         void registerForTableSchemaChanges(TableSchemaChangeListenerInterface& listener, 
00711             TableSchema& schema);
00712 
00713         void unregisterForTableSchemaChanges(TableSchemaChangeListenerInterface& listener, 
00714             TableSchema &schema);
00715 
00716         void unregisterForTablesSchemaChanges(TableSchemaChangeListenerInterface& listener);
00717 
00718         QPtrList<Connection::TableSchemaChangeListenerInterface>*
00719             tableSchemaChangeListeners(TableSchema& tableSchema) const;
00720 
00721         tristate closeAllTableSchemaChangeListeners(TableSchema& tableSchema);
00722 
00725         void removeTableSchemaInternal(KexiDB::TableSchema *tableSchema);
00726 
00732         void insertInternalTableSchema(TableSchema *tableSchema);
00733 
00735 
00739         virtual bool drv_containsTable( const QString &tableName ) = 0;
00740 
00750         virtual bool drv_createTable( const TableSchema& tableSchema );
00751 
00753         virtual PreparedStatement::Ptr prepareStatement(PreparedStatement::StatementType type, 
00754             FieldList& fields) = 0;
00755 
00756         bool isInternalTableSchema(const QString& tableName);
00757 
00758     protected:
00760         Connection( Driver *driver, ConnectionData &conn_data );
00761 
00764         void destroy();
00765 
00771         tristate dropTable( KexiDB::TableSchema* tableSchema, bool alsoRemoveSchema);
00772 
00775         virtual bool drv_connect() = 0;
00776 
00779         virtual bool drv_disconnect() = 0;
00780 
00784         virtual bool drv_executeSQL( const QString& statement ) = 0;
00785 
00792         virtual bool drv_getDatabasesList( QStringList &list );
00793 
00795 
00799         virtual bool drv_getTablesList( QStringList &list ) = 0;
00800 
00813         virtual bool drv_databaseExists( const QString &dbName, bool ignoreErrors = true );
00814 
00816         virtual bool drv_createDatabase( const QString &dbName = QString::null ) = 0;
00817 
00820         virtual bool drv_useDatabase( const QString &dbName = QString::null, bool *cancelled = 0, 
00821             MessageHandler* msgHandler = 0 ) = 0;
00822 
00825         virtual bool drv_closeDatabase() = 0;
00826         
00840         virtual bool drv_isDatabaseUsed() const { return true; }
00841 
00845         virtual bool drv_dropDatabase( const QString &dbName = QString::null ) = 0;
00846 
00853         QString createTableStatement( const TableSchema& tableSchema ) const;
00854 
00855 
00863         QString selectStatement( TableSchema& tableSchema ) const;
00864 
00868         QString selectStatement( QuerySchema& querySchema, bool alsoRetrieveROWID, int idEscaping = Driver::EscapeDriver|Driver::EscapeAsNecessary ) const;
00869 
00877         virtual bool drv_createTable( const QString& tableSchemaName );
00878 
00879 //      /*! Executes query \a statement and returns resulting rows 
00880 //          (used mostly for SELECT query). */
00881 //      virtual bool drv_executeQuery( const QString& statement ) = 0;
00882 
00889         virtual Q_ULLONG drv_lastInsertRowID() = 0;
00890 
00904         virtual TransactionData* drv_beginTransaction();
00905         
00912         virtual bool drv_commitTransaction(TransactionData* trans);
00913         
00920         virtual bool drv_rollbackTransaction(TransactionData* trans);
00921 
00936         virtual bool drv_setAutoCommit(bool on);
00937 
00941         virtual bool drv_dropTable( const QString& name );
00942 
00952         virtual bool drv_alterTableName(TableSchema& tableSchema, const QString& newName, bool replace = false);
00953 
00969         bool beginAutoCommitTransaction(TransactionGuard& tg);
00970         
00980         bool commitAutoCommitTransaction(const Transaction& trans);
00981         
00991         bool rollbackAutoCommitTransaction(const Transaction& trans);
00992 
00995 //      virtual CursorData* drv_createCursor( const QString& statement ) = 0;
00997 //      virtual bool drv_deleteCursor( CursorData *data ) = 0;
00998         
01001         bool checkConnected();
01002 
01005         bool checkIsDatabaseUsed();
01006 
01010         bool setupObjectSchemaData( const RowData &data, SchemaData &sdata );
01011 
01014         TableSchema* setupTableSchema( const RowData &data );
01015 
01018         QuerySchema* setupQuerySchema( const RowData &data );
01019 
01020         bool updateRow(QuerySchema &query, RowData& data, RowEditBuffer& buf, bool useROWID = false);
01021 
01022         bool insertRow(QuerySchema &query, RowData& data, RowEditBuffer& buf, bool getROWID = false);
01023 
01024         bool deleteRow(QuerySchema &query, RowData& data, bool useROWID = false);
01025 
01026         bool deleteAllRows(QuerySchema &query);
01027 
01034         bool setupKexiDBSystemSchema();
01035 
01040         TableSchema* newKexiDBSystemTableSchema(const QString& tsname);
01041 
01043 
01054         inline QString escapeIdentifier(const QString& id, 
01055             int drvEscaping = Driver::EscapeDriver|Driver::EscapeAsNecessary ) const {
01056             return m_driver->escapeIdentifier(id, drvEscaping);
01057         }
01058         
01061         void removeMe(TableSchema *ts);
01062 
01066         bool checkIfColumnExists(Cursor *cursor, uint column);
01067 
01069         tristate querySingleRecordInternal(RowData &data, const QString* sql, QuerySchema* query);
01070 
01073         void setReadOnly(bool set);
01074 
01075         QGuardedPtr<ConnectionData> m_data;
01076         QString m_name;
01077         QString m_usedDatabase; 
01078 
01080         QIntDict<TableSchema> m_tables;
01081         QDict<TableSchema> m_tables_byname;
01082         QIntDict<QuerySchema> m_queries;
01083         QDict<QuerySchema> m_queries_byname;
01084 
01086         QPtrDict<TableSchema> m_kexiDBSystemTables;
01087 
01089         QPtrDict<KexiDB::Cursor> m_cursors;
01090 
01091     friend class KexiDB::Driver;
01092     friend class KexiDB::Cursor;
01093     friend class KexiDB::TableSchema; 
01094     friend class KexiDB::DatabaseProperties; 
01095     friend class ConnectionPrivate;
01096 
01097         ConnectionPrivate *d;
01098     private:
01099         Driver *m_driver;
01100         bool m_destructor_started : 1; 
01101 };
01102 
01103 } //namespace KexiDB
01104 
01105 #endif
01106 
KDE Home | KDE Accessibility Home | Description of Access Keys