| 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 | |
| 45 | QT_BEGIN_NAMESPACE |
| 46 | |
| 47 | /*! |
| 48 | \class QSqlIndex |
| 49 | \brief The QSqlIndex class provides functions to manipulate and |
| 50 | describe database indexes. |
| 51 | |
| 52 | \ingroup database |
| 53 | \inmodule QtSql |
| 54 | |
| 55 | An \e index refers to a single table or view in a database. |
| 56 | Information about the fields that comprise the index can be used |
| 57 | to generate SQL statements. |
| 58 | */ |
| 59 | |
| 60 | /*! |
| 61 | Constructs an empty index using the cursor name \a cursorname and |
| 62 | index name \a name. |
| 63 | */ |
| 64 | |
| 65 | QSqlIndex::QSqlIndex(const QString& cursorname, const QString& name) |
| 66 | : cursor(cursorname), nm(name) |
| 67 | { |
| 68 | } |
| 69 | |
| 70 | /*! |
| 71 | Constructs a copy of \a other. |
| 72 | */ |
| 73 | |
| 74 | QSqlIndex::QSqlIndex(const QSqlIndex& other) |
| 75 | : QSqlRecord(other), cursor(other.cursor), nm(other.nm), sorts(other.sorts) |
| 76 | { |
| 77 | } |
| 78 | |
| 79 | /*! |
| 80 | Sets the index equal to \a other. |
| 81 | */ |
| 82 | |
| 83 | QSqlIndex& QSqlIndex::operator=(const QSqlIndex& other) |
| 84 | { |
| 85 | cursor = other.cursor; |
| 86 | nm = other.nm; |
| 87 | sorts = other.sorts; |
| 88 | QSqlRecord::operator=(other); |
| 89 | return *this; |
| 90 | } |
| 91 | |
| 92 | /*! |
| 93 | Destroys the object and frees any allocated resources. |
| 94 | */ |
| 95 | |
| 96 | QSqlIndex::~QSqlIndex() |
| 97 | { |
| 98 | |
| 99 | } |
| 100 | |
| 101 | /*! |
| 102 | Sets the name of the index to \a name. |
| 103 | */ |
| 104 | |
| 105 | void QSqlIndex::setName(const QString& name) |
| 106 | { |
| 107 | nm = name; |
| 108 | } |
| 109 | |
| 110 | /*! |
| 111 | \fn QString QSqlIndex::name() const |
| 112 | |
| 113 | Returns the name of the index. |
| 114 | */ |
| 115 | |
| 116 | /*! |
| 117 | Appends the field \a field to the list of indexed fields. The |
| 118 | field is appended with an ascending sort order. |
| 119 | */ |
| 120 | |
| 121 | void QSqlIndex::append(const QSqlField& field) |
| 122 | { |
| 123 | append(field, false); |
| 124 | } |
| 125 | |
| 126 | /*! |
| 127 | \overload |
| 128 | |
| 129 | Appends the field \a field to the list of indexed fields. The |
| 130 | field is appended with an ascending sort order, unless \a desc is |
| 131 | true. |
| 132 | */ |
| 133 | |
| 134 | void QSqlIndex::append(const QSqlField& field, bool desc) |
| 135 | { |
| 136 | sorts.append(desc); |
| 137 | QSqlRecord::append(field); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | /*! |
| 142 | Returns \c true if field \a i in the index is sorted in descending |
| 143 | order; otherwise returns \c false. |
| 144 | */ |
| 145 | |
| 146 | bool QSqlIndex::isDescending(int i) const |
| 147 | { |
| 148 | if (i >= 0 && i < sorts.size()) |
| 149 | return sorts[i]; |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | /*! |
| 154 | If \a desc is true, field \a i is sorted in descending order. |
| 155 | Otherwise, field \a i is sorted in ascending order (the default). |
| 156 | If the field does not exist, nothing happens. |
| 157 | */ |
| 158 | |
| 159 | void QSqlIndex::setDescending(int i, bool desc) |
| 160 | { |
| 161 | if (i >= 0 && i < sorts.size()) |
| 162 | sorts[i] = desc; |
| 163 | } |
| 164 | |
| 165 | /*! \internal |
| 166 | |
| 167 | Creates a string representing the field number \a i using prefix \a |
| 168 | prefix. If \a verbose is true, ASC or DESC is included in the field |
| 169 | description if the field is sorted in ASCending or DESCending order. |
| 170 | */ |
| 171 | |
| 172 | QString QSqlIndex::createField(int i, const QString& prefix, bool verbose) const |
| 173 | { |
| 174 | QString f; |
| 175 | if (!prefix.isEmpty()) |
| 176 | f += prefix + QLatin1Char('.'); |
| 177 | f += field(i).name(); |
| 178 | if (verbose) |
| 179 | f += QLatin1Char(' ') + QString((isDescending(i) |
| 180 | ? QLatin1String("DESC" ) : QLatin1String("ASC" ))); |
| 181 | return f; |
| 182 | } |
| 183 | |
| 184 | /*! |
| 185 | \fn QString QSqlIndex::cursorName() const |
| 186 | |
| 187 | Returns the name of the cursor which the index is associated with. |
| 188 | */ |
| 189 | |
| 190 | |
| 191 | /*! |
| 192 | Sets the name of the cursor that the index is associated with to |
| 193 | \a cursorName. |
| 194 | */ |
| 195 | void QSqlIndex::setCursorName(const QString& cursorName) |
| 196 | { |
| 197 | cursor = cursorName; |
| 198 | } |
| 199 | |
| 200 | QT_END_NAMESPACE |
| 201 | |