[196] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
| 3 | ** Implementation of QSqlDatabase class
|
---|
| 4 | **
|
---|
| 5 | ** Created : 2000-11-03
|
---|
| 6 | **
|
---|
| 7 | ** Copyright (C) 2000-2007 Trolltech ASA. All rights reserved.
|
---|
| 8 | **
|
---|
| 9 | ** This file is part of the sql module of the Qt GUI Toolkit.
|
---|
| 10 | **
|
---|
| 11 | ** This file may be distributed under the terms of the Q Public License
|
---|
| 12 | ** as defined by Trolltech ASA of Norway and appearing in the file
|
---|
| 13 | ** LICENSE.QPL included in the packaging of this file.
|
---|
| 14 | **
|
---|
| 15 | ** This file may be distributed and/or modified under the terms of the
|
---|
| 16 | ** GNU General Public License version 2 as published by the Free Software
|
---|
| 17 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 18 | ** packaging of this file.
|
---|
| 19 | **
|
---|
| 20 | ** Licensees holding valid Qt Enterprise Edition licenses may use this
|
---|
| 21 | ** file in accordance with the Qt Commercial License Agreement provided
|
---|
| 22 | ** with the Software.
|
---|
| 23 | **
|
---|
| 24 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
---|
| 25 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
---|
| 26 | **
|
---|
| 27 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
---|
| 28 | ** information about Qt Commercial License Agreements.
|
---|
| 29 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
---|
| 30 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
---|
| 31 | **
|
---|
| 32 | ** Contact info@trolltech.com if any conditions of this licensing are
|
---|
| 33 | ** not clear to you.
|
---|
| 34 | **
|
---|
| 35 | **********************************************************************/
|
---|
| 36 |
|
---|
| 37 | #include "qsqldatabase.h"
|
---|
| 38 |
|
---|
| 39 | #ifndef QT_NO_SQL
|
---|
| 40 |
|
---|
| 41 | #ifdef Q_OS_WIN32
|
---|
| 42 | // Conflicting declarations of LPCBYTE in sqlfront.h and winscard.h
|
---|
| 43 | #define _WINSCARD_H_
|
---|
| 44 | #endif
|
---|
| 45 |
|
---|
| 46 | #ifdef QT_SQL_POSTGRES
|
---|
| 47 | #include "drivers/psql/qsql_psql.h"
|
---|
| 48 | #endif
|
---|
| 49 | #ifdef QT_SQL_MYSQL
|
---|
| 50 | #include "drivers/mysql/qsql_mysql.h"
|
---|
| 51 | #endif
|
---|
| 52 | #ifdef QT_SQL_ODBC
|
---|
| 53 | #include "drivers/odbc/qsql_odbc.h"
|
---|
| 54 | #endif
|
---|
| 55 | #ifdef QT_SQL_OCI
|
---|
| 56 | #include "drivers/oci/qsql_oci.h"
|
---|
| 57 | #endif
|
---|
| 58 | #ifdef QT_SQL_TDS
|
---|
| 59 | #include "drivers/tds/qsql_tds.h"
|
---|
| 60 | #endif
|
---|
| 61 | #ifdef QT_SQL_DB2
|
---|
| 62 | #include "drivers/db2/qsql_db2.h"
|
---|
| 63 | #endif
|
---|
| 64 | #ifdef QT_SQL_SQLITE
|
---|
| 65 | #include "drivers/sqlite/qsql_sqlite.h"
|
---|
| 66 | #endif
|
---|
| 67 | #ifdef QT_SQL_IBASE
|
---|
| 68 | #include "drivers/ibase/qsql_ibase.h"
|
---|
| 69 | #endif
|
---|
| 70 |
|
---|
| 71 | #include "qapplication.h"
|
---|
| 72 | #include "qsqlresult.h"
|
---|
| 73 | #include "qsqldriver.h"
|
---|
| 74 | #include "qsqldriverinterface_p.h"
|
---|
| 75 | #include <private/qpluginmanager_p.h>
|
---|
| 76 | #include <private/qsqlextension_p.h>
|
---|
| 77 | #include "qobject.h"
|
---|
| 78 | #include "qguardedptr.h"
|
---|
| 79 | #include "qcleanuphandler.h"
|
---|
| 80 | #include "qdict.h"
|
---|
| 81 | #include <stdlib.h>
|
---|
| 82 |
|
---|
| 83 | QT_STATIC_CONST_IMPL char * const QSqlDatabase::defaultConnection = "qt_sql_default_connection";
|
---|
| 84 |
|
---|
| 85 | QPtrDict<QSqlDriverExtension> *qt_driver_extension_dict = 0;
|
---|
| 86 | QPtrDict<QSqlOpenExtension> *qt_open_extension_dict = 0;
|
---|
| 87 |
|
---|
| 88 | static QSingleCleanupHandler< QPtrDict<QSqlDriverExtension> > qt_driver_ext_cleanup;
|
---|
| 89 | static QSingleCleanupHandler< QPtrDict<QSqlOpenExtension> > qt_open_ext_cleanup;
|
---|
| 90 |
|
---|
| 91 | Q_EXPORT QPtrDict<QSqlDriverExtension> *qSqlDriverExtDict()
|
---|
| 92 | {
|
---|
| 93 | if ( !qt_driver_extension_dict ) {
|
---|
| 94 | qt_driver_extension_dict = new QPtrDict<QSqlDriverExtension>;
|
---|
| 95 | qt_driver_ext_cleanup.set( &qt_driver_extension_dict );
|
---|
| 96 | }
|
---|
| 97 | return qt_driver_extension_dict;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | Q_EXPORT QPtrDict<QSqlOpenExtension> *qSqlOpenExtDict()
|
---|
| 101 | {
|
---|
| 102 | if ( !qt_open_extension_dict ) {
|
---|
| 103 | qt_open_extension_dict = new QPtrDict<QSqlOpenExtension>;
|
---|
| 104 | qt_open_ext_cleanup.set( &qt_open_extension_dict );
|
---|
| 105 | }
|
---|
| 106 | return qt_open_extension_dict;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | class QNullResult : public QSqlResult
|
---|
| 110 | {
|
---|
| 111 | public:
|
---|
| 112 | QNullResult(const QSqlDriver* d): QSqlResult(d){}
|
---|
| 113 | ~QNullResult(){}
|
---|
| 114 | protected:
|
---|
| 115 | QVariant data( int ) { return QVariant(); }
|
---|
| 116 | bool reset ( const QString& sqlquery ) { QString s(sqlquery); return FALSE; }
|
---|
| 117 | bool fetch( int i ) { i = i; return FALSE; }
|
---|
| 118 | bool fetchFirst() { return FALSE; }
|
---|
| 119 | bool fetchLast() { return FALSE; }
|
---|
| 120 | bool isNull( int ) {return FALSE; }
|
---|
| 121 | QSqlRecord record() {return QSqlRecord();}
|
---|
| 122 | int size() {return 0;}
|
---|
| 123 | int numRowsAffected() {return 0;}
|
---|
| 124 | };
|
---|
| 125 |
|
---|
| 126 | class QNullDriver : public QSqlDriver
|
---|
| 127 | {
|
---|
| 128 | public:
|
---|
| 129 | QNullDriver(): QSqlDriver(){}
|
---|
| 130 | ~QNullDriver(){}
|
---|
| 131 | bool hasFeature( DriverFeature /* f */ ) const { return FALSE; } ;
|
---|
| 132 | bool open( const QString & ,
|
---|
| 133 | const QString & ,
|
---|
| 134 | const QString & ,
|
---|
| 135 | const QString &,
|
---|
| 136 | int ) {
|
---|
| 137 | return FALSE;
|
---|
| 138 | }
|
---|
| 139 | void close() {}
|
---|
| 140 | QSqlQuery createQuery() const { return QSqlQuery( new QNullResult(this) ); }
|
---|
| 141 | };
|
---|
| 142 |
|
---|
| 143 | typedef QDict<QSqlDriverCreatorBase> QDriverDict;
|
---|
| 144 |
|
---|
| 145 | class QSqlDatabaseManager : public QObject
|
---|
| 146 | {
|
---|
| 147 | public:
|
---|
| 148 | QSqlDatabaseManager( QObject * parent = 0, const char * name = 0 );
|
---|
| 149 | ~QSqlDatabaseManager();
|
---|
| 150 | static QSqlDatabase* database( const QString& name, bool open );
|
---|
| 151 | static QSqlDatabase* addDatabase( QSqlDatabase* db, const QString & name );
|
---|
| 152 | static void removeDatabase( const QString& name );
|
---|
| 153 | static void removeDatabase( QSqlDatabase* db );
|
---|
| 154 | static bool contains( const QString& name );
|
---|
| 155 | static QDriverDict* driverDict();
|
---|
| 156 |
|
---|
| 157 | protected:
|
---|
| 158 | static QSqlDatabaseManager* instance();
|
---|
| 159 | QDict< QSqlDatabase > dbDict;
|
---|
| 160 | QDriverDict* drDict;
|
---|
| 161 | };
|
---|
| 162 |
|
---|
| 163 | /*!
|
---|
| 164 | Constructs an SQL database manager.
|
---|
| 165 | */
|
---|
| 166 |
|
---|
| 167 | QSqlDatabaseManager::QSqlDatabaseManager( QObject * parent, const char * name )
|
---|
| 168 | : QObject( parent, name ), dbDict( 1 ), drDict( 0 )
|
---|
| 169 | {
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | /*!
|
---|
| 173 | Destroys the object and frees any allocated resources. All open
|
---|
| 174 | database connections are closed. All database connections are
|
---|
| 175 | deleted.
|
---|
| 176 | */
|
---|
| 177 |
|
---|
| 178 | QSqlDatabaseManager::~QSqlDatabaseManager()
|
---|
| 179 | {
|
---|
| 180 | QDictIterator< QSqlDatabase > it( dbDict );
|
---|
| 181 | while ( it.current() ) {
|
---|
| 182 | it.current()->close();
|
---|
| 183 | delete it.current();
|
---|
| 184 | ++it;
|
---|
| 185 | }
|
---|
| 186 | delete drDict;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | /*!
|
---|
| 190 | \internal
|
---|
| 191 | */
|
---|
| 192 | QDriverDict* QSqlDatabaseManager::driverDict()
|
---|
| 193 | {
|
---|
| 194 | QSqlDatabaseManager* sqlConnection = instance();
|
---|
| 195 | if ( !sqlConnection->drDict ) {
|
---|
| 196 | sqlConnection->drDict = new QDriverDict();
|
---|
| 197 | sqlConnection->drDict->setAutoDelete( TRUE );
|
---|
| 198 | }
|
---|
| 199 | return sqlConnection->drDict;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 |
|
---|
| 203 | /*!
|
---|
| 204 | \internal
|
---|
| 205 | */
|
---|
| 206 | QSqlDatabaseManager* QSqlDatabaseManager::instance()
|
---|
| 207 | {
|
---|
| 208 | static QGuardedPtr<QSqlDatabaseManager> sqlConnection = 0;
|
---|
| 209 | if ( !sqlConnection ) {
|
---|
| 210 | if( qApp == 0 ){
|
---|
| 211 | qFatal( "QSqlDatabaseManager: A QApplication object has to be "
|
---|
| 212 | "instantiated in order to use the SQL module." );
|
---|
| 213 | return 0;
|
---|
| 214 | }
|
---|
| 215 | sqlConnection = new QSqlDatabaseManager( qApp, "database manager" );
|
---|
| 216 | }
|
---|
| 217 | return (QSqlDatabaseManager*)sqlConnection;
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | /*!
|
---|
| 221 | Returns the database connection called \a name. If \a open is
|
---|
| 222 | TRUE, the database connection is opened. If \a name does not exist
|
---|
| 223 | in the list of managed databases, 0 is returned.
|
---|
| 224 | */
|
---|
| 225 |
|
---|
| 226 | QSqlDatabase* QSqlDatabaseManager::database( const QString& name, bool open )
|
---|
| 227 | {
|
---|
| 228 | if ( !contains( name ) )
|
---|
| 229 | return 0;
|
---|
| 230 |
|
---|
| 231 | QSqlDatabaseManager* sqlConnection = instance();
|
---|
| 232 | QSqlDatabase* db = sqlConnection->dbDict.find( name );
|
---|
| 233 | if ( db && !db->isOpen() && open ) {
|
---|
| 234 | db->open();
|
---|
| 235 | #ifdef QT_CHECK_RANGE
|
---|
| 236 | if ( !db->isOpen() )
|
---|
| 237 | qWarning("QSqlDatabaseManager::database: unable to open database: " + db->lastError().databaseText() + ": " + db->lastError().driverText() );
|
---|
| 238 | #endif
|
---|
| 239 | }
|
---|
| 240 | return db;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | /*!
|
---|
| 244 | Returns TRUE if the list of database connections contains \a name;
|
---|
| 245 | otherwise returns FALSE.
|
---|
| 246 | */
|
---|
| 247 |
|
---|
| 248 | bool QSqlDatabaseManager::contains( const QString& name )
|
---|
| 249 | {
|
---|
| 250 | QSqlDatabaseManager* sqlConnection = instance();
|
---|
| 251 | QSqlDatabase* db = sqlConnection->dbDict.find( name );
|
---|
| 252 | if ( db )
|
---|
| 253 | return TRUE;
|
---|
| 254 | return FALSE;
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 |
|
---|
| 258 | /*!
|
---|
| 259 | Adds a database to the SQL connection manager. The database
|
---|
| 260 | connection is referred to by \a name. The newly added database
|
---|
| 261 | connection is returned. This function will only return 0 if it is
|
---|
| 262 | called \e before a QApplication object has been instantiated. Use
|
---|
| 263 | the output of drivers() to determine whether a particular driver
|
---|
| 264 | is available or not.
|
---|
| 265 |
|
---|
| 266 | The returned QSqlDatabase object is owned by the framework and
|
---|
| 267 | must not be deleted. If you want to explicitly remove the connection,
|
---|
| 268 | use removeDatabase().
|
---|
| 269 |
|
---|
| 270 | \sa QSqlDatabase database()
|
---|
| 271 | */
|
---|
| 272 |
|
---|
| 273 | QSqlDatabase* QSqlDatabaseManager::addDatabase( QSqlDatabase* db, const QString & name )
|
---|
| 274 | {
|
---|
| 275 | QSqlDatabaseManager* sqlConnection = instance();
|
---|
| 276 | if( sqlConnection == 0 )
|
---|
| 277 | return 0;
|
---|
| 278 | if ( contains( name ) )
|
---|
| 279 | sqlConnection->removeDatabase( name );
|
---|
| 280 | sqlConnection->dbDict.insert( name, db );
|
---|
| 281 | return db;
|
---|
| 282 | }
|
---|
| 283 |
|
---|
| 284 | /*!
|
---|
| 285 | Removes the database connection \a name from the SQL connection
|
---|
| 286 | manager.
|
---|
| 287 |
|
---|
| 288 | \warning There should be no open queries on the database
|
---|
| 289 | connection when this function is called, otherwise a resource leak
|
---|
| 290 | will occur.
|
---|
| 291 | */
|
---|
| 292 |
|
---|
| 293 | void QSqlDatabaseManager::removeDatabase( const QString& name )
|
---|
| 294 | {
|
---|
| 295 | QSqlDatabaseManager* sqlConnection = instance();
|
---|
| 296 | sqlConnection->dbDict.setAutoDelete( TRUE );
|
---|
| 297 | sqlConnection->dbDict.remove( name );
|
---|
| 298 | sqlConnection->dbDict.setAutoDelete( FALSE );
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 |
|
---|
| 302 | /*!
|
---|
| 303 | Removes the database connection \a db from the SQL connection
|
---|
| 304 | manager. The QSqlDatabase object is destroyed when it is removed
|
---|
| 305 | from the manager.
|
---|
| 306 |
|
---|
| 307 | \warning The \a db pointer is not valid after this function has
|
---|
| 308 | been called.
|
---|
| 309 | */
|
---|
| 310 |
|
---|
| 311 | void QSqlDatabaseManager::removeDatabase( QSqlDatabase* db )
|
---|
| 312 | {
|
---|
| 313 | QSqlDatabaseManager* sqlConnection = instance();
|
---|
| 314 | if ( !sqlConnection )
|
---|
| 315 | return;
|
---|
| 316 | QDictIterator< QSqlDatabase > it( sqlConnection->dbDict );
|
---|
| 317 | while ( it.current() ) {
|
---|
| 318 | if ( it.current() == db ) {
|
---|
| 319 | sqlConnection->dbDict.remove( it.currentKey() );
|
---|
| 320 | db->close();
|
---|
| 321 | delete db;
|
---|
| 322 | break;
|
---|
| 323 | }
|
---|
| 324 | ++it;
|
---|
| 325 | }
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | class QSqlDatabasePrivate
|
---|
| 329 | {
|
---|
| 330 | public:
|
---|
| 331 | QSqlDatabasePrivate():
|
---|
| 332 | driver(0),
|
---|
| 333 | #ifndef QT_NO_COMPONENT
|
---|
| 334 | plugIns(0),
|
---|
| 335 | #endif
|
---|
| 336 | port(-1) {}
|
---|
| 337 | ~QSqlDatabasePrivate()
|
---|
| 338 | {
|
---|
| 339 | }
|
---|
| 340 | QSqlDriver* driver;
|
---|
| 341 | #ifndef QT_NO_COMPONENT
|
---|
| 342 | QPluginManager<QSqlDriverFactoryInterface> *plugIns;
|
---|
| 343 | #endif
|
---|
| 344 | QString dbname;
|
---|
| 345 | QString uname;
|
---|
| 346 | QString pword;
|
---|
| 347 | QString hname;
|
---|
| 348 | QString drvName;
|
---|
| 349 | int port;
|
---|
| 350 | QString connOptions;
|
---|
| 351 | };
|
---|
| 352 |
|
---|
| 353 | /*!
|
---|
| 354 | \class QSqlDatabase qsqldatabase.h
|
---|
| 355 | \brief The QSqlDatabase class is used to create SQL database
|
---|
| 356 | connections and to provide transaction handling.
|
---|
| 357 |
|
---|
| 358 | \ingroup database
|
---|
| 359 | \mainclass
|
---|
| 360 | \module sql
|
---|
| 361 |
|
---|
| 362 | Note that transaction handling is not supported by every SQL
|
---|
| 363 | database. You can find out whether transactions are supported
|
---|
| 364 | using QSqlDriver::hasFeature().
|
---|
| 365 |
|
---|
| 366 | The QSqlDatabase class provides an abstract interface for
|
---|
| 367 | accessing many types of database backends. Database-specific
|
---|
| 368 | drivers are used internally to actually access and manipulate
|
---|
| 369 | data, (see QSqlDriver). Result set objects provide the interface
|
---|
| 370 | for executing and manipulating SQL queries (see QSqlQuery).
|
---|
| 371 | */
|
---|
| 372 |
|
---|
| 373 | /*!
|
---|
| 374 | Adds a database to the list of database connections using the
|
---|
| 375 | driver \a type and the connection name \a connectionName.
|
---|
| 376 |
|
---|
| 377 | The database connection is referred to by \a connectionName. The
|
---|
| 378 | newly added database connection is returned. This pointer is owned
|
---|
| 379 | by QSqlDatabase and will be deleted on program exit or when
|
---|
| 380 | removeDatabase() is called.
|
---|
| 381 |
|
---|
| 382 | If \a connectionName is not specified, the newly added database
|
---|
| 383 | connection becomes the default database connection for the
|
---|
| 384 | application, and subsequent calls to database() (without a
|
---|
| 385 | database name parameter) will return a pointer to it. If \a
|
---|
| 386 | connectionName is given, use \link QSqlDatabase::database()
|
---|
| 387 | database(connectionName)\endlink to retrieve a pointer to the
|
---|
| 388 | database connection.
|
---|
| 389 |
|
---|
| 390 | \warning If you add a database with the same name as an
|
---|
| 391 | existing database, the new database will replace the old one.
|
---|
| 392 | This will happen automatically if you call this function more
|
---|
| 393 | than once without specifying \a connectionName.
|
---|
| 394 |
|
---|
| 395 | \sa database() removeDatabase()
|
---|
| 396 | */
|
---|
| 397 | QSqlDatabase* QSqlDatabase::addDatabase( const QString& type, const QString& connectionName )
|
---|
| 398 | {
|
---|
| 399 | return QSqlDatabaseManager::addDatabase( new QSqlDatabase( type, connectionName ), connectionName );
|
---|
| 400 | }
|
---|
| 401 |
|
---|
| 402 | /*!
|
---|
| 403 | Returns the database connection called \a connectionName. The
|
---|
| 404 | database connection must have been previously added with
|
---|
| 405 | addDatabase(). If \a open is TRUE (the default) and the database
|
---|
| 406 | connection is not already open it is opened now. If no \a
|
---|
| 407 | connectionName is specified the default connection is used. If \a
|
---|
| 408 | connectionName does not exist in the list of databases, 0 is
|
---|
| 409 | returned. The pointer returned is owned by QSqlDatabase and should
|
---|
| 410 | \e not be deleted.
|
---|
| 411 |
|
---|
| 412 | \warning There are restrictions on the use of database connections
|
---|
| 413 | in threaded applications. Please see the \link threads.html#threads-sql
|
---|
| 414 | Thread Support in Qt\endlink document for more information about
|
---|
| 415 | threading and SQL databases.
|
---|
| 416 | */
|
---|
| 417 |
|
---|
| 418 | QSqlDatabase* QSqlDatabase::database( const QString& connectionName, bool open )
|
---|
| 419 | {
|
---|
| 420 | return QSqlDatabaseManager::database( connectionName, open );
|
---|
| 421 | }
|
---|
| 422 |
|
---|
| 423 | /*!
|
---|
| 424 | Removes the database connection \a connectionName from the list of
|
---|
| 425 | database connections.
|
---|
| 426 |
|
---|
| 427 | \warning There should be no open queries on the database
|
---|
| 428 | connection when this function is called, otherwise a resource leak
|
---|
| 429 | will occur.
|
---|
| 430 | */
|
---|
| 431 |
|
---|
| 432 | void QSqlDatabase::removeDatabase( const QString& connectionName )
|
---|
| 433 | {
|
---|
| 434 | QSqlDatabaseManager::removeDatabase( connectionName );
|
---|
| 435 | }
|
---|
| 436 |
|
---|
| 437 | /*!
|
---|
| 438 | \overload
|
---|
| 439 |
|
---|
| 440 | Removes the database connection \a db from the list of database
|
---|
| 441 | connections. The QSqlDatabase object is destroyed when it is removed
|
---|
| 442 | from the list.
|
---|
| 443 |
|
---|
| 444 | \warning The \a db pointer is not valid after this function has
|
---|
| 445 | been called. There should be no open queries on the database
|
---|
| 446 | connection when this function is called, otherwise a resource leak
|
---|
| 447 | will occur.
|
---|
| 448 | */
|
---|
| 449 |
|
---|
| 450 | void QSqlDatabase::removeDatabase( QSqlDatabase* db )
|
---|
| 451 | {
|
---|
| 452 | QSqlDatabaseManager::removeDatabase( db );
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 | /*!
|
---|
| 456 | Returns a list of all the available database drivers.
|
---|
| 457 |
|
---|
| 458 | Note that if you want to iterate over the list, you should iterate
|
---|
| 459 | over a copy, e.g.
|
---|
| 460 | \code
|
---|
| 461 | QStringList list = QSqlDatabase::drivers();
|
---|
| 462 | QStringList::Iterator it = list.begin();
|
---|
| 463 | while( it != list.end() ) {
|
---|
| 464 | myProcessing( *it );
|
---|
| 465 | ++it;
|
---|
| 466 | }
|
---|
| 467 | \endcode
|
---|
| 468 | */
|
---|
| 469 |
|
---|
| 470 | QStringList QSqlDatabase::drivers()
|
---|
| 471 | {
|
---|
| 472 | QStringList l;
|
---|
| 473 |
|
---|
| 474 | #ifndef QT_NO_COMPONENT
|
---|
| 475 | QPluginManager<QSqlDriverFactoryInterface> *plugIns;
|
---|
| 476 | plugIns = new QPluginManager<QSqlDriverFactoryInterface>( IID_QSqlDriverFactory, QApplication::libraryPaths(), "/sqldrivers" );
|
---|
| 477 |
|
---|
| 478 | l = plugIns->featureList();
|
---|
| 479 | delete plugIns;
|
---|
| 480 | #endif
|
---|
| 481 |
|
---|
| 482 | QDictIterator<QSqlDriverCreatorBase> itd( *QSqlDatabaseManager::driverDict() );
|
---|
| 483 | while ( itd.current() ) {
|
---|
| 484 | if ( !l.contains( itd.currentKey() ) )
|
---|
| 485 | l << itd.currentKey();
|
---|
| 486 | ++itd;
|
---|
| 487 | }
|
---|
| 488 |
|
---|
| 489 | #ifdef QT_SQL_POSTGRES
|
---|
| 490 | if ( !l.contains( "QPSQL7" ) )
|
---|
| 491 | l << "QPSQL7";
|
---|
| 492 | #endif
|
---|
| 493 | #ifdef QT_SQL_MYSQL
|
---|
| 494 | if ( !l.contains( "QMYSQL3" ) )
|
---|
| 495 | l << "QMYSQL3";
|
---|
| 496 | #endif
|
---|
| 497 | #ifdef QT_SQL_ODBC
|
---|
| 498 | if ( !l.contains( "QODBC3" ) )
|
---|
| 499 | l << "QODBC3";
|
---|
| 500 | #endif
|
---|
| 501 | #ifdef QT_SQL_OCI
|
---|
| 502 | if ( !l.contains( "QOCI8" ) )
|
---|
| 503 | l << "QOCI8";
|
---|
| 504 | #endif
|
---|
| 505 | #ifdef QT_SQL_TDS
|
---|
| 506 | if ( !l.contains( "QTDS7" ) )
|
---|
| 507 | l << "QTDS7";
|
---|
| 508 | #endif
|
---|
| 509 | #ifdef QT_SQL_DB2
|
---|
| 510 | if ( !l.contains( "QDB2" ) )
|
---|
| 511 | l << "QDB2";
|
---|
| 512 | #endif
|
---|
| 513 | #ifdef QT_SQL_SQLITE
|
---|
| 514 | if ( !l.contains( "QSQLITE" ) )
|
---|
| 515 | l << "QSQLITE";
|
---|
| 516 | #endif
|
---|
| 517 | #ifdef QT_SQL_IBASE
|
---|
| 518 | if ( !l.contains( "QIBASE" ) )
|
---|
| 519 | l << "QIBASE";
|
---|
| 520 | #endif
|
---|
| 521 |
|
---|
| 522 | return l;
|
---|
| 523 | }
|
---|
| 524 |
|
---|
| 525 | /*!
|
---|
| 526 | This function registers a new SQL driver called \a name, within
|
---|
| 527 | the SQL framework. This is useful if you have a custom SQL driver
|
---|
| 528 | and don't want to compile it as a plugin.
|
---|
| 529 |
|
---|
| 530 | Example usage:
|
---|
| 531 |
|
---|
| 532 | \code
|
---|
| 533 | QSqlDatabase::registerSqlDriver( "MYDRIVER", new QSqlDriverCreator<MyDatabaseDriver> );
|
---|
| 534 | QSqlDatabase* db = QSqlDatabase::addDatabase( "MYDRIVER" );
|
---|
| 535 | ...
|
---|
| 536 | \endcode
|
---|
| 537 |
|
---|
| 538 | \warning The framework takes ownership of the \a creator pointer,
|
---|
| 539 | so it should not be deleted.
|
---|
| 540 | */
|
---|
| 541 | void QSqlDatabase::registerSqlDriver( const QString& name, const QSqlDriverCreatorBase* creator )
|
---|
| 542 | {
|
---|
| 543 | QSqlDatabaseManager::driverDict()->remove( name );
|
---|
| 544 | if ( creator )
|
---|
| 545 | QSqlDatabaseManager::driverDict()->insert( name, creator );
|
---|
| 546 | }
|
---|
| 547 |
|
---|
| 548 | /*!
|
---|
| 549 | Returns TRUE if the list of database connections contains \a
|
---|
| 550 | connectionName; otherwise returns FALSE.
|
---|
| 551 | */
|
---|
| 552 |
|
---|
| 553 | bool QSqlDatabase::contains( const QString& connectionName )
|
---|
| 554 | {
|
---|
| 555 | return QSqlDatabaseManager::contains( connectionName );
|
---|
| 556 | }
|
---|
| 557 |
|
---|
| 558 |
|
---|
| 559 | /*!
|
---|
| 560 | Creates a QSqlDatabase connection called \a name that uses the
|
---|
| 561 | driver referred to by \a type, with the parent \a parent and the
|
---|
| 562 | object name \a objname. If the \a type is not recognized, the
|
---|
| 563 | database connection will have no functionality.
|
---|
| 564 |
|
---|
| 565 | The currently available drivers are:
|
---|
| 566 |
|
---|
| 567 | \table
|
---|
| 568 | \header \i Driver Type \i Description
|
---|
| 569 | \row \i QODBC3 \i ODBC Driver (includes Microsoft SQL Server)
|
---|
| 570 | \row \i QOCI8 \i Oracle Call Interface Driver
|
---|
| 571 | \row \i QPSQL7 \i PostgreSQL v6.x and v7.x Driver
|
---|
| 572 | \row \i QTDS7 \i Sybase Adaptive Server
|
---|
| 573 | \row \i QMYSQL3 \i MySQL Driver
|
---|
| 574 | \row \i QDB2 \i IBM DB2, v7.1 and higher
|
---|
| 575 | \row \i QSQLITE \i SQLite Driver
|
---|
| 576 | \row \i QIBASE \i Borland Interbase Driver
|
---|
| 577 | \endtable
|
---|
| 578 |
|
---|
| 579 | Additional third party drivers, including your own custom drivers,
|
---|
| 580 | can be loaded dynamically.
|
---|
| 581 |
|
---|
| 582 | \sa registerSqlDriver()
|
---|
| 583 | */
|
---|
| 584 |
|
---|
| 585 | QSqlDatabase::QSqlDatabase( const QString& type, const QString& name, QObject * parent, const char * objname )
|
---|
| 586 | : QObject( parent, objname )
|
---|
| 587 | {
|
---|
| 588 | init( type, name );
|
---|
| 589 | }
|
---|
| 590 |
|
---|
| 591 |
|
---|
| 592 | /*!
|
---|
| 593 | \overload
|
---|
| 594 |
|
---|
| 595 | Creates a database connection using the driver \a driver, with
|
---|
| 596 | the parent \a parent and the object name \a objname.
|
---|
| 597 |
|
---|
| 598 | \warning The framework takes ownership of the \a driver pointer,
|
---|
| 599 | so it should not be deleted.
|
---|
| 600 | */
|
---|
| 601 |
|
---|
| 602 | QSqlDatabase::QSqlDatabase( QSqlDriver* driver, QObject * parent, const char * objname )
|
---|
| 603 | : QObject( parent, objname )
|
---|
| 604 | {
|
---|
| 605 | d = new QSqlDatabasePrivate();
|
---|
| 606 | d->driver = driver;
|
---|
| 607 | }
|
---|
| 608 |
|
---|
| 609 | /*!
|
---|
| 610 | \internal
|
---|
| 611 |
|
---|
| 612 | Create the actual driver instance \a type.
|
---|
| 613 | */
|
---|
| 614 |
|
---|
| 615 | void QSqlDatabase::init( const QString& type, const QString& )
|
---|
| 616 | {
|
---|
| 617 | d = new QSqlDatabasePrivate();
|
---|
| 618 | d->drvName = type;
|
---|
| 619 |
|
---|
| 620 | if ( !d->driver ) {
|
---|
| 621 |
|
---|
| 622 | #ifdef QT_SQL_POSTGRES
|
---|
| 623 | if ( type == "QPSQL7" )
|
---|
| 624 | d->driver = new QPSQLDriver();
|
---|
| 625 | #endif
|
---|
| 626 |
|
---|
| 627 | #ifdef QT_SQL_MYSQL
|
---|
| 628 | if ( type == "QMYSQL3" )
|
---|
| 629 | d->driver = new QMYSQLDriver();
|
---|
| 630 | #endif
|
---|
| 631 |
|
---|
| 632 | #ifdef QT_SQL_ODBC
|
---|
| 633 | if ( type == "QODBC3" )
|
---|
| 634 | d->driver = new QODBCDriver();
|
---|
| 635 | #endif
|
---|
| 636 |
|
---|
| 637 | #ifdef QT_SQL_OCI
|
---|
| 638 | if ( type == "QOCI8" )
|
---|
| 639 | d->driver = new QOCIDriver();
|
---|
| 640 | #endif
|
---|
| 641 |
|
---|
| 642 | #ifdef QT_SQL_TDS
|
---|
| 643 | if ( type == "QTDS7" )
|
---|
| 644 | d->driver = new QTDSDriver();
|
---|
| 645 | #endif
|
---|
| 646 |
|
---|
| 647 | #ifdef QT_SQL_DB2
|
---|
| 648 | if ( type == "QDB2" )
|
---|
| 649 | d->driver = new QDB2Driver();
|
---|
| 650 | #endif
|
---|
| 651 |
|
---|
| 652 | #ifdef QT_SQL_SQLITE
|
---|
| 653 | if ( type == "QSQLITE" )
|
---|
| 654 | d->driver = new QSQLiteDriver();
|
---|
| 655 | #endif
|
---|
| 656 |
|
---|
| 657 | #ifdef QT_SQL_IBASE
|
---|
| 658 | if ( type == "QIBASE" )
|
---|
| 659 | d->driver = new QIBaseDriver();
|
---|
| 660 | #endif
|
---|
| 661 |
|
---|
| 662 | }
|
---|
| 663 |
|
---|
| 664 | if ( !d->driver ) {
|
---|
| 665 | QDictIterator<QSqlDriverCreatorBase> it( *QSqlDatabaseManager::driverDict() );
|
---|
| 666 | while ( it.current() && !d->driver ) {
|
---|
| 667 | if ( type == it.currentKey() ) {
|
---|
| 668 | d->driver = it.current()->createObject();
|
---|
| 669 | }
|
---|
| 670 | ++it;
|
---|
| 671 | }
|
---|
| 672 | }
|
---|
| 673 |
|
---|
| 674 | #ifndef QT_NO_COMPONENT
|
---|
| 675 | if ( !d->driver ) {
|
---|
| 676 | d->plugIns =
|
---|
| 677 | new QPluginManager<QSqlDriverFactoryInterface>( IID_QSqlDriverFactory, QApplication::libraryPaths(), "/sqldrivers" );
|
---|
| 678 |
|
---|
| 679 | QInterfacePtr<QSqlDriverFactoryInterface> iface = 0;
|
---|
| 680 | d->plugIns->queryInterface( type, &iface );
|
---|
| 681 | if( iface )
|
---|
| 682 | d->driver = iface->create( type );
|
---|
| 683 | }
|
---|
| 684 | #endif
|
---|
| 685 |
|
---|
| 686 | if ( !d->driver ) {
|
---|
| 687 | #ifdef QT_CHECK_RANGE
|
---|
| 688 | qWarning( "QSqlDatabase: %s driver not loaded", type.latin1() );
|
---|
| 689 | qWarning( "QSqlDatabase: available drivers: " + drivers().join(" ") );
|
---|
| 690 | #endif
|
---|
| 691 | d->driver = new QNullDriver();
|
---|
| 692 | d->driver->setLastError( QSqlError( "Driver not loaded", "Driver not loaded" ) );
|
---|
| 693 | }
|
---|
| 694 | }
|
---|
| 695 |
|
---|
| 696 | /*!
|
---|
| 697 | Destroys the object and frees any allocated resources.
|
---|
| 698 | */
|
---|
| 699 |
|
---|
| 700 | QSqlDatabase::~QSqlDatabase()
|
---|
| 701 | {
|
---|
| 702 | delete d->driver;
|
---|
| 703 | #ifndef QT_NO_COMPONENT
|
---|
| 704 | delete d->plugIns;
|
---|
| 705 | #endif
|
---|
| 706 | delete d;
|
---|
| 707 | }
|
---|
| 708 |
|
---|
| 709 | /*!
|
---|
| 710 | Executes a SQL statement (e.g. an \c INSERT, \c UPDATE or \c
|
---|
| 711 | DELETE statement) on the database, and returns a QSqlQuery object.
|
---|
| 712 | Use lastError() to retrieve error information. If \a query is
|
---|
| 713 | QString::null, an empty, invalid query is returned and lastError()
|
---|
| 714 | is not affected.
|
---|
| 715 |
|
---|
| 716 | \sa QSqlQuery lastError()
|
---|
| 717 | */
|
---|
| 718 |
|
---|
| 719 | QSqlQuery QSqlDatabase::exec( const QString & query ) const
|
---|
| 720 | {
|
---|
| 721 | QSqlQuery r = d->driver->createQuery();
|
---|
| 722 | if ( !query.isNull() ) {
|
---|
| 723 | r.exec( query );
|
---|
| 724 | d->driver->setLastError( r.lastError() );
|
---|
| 725 | }
|
---|
| 726 | return r;
|
---|
| 727 | }
|
---|
| 728 |
|
---|
| 729 | /*!
|
---|
| 730 | Opens the database connection using the current connection values.
|
---|
| 731 | Returns TRUE on success; otherwise returns FALSE. Error
|
---|
| 732 | information can be retrieved using the lastError() function.
|
---|
| 733 |
|
---|
| 734 | \sa lastError()
|
---|
| 735 | */
|
---|
| 736 |
|
---|
| 737 | bool QSqlDatabase::open()
|
---|
| 738 | {
|
---|
| 739 | return d->driver->open( d->dbname, d->uname, d->pword, d->hname,
|
---|
| 740 | d->port, d->connOptions );
|
---|
| 741 | }
|
---|
| 742 |
|
---|
| 743 | /*!
|
---|
| 744 | \overload
|
---|
| 745 |
|
---|
| 746 | Opens the database connection using the given \a user name and \a
|
---|
| 747 | password. Returns TRUE on success; otherwise returns FALSE. Error
|
---|
| 748 | information can be retrieved using the lastError() function.
|
---|
| 749 |
|
---|
| 750 | This function does not store the password it is given. Instead,
|
---|
| 751 | the password is passed directly to the driver for opening a
|
---|
| 752 | connection and is then discarded.
|
---|
| 753 |
|
---|
| 754 | \sa lastError()
|
---|
| 755 | */
|
---|
| 756 |
|
---|
| 757 | bool QSqlDatabase::open( const QString& user, const QString& password )
|
---|
| 758 | {
|
---|
| 759 | setUserName( user );
|
---|
| 760 | return d->driver->open( d->dbname, user, password, d->hname,
|
---|
| 761 | d->port, d->connOptions );
|
---|
| 762 | }
|
---|
| 763 |
|
---|
| 764 | /*!
|
---|
| 765 | Closes the database connection, freeing any resources acquired.
|
---|
| 766 |
|
---|
| 767 | \sa removeDatabase()
|
---|
| 768 | */
|
---|
| 769 |
|
---|
| 770 | void QSqlDatabase::close()
|
---|
| 771 | {
|
---|
| 772 | d->driver->close();
|
---|
| 773 | }
|
---|
| 774 |
|
---|
| 775 | /*!
|
---|
| 776 | Returns TRUE if the database connection is currently open;
|
---|
| 777 | otherwise returns FALSE.
|
---|
| 778 | */
|
---|
| 779 |
|
---|
| 780 | bool QSqlDatabase::isOpen() const
|
---|
| 781 | {
|
---|
| 782 | return d->driver->isOpen();
|
---|
| 783 | }
|
---|
| 784 |
|
---|
| 785 | /*!
|
---|
| 786 | Returns TRUE if there was an error opening the database
|
---|
| 787 | connection; otherwise returns FALSE. Error information can be
|
---|
| 788 | retrieved using the lastError() function.
|
---|
| 789 | */
|
---|
| 790 |
|
---|
| 791 | bool QSqlDatabase::isOpenError() const
|
---|
| 792 | {
|
---|
| 793 | return d->driver->isOpenError();
|
---|
| 794 | }
|
---|
| 795 |
|
---|
| 796 | /*!
|
---|
| 797 | Begins a transaction on the database if the driver supports
|
---|
| 798 | transactions. Returns TRUE if the operation succeeded; otherwise
|
---|
| 799 | returns FALSE.
|
---|
| 800 |
|
---|
| 801 | \sa QSqlDriver::hasFeature() commit() rollback()
|
---|
| 802 | */
|
---|
| 803 |
|
---|
| 804 | bool QSqlDatabase::transaction()
|
---|
| 805 | {
|
---|
| 806 | if ( !d->driver->hasFeature( QSqlDriver::Transactions ) )
|
---|
| 807 | return FALSE;
|
---|
| 808 | return d->driver->beginTransaction();
|
---|
| 809 | }
|
---|
| 810 |
|
---|
| 811 | /*!
|
---|
| 812 | Commits a transaction to the database if the driver supports
|
---|
| 813 | transactions. Returns TRUE if the operation succeeded; otherwise
|
---|
| 814 | returns FALSE.
|
---|
| 815 |
|
---|
| 816 | \sa QSqlDriver::hasFeature() rollback()
|
---|
| 817 | */
|
---|
| 818 |
|
---|
| 819 | bool QSqlDatabase::commit()
|
---|
| 820 | {
|
---|
| 821 | if ( !d->driver->hasFeature( QSqlDriver::Transactions ) )
|
---|
| 822 | return FALSE;
|
---|
| 823 | return d->driver->commitTransaction();
|
---|
| 824 | }
|
---|
| 825 |
|
---|
| 826 | /*!
|
---|
| 827 | Rolls a transaction back on the database if the driver supports
|
---|
| 828 | transactions. Returns TRUE if the operation succeeded; otherwise
|
---|
| 829 | returns FALSE.
|
---|
| 830 |
|
---|
| 831 | \sa QSqlDriver::hasFeature() commit() transaction()
|
---|
| 832 | */
|
---|
| 833 |
|
---|
| 834 | bool QSqlDatabase::rollback()
|
---|
| 835 | {
|
---|
| 836 | if ( !d->driver->hasFeature( QSqlDriver::Transactions ) )
|
---|
| 837 | return FALSE;
|
---|
| 838 | return d->driver->rollbackTransaction();
|
---|
| 839 | }
|
---|
| 840 |
|
---|
| 841 | /*!
|
---|
| 842 | \property QSqlDatabase::databaseName
|
---|
| 843 | \brief the name of the database
|
---|
| 844 |
|
---|
| 845 | Note that the database name is the TNS Service Name for the QOCI8
|
---|
| 846 | (Oracle) driver.
|
---|
| 847 |
|
---|
| 848 | For the QODBC3 driver it can either be a DSN, a DSN filename (the
|
---|
| 849 | file must have a \c .dsn extension), or a connection string. MS
|
---|
| 850 | Access users can for example use the following connection string
|
---|
| 851 | to open a \c .mdb file directly, instead of having to create a DSN
|
---|
| 852 | entry in the ODBC manager:
|
---|
| 853 |
|
---|
| 854 | \code
|
---|
| 855 | ...
|
---|
| 856 | db = QSqlDatabase::addDatabase( "QODBC3" );
|
---|
| 857 | db->setDatabaseName( "DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=myaccessfile.mdb" );
|
---|
| 858 | if ( db->open() ) {
|
---|
| 859 | // success!
|
---|
| 860 | }
|
---|
| 861 | ...
|
---|
| 862 | \endcode
|
---|
| 863 | ("FIL" is the required spelling in Microsoft's API.)
|
---|
| 864 |
|
---|
| 865 | There is no default value.
|
---|
| 866 | */
|
---|
| 867 |
|
---|
| 868 | void QSqlDatabase::setDatabaseName( const QString& name )
|
---|
| 869 | {
|
---|
| 870 | d->dbname = name;
|
---|
| 871 | }
|
---|
| 872 |
|
---|
| 873 | /*!
|
---|
| 874 | \property QSqlDatabase::userName
|
---|
| 875 | \brief the user name connected to the database
|
---|
| 876 |
|
---|
| 877 | There is no default value.
|
---|
| 878 | */
|
---|
| 879 |
|
---|
| 880 | void QSqlDatabase::setUserName( const QString& name )
|
---|
| 881 | {
|
---|
| 882 | d->uname = name;
|
---|
| 883 | }
|
---|
| 884 |
|
---|
| 885 | /*!
|
---|
| 886 | \property QSqlDatabase::password
|
---|
| 887 | \brief the password used to connect to the database
|
---|
| 888 |
|
---|
| 889 | There is no default value.
|
---|
| 890 |
|
---|
| 891 | \warning This function stores the password in plain text within
|
---|
| 892 | Qt. Use the open() call that takes a password as parameter to
|
---|
| 893 | avoid this behaviour.
|
---|
| 894 |
|
---|
| 895 | \sa open()
|
---|
| 896 | */
|
---|
| 897 |
|
---|
| 898 | void QSqlDatabase::setPassword( const QString& password )
|
---|
| 899 | {
|
---|
| 900 | d->pword = password;
|
---|
| 901 | }
|
---|
| 902 |
|
---|
| 903 | /*!
|
---|
| 904 | \property QSqlDatabase::hostName
|
---|
| 905 | \brief the host name where the database resides
|
---|
| 906 |
|
---|
| 907 | There is no default value.
|
---|
| 908 | */
|
---|
| 909 |
|
---|
| 910 | void QSqlDatabase::setHostName( const QString& host )
|
---|
| 911 | {
|
---|
| 912 | d->hname = host;
|
---|
| 913 | }
|
---|
| 914 |
|
---|
| 915 | /*!
|
---|
| 916 | \property QSqlDatabase::port
|
---|
| 917 | \brief the port used to connect to the database
|
---|
| 918 |
|
---|
| 919 | There is no default value.
|
---|
| 920 | */
|
---|
| 921 |
|
---|
| 922 | void QSqlDatabase::setPort( int p )
|
---|
| 923 | {
|
---|
| 924 | d->port = p;
|
---|
| 925 | }
|
---|
| 926 |
|
---|
| 927 | QString QSqlDatabase::databaseName() const
|
---|
| 928 | {
|
---|
| 929 | return d->dbname;
|
---|
| 930 | }
|
---|
| 931 |
|
---|
| 932 | QString QSqlDatabase::userName() const
|
---|
| 933 | {
|
---|
| 934 | return d->uname;
|
---|
| 935 | }
|
---|
| 936 |
|
---|
| 937 | QString QSqlDatabase::password() const
|
---|
| 938 | {
|
---|
| 939 | return d->pword;
|
---|
| 940 | }
|
---|
| 941 |
|
---|
| 942 | QString QSqlDatabase::hostName() const
|
---|
| 943 | {
|
---|
| 944 | return d->hname;
|
---|
| 945 | }
|
---|
| 946 |
|
---|
| 947 | /*!
|
---|
| 948 | Returns the name of the driver used by the database connection.
|
---|
| 949 | */
|
---|
| 950 | QString QSqlDatabase::driverName() const
|
---|
| 951 | {
|
---|
| 952 | return d->drvName;
|
---|
| 953 | }
|
---|
| 954 |
|
---|
| 955 | int QSqlDatabase::port() const
|
---|
| 956 | {
|
---|
| 957 | return d->port;
|
---|
| 958 | }
|
---|
| 959 |
|
---|
| 960 | /*!
|
---|
| 961 | Returns the database driver used to access the database
|
---|
| 962 | connection.
|
---|
| 963 | */
|
---|
| 964 |
|
---|
| 965 | QSqlDriver* QSqlDatabase::driver() const
|
---|
| 966 | {
|
---|
| 967 | return d->driver;
|
---|
| 968 | }
|
---|
| 969 |
|
---|
| 970 | /*!
|
---|
| 971 | Returns information about the last error that occurred on the
|
---|
| 972 | database. See QSqlError for more information.
|
---|
| 973 | */
|
---|
| 974 |
|
---|
| 975 | QSqlError QSqlDatabase::lastError() const
|
---|
| 976 | {
|
---|
| 977 | return d->driver->lastError();
|
---|
| 978 | }
|
---|
| 979 |
|
---|
| 980 |
|
---|
| 981 | /*!
|
---|
| 982 | \overload
|
---|
| 983 |
|
---|
| 984 | Returns a list of the database's tables that are visible to the
|
---|
| 985 | user. To include views or system tables, use the version of this
|
---|
| 986 | function that takes a table \c type parameter.
|
---|
| 987 |
|
---|
| 988 | Note that if you want to iterate over the list, you should iterate
|
---|
| 989 | over a copy, e.g.
|
---|
| 990 | \code
|
---|
| 991 | QStringList list = myDatabase.tables();
|
---|
| 992 | QStringList::Iterator it = list.begin();
|
---|
| 993 | while( it != list.end() ) {
|
---|
| 994 | myProcessing( *it );
|
---|
| 995 | ++it;
|
---|
| 996 | }
|
---|
| 997 | \endcode
|
---|
| 998 | */
|
---|
| 999 |
|
---|
| 1000 | QStringList QSqlDatabase::tables() const
|
---|
| 1001 | {
|
---|
| 1002 | return tables( QSql::Tables );
|
---|
| 1003 | }
|
---|
| 1004 |
|
---|
| 1005 | /*!
|
---|
| 1006 | Returns a list of the database's tables, system tables and views,
|
---|
| 1007 | as specified by the parameter \a type.
|
---|
| 1008 |
|
---|
| 1009 | Note that if you want to iterate over the list, you should iterate
|
---|
| 1010 | over a copy, e.g.
|
---|
| 1011 | \code
|
---|
| 1012 | QStringList list = myDatabase.tables( QSql::Tables | QSql::Views );
|
---|
| 1013 | QStringList::Iterator it = list.begin();
|
---|
| 1014 | while( it != list.end() ) {
|
---|
| 1015 | myProcessing( *it );
|
---|
| 1016 | ++it;
|
---|
| 1017 | }
|
---|
| 1018 | \endcode
|
---|
| 1019 | */
|
---|
| 1020 |
|
---|
| 1021 | QStringList QSqlDatabase::tables( QSql::TableType type ) const
|
---|
| 1022 | {
|
---|
| 1023 | return d->driver->tables( QString::number( (int)type ) );
|
---|
| 1024 | }
|
---|
| 1025 |
|
---|
| 1026 | /*!
|
---|
| 1027 | Returns the primary index for table \a tablename. If no primary
|
---|
| 1028 | index exists an empty QSqlIndex will be returned.
|
---|
| 1029 | */
|
---|
| 1030 |
|
---|
| 1031 | QSqlIndex QSqlDatabase::primaryIndex( const QString& tablename ) const
|
---|
| 1032 | {
|
---|
| 1033 | return d->driver->primaryIndex( tablename );
|
---|
| 1034 | }
|
---|
| 1035 |
|
---|
| 1036 |
|
---|
| 1037 | /*!
|
---|
| 1038 | Returns a QSqlRecord populated with the names of all the fields in
|
---|
| 1039 | the table (or view) called \a tablename. The order in which the
|
---|
| 1040 | fields appear in the record is undefined. If no such table (or
|
---|
| 1041 | view) exists, an empty record is returned.
|
---|
| 1042 |
|
---|
| 1043 | \sa recordInfo()
|
---|
| 1044 | */
|
---|
| 1045 |
|
---|
| 1046 | QSqlRecord QSqlDatabase::record( const QString& tablename ) const
|
---|
| 1047 | {
|
---|
| 1048 | return d->driver->record( tablename );
|
---|
| 1049 | }
|
---|
| 1050 |
|
---|
| 1051 |
|
---|
| 1052 | /*!
|
---|
| 1053 | \overload
|
---|
| 1054 |
|
---|
| 1055 | Returns a QSqlRecord populated with the names of all the fields
|
---|
| 1056 | used in the SQL \a query. If the query is a "SELECT *" the order
|
---|
| 1057 | in which fields appear in the record is undefined.
|
---|
| 1058 |
|
---|
| 1059 | \sa recordInfo()
|
---|
| 1060 | */
|
---|
| 1061 |
|
---|
| 1062 | QSqlRecord QSqlDatabase::record( const QSqlQuery& query ) const
|
---|
| 1063 | {
|
---|
| 1064 | return d->driver->record( query );
|
---|
| 1065 | }
|
---|
| 1066 |
|
---|
| 1067 | /*!
|
---|
| 1068 | Returns a QSqlRecordInfo populated with meta data about the table
|
---|
| 1069 | or view \a tablename. If no such table (or view) exists, an empty
|
---|
| 1070 | record is returned.
|
---|
| 1071 |
|
---|
| 1072 | \sa QSqlRecordInfo, QSqlFieldInfo, record()
|
---|
| 1073 | */
|
---|
| 1074 | QSqlRecordInfo QSqlDatabase::recordInfo( const QString& tablename ) const
|
---|
| 1075 | {
|
---|
| 1076 | return d->driver->recordInfo( tablename );
|
---|
| 1077 | }
|
---|
| 1078 |
|
---|
| 1079 | /*!
|
---|
| 1080 | \overload
|
---|
| 1081 |
|
---|
| 1082 | Returns a QSqlRecordInfo object with meta data for the QSqlQuery
|
---|
| 1083 | \a query. Note that this overloaded function may return less
|
---|
| 1084 | information than the recordInfo() function which takes the name of
|
---|
| 1085 | a table as parameter.
|
---|
| 1086 |
|
---|
| 1087 | \sa QSqlRecordInfo, QSqlFieldInfo, record()
|
---|
| 1088 | */
|
---|
| 1089 | QSqlRecordInfo QSqlDatabase::recordInfo( const QSqlQuery& query ) const
|
---|
| 1090 | {
|
---|
| 1091 | return d->driver->recordInfo( query );
|
---|
| 1092 | }
|
---|
| 1093 |
|
---|
| 1094 | /*!
|
---|
| 1095 | \property QSqlDatabase::connectOptions
|
---|
| 1096 | \brief the database connect options
|
---|
| 1097 |
|
---|
| 1098 | The format of the options string is a semi-colon separated list of
|
---|
| 1099 | option names or option = value pairs. The options depend on the
|
---|
| 1100 | database client used:
|
---|
| 1101 |
|
---|
| 1102 | \table
|
---|
| 1103 | \header \i ODBC \i MySQL \i PostgreSQL
|
---|
| 1104 | \row
|
---|
| 1105 |
|
---|
| 1106 | \i
|
---|
| 1107 | \list
|
---|
| 1108 | \i SQL_ATTR_ACCESS_MODE
|
---|
| 1109 | \i SQL_ATTR_LOGIN_TIMEOUT
|
---|
| 1110 | \i SQL_ATTR_CONNECTION_TIMEOUT
|
---|
| 1111 | \i SQL_ATTR_CURRENT_CATALOG
|
---|
| 1112 | \i SQL_ATTR_METADATA_ID
|
---|
| 1113 | \i SQL_ATTR_PACKET_SIZE
|
---|
| 1114 | \i SQL_ATTR_TRACEFILE
|
---|
| 1115 | \i SQL_ATTR_TRACE
|
---|
| 1116 | \endlist
|
---|
| 1117 |
|
---|
| 1118 | \i
|
---|
| 1119 | \list
|
---|
| 1120 | \i CLIENT_COMPRESS
|
---|
| 1121 | \i CLIENT_FOUND_ROWS
|
---|
| 1122 | \i CLIENT_IGNORE_SPACE
|
---|
| 1123 | \i CLIENT_SSL
|
---|
| 1124 | \i CLIENT_ODBC
|
---|
| 1125 | \i CLIENT_NO_SCHEMA
|
---|
| 1126 | \i CLIENT_INTERACTIVE
|
---|
| 1127 | \endlist
|
---|
| 1128 |
|
---|
| 1129 | \i
|
---|
| 1130 | \list
|
---|
| 1131 | \i connect_timeout
|
---|
| 1132 | \i options
|
---|
| 1133 | \i tty
|
---|
| 1134 | \i requiressl
|
---|
| 1135 | \i service
|
---|
| 1136 | \endlist
|
---|
| 1137 |
|
---|
| 1138 | \header \i DB2 \i OCI \i TDS
|
---|
| 1139 | \row
|
---|
| 1140 |
|
---|
| 1141 | \i
|
---|
| 1142 | \list
|
---|
| 1143 | \i SQL_ATTR_ACCESS_MODE
|
---|
| 1144 | \i SQL_ATTR_LOGIN_TIMEOUT
|
---|
| 1145 | \endlist
|
---|
| 1146 |
|
---|
| 1147 | \i
|
---|
| 1148 | \e none
|
---|
| 1149 |
|
---|
| 1150 | \i
|
---|
| 1151 | \e none
|
---|
| 1152 |
|
---|
| 1153 | \endtable
|
---|
| 1154 |
|
---|
| 1155 | Example of usage:
|
---|
| 1156 | \code
|
---|
| 1157 | ...
|
---|
| 1158 | // MySQL connection
|
---|
| 1159 | db->setConnectOptions( "CLIENT_SSL;CLIENT_IGNORE_SPACE" ); // use an SSL connection to the server
|
---|
| 1160 | if ( !db->open() ) {
|
---|
| 1161 | db->setConnectOptions(); // clears the connect option string
|
---|
| 1162 | ...
|
---|
| 1163 | }
|
---|
| 1164 | ...
|
---|
| 1165 | // PostgreSQL connection
|
---|
| 1166 | db->setConnectOptions( "requiressl=1" ); // enable PostgreSQL SSL connections
|
---|
| 1167 | if ( !db->open() ) {
|
---|
| 1168 | db->setConnectOptions(); // clear options
|
---|
| 1169 | ...
|
---|
| 1170 | }
|
---|
| 1171 | ...
|
---|
| 1172 | // ODBC connection
|
---|
| 1173 | db->setConnectOptions( "SQL_ATTR_ACCESS_MODE=SQL_MODE_READ_ONLY;SQL_ATTR_TRACE=SQL_OPT_TRACE_ON" ); // set ODBC options
|
---|
| 1174 | if ( !db->open() ) {
|
---|
| 1175 | db->setConnectOptions(); // don't try to set this option
|
---|
| 1176 | ...
|
---|
| 1177 | }
|
---|
| 1178 | \endcode
|
---|
| 1179 |
|
---|
| 1180 | Please refer to the client library documentation for more
|
---|
| 1181 | information about the different options. The options will be set
|
---|
| 1182 | prior to opening the database connection. Setting new options
|
---|
| 1183 | without re-opening the connection does nothing.
|
---|
| 1184 |
|
---|
| 1185 | \sa connectOptions()
|
---|
| 1186 | */
|
---|
| 1187 |
|
---|
| 1188 | void QSqlDatabase::setConnectOptions( const QString& options )
|
---|
| 1189 | {
|
---|
| 1190 | d->connOptions = options;
|
---|
| 1191 | }
|
---|
| 1192 |
|
---|
| 1193 | QString QSqlDatabase::connectOptions() const
|
---|
| 1194 | {
|
---|
| 1195 | return d->connOptions;
|
---|
| 1196 | }
|
---|
| 1197 |
|
---|
| 1198 | /*!
|
---|
| 1199 | Returns TRUE if a driver called \a name is available; otherwise
|
---|
| 1200 | returns FALSE.
|
---|
| 1201 |
|
---|
| 1202 | \sa drivers()
|
---|
| 1203 | */
|
---|
| 1204 |
|
---|
| 1205 | bool QSqlDatabase::isDriverAvailable( const QString& name )
|
---|
| 1206 | {
|
---|
| 1207 | QStringList l = drivers();
|
---|
| 1208 | QStringList::ConstIterator it = l.begin();
|
---|
| 1209 | for ( ;it != l.end(); ++it ) {
|
---|
| 1210 | if ( *it == name )
|
---|
| 1211 | return TRUE;
|
---|
| 1212 | }
|
---|
| 1213 | return FALSE;
|
---|
| 1214 | }
|
---|
| 1215 |
|
---|
| 1216 | /*! \overload
|
---|
| 1217 |
|
---|
| 1218 | This function is useful if you need to set up the database
|
---|
| 1219 | connection and instantiate the driver yourself. If you do this, it
|
---|
| 1220 | is recommended that you include the driver code in your own
|
---|
| 1221 | application. For example, setting up a custom PostgreSQL
|
---|
| 1222 | connection and instantiating the QPSQL7 driver can be done the
|
---|
| 1223 | following way:
|
---|
| 1224 |
|
---|
| 1225 | \code
|
---|
| 1226 | #include "qtdir/src/sql/drivers/psql/qsql_psql.cpp"
|
---|
| 1227 | \endcode
|
---|
| 1228 | (We assume that \c qtdir is the directory where Qt is installed.)
|
---|
| 1229 | This will pull in the code that is needed to use the PostgreSQL
|
---|
| 1230 | client library and to instantiate a QPSQLDriver object, assuming
|
---|
| 1231 | that you have the PostgreSQL headers somewhere in your include
|
---|
| 1232 | search path.
|
---|
| 1233 |
|
---|
| 1234 | \code
|
---|
| 1235 | PGconn* con = PQconnectdb( "host=server user=bart password=simpson dbname=springfield" );
|
---|
| 1236 | QPSQLDriver* drv = new QPSQLDriver( con );
|
---|
| 1237 | QSqlDatabase* db = QSqlDatabase::addDatabase( drv ); // becomes the new default connection
|
---|
| 1238 | QSqlQuery q;
|
---|
| 1239 | q.exec( "SELECT * FROM people" );
|
---|
| 1240 | ...
|
---|
| 1241 | \endcode
|
---|
| 1242 |
|
---|
| 1243 | The above code sets up a PostgreSQL connection and instantiates a
|
---|
| 1244 | QPSQLDriver object. Next, addDatabase() is called to add the
|
---|
| 1245 | connection to the known connections so that it can be used by the
|
---|
| 1246 | Qt SQL classes. When a driver is instantiated with a connection
|
---|
| 1247 | handle (or set of handles), Qt assumes that you have already
|
---|
| 1248 | opened the database connection.
|
---|
| 1249 |
|
---|
| 1250 | Remember that you must link your application against the database
|
---|
| 1251 | client library as well. The simplest way to do this is to add
|
---|
| 1252 | lines like those below to your \c .pro file:
|
---|
| 1253 |
|
---|
| 1254 | \code
|
---|
| 1255 | unix:LIBS += -lpq
|
---|
| 1256 | win32:LIBS += libpqdll.lib
|
---|
| 1257 | \endcode
|
---|
| 1258 |
|
---|
| 1259 | You will need to have the client library in your linker's search
|
---|
| 1260 | path.
|
---|
| 1261 |
|
---|
| 1262 | The method described above will work for all the drivers, the only
|
---|
| 1263 | difference is the arguments the driver constructors take. Below is
|
---|
| 1264 | an overview of the drivers and their constructor arguments.
|
---|
| 1265 |
|
---|
| 1266 | \table
|
---|
| 1267 | \header \i Driver \i Class name \i Constructor arguments \i File to include
|
---|
| 1268 | \row
|
---|
| 1269 | \i QPSQL7
|
---|
| 1270 | \i QPSQLDriver
|
---|
| 1271 | \i PGconn* connection
|
---|
| 1272 | \i \c qsql_psql.cpp
|
---|
| 1273 | \row
|
---|
| 1274 | \i QMYSQL3
|
---|
| 1275 | \i QMYSQLDriver
|
---|
| 1276 | \i MYSQL* connection
|
---|
| 1277 | \i \c qsql_mysql.cpp
|
---|
| 1278 | \row
|
---|
| 1279 | \i QOCI8
|
---|
| 1280 | \i QOCIDriver
|
---|
| 1281 | \i OCIEnv* environment, OCIError* error, OCISvcCtx* serviceContext
|
---|
| 1282 | \i \c qsql_oci.cpp
|
---|
| 1283 | \row
|
---|
| 1284 | \i QODBC3
|
---|
| 1285 | \i QODBCDriver
|
---|
| 1286 | \i SQLHANDLE environment, SQLHANDLE connection
|
---|
| 1287 | \i \c qsql_odbc.cpp
|
---|
| 1288 | \row
|
---|
| 1289 | \i QDB2
|
---|
| 1290 | \i QDB2
|
---|
| 1291 | \i SQLHANDLE environment, SQLHANDLE connection
|
---|
| 1292 | \i \c qsql_db2.cpp
|
---|
| 1293 | \row
|
---|
| 1294 | \i QTDS7
|
---|
| 1295 | \i QTDSDriver
|
---|
| 1296 | \i LOGINREC* loginRecord, DBPROCESS* dbProcess, const QString& hostName
|
---|
| 1297 | \i \c qsql_tds.cpp
|
---|
| 1298 | \row
|
---|
| 1299 | \i QSQLITE
|
---|
| 1300 | \i QSQLiteDriver
|
---|
| 1301 | \i sqlite* connection
|
---|
| 1302 | \i \c qsql_sqlite.cpp
|
---|
| 1303 | \row
|
---|
| 1304 | \i QIBASE
|
---|
| 1305 | \i QIBaseDriver
|
---|
| 1306 | \i isc_db_handle connection
|
---|
| 1307 | \i \c qsql_ibase.cpp
|
---|
| 1308 | \endtable
|
---|
| 1309 |
|
---|
| 1310 | Note: The host name (or service name) is needed when constructing
|
---|
| 1311 | the QTDSDriver for creating new connections for internal
|
---|
| 1312 | queries. This is to prevent the simultaneous usage of several
|
---|
| 1313 | QSqlQuery/\l{QSqlCursor} objects from blocking each other.
|
---|
| 1314 |
|
---|
| 1315 | \warning The SQL framework takes ownership of the \a driver pointer,
|
---|
| 1316 | and it should not be deleted. The returned QSqlDatabase object is
|
---|
| 1317 | owned by the framework and must not be deleted. If you want to
|
---|
| 1318 | explicitly remove the connection, use removeDatabase()
|
---|
| 1319 |
|
---|
| 1320 | \sa drivers()
|
---|
| 1321 | */
|
---|
| 1322 |
|
---|
| 1323 | QSqlDatabase* QSqlDatabase::addDatabase( QSqlDriver* driver, const QString& connectionName )
|
---|
| 1324 | {
|
---|
| 1325 | return QSqlDatabaseManager::addDatabase( new QSqlDatabase( driver ), connectionName );
|
---|
| 1326 | }
|
---|
| 1327 | #endif // QT_NO_SQL
|
---|