1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Implementation of cached Qt SQL result classes
|
---|
4 | **
|
---|
5 | **
|
---|
6 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
7 | **
|
---|
8 | ** This file is part of the sql module of the Qt GUI Toolkit.
|
---|
9 | **
|
---|
10 | ** This file may be distributed under the terms of the Q Public License
|
---|
11 | ** as defined by Trolltech ASA of Norway and appearing in the file
|
---|
12 | ** LICENSE.QPL included in the packaging of this file.
|
---|
13 | **
|
---|
14 | ** This file may be distributed and/or modified under the terms of the
|
---|
15 | ** GNU General Public License version 2 as published by the Free Software
|
---|
16 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
17 | ** packaging of this file.
|
---|
18 | **
|
---|
19 | ** Licensees holding valid Qt Enterprise Edition licenses may use this
|
---|
20 | ** file in accordance with the Qt Commercial License Agreement provided
|
---|
21 | ** with the Software.
|
---|
22 | **
|
---|
23 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
---|
24 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
---|
25 | **
|
---|
26 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
---|
27 | ** information about Qt Commercial License Agreements.
|
---|
28 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
---|
29 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
---|
30 | **
|
---|
31 | ** Contact info@trolltech.com if any conditions of this licensing are
|
---|
32 | ** not clear to you.
|
---|
33 | **
|
---|
34 | **********************************************************************/
|
---|
35 |
|
---|
36 | #include "qsqlcachedresult.h"
|
---|
37 | #include <qdatetime.h>
|
---|
38 |
|
---|
39 | #ifndef QT_NO_SQL
|
---|
40 | static const uint initial_cache_size = 128;
|
---|
41 |
|
---|
42 | class QtSqlCachedResultPrivate
|
---|
43 | {
|
---|
44 | public:
|
---|
45 | QtSqlCachedResultPrivate();
|
---|
46 | bool seek(int i);
|
---|
47 | void init(int count, bool fo);
|
---|
48 | void cleanup();
|
---|
49 | QtSqlCachedResult::RowCache* next();
|
---|
50 | void revertLast();
|
---|
51 |
|
---|
52 | QtSqlCachedResult::RowsetCache *cache;
|
---|
53 | QtSqlCachedResult::RowCache *current;
|
---|
54 | int rowCacheEnd;
|
---|
55 | int colCount;
|
---|
56 | bool forwardOnly;
|
---|
57 | };
|
---|
58 |
|
---|
59 | QtSqlCachedResultPrivate::QtSqlCachedResultPrivate():
|
---|
60 | cache(0), current(0), rowCacheEnd(0), colCount(0), forwardOnly(FALSE)
|
---|
61 | {
|
---|
62 | }
|
---|
63 |
|
---|
64 | void QtSqlCachedResultPrivate::cleanup()
|
---|
65 | {
|
---|
66 | if (cache) {
|
---|
67 | for (int i = 0; i < rowCacheEnd; ++i)
|
---|
68 | delete (*cache)[i];
|
---|
69 | delete cache;
|
---|
70 | cache = 0;
|
---|
71 | }
|
---|
72 | if (forwardOnly)
|
---|
73 | delete current;
|
---|
74 | current = 0;
|
---|
75 | forwardOnly = FALSE;
|
---|
76 | colCount = 0;
|
---|
77 | rowCacheEnd = 0;
|
---|
78 | }
|
---|
79 |
|
---|
80 | void QtSqlCachedResultPrivate::init(int count, bool fo)
|
---|
81 | {
|
---|
82 | cleanup();
|
---|
83 | forwardOnly = fo;
|
---|
84 | colCount = count;
|
---|
85 | if (fo)
|
---|
86 | current = new QtSqlCachedResult::RowCache(count);
|
---|
87 | else
|
---|
88 | cache = new QtSqlCachedResult::RowsetCache(initial_cache_size);
|
---|
89 | }
|
---|
90 |
|
---|
91 | QtSqlCachedResult::RowCache *QtSqlCachedResultPrivate::next()
|
---|
92 | {
|
---|
93 | if (forwardOnly)
|
---|
94 | return current;
|
---|
95 |
|
---|
96 | Q_ASSERT(cache);
|
---|
97 | current = new QtSqlCachedResult::RowCache(colCount);
|
---|
98 | if (rowCacheEnd == (int)cache->size())
|
---|
99 | cache->resize(cache->size() * 2);
|
---|
100 | cache->insert(rowCacheEnd++, current);
|
---|
101 | return current;
|
---|
102 | }
|
---|
103 |
|
---|
104 | bool QtSqlCachedResultPrivate::seek(int i)
|
---|
105 | {
|
---|
106 | if (forwardOnly || i < 0)
|
---|
107 | return FALSE;
|
---|
108 | if (i >= rowCacheEnd)
|
---|
109 | return FALSE;
|
---|
110 | current = (*cache)[i];
|
---|
111 | return TRUE;
|
---|
112 | }
|
---|
113 |
|
---|
114 | void QtSqlCachedResultPrivate::revertLast()
|
---|
115 | {
|
---|
116 | if (forwardOnly)
|
---|
117 | return;
|
---|
118 | --rowCacheEnd;
|
---|
119 | delete current;
|
---|
120 | current = 0;
|
---|
121 | }
|
---|
122 |
|
---|
123 | //////////////
|
---|
124 |
|
---|
125 | QtSqlCachedResult::QtSqlCachedResult(const QSqlDriver * db ): QSqlResult ( db )
|
---|
126 | {
|
---|
127 | d = new QtSqlCachedResultPrivate();
|
---|
128 | }
|
---|
129 |
|
---|
130 | QtSqlCachedResult::~QtSqlCachedResult()
|
---|
131 | {
|
---|
132 | delete d;
|
---|
133 | }
|
---|
134 |
|
---|
135 | void QtSqlCachedResult::init(int colCount)
|
---|
136 | {
|
---|
137 | d->init(colCount, isForwardOnly());
|
---|
138 | }
|
---|
139 |
|
---|
140 | bool QtSqlCachedResult::fetch(int i)
|
---|
141 | {
|
---|
142 | if ((!isActive()) || (i < 0))
|
---|
143 | return FALSE;
|
---|
144 | if (at() == i)
|
---|
145 | return TRUE;
|
---|
146 | if (d->forwardOnly) {
|
---|
147 | // speed hack - do not copy values if not needed
|
---|
148 | if (at() > i || at() == QSql::AfterLast)
|
---|
149 | return FALSE;
|
---|
150 | while(at() < i - 1) {
|
---|
151 | if (!gotoNext(0))
|
---|
152 | return FALSE;
|
---|
153 | setAt(at() + 1);
|
---|
154 | }
|
---|
155 | if (!gotoNext(d->current))
|
---|
156 | return FALSE;
|
---|
157 | setAt(at() + 1);
|
---|
158 | return TRUE;
|
---|
159 | }
|
---|
160 | if (d->seek(i)) {
|
---|
161 | setAt(i);
|
---|
162 | return TRUE;
|
---|
163 | }
|
---|
164 | setAt(d->rowCacheEnd - 1);
|
---|
165 | while (at() < i) {
|
---|
166 | if (!cacheNext())
|
---|
167 | return FALSE;
|
---|
168 | }
|
---|
169 | return TRUE;
|
---|
170 | }
|
---|
171 |
|
---|
172 | bool QtSqlCachedResult::fetchNext()
|
---|
173 | {
|
---|
174 | if (d->seek(at() + 1)) {
|
---|
175 | setAt(at() + 1);
|
---|
176 | return TRUE;
|
---|
177 | }
|
---|
178 | return cacheNext();
|
---|
179 | }
|
---|
180 |
|
---|
181 | bool QtSqlCachedResult::fetchPrev()
|
---|
182 | {
|
---|
183 | return fetch(at() - 1);
|
---|
184 | }
|
---|
185 |
|
---|
186 | bool QtSqlCachedResult::fetchFirst()
|
---|
187 | {
|
---|
188 | if (d->forwardOnly && at() != QSql::BeforeFirst) {
|
---|
189 | return FALSE;
|
---|
190 | }
|
---|
191 | if (d->seek(0)) {
|
---|
192 | setAt(0);
|
---|
193 | return TRUE;
|
---|
194 | }
|
---|
195 | return cacheNext();
|
---|
196 | }
|
---|
197 |
|
---|
198 | bool QtSqlCachedResult::fetchLast()
|
---|
199 | {
|
---|
200 | if (at() == QSql::AfterLast) {
|
---|
201 | if (d->forwardOnly)
|
---|
202 | return FALSE;
|
---|
203 | else
|
---|
204 | return fetch(d->rowCacheEnd - 1);
|
---|
205 | }
|
---|
206 |
|
---|
207 | int i = at();
|
---|
208 | while (fetchNext())
|
---|
209 | i++; /* brute force */
|
---|
210 | if (d->forwardOnly && at() == QSql::AfterLast) {
|
---|
211 | setAt(i);
|
---|
212 | return TRUE;
|
---|
213 | } else {
|
---|
214 | return fetch(d->rowCacheEnd - 1);
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | QVariant QtSqlCachedResult::data(int i)
|
---|
219 | {
|
---|
220 | if (!d->current || i >= (int)d->current->size() || i < 0)
|
---|
221 | return QVariant();
|
---|
222 |
|
---|
223 | return (*d->current)[i];
|
---|
224 | }
|
---|
225 |
|
---|
226 | bool QtSqlCachedResult::isNull(int i)
|
---|
227 | {
|
---|
228 | if (!d->current || i >= (int)d->current->size() || i < 0)
|
---|
229 | return TRUE;
|
---|
230 |
|
---|
231 | return (*d->current)[i].isNull();
|
---|
232 | }
|
---|
233 |
|
---|
234 | void QtSqlCachedResult::cleanup()
|
---|
235 | {
|
---|
236 | setAt(QSql::BeforeFirst);
|
---|
237 | setActive(FALSE);
|
---|
238 | d->cleanup();
|
---|
239 | }
|
---|
240 |
|
---|
241 | bool QtSqlCachedResult::cacheNext()
|
---|
242 | {
|
---|
243 | if (!gotoNext(d->next())) {
|
---|
244 | d->revertLast();
|
---|
245 | return FALSE;
|
---|
246 | }
|
---|
247 | setAt(at() + 1);
|
---|
248 | return TRUE;
|
---|
249 | }
|
---|
250 |
|
---|
251 | int QtSqlCachedResult::colCount() const
|
---|
252 | {
|
---|
253 | return d->colCount;
|
---|
254 | }
|
---|
255 | #endif // QT_NO_SQL
|
---|