1 /****************************************************************************
2 **
3 ** Definition of QSqlDatabase class
4 **
5 ** Created : 2000-11-03
6 **
7 ** Copyright (C) 2005-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 #ifndef QSQLDATABASE_H
38 #define QSQLDATABASE_H
39 
40 #ifndef QT_H
41 #include "qobject.h"
42 #include "qstring.h"
43 #include "qsqlquery.h"
44 #include "qstringlist.h"
45 #endif // QT_H
46 
47 #if !defined( QT_MODULE_SQL ) || defined( QT_LICENSE_PROFESSIONAL )
48 #define QM_EXPORT_SQL
49 #else
50 #define QM_EXPORT_SQL Q_EXPORT
51 #endif
52 
53 #ifndef QT_NO_SQL
54 
55 class QSqlError;
56 class QSqlDriver;
57 class QSqlIndex;
58 class QSqlRecord;
59 class QSqlRecordInfo;
60 class QSqlDatabasePrivate;
61 
62 class QM_EXPORT_SQL QSqlDriverCreatorBase
63 {
64 public:
65     virtual QSqlDriver* createObject() = 0;
66 };
67 
68 template <class type>
69 class QM_EXPORT_SQL QSqlDriverCreator: public QSqlDriverCreatorBase
70 {
71 public:
createObject()72     QSqlDriver* createObject() { return new type; }
73 };
74 
75 class QM_EXPORT_SQL QSqlDatabase : public QObject
76 {
77     Q_OBJECT
78     Q_PROPERTY( QString databaseName  READ databaseName WRITE setDatabaseName )
79     Q_PROPERTY( QString userName  READ userName WRITE setUserName )
80     Q_PROPERTY( QString password  READ password WRITE setPassword )
81     Q_PROPERTY( QString hostName  READ hostName WRITE setHostName )
82     Q_PROPERTY( int port READ port WRITE setPort )
83     Q_PROPERTY( QString connectOptions READ connectOptions WRITE setConnectOptions )
84 
85 public:
86     ~QSqlDatabase();
87 
88     bool		open();
89     bool		open( const QString& user, const QString& password );
90     void		close();
91     bool		isOpen() const;
92     bool		isOpenError() const;
93     QStringList		tables() const;
94     QStringList		tables( QSql::TableType type ) const;
95     QSqlIndex		primaryIndex( const QString& tablename ) const;
96     QSqlRecord		record( const QString& tablename ) const;
97     QSqlRecord		record( const QSqlQuery& query ) const;
98     QSqlRecordInfo	recordInfo( const QString& tablename ) const;
99     QSqlRecordInfo	recordInfo( const QSqlQuery& query ) const;
100     QSqlQuery		exec( const QString& query = QString::null ) const;
101     QSqlError		lastError() const;
102 
103     bool		transaction();
104     bool		commit();
105     bool		rollback();
106 
107     virtual void	setDatabaseName( const QString& name );
108     virtual void	setUserName( const QString& name );
109     virtual void	setPassword( const QString& password );
110     virtual void	setHostName( const QString& host );
111     virtual void	setPort( int p );
112     void 		setConnectOptions( const QString& options = QString::null );
113     QString		databaseName() const;
114     QString		userName() const;
115     QString		password() const;
116     QString		hostName() const;
117     QString		driverName() const;
118     int         	port() const;
119     QString 		connectOptions() const;
120 
121     QSqlDriver*		driver() const;
122 
123     // MOC_SKIP_BEGIN
124     QT_STATIC_CONST char * const defaultConnection;
125     // MOC_SKIP_END
126 
127     static QSqlDatabase* addDatabase( const QString& type, const QString& connectionName = defaultConnection );
128     static QSqlDatabase* addDatabase( QSqlDriver* driver, const QString& connectionName = defaultConnection );
129     static QSqlDatabase* database( const QString& connectionName = defaultConnection, bool open = TRUE );
130     static void          removeDatabase( const QString& connectionName );
131     static void          removeDatabase( QSqlDatabase* db );
132     static bool          contains( const QString& connectionName = defaultConnection );
133     static QStringList   drivers();
134     static void          registerSqlDriver( const QString& name, const QSqlDriverCreatorBase* creator ); // ### 4.0: creator should not be const
135     static bool 	 isDriverAvailable( const QString& name );
136 
137 protected:
138     QSqlDatabase( const QString& type, const QString& name, QObject * parent=0, const char * objname=0 );
139     QSqlDatabase( QSqlDriver* driver, QObject * parent=0, const char * objname=0 );
140 private:
141     void 	init( const QString& type, const QString& name );
142     QSqlDatabasePrivate* d;
143 #if defined(Q_DISABLE_COPY) // Disabled copy constructor and operator=
144     QSqlDatabase( const QSqlDatabase & );
145     QSqlDatabase &operator=( const QSqlDatabase & );
146 #endif
147 
148 };
149 
150 #endif // QT_NO_SQL
151 #endif
152