1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtSql 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 https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qsqlindex.h"
41
42#include "qsqlfield.h"
43#include "qstringlist.h"
44
45QT_BEGIN_NAMESPACE
46
47// ### Qt 6: remove the static assertion, the 'sorts' field was changed from QList to QVector in Qt 5.6
48Q_STATIC_ASSERT((sizeof(QList<bool>) == sizeof(QVector<bool>)));
49
50/*!
51 \class QSqlIndex
52 \brief The QSqlIndex class provides functions to manipulate and
53 describe database indexes.
54
55 \ingroup database
56 \inmodule QtSql
57
58 An \e index refers to a single table or view in a database.
59 Information about the fields that comprise the index can be used
60 to generate SQL statements.
61*/
62
63/*!
64 Constructs an empty index using the cursor name \a cursorname and
65 index name \a name.
66*/
67
68QSqlIndex::QSqlIndex(const QString& cursorname, const QString& name)
69 : cursor(cursorname), nm(name)
70{
71}
72
73/*!
74 Constructs a copy of \a other.
75*/
76
77QSqlIndex::QSqlIndex(const QSqlIndex& other)
78 : QSqlRecord(other), cursor(other.cursor), nm(other.nm), sorts(other.sorts)
79{
80}
81
82/*!
83 Sets the index equal to \a other.
84*/
85
86QSqlIndex& QSqlIndex::operator=(const QSqlIndex& other)
87{
88 cursor = other.cursor;
89 nm = other.nm;
90 sorts = other.sorts;
91 QSqlRecord::operator=(other);
92 return *this;
93}
94
95/*!
96 Destroys the object and frees any allocated resources.
97*/
98
99QSqlIndex::~QSqlIndex()
100{
101
102}
103
104/*!
105 Sets the name of the index to \a name.
106*/
107
108void QSqlIndex::setName(const QString& name)
109{
110 nm = name;
111}
112
113/*!
114 \fn QString QSqlIndex::name() const
115
116 Returns the name of the index.
117*/
118
119/*!
120 Appends the field \a field to the list of indexed fields. The
121 field is appended with an ascending sort order.
122*/
123
124void QSqlIndex::append(const QSqlField& field)
125{
126 append(field, desc: false);
127}
128
129/*!
130 \overload
131
132 Appends the field \a field to the list of indexed fields. The
133 field is appended with an ascending sort order, unless \a desc is
134 true.
135*/
136
137void QSqlIndex::append(const QSqlField& field, bool desc)
138{
139 sorts.append(t: desc);
140 QSqlRecord::append(field);
141}
142
143
144/*!
145 Returns \c true if field \a i in the index is sorted in descending
146 order; otherwise returns \c false.
147*/
148
149bool QSqlIndex::isDescending(int i) const
150{
151 if (i >= 0 && i < sorts.size())
152 return sorts[i];
153 return false;
154}
155
156/*!
157 If \a desc is true, field \a i is sorted in descending order.
158 Otherwise, field \a i is sorted in ascending order (the default).
159 If the field does not exist, nothing happens.
160*/
161
162void QSqlIndex::setDescending(int i, bool desc)
163{
164 if (i >= 0 && i < sorts.size())
165 sorts[i] = desc;
166}
167
168/*! \internal
169
170 Creates a string representing the field number \a i using prefix \a
171 prefix. If \a verbose is true, ASC or DESC is included in the field
172 description if the field is sorted in ASCending or DESCending order.
173*/
174
175QString QSqlIndex::createField(int i, const QString& prefix, bool verbose) const
176{
177 QString f;
178 if (!prefix.isEmpty())
179 f += prefix + QLatin1Char('.');
180 f += field(i).name();
181 if (verbose)
182 f += QLatin1Char(' ') + QString((isDescending(i)
183 ? QLatin1String("DESC") : QLatin1String("ASC")));
184 return f;
185}
186
187/*!
188 \fn QString QSqlIndex::cursorName() const
189
190 Returns the name of the cursor which the index is associated with.
191*/
192
193
194/*!
195 Sets the name of the cursor that the index is associated with to
196 \a cursorName.
197*/
198void QSqlIndex::setCursorName(const QString& cursorName)
199{
200 cursor = cursorName;
201}
202
203QT_END_NAMESPACE
204

source code of qtbase/src/sql/kernel/qsqlindex.cpp