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 };
00682
00685 QString selectStatement( QuerySchema& querySchema,
00686 const QValueList<QVariant>& params,
00687 const SelectStatementOptions& options = SelectStatementOptions() ) const;
00688
00694 inline QString selectStatement( QuerySchema& querySchema,
00695 const SelectStatementOptions& options = SelectStatementOptions() ) const
00696 {
00697 return selectStatement(querySchema, QValueList<QVariant>(), options);
00698 }
00699
00709 bool storeObjectSchemaData( SchemaData &sdata, bool newObject );
00710
00714 tristate loadObjectSchemaData( int objectID, SchemaData &sdata );
00715
00719 tristate loadObjectSchemaData( int objectType, const QString& objectName, SchemaData &sdata );
00720
00725 tristate loadDataBlock( int objectID, QString &dataString, const QString& dataID );
00726
00733 bool storeDataBlock( int objectID, const QString &dataString, const QString& dataID = QString::null );
00734
00740 bool removeDataBlock( int objectID, const QString& dataID = QString::null);
00741
00742 class KEXI_DB_EXPORT TableSchemaChangeListenerInterface
00743 {
00744 public:
00745 TableSchemaChangeListenerInterface() {}
00746 virtual ~TableSchemaChangeListenerInterface() {}
00749 virtual tristate closeListener() = 0;
00750
00753 QString listenerInfoString;
00754 };
00755
00758 void registerForTableSchemaChanges(TableSchemaChangeListenerInterface& listener,
00759 TableSchema& schema);
00760
00761 void unregisterForTableSchemaChanges(TableSchemaChangeListenerInterface& listener,
00762 TableSchema &schema);
00763
00764 void unregisterForTablesSchemaChanges(TableSchemaChangeListenerInterface& listener);
00765
00766 QPtrList<Connection::TableSchemaChangeListenerInterface>*
00767 tableSchemaChangeListeners(TableSchema& tableSchema) const;
00768
00769 tristate closeAllTableSchemaChangeListeners(TableSchema& tableSchema);
00770
00773 void removeTableSchemaInternal(KexiDB::TableSchema *tableSchema);
00774
00780 void insertInternalTableSchema(TableSchema *tableSchema);
00781
00783
00787 virtual bool drv_containsTable( const QString &tableName ) = 0;
00788
00798 virtual bool drv_createTable( const TableSchema& tableSchema );
00799
00801 virtual PreparedStatement::Ptr prepareStatement(PreparedStatement::StatementType type,
00802 FieldList& fields) = 0;
00803
00804 bool isInternalTableSchema(const QString& tableName);
00805
00806 protected:
00808 Connection( Driver *driver, ConnectionData &conn_data );
00809
00812 void destroy();
00813
00819 tristate dropTable( KexiDB::TableSchema* tableSchema, bool alsoRemoveSchema);
00820
00824 virtual bool drv_connect(KexiDB::ServerVersionInfo& version) = 0;
00825
00828 virtual bool drv_disconnect() = 0;
00829
00833 virtual bool drv_executeSQL( const QString& statement ) = 0;
00834
00841 virtual bool drv_getDatabasesList( QStringList &list );
00842
00844
00848 virtual bool drv_getTablesList( QStringList &list ) = 0;
00849
00862 virtual bool drv_databaseExists( const QString &dbName, bool ignoreErrors = true );
00863
00865 virtual bool drv_createDatabase( const QString &dbName = QString::null ) = 0;
00866
00869 virtual bool drv_useDatabase( const QString &dbName = QString::null, bool *cancelled = 0,
00870 MessageHandler* msgHandler = 0 ) = 0;
00871
00874 virtual bool drv_closeDatabase() = 0;
00875
00889 virtual bool drv_isDatabaseUsed() const { return true; }
00890
00894 virtual bool drv_dropDatabase( const QString &dbName = QString::null ) = 0;
00895
00902 QString createTableStatement( const TableSchema& tableSchema ) const;
00903
00904
00912 QString selectStatement( TableSchema& tableSchema,
00913 const SelectStatementOptions& options = SelectStatementOptions() ) const;
00914
00922 virtual bool drv_createTable( const QString& tableSchemaName );
00923
00924
00925
00926
00927
00934 virtual Q_ULLONG drv_lastInsertRowID() = 0;
00935
00949 virtual TransactionData* drv_beginTransaction();
00950
00957 virtual bool drv_commitTransaction(TransactionData* trans);
00958
00965 virtual bool drv_rollbackTransaction(TransactionData* trans);
00966
00981 virtual bool drv_setAutoCommit(bool on);
00982
00986 virtual bool drv_dropTable( const QString& name );
00987
00995 virtual bool drv_alterTableName(TableSchema& tableSchema, const QString& newName);
00996
01012 bool beginAutoCommitTransaction(TransactionGuard& tg);
01013
01023 bool commitAutoCommitTransaction(const Transaction& trans);
01024
01034 bool rollbackAutoCommitTransaction(const Transaction& trans);
01035
01038
01040
01041
01044 bool checkConnected();
01045
01048 bool checkIsDatabaseUsed();
01049
01053 bool setupObjectSchemaData( const RowData &data, SchemaData &sdata );
01054
01057 TableSchema* setupTableSchema( const RowData &data );
01058
01061 QuerySchema* setupQuerySchema( const RowData &data );
01062
01064 bool updateRow(QuerySchema &query, RowData& data, RowEditBuffer& buf, bool useROWID = false);
01066 bool insertRow(QuerySchema &query, RowData& data, RowEditBuffer& buf, bool getROWID = false);
01068 bool deleteRow(QuerySchema &query, RowData& data, bool useROWID = false);
01070 bool deleteAllRows(QuerySchema &query);
01071
01078 bool setupKexiDBSystemSchema();
01079
01084 TableSchema* newKexiDBSystemTableSchema(const QString& tsname);
01085
01087
01098 inline QString escapeIdentifier(const QString& id,
01099 int drvEscaping = Driver::EscapeDriver|Driver::EscapeAsNecessary ) const {
01100 return m_driver->escapeIdentifier(id, drvEscaping);
01101 }
01102
01105 void removeMe(TableSchema *ts);
01106
01110 bool checkIfColumnExists(Cursor *cursor, uint column);
01111
01114 tristate querySingleRecordInternal(RowData &data, const QString* sql,
01115 QuerySchema* query, bool addLimitTo1 = true);
01116
01119 void setReadOnly(bool set);
01120
01124 bool loadExtendedTableSchemaData(TableSchema& tableSchema);
01125
01132 bool storeExtendedTableSchemaData(TableSchema& tableSchema);
01133
01138 bool storeMainFieldSchema(Field *field);
01139
01140
01141
01149 virtual tristate drv_changeFieldProperty(TableSchema &table, Field& field,
01150 const QString& propertyName, const QVariant& value) {
01151 Q_UNUSED(table); Q_UNUSED(field); Q_UNUSED(propertyName); Q_UNUSED(value);
01152 return cancelled; }
01153
01155 QPtrDict<KexiDB::Cursor> m_cursors;
01156
01157 private:
01158 ConnectionPrivate* d;
01159 Driver* const m_driver;
01160 bool m_destructor_started : 1;
01161
01162 friend class KexiDB::Driver;
01163 friend class KexiDB::Cursor;
01164 friend class KexiDB::TableSchema;
01165 friend class KexiDB::DatabaseProperties;
01166 friend class ConnectionPrivate;
01167 friend class KexiDB::AlterTableHandler;
01168 };
01169
01170 }
01171
01172 #endif
01173