00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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/queryschemaparameter.h>
00038 #include <kexidb/transaction.h>
00039 #include <kexidb/driver.h>
00040 #include <kexidb/preparedstatement.h>
00041
00042 #include <kexiutils/tristate.h>
00043
00044 namespace KexiDB {
00045
00047 typedef QValueVector<QVariant> RowData;
00048
00049 class Cursor;
00050 class ConnectionPrivate;
00051 class RowEditBuffer;
00052 class DatabaseProperties;
00053 class AlterTableHandler;
00054
00061 class KEXI_DB_EXPORT Connection : public QObject, public KexiDB::Object
00062 {
00063 Q_OBJECT
00064
00065 public:
00066
00071 virtual ~Connection();
00072
00074 ConnectionData* data() const;
00075
00077 inline Driver* driver() const { return m_driver; }
00078
00082 bool connect();
00083
00085 bool isConnected() const;
00086
00090 bool isDatabaseUsed() const;
00091
00095 virtual bool isReadOnly() const;
00096
00099 virtual void clearError();
00100
00106 bool disconnect();
00107
00110 QStringList databaseNames(bool also_system_db = false);
00111
00117 bool databaseExists( const QString &dbName, bool ignoreErrors = true );
00118
00130 bool createDatabase( const QString &dbName );
00131
00142 bool useDatabase( const QString &dbName, bool kexiCompatible = true, bool *cancelled = 0,
00143 MessageHandler* msgHandler = 0 );
00144
00150 bool closeDatabase();
00151
00156 QString currentDatabase() const;
00157
00163 bool dropDatabase( const QString &dbName = QString::null );
00164
00170 QStringList objectNames(int objType = KexiDB::AnyObjectType, bool* ok = 0);
00171
00176 QStringList tableNames(bool also_system_tables = false);
00177
00187 static const QStringList& kexiDBSystemTableNames();
00188
00191 KexiDB::ServerVersionInfo* serverVersion() const;
00192
00197 KexiDB::DatabaseVersionInfo* databaseVersion() const;
00198
00201 DatabaseProperties& databaseProperties();
00202
00209 QValueList<int> tableIds();
00210
00214 QValueList<int> queryIds();
00215
00218 QValueList<int> objectIds(int objType);
00219
00231 Transaction beginTransaction();
00232
00245 bool commitTransaction( Transaction trans = Transaction::null,
00246 bool ignore_inactive = false );
00247
00259 bool rollbackTransaction( Transaction trans = Transaction::null,
00260 bool ignore_inactive = false );
00261
00274 Transaction& defaultTransaction() const;
00275
00278 void setDefaultTransaction(const Transaction& trans);
00279
00286 const QValueList<Transaction>& transactions();
00287
00315 bool autoCommit() const;
00316
00320 bool setAutoCommit(bool on);
00321
00323
00324
00325
00337 virtual Cursor* prepareQuery( const QString& statement, uint cursor_options = 0) = 0;
00338
00348 Cursor* prepareQuery( QuerySchema& query, const QValueList<QVariant>& params,
00349 uint cursor_options = 0 );
00350
00355 virtual Cursor* prepareQuery( QuerySchema& query, uint cursor_options = 0 ) = 0;
00356
00360 Cursor* prepareQuery( TableSchema& table, uint cursor_options = 0);
00361
00370 Cursor* executeQuery( const QString& statement, uint cursor_options = 0 );
00371
00378 Cursor* executeQuery( QuerySchema& query, const QValueList<QVariant>& params,
00379 uint cursor_options = 0 );
00380
00383 Cursor* executeQuery( QuerySchema& query, uint cursor_options = 0 );
00384
00389 Cursor* executeQuery( TableSchema& table, uint cursor_options = 0 );
00390
00396 bool deleteCursor(Cursor *cursor);
00397
00401 TableSchema* tableSchema( int tableId );
00402
00406 TableSchema* tableSchema( const QString& tableName );
00407
00411 QuerySchema* querySchema( int queryId );
00412
00415 QuerySchema* querySchema( const QString& queryName );
00416
00426 bool setQuerySchemaObsolete( const QString& queryName );
00427
00428
00429
00430
00438 tristate querySingleRecord(const QString& sql, RowData &data, bool addLimitTo1 = true);
00439
00443 tristate querySingleRecord(QuerySchema& query, RowData &data, bool addLimitTo1 = true);
00444
00453 tristate querySingleString(const QString& sql, QString &value, uint column = 0,
00454 bool addLimitTo1 = true);
00455
00461 tristate querySingleNumber(const QString& sql, int &number, uint column = 0,
00462 bool addLimitTo1 = true);
00463
00471 bool queryStringList(const QString& sql, QStringList& list, uint column = 0);
00472
00479 bool resultExists(const QString& sql, bool &success, bool addLimitTo1 = true);
00480
00482 bool isEmpty( TableSchema& table, bool &success );
00483
00485
00489 int resultCount(const QString& sql);
00490
00491
00492 #define A , const QVariant&
00493 #define H_INS_REC(args) bool insertRecord(TableSchema &tableSchema args)
00494 #define H_INS_REC_ALL \
00495 H_INS_REC(A); \
00496 H_INS_REC(A A); \
00497 H_INS_REC(A A A); \
00498 H_INS_REC(A A A A); \
00499 H_INS_REC(A A A A A); \
00500 H_INS_REC(A A A A A A); \
00501 H_INS_REC(A A A A A A A); \
00502 H_INS_REC(A A A A A A A A)
00503 H_INS_REC_ALL;
00504
00505 #undef H_INS_REC
00506 #define H_INS_REC(args) bool insertRecord(FieldList& fields args)
00507
00508 H_INS_REC_ALL;
00509 #undef H_INS_REC_ALL
00510 #undef H_INS_REC
00511 #undef A
00512
00513 bool insertRecord(TableSchema &tableSchema, QValueList<QVariant>& values);
00514
00515 bool insertRecord(FieldList& fields, QValueList<QVariant>& values);
00516
00537 bool createTable( TableSchema* tableSchema, bool replaceExisting = false );
00538
00545
00546 tristate dropTable( TableSchema* tableSchema );
00547
00550 tristate dropTable( const QString& table );
00551
00554
00555
00556 tristate alterTable( TableSchema& tableSchema, TableSchema& newTableSchema);
00557
00568 bool alterTableName(TableSchema& tableSchema, const QString& newName, bool replace = false);
00569
00574 bool dropQuery( QuerySchema* querySchema );
00575
00578 bool dropQuery( const QString& query );
00579
00583 bool removeObject( uint objId );
00584
00589 Field* findSystemFieldName(FieldList *fieldlist);
00590
00613 virtual QString anyAvailableDatabaseName();
00614
00625 void setAvailableDatabaseName(const QString& dbName);
00626
00642 bool useTemporaryDatabaseIfNeeded(QString &tmpdbName);
00643
00656 Q_ULLONG lastInsertedAutoIncValue(const QString& aiFieldName, const QString& tableName,
00657 Q_ULLONG* ROWID = 0);
00658
00661 Q_ULLONG lastInsertedAutoIncValue(const QString& aiFieldName,
00662 const TableSchema& table, Q_ULLONG* ROWID = 0);
00663
00667 bool executeSQL( const QString& statement );
00668
00670 class KEXI_DB_EXPORT SelectStatementOptions
00671 {
00672 public:
00673 SelectStatementOptions();
00674 ~SelectStatementOptions();
00675
00677 int identifierEscaping;
00678
00680 bool alsoRetrieveROWID : 1;
00681
00685 bool addVisibleLookupColumns : 1;
00686 };
00687
00690 QString selectStatement( QuerySchema& querySchema,
00691 const QValueList<QVariant>& params,
00692 const SelectStatementOptions& options = SelectStatementOptions() ) const;
00693
00699 inline QString selectStatement( QuerySchema& querySchema,
00700 const SelectStatementOptions& options = SelectStatementOptions() ) const
00701 {
00702 return selectStatement(querySchema, QValueList<QVariant>(), options);
00703 }
00704
00714 bool storeObjectSchemaData( SchemaData &sdata, bool newObject );
00715
00719 tristate loadObjectSchemaData( int objectID, SchemaData &sdata );
00720
00724 tristate loadObjectSchemaData( int objectType, const QString& objectName, SchemaData &sdata );
00725
00730 tristate loadDataBlock( int objectID, QString &dataString, const QString& dataID );
00731
00738 bool storeDataBlock( int objectID, const QString &dataString, const QString& dataID = QString::null );
00739
00745 bool removeDataBlock( int objectID, const QString& dataID = QString::null);
00746
00747 class KEXI_DB_EXPORT TableSchemaChangeListenerInterface
00748 {
00749 public:
00750 TableSchemaChangeListenerInterface() {}
00751 virtual ~TableSchemaChangeListenerInterface() {}
00754 virtual tristate closeListener() = 0;
00755
00758 QString listenerInfoString;
00759 };
00760
00763 void registerForTableSchemaChanges(TableSchemaChangeListenerInterface& listener,
00764 TableSchema& schema);
00765
00766 void unregisterForTableSchemaChanges(TableSchemaChangeListenerInterface& listener,
00767 TableSchema &schema);
00768
00769 void unregisterForTablesSchemaChanges(TableSchemaChangeListenerInterface& listener);
00770
00771 QPtrList<Connection::TableSchemaChangeListenerInterface>*
00772 tableSchemaChangeListeners(TableSchema& tableSchema) const;
00773
00774 tristate closeAllTableSchemaChangeListeners(TableSchema& tableSchema);
00775
00778 void removeTableSchemaInternal(KexiDB::TableSchema *tableSchema);
00779
00785 void insertInternalTableSchema(TableSchema *tableSchema);
00786
00788
00792 virtual bool drv_containsTable( const QString &tableName ) = 0;
00793
00803 virtual bool drv_createTable( const TableSchema& tableSchema );
00804
00816 virtual bool drv_alterTableName(TableSchema& tableSchema, const QString& newName);
00817
00825 virtual bool drv_dropTable( const QString& name );
00826
00828 virtual PreparedStatement::Ptr prepareStatement(PreparedStatement::StatementType type,
00829 FieldList& fields) = 0;
00830
00831 bool isInternalTableSchema(const QString& tableName);
00832
00840 bool setupObjectSchemaData( const RowData &data, SchemaData &sdata );
00841
00848 KexiDB::Field* setupField( const RowData &data );
00849
00850 protected:
00852 Connection( Driver *driver, ConnectionData &conn_data );
00853
00856 void destroy();
00857
00863 tristate dropTable( KexiDB::TableSchema* tableSchema, bool alsoRemoveSchema);
00864
00868 virtual bool drv_connect(KexiDB::ServerVersionInfo& version) = 0;
00869
00872 virtual bool drv_disconnect() = 0;
00873
00877 virtual bool drv_executeSQL( const QString& statement ) = 0;
00878
00885 virtual bool drv_getDatabasesList( QStringList &list );
00886
00888
00892 virtual bool drv_getTablesList( QStringList &list ) = 0;
00893
00906 virtual bool drv_databaseExists( const QString &dbName, bool ignoreErrors = true );
00907
00909 virtual bool drv_createDatabase( const QString &dbName = QString::null ) = 0;
00910
00913 virtual bool drv_useDatabase( const QString &dbName = QString::null, bool *cancelled = 0,
00914 MessageHandler* msgHandler = 0 ) = 0;
00915
00918 virtual bool drv_closeDatabase() = 0;
00919
00933 virtual bool drv_isDatabaseUsed() const { return true; }
00934
00938 virtual bool drv_dropDatabase( const QString &dbName = QString::null ) = 0;
00939
00946 QString createTableStatement( const TableSchema& tableSchema ) const;
00947
00948
00956 QString selectStatement( TableSchema& tableSchema,
00957 const SelectStatementOptions& options = SelectStatementOptions() ) const;
00958
00966 virtual bool drv_createTable( const QString& tableSchemaName );
00967
00968
00969
00970
00971
00978 virtual Q_ULLONG drv_lastInsertRowID() = 0;
00979
00993 virtual TransactionData* drv_beginTransaction();
00994
01001 virtual bool drv_commitTransaction(TransactionData* trans);
01002
01009 virtual bool drv_rollbackTransaction(TransactionData* trans);
01010
01025 virtual bool drv_setAutoCommit(bool on);
01026
01042 bool beginAutoCommitTransaction(TransactionGuard& tg);
01043
01053 bool commitAutoCommitTransaction(const Transaction& trans);
01054
01064 bool rollbackAutoCommitTransaction(const Transaction& trans);
01065
01068
01070
01071
01074 bool checkConnected();
01075
01078 bool checkIsDatabaseUsed();
01079
01082 TableSchema* setupTableSchema( const RowData &data );
01083
01086 QuerySchema* setupQuerySchema( const RowData &data );
01087
01089 bool updateRow(QuerySchema &query, RowData& data, RowEditBuffer& buf, bool useROWID = false);
01091 bool insertRow(QuerySchema &query, RowData& data, RowEditBuffer& buf, bool getROWID = false);
01093 bool deleteRow(QuerySchema &query, RowData& data, bool useROWID = false);
01095 bool deleteAllRows(QuerySchema &query);
01096
01103 bool setupKexiDBSystemSchema();
01104
01109 TableSchema* newKexiDBSystemTableSchema(const QString& tsname);
01110
01112
01123 inline QString escapeIdentifier(const QString& id,
01124 int escaping = Driver::EscapeDriver|Driver::EscapeAsNecessary ) const {
01125 return m_driver->escapeIdentifier(id, escaping);
01126 }
01127
01130 void removeMe(TableSchema *ts);
01131
01135 bool checkIfColumnExists(Cursor *cursor, uint column);
01136
01139 tristate querySingleRecordInternal(RowData &data, const QString* sql,
01140 QuerySchema* query, bool addLimitTo1 = true);
01141
01144 void setReadOnly(bool set);
01145
01149 bool loadExtendedTableSchemaData(TableSchema& tableSchema);
01150
01157 bool storeExtendedTableSchemaData(TableSchema& tableSchema);
01158
01163 bool storeMainFieldSchema(Field *field);
01164
01165
01166
01174 virtual tristate drv_changeFieldProperty(TableSchema &table, Field& field,
01175 const QString& propertyName, const QVariant& value) {
01176 Q_UNUSED(table); Q_UNUSED(field); Q_UNUSED(propertyName); Q_UNUSED(value);
01177 return cancelled; }
01178
01180 QPtrDict<KexiDB::Cursor> m_cursors;
01181
01182 private:
01183 ConnectionPrivate* d;
01184 Driver* const m_driver;
01185 bool m_destructor_started : 1;
01186
01187 friend class KexiDB::Driver;
01188 friend class KexiDB::Cursor;
01189 friend class KexiDB::TableSchema;
01190 friend class KexiDB::DatabaseProperties;
01191 friend class ConnectionPrivate;
01192 friend class KexiDB::AlterTableHandler;
01193 };
01194
01195 }
01196
01197 #endif
01198