1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qsqlindex.h"
5
6#include "qsqlfield.h"
7
8QT_BEGIN_NAMESPACE
9
10using namespace Qt::StringLiterals;
11
12/*!
13 \class QSqlIndex
14 \brief The QSqlIndex class provides functions to manipulate and
15 describe database indexes.
16
17 \ingroup database
18 \inmodule QtSql
19
20 An \e index refers to a single table or view in a database.
21 Information about the fields that comprise the index can be used
22 to generate SQL statements.
23*/
24
25/*!
26 Constructs an empty index using the cursor name \a cursorname and
27 index name \a name.
28*/
29
30QSqlIndex::QSqlIndex(const QString& cursorname, const QString& name)
31 : cursor(cursorname), nm(name)
32{
33}
34
35/*!
36 Constructs a copy of \a other.
37*/
38
39QSqlIndex::QSqlIndex(const QSqlIndex& other)
40 : QSqlRecord(other), cursor(other.cursor), nm(other.nm), sorts(other.sorts)
41{
42}
43
44/*! \fn QSqlIndex::QSqlIndex(QSqlIndex &&other)
45 Move-constructs a new QSqlIndex from \a other.
46
47 \note The moved-from object \a other is placed in a
48 partially-formed state, in which the only valid operations are
49 destruction and assignment of a new value.
50
51 \since 6.6
52*/
53/*! \fn QSqlIndex& QSqlIndex::operator=(QSqlIndex &&other)
54 Move-assigns \a other to this QSqlIndex instance.
55
56 \note The moved-from object \a other is placed in a
57 partially-formed state, in which the only valid operations are
58 destruction and assignment of a new value.
59
60 \since 6.6
61*/
62
63/*!
64 Sets the index equal to \a other.
65*/
66
67QSqlIndex& QSqlIndex::operator=(const QSqlIndex& other)
68{
69 cursor = other.cursor;
70 nm = other.nm;
71 sorts = other.sorts;
72 QSqlRecord::operator=(other);
73 return *this;
74}
75
76
77/*!
78 Destroys the object and frees any allocated resources.
79*/
80
81QSqlIndex::~QSqlIndex()
82{
83
84}
85
86/*!
87 Sets the name of the index to \a name.
88*/
89
90void QSqlIndex::setName(const QString& name)
91{
92 nm = name;
93}
94
95/*!
96 \fn QString QSqlIndex::name() const
97
98 Returns the name of the index.
99*/
100
101/*!
102 Appends the field \a field to the list of indexed fields. The
103 field is appended with an ascending sort order.
104*/
105
106void QSqlIndex::append(const QSqlField& field)
107{
108 append(field, desc: false);
109}
110
111/*!
112 \overload
113
114 Appends the field \a field to the list of indexed fields. The
115 field is appended with an ascending sort order, unless \a desc is
116 true.
117*/
118
119void QSqlIndex::append(const QSqlField& field, bool desc)
120{
121 sorts.append(t: desc);
122 QSqlRecord::append(field);
123}
124
125
126/*!
127 Returns \c true if field \a i in the index is sorted in descending
128 order; otherwise returns \c false.
129*/
130
131bool QSqlIndex::isDescending(int i) const
132{
133 if (i >= 0 && i < sorts.size())
134 return sorts[i];
135 return false;
136}
137
138/*!
139 If \a desc is true, field \a i is sorted in descending order.
140 Otherwise, field \a i is sorted in ascending order (the default).
141 If the field does not exist, nothing happens.
142*/
143
144void QSqlIndex::setDescending(int i, bool desc)
145{
146 if (i >= 0 && i < sorts.size())
147 sorts[i] = desc;
148}
149
150/*! \internal
151
152 Creates a string representing the field number \a i using prefix \a
153 prefix. If \a verbose is true, ASC or DESC is included in the field
154 description if the field is sorted in ASCending or DESCending order.
155*/
156
157QString QSqlIndex::createField(int i, const QString& prefix, bool verbose) const
158{
159 QString f;
160 if (!prefix.isEmpty())
161 f += prefix + u'.';
162 f += field(i).name();
163 if (verbose)
164 f += u' ' + QString((isDescending(i) ? "DESC"_L1 : "ASC"_L1));
165 return f;
166}
167
168/*!
169 \fn QString QSqlIndex::cursorName() const
170
171 Returns the name of the cursor which the index is associated with.
172*/
173
174
175/*!
176 Sets the name of the cursor that the index is associated with to
177 \a cursorName.
178*/
179void QSqlIndex::setCursorName(const QString& cursorName)
180{
181 cursor = cursorName;
182}
183
184QT_END_NAMESPACE
185

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