1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt3Support module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
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:
Q3SqlSelectCursorPrivate()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  */
Q3SqlSelectCursor(const QString & query,QSqlDatabase db)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 */
Q3SqlSelectCursor(const Q3SqlSelectCursor & 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 */
~Q3SqlSelectCursor()99 Q3SqlSelectCursor::~Q3SqlSelectCursor()
100 {
101     delete d;
102 }
103 
104 /*! \internal */
exec(const QString & query)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 */
select(const QString &,const QSqlIndex &)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 */
populateCursor()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
264