KDb

KDbIndexSchema.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2003-2016 Jarosław Staniek <staniek@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "KDbConnection.h"
21#include "KDbDriver.h"
22#include "KDbIndexSchema.h"
23#include "KDbRelationship.h"
24#include "KDbTableSchema.h"
25#include "kdb_debug.h"
26
27class Q_DECL_HIDDEN KDbIndexSchema::Private
28{
29public:
30 Private()
31 : tableSchema(nullptr)
32 , isPrimary(false)
33 , isUnique(false)
34 , isAutoGenerated(false)
35 , isForeignKey(false)
36 {
37 }
38 ~Private()
39 {
40 /* It's a list of relationships to the table (of this index), i.e. any such
41 * relationship in which the table is at 'master' side will be cleared and
42 * relationships will be destroyed.
43 * So all these relationships must be detached from details-side, corresponding
44 * indices.
45 */
46 for (KDbRelationship* rel : qAsConst(masterOwnedRelationships)) {
47 if (rel->detailsIndex()) {
48 rel->detailsIndex()->detachRelationship(rel);
49 }
50 }
51 qDeleteAll(masterOwnedRelationships);
52 }
53
54 //! table on that index is built
55 KDbTableSchema *tableSchema;
56
57 /*! A set of master relationships for the table (of this index),
58 this index is a master key for these relationships
59 and therefore - owner of these */
60 QSet<KDbRelationship*> masterOwnedRelationships;
61
62 /*! A list of master relationships that are not owned by this schema */
63 QList<const KDbRelationship*> masterRelationships;
64
65 /*! A list of relationships to table (of this index) */
66 QList<const KDbRelationship*> detailsRelationships;
67
68 bool isPrimary;
69 bool isUnique;
70 bool isAutoGenerated;
71 bool isForeignKey;
72};
73
75 : KDbFieldList(false)//fields are not owned by KDbIndexSchema object
76 , KDbObject(KDb::IndexObjectType)
77 , d(new Private)
78{
79}
80
82 : KDbFieldList(false)//fields are not owned by KDbIndexSchema object
83 , KDbObject(static_cast<const KDbObject&>(index))
84 , d(new Private)
85{
86 d->isPrimary = index.isPrimaryKey();
87 d->isUnique = index.isUnique();
88 d->isAutoGenerated = index.isAutoGenerated();
89 d->isForeignKey = index.isForeignKey();
90 // deep copy the field references
91 for(KDbField *f : *index.fields()) {
92 KDbField *parentTableField = parentTable->field(f->name());
93 if (!parentTableField) {
94 kdbWarning() << "Could not find field" << f->name() << "in parentTable. Empty index will be created!";
96 break;
97 }
99 }
100
101//! @todo copy relationships!
102// Reference::List m_refs_to; //! list of references to table (of this index)
103// Reference::List m_refs_from; //! list of references from the table (of this index),
104// //! this index is foreign key for these references
105// //! and therefore - owner of these
106}
107
109{
110 delete d;
111}
112
114{
115 if (this->table()) {
116 kdbWarning() << "Table is already assigned to this index";
117 return;
118 }
119 if (table) {
120 d->tableSchema = table;
121 }
122}
123
125{
126 if (!d->tableSchema || field->table() != d->tableSchema) {
127 kdbWarning() << (field ? field->name() : QString())
128 << "WARNING: field does not belong to the same table"
129 << (field && field->table() ? field->table()->name() : QString())
130 << "as index!";
131 return false;
132 }
134}
135
137{
138 return d->tableSchema;
139}
140
142{
143 return d->tableSchema;
144}
145
147{
148 return d->masterRelationships;
149}
150
152{
153 return d->detailsRelationships;
154}
155
157{
158 return d->isAutoGenerated;
159}
160
162{
163 d->isAutoGenerated = set;
164}
165
167{
168 return d->isPrimary;
169}
170
172{
173 d->isPrimary = set;
174 if (d->isPrimary)
175 d->isUnique = true;
176}
177
179{
180 return d->isUnique;
181}
182
184{
185 d->isUnique = set;
186 if (!d->isUnique)
187 d->isPrimary = false;
188}
189
191{
192 return d->isForeignKey;
193}
194
196{
197 d->isForeignKey = set;
198 if (d->isForeignKey) {
199 setUnique(false);
200 }
201 if (fieldCount() == 1) {
202 fields()->first()->setForeignKey(true);
203 }
204}
205
206KDB_EXPORT QDebug operator<<(QDebug dbg, const KDbIndexSchema& index)
207{
208 dbg.nospace() << QLatin1String("INDEX");
209 dbg.space() << static_cast<const KDbObject&>(index) << '\n';
210 dbg.space() << (index.isForeignKey() ? "FOREIGN KEY" : "");
211 dbg.space() << (index.isAutoGenerated() ? "AUTOGENERATED" : "");
212 dbg.space() << (index.isPrimaryKey() ? "PRIMARY" : "");
213 dbg.space() << ((!index.isPrimaryKey()) && index.isUnique() ? "UNIQUE" : "");
214 dbg.space() << static_cast<const KDbFieldList&>(index);
215 return dbg.space();
216}
217
218void KDbIndexSchema::attachRelationship(KDbRelationship *rel)
219{
220 attachRelationship(rel, true);
221}
222
223void KDbIndexSchema::attachRelationship(KDbRelationship *rel, bool ownedByMaster)
224{
225 if (!rel)
226 return;
227 if (rel->masterIndex() == this) {
228 if (ownedByMaster) {
229 if (!d->masterOwnedRelationships.contains(rel)) {
230 d->masterOwnedRelationships.insert(rel);
231 }
232 } else {//not owned
233 if (!d->masterRelationships.contains(rel)) {
234 d->masterRelationships.append(rel);
235 }
236 }
237 } else if (rel->detailsIndex() == this) {
238 if (!d->detailsRelationships.contains(rel)) {
239 d->detailsRelationships.append(rel);
240 }
241 }
242}
243
244void KDbIndexSchema::detachRelationship(KDbRelationship *rel)
245{
246 if (!rel)
247 return;
248 d->masterOwnedRelationships.remove(rel); //for sanity
249 d->masterRelationships.takeAt(d->masterRelationships.indexOf(rel)); //for sanity
250 d->detailsRelationships.takeAt(d->detailsRelationships.indexOf(rel)); //for sanity
251}
bool addField(KDbField *field)
virtual KDbField * field(int id)
virtual void clear()
int fieldCount() const
KDbField::List * fields()
Meta-data for a field.
Definition KDbField.h:72
KDbTableSchema * table()
Definition KDbField.cpp:585
QString name() const
Definition KDbField.cpp:256
Provides information about database index that can be created for a database table.
virtual bool addField(KDbField *field)
void setAutoGenerated(bool set)
bool isUnique() const
void setPrimaryKey(bool set)
QList< const KDbRelationship * > detailsRelationships() const
bool isPrimaryKey() const
bool isAutoGenerated() const
void attachRelationship(KDbRelationship *rel)
~KDbIndexSchema() override
QList< const KDbRelationship * > masterRelationships() const
void setForeignKey(bool set)
void detachRelationship(KDbRelationship *rel)
bool isForeignKey() const
KDbTableSchema * table()
void setUnique(bool set)
void setTable(KDbTableSchema *table)
Assigns this index to table table() must be nullptr and table must be not be nullptr.
A database connectivity and creation framework.
QDebug & nospace()
QDebug & space()
void append(QList< T > &&value)
bool contains(const AT &value) const const
T & first()
qsizetype indexOf(const AT &value, qsizetype from) const const
T takeAt(qsizetype i)
bool contains(const QSet< T > &other) const const
iterator insert(const T &value)
bool remove(const T &value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 19 2024 12:00:07 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.