KDb

KDbIndexSchema.h
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#ifndef KDB_INDEXSCHEMA_H
21#define KDB_INDEXSCHEMA_H
22
23#include <QSet>
24
25#include "KDbObject.h"
26#include "KDbFieldList.h"
27
28class KDbConnection;
29class KDbTableSchema;
30class KDbQuerySchema;
31class KDbRelationship;
32
33/*! @short Provides information about database index that can be created for a database table.
34
35 KDbIndexSchema object stores information about table fields that
36 defines this index and additional properties like: whether index is unique
37 or primary key (requires unique). Single-field index can be also auto generated.
38*/
39class KDB_EXPORT KDbIndexSchema : public KDbFieldList, public KDbObject
40{
41public:
42 /*! Constructs empty index schema object that not assigned to any @a table.
43 KDbTableSchema::addIndex() should be called afterwards, before adding any fields
44 or attaching relationships.
45 Any fields added with addField() will not be owned by index but by their table.
46 */
48
49 /*! Deletes the index. Referenced KDbField objects are not deleted.
50 All KDbRelationship objects listed by masterRelationships() are detached from
51 detail-side indices and then deleted.
52 KDbRelationship objects listed by detailsRelationships() are not deleted. */
53 ~KDbIndexSchema() override;
54
55 /*! Adds field at the end of field list.
56 KDbField will not be owned by index. KDbField must belong to a table
57 specified by a KDbTableSchema::addIndex() call, otherwise field couldn't be added.
58 @note Do not forget to add the field to a table, because adding it only to
59 the KDbIndexSchema is not enough. */
60 virtual bool addField(KDbField *field);
61
62 /*! @return table that index belongs to
63 Index should be assigned to a table using KDbTableSchema::addIndex().
64 If it is not, table() returns @c nullptr. */
65 KDbTableSchema* table();
66
67 /*! @return table that index is defined for, const version. */
68 const KDbTableSchema* table() const;
69
70 /*! @return list of relationships from the table (of this index),
71 i.e. any such relationship in which this table is at 'master' side.
72 See KDbRelationship class documentation for more information.
73 All objects on this list will be automatically deleted when this KDbIndexSchema
74 object is deleted. */
75 QList<const KDbRelationship*> masterRelationships() const;
76
77 /*! @return list of relationships to the table (of this index),
78 i.e. any such relationship in which this table is at 'details' side.
79 See KDbRelationship class documentation for more information. */
80 QList<const KDbRelationship*> detailsRelationships() const;
81
82 /*! Attaches relationship definition @a rel to this KDbIndexSchema object.
83 If @a rel relationship has this KDbIndexSchema defined at the master-side,
84 @a rel is added to the list of master relationships (available with masterRelationships()).
85 If @a rel relationship has this KDbIndexSchema defined at the details-side,
86 @a rel is added to the list of details relationships (available with detailsRelationships()).
87 For the former case, attached @a rel object is now owned by this KDbIndexSchema object.
88
89 Note: call detachRelationship() for KDbIndexSchema object that @a rel
90 was previously attached to, if any.
91 @note Before using attachRelationship() the index KDbField must already belong to a table
92 specified by a KDbTableSchema::addIndex() call. */
93 void attachRelationship(KDbRelationship *rel);
94
95 /*! Detaches relationship definition @a rel for this KDbIndexSchema object
96 from the list of master relationships (available with masterRelationships()),
97 or from details relationships list, depending for which side of the relationship
98 is this IndexSchem object assigned.
99
100 Note: If @a rel was detached from masterRelationships() list,
101 this object now has no parent, so it must be attached to other index or deleted.
102 */
103 void detachRelationship(KDbRelationship *rel);
104
105 /*! @return true if index is auto-generated.
106 Auto-generated index is one-field index
107 that was automatically generated
108 for CREATE TABLE statement when the field has
109 UNIQUE or PRIMARY KEY constraint enabled.
110
111 Any newly created KDbIndexSchema object
112 has this flag set to false.
113
114 This flag is handled internally by KDbTableSchema.
115 It can be usable for GUI application if we do not
116 want display implicity/auto generated indices
117 on the indices list or we if want to show these
118 indices to the user in a special way.
119 */
120 bool isAutoGenerated() const;
121
122 /*! @return true if this index is primary key of its table.
123 This can be one or multifield. */
124 bool isPrimaryKey() const;
125
126 /*! Sets PRIMARY KEY flag. @see isPrimary().
127 Note: Setting PRIMARY KEY on (true),
128 UNIQUE flag will be also implicity set. */
129 void setPrimaryKey(bool set);
130
131 /*! @return true if this is unique index.
132 This can be one or multifield. */
133 bool isUnique() const;
134
135 /*! Sets UNIQUE flag. @see isUnique().
136 Note: Setting UNIQUE off (false), PRIMARY KEY flag will
137 be also implicity set off, because this UNIQUE
138 is the requirement for PRIMARY KEYS. */
139 void setUnique(bool set);
140
141 /*! @return true if the index defines a foreign key,
142 Created implicity for KDbRelationship object.*/
143 bool isForeignKey() const;
144
145protected:
146 //! Used by KDbTableSchema::copyIndex(const KDbIndexSchema&)
147 KDbIndexSchema(const KDbIndexSchema& index, KDbTableSchema* parentTable);
148
149 //! Assigns this index to @a table
150 //! table() must be @c nullptr and @a table must be not be @a nullptr.
151 //! @since 3.1
152 void setTable(KDbTableSchema *table);
153
154 /*! Sets auto-generated flag. This method should be called only
155 from KDbTableSchema code
156 @see isAutoGenerated(). */
157 void setAutoGenerated(bool set);
158
159 /*! If @a set is true, declares that the index defines a foreign key,
160 created implicity for KDbRelationship object. Setting this to true, implies
161 clearing 'primary key', 'unique' and 'auto generated' flags.
162 If this index contains just single field, it's 'foreign field'
163 flag will be set to true as well. */
164 void setForeignKey(bool set);
165
166 /*! Internal version of attachRelationship(). If @a ownedByMaster is true,
167 attached @a rel object will be owned by this index. */
168 void attachRelationship(KDbRelationship *rel, bool ownedByMaster);
169
170 friend class KDbConnection;
171 friend class KDbTableSchema;
172 friend class KDbQuerySchema;
173 friend class KDbRelationship;
174private:
175 class Private;
176 Private * const d;
177 Q_DISABLE_COPY(KDbIndexSchema)
178};
179
180//! Sends information about index schema @a index to debug output @a dbg.
181KDB_EXPORT QDebug operator<<(QDebug dbg, const KDbIndexSchema& index);
182
183#endif
Provides database connection, allowing queries and data modification.
bool addField(KDbField *field)
Meta-data for a field.
Definition KDbField.h:72
Provides information about database index that can be created for a database table.
KDbQuerySchema provides information about database query.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:00:42 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.