1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the sql module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
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.
27
****************************************************************************/
29
#include "qsqlindex.h"
31
#include "qsqlfield.h"
32
#include "qstringlist.h"
36
\brief The QSqlIndex class provides functions to manipulate and
37
describe database indexes.
42
An \e index refers to a single table or view in a database.
43
Information about the fields that comprise the index can be used
44
to generate SQL statements, or to affect the behavior of a
49
Constructs an empty index using the cursor name \a cursorname and
53
QSqlIndex::QSqlIndex(const QString& cursorname, const QString& name)
54
: cursor(cursorname), nm(name)
59
Constructs a copy of \a other.
62
QSqlIndex::QSqlIndex(const QSqlIndex& other)
63
: QSqlRecord(other), cursor(other.cursor), nm(other.nm), sorts(other.sorts)
68
Sets the index equal to \a other.
71
QSqlIndex& QSqlIndex::operator=(const QSqlIndex& other)
73
cursor = other.cursor;
76
QSqlRecord::operator=(other);
81
Destroys the object and frees any allocated resources.
84
QSqlIndex::~QSqlIndex()
90
Sets the name of the index to \a name.
93
void QSqlIndex::setName(const QString& name)
99
\fn QString QSqlIndex::name() const
101
Returns the name of the index.
105
Appends the field \a field to the list of indexed fields. The
106
field is appended with an ascending sort order.
109
void QSqlIndex::append(const QSqlField& field)
111
append(field, false);
117
Appends the field \a field to the list of indexed fields. The
118
field is appended with an ascending sort order, unless \a desc is
122
void QSqlIndex::append(const QSqlField& field, bool desc)
125
QSqlRecord::append(field);
130
Returns true if field \a i in the index is sorted in descending
131
order; otherwise returns false.
134
bool QSqlIndex::isDescending(int i) const
136
if (i >= 0 && i < sorts.size())
142
If \a desc is true, field \a i is sorted in descending order.
143
Otherwise, field \a i is sorted in ascending order (the default).
144
If the field does not exist, nothing happens.
147
void QSqlIndex::setDescending(int i, bool desc)
149
if (i >= 0 && i < sorts.size())
156
Returns a comma-separated list of all the index's field names as a
157
string. This string is suitable, for example, for generating a
158
SQL SELECT statement. Only generated fields are included in the
159
list (see \l{isGenerated()}). If a \a prefix is specified, e.g. a
160
table name, it is prepended before all field names in the form:
162
"\a{prefix}.<fieldname>"
164
If \a sep is specified, each field is separated by \a sep. If \a
165
verbose is true (the default), each field contains a suffix
166
indicating an ASCending or DESCending sort order.
169
QString QSqlIndex::toString(const QString& prefix, const QString& sep, bool verbose) const
173
for (int i = 0; i < count(); ++i) {
175
s += sep + QLatin1Char(' ');
176
s += createField(i, prefix, verbose);
183
Returns a list of all the index's field names. Only generated
184
fields are included in the list (see \l{isGenerated()}). If a \a
185
prefix is specified, e.g. a table name, all fields are prefixed in
188
"\a{prefix}.<fieldname>"
190
If \a verbose is true (the default), each field contains a suffix
191
indicating an ASCending or DESCending sort order.
193
Note that if you want to iterate over the list, you should iterate
196
QStringList list = myIndex.toStringList();
197
QStringList::Iterator it = list.begin();
198
while(it != list.end()) {
205
QStringList QSqlIndex::toStringList(const QString& prefix, bool verbose) const
208
for (int i = 0; i < count(); ++i)
209
s += createField(i, prefix, verbose);
216
Creates a string representing the field number \a i using prefix \a
217
prefix. If \a verbose is true, ASC or DESC is included in the field
218
description if the field is sorted in ASCending or DESCending order.
221
QString QSqlIndex::createField(int i, const QString& prefix, bool verbose) const
224
if (!prefix.isEmpty())
225
f += prefix + QLatin1Char('.');
226
f += field(i).name();
228
f += QLatin1Char(' ') + QString((isDescending(i)
229
? QLatin1String("DESC") : QLatin1String("ASC")));
234
\fn QString QSqlIndex::cursorName() const
236
Returns the name of the cursor which the index is associated with.
241
Sets the name of the cursor that the index is associated with to
244
void QSqlIndex::setCursorName(const QString& cursorName)