1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Implementation of QSqlIndex class
|
---|
4 | **
|
---|
5 | ** Created : 2000-11-03
|
---|
6 | **
|
---|
7 | ** Copyright (C) 2000-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 "qsqlindex.h"
|
---|
38 |
|
---|
39 | #ifndef QT_NO_SQL
|
---|
40 |
|
---|
41 | #include "qsqlcursor.h"
|
---|
42 |
|
---|
43 | /*!
|
---|
44 | \class QSqlIndex qsqlindex.h
|
---|
45 | \brief The QSqlIndex class provides functions to manipulate and
|
---|
46 | describe QSqlCursor and QSqlDatabase indexes.
|
---|
47 |
|
---|
48 | \ingroup database
|
---|
49 | \module sql
|
---|
50 |
|
---|
51 | This class is used to describe and manipulate QSqlCursor and
|
---|
52 | QSqlDatabase indexes. An index refers to a single table or view
|
---|
53 | in a database. Information about the fields that comprise the
|
---|
54 | index can be used to generate SQL statements, or to affect the
|
---|
55 | behavior of a \l QSqlCursor object.
|
---|
56 |
|
---|
57 | Normally, QSqlIndex objects are created by \l QSqlDatabase or
|
---|
58 | QSqlCursor.
|
---|
59 | */
|
---|
60 |
|
---|
61 | /*!
|
---|
62 | Constructs an empty index using the cursor name \a cursorname and
|
---|
63 | index name \a name.
|
---|
64 | */
|
---|
65 |
|
---|
66 | QSqlIndex::QSqlIndex( const QString& cursorname, const QString& name )
|
---|
67 | : QSqlRecord(), cursor(cursorname), nm(name)
|
---|
68 | {
|
---|
69 |
|
---|
70 | }
|
---|
71 |
|
---|
72 | /*!
|
---|
73 | Constructs a copy of \a other.
|
---|
74 | */
|
---|
75 |
|
---|
76 | QSqlIndex::QSqlIndex( const QSqlIndex& other )
|
---|
77 | : QSqlRecord(other), cursor(other.cursor), nm(other.nm), sorts(other.sorts)
|
---|
78 | {
|
---|
79 | }
|
---|
80 |
|
---|
81 | /*!
|
---|
82 | Sets the index equal to \a other.
|
---|
83 | */
|
---|
84 |
|
---|
85 | QSqlIndex& QSqlIndex::operator=( const QSqlIndex& other )
|
---|
86 | {
|
---|
87 | cursor = other.cursor;
|
---|
88 | nm = other.nm;
|
---|
89 | sorts = other.sorts;
|
---|
90 | QSqlRecord::operator=( other );
|
---|
91 | return *this;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /*!
|
---|
95 | Destroys the object and frees any allocated resources.
|
---|
96 | */
|
---|
97 |
|
---|
98 | QSqlIndex::~QSqlIndex()
|
---|
99 | {
|
---|
100 |
|
---|
101 | }
|
---|
102 |
|
---|
103 | /*!
|
---|
104 | Sets the name of the index to \a name.
|
---|
105 | */
|
---|
106 |
|
---|
107 | void QSqlIndex::setName( const QString& name )
|
---|
108 | {
|
---|
109 | nm = name;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /*!
|
---|
113 | \fn QString QSqlIndex::name() const
|
---|
114 |
|
---|
115 | Returns the name of the index.
|
---|
116 | */
|
---|
117 |
|
---|
118 | /*!
|
---|
119 | Appends the field \a field to the list of indexed fields. The
|
---|
120 | field is appended with an ascending sort order.
|
---|
121 | */
|
---|
122 |
|
---|
123 | void QSqlIndex::append( const QSqlField& field )
|
---|
124 | {
|
---|
125 | append( field, FALSE );
|
---|
126 | }
|
---|
127 |
|
---|
128 | /*!
|
---|
129 | \overload
|
---|
130 |
|
---|
131 | Appends the field \a field to the list of indexed fields. The
|
---|
132 | field is appended with an ascending sort order, unless \a desc is
|
---|
133 | TRUE.
|
---|
134 | */
|
---|
135 |
|
---|
136 | void QSqlIndex::append( const QSqlField& field, bool desc )
|
---|
137 | {
|
---|
138 | sorts.append( desc );
|
---|
139 | QSqlRecord::append( field );
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | /*!
|
---|
144 | Returns TRUE if field \a i in the index is sorted in descending
|
---|
145 | order; otherwise returns FALSE.
|
---|
146 | */
|
---|
147 |
|
---|
148 | bool QSqlIndex::isDescending( int i ) const
|
---|
149 | {
|
---|
150 | if ( sorts.at( i ) != sorts.end() )
|
---|
151 | return sorts[i];
|
---|
152 | return FALSE;
|
---|
153 | }
|
---|
154 |
|
---|
155 | /*!
|
---|
156 | If \a desc is TRUE, field \a i is sorted in descending order.
|
---|
157 | Otherwise, field \a i is sorted in ascending order (the default).
|
---|
158 | If the field does not exist, nothing happens.
|
---|
159 | */
|
---|
160 |
|
---|
161 | void QSqlIndex::setDescending( int i, bool desc )
|
---|
162 | {
|
---|
163 | if ( sorts.at( i ) != sorts.end() )
|
---|
164 | sorts[i] = desc;
|
---|
165 | }
|
---|
166 |
|
---|
167 | /*!
|
---|
168 | \reimp
|
---|
169 |
|
---|
170 | Returns a comma-separated list of all the index's field names as a
|
---|
171 | string. This string is suitable, for example, for generating a
|
---|
172 | SQL SELECT statement. Only generated fields are included in the
|
---|
173 | list (see \l{isGenerated()}). If a \a prefix is specified, e.g. a
|
---|
174 | table name, it is prepended before all field names in the form:
|
---|
175 |
|
---|
176 | "\a{prefix}.<fieldname>"
|
---|
177 |
|
---|
178 | If \a sep is specified, each field is separated by \a sep. If \a
|
---|
179 | verbose is TRUE (the default), each field contains a suffix
|
---|
180 | indicating an ASCending or DESCending sort order.
|
---|
181 | */
|
---|
182 |
|
---|
183 | QString QSqlIndex::toString( const QString& prefix, const QString& sep, bool verbose ) const
|
---|
184 | {
|
---|
185 | QString s;
|
---|
186 | bool comma = FALSE;
|
---|
187 | for ( uint i = 0; i < count(); ++i ) {
|
---|
188 | if( comma )
|
---|
189 | s += sep + " ";
|
---|
190 | s += createField( i, prefix, verbose );
|
---|
191 | comma = TRUE;
|
---|
192 | }
|
---|
193 | return s;
|
---|
194 | }
|
---|
195 |
|
---|
196 | /*!
|
---|
197 | \reimp
|
---|
198 |
|
---|
199 | Returns a list of all the index's field names. Only generated
|
---|
200 | fields are included in the list (see \l{isGenerated()}). If a \a
|
---|
201 | prefix is specified, e.g. a table name, all fields are prefixed in
|
---|
202 | the form:
|
---|
203 |
|
---|
204 | "\a{prefix}.<fieldname>"
|
---|
205 |
|
---|
206 | If \a verbose is TRUE (the default), each field contains a suffix
|
---|
207 | indicating an ASCending or DESCending sort order.
|
---|
208 |
|
---|
209 | Note that if you want to iterate over the list, you should iterate
|
---|
210 | over a copy, e.g.
|
---|
211 | \code
|
---|
212 | QStringList list = myIndex.toStringList();
|
---|
213 | QStringList::Iterator it = list.begin();
|
---|
214 | while( it != list.end() ) {
|
---|
215 | myProcessing( *it );
|
---|
216 | ++it;
|
---|
217 | }
|
---|
218 | \endcode
|
---|
219 |
|
---|
220 | */
|
---|
221 | QStringList QSqlIndex::toStringList( const QString& prefix, bool verbose ) const
|
---|
222 | {
|
---|
223 | QStringList s;
|
---|
224 | for ( uint i = 0; i < count(); ++i )
|
---|
225 | s += createField( i, prefix, verbose );
|
---|
226 | return s;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /*! \internal
|
---|
230 |
|
---|
231 | Creates a string representing the field number \a i using prefix \a
|
---|
232 | prefix. If \a verbose is TRUE, ASC or DESC is included in the field
|
---|
233 | description if the field is sorted in ASCending or DESCending order.
|
---|
234 | */
|
---|
235 |
|
---|
236 | QString QSqlIndex::createField( int i, const QString& prefix, bool verbose ) const
|
---|
237 | {
|
---|
238 | QString f;
|
---|
239 | if ( !prefix.isEmpty() )
|
---|
240 | f += prefix + ".";
|
---|
241 | f += field( i )->name();
|
---|
242 | if ( verbose )
|
---|
243 | f += " " + QString( ( isDescending( i ) ? "DESC" : "ASC" ) );
|
---|
244 | return f;
|
---|
245 | }
|
---|
246 |
|
---|
247 | /*!
|
---|
248 | Returns an index based on the field descriptions in \a l and the
|
---|
249 | cursor \a cursor. The field descriptions should be in the same
|
---|
250 | format that toStringList() produces, for example, a surname field
|
---|
251 | in the people table might be in one of these forms: "surname",
|
---|
252 | "surname DESC" or "people.surname ASC".
|
---|
253 |
|
---|
254 | \sa toStringList()
|
---|
255 | */
|
---|
256 |
|
---|
257 | QSqlIndex QSqlIndex::fromStringList( const QStringList& l, const QSqlCursor* cursor )
|
---|
258 | {
|
---|
259 | QSqlIndex newSort;
|
---|
260 | for ( uint i = 0; i < l.count(); ++i ) {
|
---|
261 | QString f = l[ i ];
|
---|
262 | bool desc = FALSE;
|
---|
263 | if ( f.mid( f.length()-3 ) == "ASC" )
|
---|
264 | f = f.mid( 0, f.length()-3 );
|
---|
265 | if ( f.mid( f.length()-4 ) == "DESC" ) {
|
---|
266 | desc = TRUE;
|
---|
267 | f = f.mid( 0, f.length()-4 );
|
---|
268 | }
|
---|
269 | int dot = f.findRev( '.' );
|
---|
270 | if ( dot != -1 )
|
---|
271 | f = f.mid( dot+1 );
|
---|
272 | const QSqlField* field = cursor->field( f.simplifyWhiteSpace() );
|
---|
273 | if ( field )
|
---|
274 | newSort.append( *field, desc );
|
---|
275 | else
|
---|
276 | qWarning( "QSqlIndex::fromStringList: unknown field: '" + f + "'" );
|
---|
277 | }
|
---|
278 | return newSort;
|
---|
279 | }
|
---|
280 |
|
---|
281 | /*!
|
---|
282 | \fn QString QSqlIndex::cursorName() const
|
---|
283 |
|
---|
284 | Returns the name of the cursor which the index is associated with.
|
---|
285 | */
|
---|
286 |
|
---|
287 |
|
---|
288 | /*!
|
---|
289 | Sets the name of the cursor that the index is associated with to
|
---|
290 | \a cursorName.
|
---|
291 | */
|
---|
292 | void QSqlIndex::setCursorName( const QString& cursorName )
|
---|
293 | {
|
---|
294 | cursor = cursorName;
|
---|
295 | }
|
---|
296 |
|
---|
297 | #endif
|
---|