1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Definition of QSqlSelectCursor class
|
---|
4 | **
|
---|
5 | ** Created : 2002-11-13
|
---|
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 | #include "qsqlselectcursor.h"
|
---|
38 | #include "qsqldriver.h"
|
---|
39 |
|
---|
40 | #ifndef QT_NO_SQL
|
---|
41 |
|
---|
42 | class QSqlSelectCursorPrivate
|
---|
43 | {
|
---|
44 | public:
|
---|
45 | QSqlSelectCursorPrivate() : populated( FALSE ) {}
|
---|
46 | QString query;
|
---|
47 | bool populated : 1;
|
---|
48 | };
|
---|
49 |
|
---|
50 | /*!
|
---|
51 | \class QSqlSelectCursor qsqlselectcursor.h
|
---|
52 | \brief The QSqlSelectCursor class provides browsing of general SQL
|
---|
53 | SELECT statements.
|
---|
54 |
|
---|
55 | \ingroup database
|
---|
56 | \module sql
|
---|
57 |
|
---|
58 | QSqlSelectCursor is a convenience class that makes it possible to
|
---|
59 | display result sets from general SQL \c SELECT statements in
|
---|
60 | data-aware Qt widgets. QSqlSelectCursor is read-only and does not
|
---|
61 | support \c INSERT, \c UPDATE or \c DELETE operations.
|
---|
62 |
|
---|
63 | Pass the query in at construction time, or use the
|
---|
64 | QSqlSelectCursor::exec() function.
|
---|
65 |
|
---|
66 | Example:
|
---|
67 | \code
|
---|
68 | ...
|
---|
69 | QSqlSelectCursor* cur = new QSqlSelectCursor( "SELECT id, firstname, lastname FROM author" );
|
---|
70 | QDataTable* table = new QDataTable( this );
|
---|
71 | table->setSqlCursor( cur, TRUE, TRUE );
|
---|
72 | table->refresh();
|
---|
73 | ...
|
---|
74 | cur->exec( "SELECT * FROM books" );
|
---|
75 | table->refresh();
|
---|
76 | ...
|
---|
77 | \endcode
|
---|
78 | */
|
---|
79 |
|
---|
80 | /*!
|
---|
81 | Constructs a read only cursor on database \a db using the query \a query.
|
---|
82 | */
|
---|
83 | QSqlSelectCursor::QSqlSelectCursor( const QString& query, QSqlDatabase* db )
|
---|
84 | : QSqlCursor( QString::null, FALSE, db )
|
---|
85 | {
|
---|
86 | d = new QSqlSelectCursorPrivate;
|
---|
87 | d->query = query;
|
---|
88 | QSqlCursor::setMode( ReadOnly );
|
---|
89 | if ( !query.isNull() )
|
---|
90 | exec( query );
|
---|
91 | }
|
---|
92 |
|
---|
93 | /*! Constructs a copy of \a other */
|
---|
94 | QSqlSelectCursor::QSqlSelectCursor( const QSqlSelectCursor& other )
|
---|
95 | : QSqlCursor( other )
|
---|
96 | {
|
---|
97 | d = new QSqlSelectCursorPrivate;
|
---|
98 | d->query = other.d->query;
|
---|
99 | d->populated = other.d->populated;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /*! Destroys the object and frees any allocated resources */
|
---|
103 | QSqlSelectCursor::~QSqlSelectCursor()
|
---|
104 | {
|
---|
105 | delete d;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /*! \reimp */
|
---|
109 | bool QSqlSelectCursor::exec( const QString& query )
|
---|
110 | {
|
---|
111 | d->query = query;
|
---|
112 | bool ret = QSqlCursor::exec( query );
|
---|
113 | if ( ret ) {
|
---|
114 | QSqlCursor::clear();
|
---|
115 | populateCursor();
|
---|
116 | }
|
---|
117 | return ret;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /*! \fn bool QSqlSelectCursor::select()
|
---|
121 | \reimp
|
---|
122 | */
|
---|
123 |
|
---|
124 | /*! \reimp */
|
---|
125 | bool QSqlSelectCursor::select( const QString&, const QSqlIndex& )
|
---|
126 | {
|
---|
127 | bool ret = QSqlCursor::exec( d->query );
|
---|
128 | if ( ret && !d->populated )
|
---|
129 | populateCursor();
|
---|
130 | return ret;
|
---|
131 | }
|
---|
132 |
|
---|
133 | /*! \internal */
|
---|
134 | void QSqlSelectCursor::populateCursor()
|
---|
135 | {
|
---|
136 | QSqlRecordInfo inf = driver()->recordInfo( *(QSqlQuery*)this );
|
---|
137 | for ( QSqlRecordInfo::const_iterator it = inf.begin(); it != inf.end(); ++it )
|
---|
138 | QSqlCursor::append( *it );
|
---|
139 | d->populated = TRUE;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /*! \fn QSqlIndex QSqlSelectCursor::primaryIndex( bool ) const
|
---|
143 | \reimp
|
---|
144 | */
|
---|
145 |
|
---|
146 | /*! \fn QSqlIndex QSqlSelectCursor::index( const QStringList& ) const
|
---|
147 | \reimp
|
---|
148 | */
|
---|
149 |
|
---|
150 | /*! \fn QSqlIndex QSqlSelectCursor::index( const QString& ) const
|
---|
151 | \reimp
|
---|
152 | */
|
---|
153 |
|
---|
154 | /*! \fn QSqlIndex QSqlSelectCursor::index( const char* ) const
|
---|
155 | \reimp
|
---|
156 | */
|
---|
157 |
|
---|
158 | /*! \fn void QSqlSelectCursor::setPrimaryIndex( const QSqlIndex& )
|
---|
159 | \reimp
|
---|
160 | */
|
---|
161 |
|
---|
162 | /*! \fn void QSqlSelectCursor::append( const QSqlFieldInfo& )
|
---|
163 | \reimp
|
---|
164 | */
|
---|
165 |
|
---|
166 | /*! \fn void QSqlSelectCursor::insert( int, const QSqlFieldInfo& )
|
---|
167 | \reimp
|
---|
168 | */
|
---|
169 |
|
---|
170 | /*! \fn void QSqlSelectCursor::remove( int )
|
---|
171 | \reimp
|
---|
172 | */
|
---|
173 |
|
---|
174 | /*! \fn void QSqlSelectCursor::clear()
|
---|
175 | \reimp
|
---|
176 | */
|
---|
177 |
|
---|
178 | /*! \fn void QSqlSelectCursor::setGenerated( const QString&, bool )
|
---|
179 | \reimp
|
---|
180 | */
|
---|
181 |
|
---|
182 | /*! \fn void QSqlSelectCursor::setGenerated( int, bool )
|
---|
183 | \reimp
|
---|
184 | */
|
---|
185 |
|
---|
186 | /*! \fn QSqlRecord* QSqlSelectCursor::editBuffer( bool )
|
---|
187 | \reimp
|
---|
188 | */
|
---|
189 |
|
---|
190 | /*! \fn QSqlRecord* QSqlSelectCursor::primeInsert()
|
---|
191 | \reimp
|
---|
192 | */
|
---|
193 |
|
---|
194 | /*! \fn QSqlRecord* QSqlSelectCursor::primeUpdate()
|
---|
195 | \reimp
|
---|
196 | */
|
---|
197 |
|
---|
198 | /*! \fn QSqlRecord* QSqlSelectCursor::primeDelete()
|
---|
199 | \reimp
|
---|
200 | */
|
---|
201 |
|
---|
202 | /*! \fn int QSqlSelectCursor::insert( bool )
|
---|
203 | \reimp
|
---|
204 | */
|
---|
205 |
|
---|
206 | /*! \fn int QSqlSelectCursor::update( bool )
|
---|
207 | \reimp
|
---|
208 | */
|
---|
209 |
|
---|
210 | /*! \fn int QSqlSelectCursor::del( bool )
|
---|
211 | \reimp
|
---|
212 | */
|
---|
213 |
|
---|
214 | /*! \fn void QSqlSelectCursor::setMode( int )
|
---|
215 | \reimp
|
---|
216 | */
|
---|
217 |
|
---|
218 | /*! \fn void QSqlSelectCursor::setSort( const QSqlIndex& )
|
---|
219 | \reimp
|
---|
220 | */
|
---|
221 |
|
---|
222 | /*! \fn QSqlIndex QSqlSelectCursor::sort() const
|
---|
223 | \reimp
|
---|
224 | */
|
---|
225 |
|
---|
226 | /*! \fn void QSqlSelectCursor::setFilter( const QString& )
|
---|
227 | \reimp
|
---|
228 | */
|
---|
229 |
|
---|
230 | /*! \fn QString QSqlSelectCursor::filter() const
|
---|
231 | \reimp
|
---|
232 | */
|
---|
233 |
|
---|
234 | /*! \fn void QSqlSelectCursor::setName( const QString&, bool )
|
---|
235 | \reimp
|
---|
236 | */
|
---|
237 |
|
---|
238 | /*! \fn QString QSqlSelectCursor::name() const
|
---|
239 | \reimp
|
---|
240 | */
|
---|
241 |
|
---|
242 | /*! \fn QString QSqlSelectCursor::toString( const QString&, const QString& ) const
|
---|
243 | \reimp
|
---|
244 | */
|
---|
245 | #endif // QT_NO_SQL
|
---|