|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt3Support module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "q3sqlselectcursor.h" |
|
43 #include "qsqldriver.h" |
|
44 #include "q3sqlrecordinfo.h" |
|
45 |
|
46 #ifndef QT_NO_SQL |
|
47 |
|
48 QT_BEGIN_NAMESPACE |
|
49 |
|
50 class Q3SqlSelectCursorPrivate |
|
51 { |
|
52 public: |
|
53 Q3SqlSelectCursorPrivate() : populated(false) {} |
|
54 QString query; |
|
55 bool populated : 1; |
|
56 }; |
|
57 |
|
58 /*! |
|
59 \class Q3SqlSelectCursor |
|
60 \brief The Q3SqlSelectCursor class provides browsing of general SQL SELECT statements. |
|
61 |
|
62 \compat |
|
63 |
|
64 Q3SqlSelectCursor is a convenience class that makes it possible to |
|
65 display result sets from general SQL \c SELECT statements in |
|
66 data-aware Qt widgets. Q3SqlSelectCursor is read-only and does not |
|
67 support \c INSERT, \c UPDATE or \c DELETE operations. |
|
68 |
|
69 Pass the query in at construction time, or use the |
|
70 Q3SqlSelectCursor::exec() function. |
|
71 |
|
72 Example: |
|
73 \snippet doc/src/snippets/code/src_qt3support_sql_q3sqlselectcursor.cpp 0 |
|
74 */ |
|
75 |
|
76 /*! |
|
77 Constructs a read only cursor on database \a db using the query \a query. |
|
78 */ |
|
79 Q3SqlSelectCursor::Q3SqlSelectCursor(const QString& query, QSqlDatabase db) |
|
80 : Q3SqlCursor(QString(), false, db) |
|
81 { |
|
82 d = new Q3SqlSelectCursorPrivate; |
|
83 d->query = query; |
|
84 Q3SqlCursor::setMode(ReadOnly); |
|
85 if (!query.isEmpty()) |
|
86 exec(query); |
|
87 } |
|
88 |
|
89 /*! Constructs a copy of \a other */ |
|
90 Q3SqlSelectCursor::Q3SqlSelectCursor(const Q3SqlSelectCursor& other) |
|
91 : Q3SqlCursor(other) |
|
92 { |
|
93 d = new Q3SqlSelectCursorPrivate; |
|
94 d->query = other.d->query; |
|
95 d->populated = other.d->populated; |
|
96 } |
|
97 |
|
98 /*! Destroys the object and frees any allocated resources */ |
|
99 Q3SqlSelectCursor::~Q3SqlSelectCursor() |
|
100 { |
|
101 delete d; |
|
102 } |
|
103 |
|
104 /*! \internal */ |
|
105 bool Q3SqlSelectCursor::exec(const QString& query) |
|
106 { |
|
107 d->query = query; |
|
108 bool ret = Q3SqlCursor::exec(query); |
|
109 if (ret) { |
|
110 Q3SqlCursor::clear(); |
|
111 populateCursor(); |
|
112 } |
|
113 return ret; |
|
114 } |
|
115 |
|
116 /*! \fn bool Q3SqlSelectCursor::select() |
|
117 \internal |
|
118 */ |
|
119 |
|
120 /*! \internal */ |
|
121 bool Q3SqlSelectCursor::select(const QString&, const QSqlIndex&) |
|
122 { |
|
123 bool ret = Q3SqlCursor::exec(d->query); |
|
124 if (ret && !d->populated) |
|
125 populateCursor(); |
|
126 return ret; |
|
127 } |
|
128 |
|
129 /*! \internal */ |
|
130 void Q3SqlSelectCursor::populateCursor() |
|
131 { |
|
132 Q3SqlRecordInfo inf = Q3SqlRecordInfo(record()); |
|
133 for (Q3SqlRecordInfo::const_iterator it = inf.begin(); it != inf.end(); ++it) |
|
134 Q3SqlCursor::append(*it); |
|
135 d->populated = true; |
|
136 } |
|
137 |
|
138 /*! \fn QSqlIndex Q3SqlSelectCursor::primaryIndex(bool) const |
|
139 \internal |
|
140 */ |
|
141 |
|
142 /*! \fn QSqlIndex Q3SqlSelectCursor::index(const QStringList&) const |
|
143 \internal |
|
144 */ |
|
145 |
|
146 /*! \fn QSqlIndex Q3SqlSelectCursor::index(const QString&) const |
|
147 \internal |
|
148 */ |
|
149 |
|
150 /*! \fn QSqlIndex Q3SqlSelectCursor::index(const char*) const |
|
151 \internal |
|
152 */ |
|
153 |
|
154 /*! \fn void Q3SqlSelectCursor::setPrimaryIndex(const QSqlIndex&) |
|
155 \internal |
|
156 */ |
|
157 |
|
158 /*! \fn void Q3SqlSelectCursor::append(const Q3SqlFieldInfo&) |
|
159 \internal |
|
160 */ |
|
161 |
|
162 /*! \fn void Q3SqlSelectCursor::insert(int, const Q3SqlFieldInfo&) |
|
163 \internal |
|
164 */ |
|
165 |
|
166 /*! \fn void Q3SqlSelectCursor::remove(int) |
|
167 \internal |
|
168 */ |
|
169 |
|
170 /*! \fn void Q3SqlSelectCursor::clear() |
|
171 \internal |
|
172 */ |
|
173 |
|
174 /*! \fn void Q3SqlSelectCursor::setGenerated(const QString&, bool) |
|
175 \internal |
|
176 */ |
|
177 |
|
178 /*! \fn void Q3SqlSelectCursor::setGenerated(int, bool) |
|
179 \internal |
|
180 */ |
|
181 |
|
182 /*! \fn QSqlRecord* Q3SqlSelectCursor::editBuffer(bool) |
|
183 \internal |
|
184 */ |
|
185 |
|
186 /*! \fn QSqlRecord* Q3SqlSelectCursor::primeInsert() |
|
187 \internal |
|
188 */ |
|
189 |
|
190 /*! \fn QSqlRecord* Q3SqlSelectCursor::primeUpdate() |
|
191 \internal |
|
192 */ |
|
193 |
|
194 /*! \fn QSqlRecord* Q3SqlSelectCursor::primeDelete() |
|
195 \internal |
|
196 */ |
|
197 |
|
198 /*! \fn int Q3SqlSelectCursor::insert(bool) |
|
199 \internal |
|
200 */ |
|
201 |
|
202 /*! \fn int Q3SqlSelectCursor::update(bool) |
|
203 \internal |
|
204 */ |
|
205 |
|
206 /*! \fn int Q3SqlSelectCursor::del(bool) |
|
207 \internal |
|
208 */ |
|
209 |
|
210 /*! \fn void Q3SqlSelectCursor::setMode(int) |
|
211 \internal |
|
212 */ |
|
213 |
|
214 /*! \fn void Q3SqlSelectCursor::setSort(const QSqlIndex&) |
|
215 \internal |
|
216 */ |
|
217 |
|
218 /*! \fn QSqlIndex Q3SqlSelectCursor::sort() const |
|
219 \internal |
|
220 */ |
|
221 |
|
222 /*! \fn void Q3SqlSelectCursor::setFilter(const QString&) |
|
223 \internal |
|
224 */ |
|
225 |
|
226 /*! \fn QString Q3SqlSelectCursor::filter() const |
|
227 \internal |
|
228 */ |
|
229 |
|
230 /*! \fn void Q3SqlSelectCursor::setName(const QString&, bool) |
|
231 \internal |
|
232 */ |
|
233 |
|
234 /*! \fn QString Q3SqlSelectCursor::name() const |
|
235 \internal |
|
236 */ |
|
237 |
|
238 /*! \fn QString Q3SqlSelectCursor::toString(const QString&, const QString&) const |
|
239 \internal |
|
240 */ |
|
241 |
|
242 /*! |
|
243 \fn int Q3SqlSelectCursor::update(const QString & filter, bool invalidate = true) |
|
244 \overload |
|
245 |
|
246 Updates the database with the current contents of the cursor edit |
|
247 buffer using the specified \a filter. Returns the number of |
|
248 records which were updated. |
|
249 For error information, use lastError(). |
|
250 |
|
251 Only records which meet the filter criteria are updated, otherwise |
|
252 all records in the table are updated. |
|
253 |
|
254 If \a invalidate is true (the default), the cursor can no longer |
|
255 be navigated. A new select() call must be made before you can move |
|
256 to a valid record. |
|
257 |
|
258 \sa Q3SqlCursor::update() primeUpdate() setMode() lastError() |
|
259 */ |
|
260 |
|
261 QT_END_NAMESPACE |
|
262 |
|
263 #endif // QT_NO_SQL |