~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/sql/kernel/qsqlindex.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the sql module of the Qt Toolkit.
 
6
**
 
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.
 
10
**
 
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.
 
15
**
 
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.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
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
****************************************************************************/
 
28
 
 
29
#include "qsqlindex.h"
 
30
 
 
31
#include "qsqlfield.h"
 
32
#include "qstringlist.h"
 
33
 
 
34
/*!
 
35
    \class QSqlIndex
 
36
    \brief The QSqlIndex class provides functions to manipulate and
 
37
    describe database indexes.
 
38
 
 
39
    \ingroup database
 
40
    \module sql
 
41
 
 
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
 
45
    QSqlCursor object.
 
46
*/
 
47
 
 
48
/*!
 
49
    Constructs an empty index using the cursor name \a cursorname and
 
50
    index name \a name.
 
51
*/
 
52
 
 
53
QSqlIndex::QSqlIndex(const QString& cursorname, const QString& name)
 
54
    : cursor(cursorname), nm(name)
 
55
{
 
56
}
 
57
 
 
58
/*!
 
59
    Constructs a copy of \a other.
 
60
*/
 
61
 
 
62
QSqlIndex::QSqlIndex(const QSqlIndex& other)
 
63
    : QSqlRecord(other), cursor(other.cursor), nm(other.nm), sorts(other.sorts)
 
64
{
 
65
}
 
66
 
 
67
/*!
 
68
    Sets the index equal to \a other.
 
69
*/
 
70
 
 
71
QSqlIndex& QSqlIndex::operator=(const QSqlIndex& other)
 
72
{
 
73
    cursor = other.cursor;
 
74
    nm = other.nm;
 
75
    sorts = other.sorts;
 
76
    QSqlRecord::operator=(other);
 
77
    return *this;
 
78
}
 
79
 
 
80
/*!
 
81
    Destroys the object and frees any allocated resources.
 
82
*/
 
83
 
 
84
QSqlIndex::~QSqlIndex()
 
85
{
 
86
 
 
87
}
 
88
 
 
89
/*!
 
90
    Sets the name of the index to \a name.
 
91
*/
 
92
 
 
93
void QSqlIndex::setName(const QString& name)
 
94
{
 
95
    nm = name;
 
96
}
 
97
 
 
98
/*!
 
99
    \fn QString QSqlIndex::name() const
 
100
 
 
101
    Returns the name of the index.
 
102
*/
 
103
 
 
104
/*!
 
105
    Appends the field \a field to the list of indexed fields. The
 
106
    field is appended with an ascending sort order.
 
107
*/
 
108
 
 
109
void QSqlIndex::append(const QSqlField& field)
 
110
{
 
111
    append(field, false);
 
112
}
 
113
 
 
114
/*!
 
115
    \overload
 
116
 
 
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
 
119
    true.
 
120
*/
 
121
 
 
122
void QSqlIndex::append(const QSqlField& field, bool desc)
 
123
{
 
124
    sorts.append(desc);
 
125
    QSqlRecord::append(field);
 
126
}
 
127
 
 
128
 
 
129
/*!
 
130
    Returns true if field \a i in the index is sorted in descending
 
131
    order; otherwise returns false.
 
132
*/
 
133
 
 
134
bool QSqlIndex::isDescending(int i) const
 
135
{
 
136
    if (i >= 0 && i < sorts.size())
 
137
        return sorts[i];
 
138
    return false;
 
139
}
 
140
 
 
141
/*!
 
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.
 
145
*/
 
146
 
 
147
void QSqlIndex::setDescending(int i, bool desc)
 
148
{
 
149
    if (i >= 0 && i < sorts.size())
 
150
        sorts[i] = desc;
 
151
}
 
152
 
 
153
#ifdef QT3_SUPPORT
 
154
 
 
155
/*!
 
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:
 
161
 
 
162
    "\a{prefix}.<fieldname>"
 
163
 
 
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.
 
167
*/
 
168
 
 
169
QString QSqlIndex::toString(const QString& prefix, const QString& sep, bool verbose) const
 
170
{
 
171
    QString s;
 
172
    bool comma = false;
 
173
    for (int i = 0; i < count(); ++i) {
 
174
        if(comma)
 
175
            s += sep + QLatin1Char(' ');
 
176
        s += createField(i, prefix, verbose);
 
177
        comma = true;
 
178
    }
 
179
    return s;
 
180
}
 
181
 
 
182
/*!
 
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
 
186
    the form:
 
187
 
 
188
    "\a{prefix}.<fieldname>"
 
189
 
 
190
    If \a verbose is true (the default), each field contains a suffix
 
191
    indicating an ASCending or DESCending sort order.
 
192
 
 
193
    Note that if you want to iterate over the list, you should iterate
 
194
    over a copy, e.g.
 
195
    \code
 
196
    QStringList list = myIndex.toStringList();
 
197
    QStringList::Iterator it = list.begin();
 
198
    while(it != list.end()) {
 
199
        myProcessing(*it);
 
200
        ++it;
 
201
    }
 
202
    \endcode
 
203
 
 
204
*/
 
205
QStringList QSqlIndex::toStringList(const QString& prefix, bool verbose) const
 
206
{
 
207
    QStringList s;
 
208
    for (int i = 0; i < count(); ++i)
 
209
        s += createField(i, prefix, verbose);
 
210
    return s;
 
211
}
 
212
#endif
 
213
 
 
214
/*! \internal
 
215
 
 
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.
 
219
*/
 
220
 
 
221
QString QSqlIndex::createField(int i, const QString& prefix, bool verbose) const
 
222
{
 
223
    QString f;
 
224
    if (!prefix.isEmpty())
 
225
        f += prefix + QLatin1Char('.');
 
226
    f += field(i).name();
 
227
    if (verbose)
 
228
        f += QLatin1Char(' ') + QString((isDescending(i)
 
229
                    ? QLatin1String("DESC") : QLatin1String("ASC")));
 
230
    return f;
 
231
}
 
232
 
 
233
/*!
 
234
    \fn QString QSqlIndex::cursorName() const
 
235
 
 
236
    Returns the name of the cursor which the index is associated with.
 
237
*/
 
238
 
 
239
 
 
240
/*!
 
241
    Sets the name of the cursor that the index is associated with to
 
242
    \a cursorName.
 
243
*/
 
244
void QSqlIndex::setCursorName(const QString& cursorName)
 
245
{
 
246
    cursor = cursorName;
 
247
}
 
248