kexi

connection.h
1/* This file is part of the KDE project
2 Copyright (C) 2003-2007 Jaroslaw Staniek <js@iidea.pl>
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#ifndef KEXIDB_CONNECTION_H
21#define KEXIDB_CONNECTION_H
22
23#include <tqobject.h>
24#include <tqstringlist.h>
25#include <tqintdict.h>
26#include <tqdict.h>
27#include <tqptrdict.h>
28#include <tqvaluevector.h>
29#include <tqvaluelist.h>
30#include <tqvariant.h>
31#include <tqguardedptr.h>
32
33#include <kexidb/object.h>
34#include <kexidb/connectiondata.h>
35#include <kexidb/tableschema.h>
36#include <kexidb/queryschema.h>
37#include <kexidb/queryschemaparameter.h>
38#include <kexidb/transaction.h>
39#include <kexidb/driver.h>
40#include <kexidb/preparedstatement.h>
41
42#include <kexiutils/tristate.h>
43
44namespace KexiDB {
45
47typedef TQValueVector<TQVariant> RowData;
48
49class Cursor;
50class ConnectionPrivate;
51class RowEditBuffer;
54
61class KEXI_DB_EXPORT Connection : public TQObject, public KexiDB::Object
62{
63 TQ_OBJECT
64
65
66 public:
67
72 virtual ~Connection();
73
75 ConnectionData* data() const;
76
78 inline Driver* driver() const { return m_driver; }
79
83 bool connect();
84
86 bool isConnected() const;
87
91 bool isDatabaseUsed() const;
92
96 virtual bool isReadOnly() const;
97
100 virtual void clearError();
101
107 bool disconnect();
108
111 TQStringList databaseNames(bool also_system_db = false);
112
118 bool databaseExists( const TQString &dbName, bool ignoreErrors = true );
119
131 bool createDatabase( const TQString &dbName );
132
143 bool useDatabase( const TQString &dbName, bool kexiCompatible = true, bool *cancelled = 0,
144 MessageHandler* msgHandler = 0 );
145
151 bool closeDatabase();
152
157 TQString currentDatabase() const;
158
164 bool dropDatabase( const TQString &dbName = TQString() );
165
171 TQStringList objectNames(int objType = KexiDB::AnyObjectType, bool* ok = 0);
172
177 TQStringList tableNames(bool also_system_tables = false);
178
188 static const TQStringList& kexiDBSystemTableNames();
189
192 KexiDB::ServerVersionInfo* serverVersion() const;
193
198 KexiDB::DatabaseVersionInfo* databaseVersion() const;
199
202 DatabaseProperties& databaseProperties();
203
210 TQValueList<int> tableIds();
211
215 TQValueList<int> queryIds();
216
219 TQValueList<int> objectIds(int objType);
220
232 Transaction beginTransaction();
233
246 bool commitTransaction( Transaction trans = Transaction::null,
247 bool ignore_inactive = false );
248
260 bool rollbackTransaction( Transaction trans = Transaction::null,
261 bool ignore_inactive = false );
262
275 Transaction& defaultTransaction() const;
276
279 void setDefaultTransaction(const Transaction& trans);
280
287 const TQValueList<Transaction>& transactions();
288
316 bool autoCommit() const;
317
321 bool setAutoCommit(bool on);
322
324//js: MOVED TO Driver virtual TQString escapeString(const TQString& str) const = 0;
325// virtual TQCString escapeString(const TQCString& str) const = 0;
326
338 virtual Cursor* prepareQuery( const TQString& statement, uint cursor_options = 0) = 0;
339
349 Cursor* prepareQuery( QuerySchema& query, const TQValueList<TQVariant>& params,
350 uint cursor_options = 0 );
351
356 virtual Cursor* prepareQuery( QuerySchema& query, uint cursor_options = 0 ) = 0;
357
361 Cursor* prepareQuery( TableSchema& table, uint cursor_options = 0);
362
371 Cursor* executeQuery( const TQString& statement, uint cursor_options = 0 );
372
379 Cursor* executeQuery( QuerySchema& query, const TQValueList<TQVariant>& params,
380 uint cursor_options = 0 );
381
384 Cursor* executeQuery( QuerySchema& query, uint cursor_options = 0 );
385
390 Cursor* executeQuery( TableSchema& table, uint cursor_options = 0 );
391
397 bool deleteCursor(Cursor *cursor);
398
402 TableSchema* tableSchema( int tableId );
403
407 TableSchema* tableSchema( const TQString& tableName );
408
412 QuerySchema* querySchema( int queryId );
413
416 QuerySchema* querySchema( const TQString& queryName );
417
427 bool setQuerySchemaObsolete( const TQString& queryName );
428
429//js: MOVED TO Driver TQString valueToSQL( const Field::Type ftype, const TQVariant& v ) const;
430// TQString valueToSQL( const Field *field, const TQVariant& v ) const;
431
439 tristate querySingleRecord(const TQString& sql, RowData &data, bool addLimitTo1 = true);
440
444 tristate querySingleRecord(QuerySchema& query, RowData &data, bool addLimitTo1 = true);
445
454 tristate querySingleString(const TQString& sql, TQString &value, uint column = 0,
455 bool addLimitTo1 = true);
456
462 tristate querySingleNumber(const TQString& sql, int &number, uint column = 0,
463 bool addLimitTo1 = true);
464
472 bool queryStringList(const TQString& sql, TQStringList& list, uint column = 0);
473
480 bool resultExists(const TQString& sql, bool &success, bool addLimitTo1 = true);
481
483 bool isEmpty( TableSchema& table, bool &success );
484
486
490 int resultCount(const TQString& sql);
491
492 //PROTOTYPE:
493 #define A , const TQVariant&
494 #define H_INS_REC(args) bool insertRecord(TableSchema &tableSchema args)
495 #define H_INS_REC_ALL \
496 H_INS_REC(A); \
497 H_INS_REC(A A); \
498 H_INS_REC(A A A); \
499 H_INS_REC(A A A A); \
500 H_INS_REC(A A A A A); \
501 H_INS_REC(A A A A A A); \
502 H_INS_REC(A A A A A A A); \
503 H_INS_REC(A A A A A A A A)
504 H_INS_REC_ALL;
505
506 #undef H_INS_REC
507 #define H_INS_REC(args) bool insertRecord(FieldList& fields args)
508
509 H_INS_REC_ALL;
510 #undef H_INS_REC_ALL
511 #undef H_INS_REC
512 #undef A
513
514 bool insertRecord(TableSchema &tableSchema, TQValueList<TQVariant>& values);
515
516 bool insertRecord(FieldList& fields, TQValueList<TQVariant>& values);
517
538 bool createTable( TableSchema* tableSchema, bool replaceExisting = false );
539
547 tristate dropTable( TableSchema* tableSchema );
548
551 tristate dropTable( const TQString& table );
552
557 tristate alterTable( TableSchema& tableSchema, TableSchema& newTableSchema);
558
569 bool alterTableName(TableSchema& tableSchema, const TQString& newName, bool replace = false);
570
575 bool dropQuery( QuerySchema* querySchema );
576
579 bool dropQuery( const TQString& query );
580
584 bool removeObject( uint objId );
585
590 Field* findSystemFieldName(FieldList *fieldlist);
591
614 virtual TQString anyAvailableDatabaseName();
615
626 void setAvailableDatabaseName(const TQString& dbName);
627
643 bool useTemporaryDatabaseIfNeeded(TQString &tmpdbName);
644
657 TQ_ULLONG lastInsertedAutoIncValue(const TQString& aiFieldName, const TQString& tableName,
658 TQ_ULLONG* ROWID = 0);
659
662 TQ_ULLONG lastInsertedAutoIncValue(const TQString& aiFieldName,
663 const TableSchema& table, TQ_ULLONG* ROWID = 0);
664
668 bool executeSQL( const TQString& statement );
669
671 class KEXI_DB_EXPORT SelectStatementOptions
672 {
673 public:
676
679
682
687 };
688
691 TQString selectStatement( QuerySchema& querySchema,
692 const TQValueList<TQVariant>& params,
693 const SelectStatementOptions& options = SelectStatementOptions() ) const;
694
700 inline TQString selectStatement( QuerySchema& querySchema,
701 const SelectStatementOptions& options = SelectStatementOptions() ) const
702 {
703 return selectStatement(querySchema, TQValueList<TQVariant>(), options);
704 }
705
715 bool storeObjectSchemaData( SchemaData &sdata, bool newObject );
716
720 tristate loadObjectSchemaData( int objectID, SchemaData &sdata );
721
725 tristate loadObjectSchemaData( int objectType, const TQString& objectName, SchemaData &sdata );
726
731 tristate loadDataBlock( int objectID, TQString &dataString, const TQString& dataID );
732
739 bool storeDataBlock( int objectID, const TQString &dataString, const TQString& dataID = TQString() );
740
746 bool removeDataBlock( int objectID, const TQString& dataID = TQString());
747
748 class KEXI_DB_EXPORT TableSchemaChangeListenerInterface
749 {
750 public:
751 TableSchemaChangeListenerInterface() {}
752 virtual ~TableSchemaChangeListenerInterface() {}
755 virtual tristate closeListener() = 0;
756
759 TQString listenerInfoString;
760 };
761//TMP// TODO: will be more generic
764 void registerForTableSchemaChanges(TableSchemaChangeListenerInterface& listener,
765 TableSchema& schema);
766
767 void unregisterForTableSchemaChanges(TableSchemaChangeListenerInterface& listener,
768 TableSchema &schema);
769
770 void unregisterForTablesSchemaChanges(TableSchemaChangeListenerInterface& listener);
771
772 TQPtrList<Connection::TableSchemaChangeListenerInterface>*
773 tableSchemaChangeListeners(TableSchema& tableSchema) const;
774
775 tristate closeAllTableSchemaChangeListeners(TableSchema& tableSchema);
776
779 void removeTableSchemaInternal(KexiDB::TableSchema *tableSchema);
780
786 void insertInternalTableSchema(TableSchema *tableSchema);
787
789
793 virtual bool drv_containsTable( const TQString &tableName ) = 0;
794
804 virtual bool drv_createTable( const TableSchema& tableSchema );
805
817 virtual bool drv_alterTableName(TableSchema& tableSchema, const TQString& newName);
818
826 virtual bool drv_dropTable( const TQString& name );
827
829 virtual PreparedStatement::Ptr prepareStatement(PreparedStatement::StatementType type,
830 FieldList& fields) = 0;
831
832 bool isInternalTableSchema(const TQString& tableName);
833
841 bool setupObjectSchemaData( const RowData &data, SchemaData &sdata );
842
849 KexiDB::Field* setupField( const RowData &data );
850
851 protected:
853 Connection( Driver *driver, ConnectionData &conn_data );
854
857 void destroy();
858
864 tristate dropTable( KexiDB::TableSchema* tableSchema, bool alsoRemoveSchema);
865
869 virtual bool drv_connect(KexiDB::ServerVersionInfo& version) = 0;
870
873 virtual bool drv_disconnect() = 0;
874
878 virtual bool drv_executeSQL( const TQString& statement ) = 0;
879
886 virtual bool drv_getDatabasesList( TQStringList &list );
887
889
893 virtual bool drv_getTablesList( TQStringList &list ) = 0;
894
907 virtual bool drv_databaseExists( const TQString &dbName, bool ignoreErrors = true );
908
910 virtual bool drv_createDatabase( const TQString &dbName = TQString() ) = 0;
911
914 virtual bool drv_useDatabase( const TQString &dbName = TQString(), bool *cancelled = 0,
915 MessageHandler* msgHandler = 0 ) = 0;
916
919 virtual bool drv_closeDatabase() = 0;
920
934 virtual bool drv_isDatabaseUsed() const { return true; }
935
939 virtual bool drv_dropDatabase( const TQString &dbName = TQString() ) = 0;
940
947 TQString createTableStatement( const TableSchema& tableSchema ) const;
948
949
957 TQString selectStatement( TableSchema& tableSchema,
958 const SelectStatementOptions& options = SelectStatementOptions() ) const;
959
967 virtual bool drv_createTable( const TQString& tableSchemaName );
968
969// /*! Executes query \a statement and returns resulting rows
970// (used mostly for SELECT query). */
971// virtual bool drv_executeQuery( const TQString& statement ) = 0;
972
979 virtual TQ_ULLONG drv_lastInsertRowID() = 0;
980
994 virtual TransactionData* drv_beginTransaction();
995
1002 virtual bool drv_commitTransaction(TransactionData* trans);
1003
1010 virtual bool drv_rollbackTransaction(TransactionData* trans);
1011
1026 virtual bool drv_setAutoCommit(bool on);
1027
1043 bool beginAutoCommitTransaction(TransactionGuard& tg);
1044
1054 bool commitAutoCommitTransaction(const Transaction& trans);
1055
1065 bool rollbackAutoCommitTransaction(const Transaction& trans);
1066
1069// virtual CursorData* drv_createCursor( const TQString& statement ) = 0;
1071// virtual bool drv_deleteCursor( CursorData *data ) = 0;
1072
1075 bool checkConnected();
1076
1079 bool checkIsDatabaseUsed();
1080
1083 TableSchema* setupTableSchema( const RowData &data );
1084
1087 QuerySchema* setupQuerySchema( const RowData &data );
1088
1090 bool updateRow(QuerySchema &query, RowData& data, RowEditBuffer& buf, bool useROWID = false);
1092 bool insertRow(QuerySchema &query, RowData& data, RowEditBuffer& buf, bool getROWID = false);
1094 bool deleteRow(QuerySchema &query, RowData& data, bool useROWID = false);
1096 bool deleteAllRows(QuerySchema &query);
1097
1104 bool setupKexiDBSystemSchema();
1105
1110 TableSchema* newKexiDBSystemTableSchema(const TQString& tsname);
1111
1113
1124 inline TQString escapeIdentifier(const TQString& id,
1125 int escaping = Driver::EscapeDriver|Driver::EscapeAsNecessary ) const {
1126 return m_driver->escapeIdentifier(id, escaping);
1127 }
1128
1131 void removeMe(TableSchema *ts);
1132
1136 bool checkIfColumnExists(Cursor *cursor, uint column);
1137
1140 tristate querySingleRecordInternal(RowData &data, const TQString* sql,
1141 QuerySchema* query, bool addLimitTo1 = true);
1142
1145 void setReadOnly(bool set);
1146
1150 bool loadExtendedTableSchemaData(TableSchema& tableSchema);
1151
1158 bool storeExtendedTableSchemaData(TableSchema& tableSchema);
1159
1164 bool storeMainFieldSchema(Field *field);
1165
1166 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1167
1176 const TQString& propertyName, const TQVariant& value) {
1177 Q_UNUSED(table); Q_UNUSED(field); Q_UNUSED(propertyName); Q_UNUSED(value);
1178 return cancelled; }
1179
1181 TQPtrDict<KexiDB::Cursor> m_cursors;
1182
1183 private:
1184 ConnectionPrivate* d;
1185 Driver* const m_driver;
1186 bool m_destructor_started : 1;
1187
1188 friend class KexiDB::Driver;
1189 friend class KexiDB::Cursor;
1190 friend class KexiDB::TableSchema;
1192 friend class ConnectionPrivate;
1193 friend class KexiDB::AlterTableHandler;
1194};
1195
1196} //namespace KexiDB
1197
1198#endif
1199
A tool for handling altering database table schema.
Definition alter.h:112
Database specific connection data, e.g. host, port.
options used in selectStatement()
Definition connection.h:672
int identifierEscaping
A mode for escaping identifier, Driver::EscapeDriver|Driver::EscapeAsNecessary by default.
Definition connection.h:678
bool alsoRetrieveROWID
True if ROWID should be also retrieved. False by default.
Definition connection.h:681
Provides database connection, allowing queries and data modification.
Definition connection.h:62
virtual bool drv_executeSQL(const TQString &statement)=0
TQString escapeIdentifier(const TQString &id, int escaping=Driver::EscapeDriver|Driver::EscapeAsNecessary) const
Identifier escaping function in the associated Driver.
virtual bool drv_getTablesList(TQStringList &list)=0
TQPtrDict< KexiDB::Cursor > m_cursors
cursors created for this connection
virtual TQ_ULLONG drv_lastInsertRowID()=0
virtual bool drv_isDatabaseUsed() const
Definition connection.h:934
virtual Cursor * prepareQuery(const TQString &statement, uint cursor_options=0)=0
virtual bool drv_createDatabase(const TQString &dbName=TQString())=0
virtual bool drv_connect(KexiDB::ServerVersionInfo &version)=0
virtual bool drv_disconnect()=0
virtual PreparedStatement::Ptr prepareStatement(PreparedStatement::StatementType type, FieldList &fields)=0
virtual bool drv_dropDatabase(const TQString &dbName=TQString())=0
virtual tristate drv_changeFieldProperty(TableSchema &table, Field &field, const TQString &propertyName, const TQVariant &value)
virtual bool drv_closeDatabase()=0
virtual bool drv_containsTable(const TQString &tableName)=0
Driver * driver() const
Definition connection.h:78
int resultCount(const TQString &sql)
virtual bool drv_useDatabase(const TQString &dbName=TQString(), bool *cancelled=0, MessageHandler *msgHandler=0)=0
Provides database cursor functionality.
Definition cursor.h:67
A set of storable database properties.
Contains database version information about a Kexi-compatible database. The version is stored as inte...
Definition global.h:109
Generic database abstraction.
Definition driver.h:67
Meta-data for a field.
Definition field.h:73
StatementType
Defines type of the prepared statement.
KexiDB::QuerySchema provides information about database query.
provides data for single edited database row KexiDB::RowEditBuffer provides data for single edited ro...
Contains version information about a database backend.
Definition global.h:126
Helper class for using inside methods for given connection.
This class encapsulates transaction handle.
Definition transaction.h:60
3-state logical type with three values: true, false and cancelled and convenient operators.
Definition tristate.h:100
High-level database connectivity library with database backend drivers.
TQValueVector< TQVariant > RowData
structure for storing single record with type information
Definition connection.h:47
KEXI_DB_EXPORT bool deleteRow(Connection &conn, TableSchema *table, const TQString &keyname, const TQString &keyval)
for convenience
@ AnyObjectType
helper
Definition global.h:149