kexi

indexschema.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2003-2004 Jaroslaw Staniek <js@iidea.pl>
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 <kexidb/indexschema.h>
21
22#include <kexidb/driver.h>
23#include <kexidb/connection.h>
24#include <kexidb/tableschema.h>
25
26#include <assert.h>
27
28#include <kdebug.h>
29
30using namespace KexiDB;
31
33 : FieldList(false)//fields are not owned by IndexSchema object
35 , m_tableSchema(tableSchema)
36 , m_primary( false )
37 , m_unique( false )
38 , m_isAutoGenerated( false )
39 , m_isForeignKey( false )
40{
41 m_master_owned_rels.setAutoDelete(true); //rels at the master-side are owned
42}
43
45// : FieldList(static_cast<const FieldList&>(idx))//fields are not owned by IndexSchema object
46 : FieldList(false)//fields are not owned by IndexSchema object
47 , SchemaData(static_cast<const SchemaData&>(idx))
48 , m_tableSchema(&parentTable)
49 , m_primary( idx.m_primary )
50 , m_unique( idx.m_unique )
51 , m_isAutoGenerated( idx.m_isAutoGenerated )
52 , m_isForeignKey( idx.m_isForeignKey )
53{
54 m_master_owned_rels.setAutoDelete(true); //rels at the master-side are owned
55
56 //deep copy of the fields
57 for (Field::ListIterator f_it(idx.m_fields); f_it.current(); ++f_it) {
58 Field *parentTableField = parentTable.field( f_it.current()->name() );
59 if (!parentTableField) {
60 KexiDBWarn << "IndexSchema::IndexSchema(const IndexSchema& idx, const TableSchema& parentTable): "
61 "cannot find field '" << f_it.current()->name() << " in parentTable. Empty index will be created!" << endl;
63 break;
64 }
65 addField( parentTableField );
66 }
67
68//js TODO: copy relationships!
69// Reference::List m_refs_to; //! list of references to table (of this index)
70// Reference::List m_refs_from; //! list of references from the table (of this index),
71// //! this index is foreign key for these references
72// //! and therefore - owner of these
73}
74
76{
77 /* It's a list of relationships to the table (of this index), i.e. any such relationship in which
78 the table is at 'master' side will be cleared and relationships will be destroyed.
79 So, we need to detach all these relationships from details-side, corresponding indices.
80 */
81
82 TQPtrListIterator<Relationship> it(m_master_owned_rels);
83 for (;it.current();++it) {
84 if (it.current()->detailsIndex()) {
85 it.current()->detailsIndex()->detachRelationship(it.current());
86 }
87 }
88 //ok, now m_master_owned_rels will be just cleared automatically
89}
90
92{
93 if (field->table() != m_tableSchema) {
94 KexiDBDbg << "IndexSchema::addField(" << (field ? field->name() : 0)
95 << "): WARNING: field doas not belong to the same table '"
96 << (field && field->table() ? field->table()->name() : 0)
97 << "'as index!" << endl;
98 return *this;
99 }
100 return FieldList::addField(field);
101}
102
103
105{
106 return m_tableSchema;
107}
108
110{
111 return m_isAutoGenerated;
112}
113
115{
116 m_isAutoGenerated = set;
117}
118
120{
121 return m_primary;
122}
123
125{
126 m_primary = set;
127 if (m_primary)
128 m_unique = true;
129}
130
132{
133 return m_unique;
134}
135
137{
138 m_unique=set;
139 if (!m_unique)
140 m_primary=false;
141}
142
144{
145 m_isForeignKey = set;
146 if (m_isForeignKey) {
147 setUnique(false);
148 }
149 if (fieldCount()==1) {
150 m_fields.first()->setForeignKey(true);
151 }
152}
153
155{
156 return TQString("INDEX ") + schemaDataDebugString() + "\n"
157 + (m_isForeignKey ? "FOREIGN KEY " : "")
158 + (m_isAutoGenerated ? "AUTOGENERATED " : "")
159 + (m_primary ? "PRIMARY " : "")
160 + ((!m_primary) && m_unique ? "UNIQUE " : "")
162}
163
164void IndexSchema::attachRelationship(Relationship *rel)
165{
166 attachRelationship(rel, true);
167}
168
169void IndexSchema::attachRelationship(Relationship *rel, bool ownedByMaster)
170{
171 if (!rel)
172 return;
173 if (rel->masterIndex()==this) {
174 if (ownedByMaster) {
175 if (m_master_owned_rels.findRef(rel)==-1) {
176 m_master_owned_rels.append(rel);
177 }
178 }
179 else {//not owned
180 if (m_master_rels.findRef(rel)==-1) {
181 m_master_rels.append(rel);
182 }
183 }
184 }
185 else if (rel->detailsIndex()==this) {
186 if (m_details_rels.findRef(rel)==-1) {
187 m_details_rels.append(rel);
188 }
189 }
190}
191
192void IndexSchema::detachRelationship(Relationship *rel)
193{
194 if (!rel)
195 return;
196 m_master_owned_rels.take( m_master_owned_rels.findRef(rel) ); //for sanity
197 m_master_rels.take( m_master_rels.findRef(rel) ); //for sanity
198 m_details_rels.take( m_details_rels.findRef(rel) ); //for sanity
199}
FieldList & addField(Field *field)
virtual TQString debugString()
uint fieldCount() const
Definition fieldlist.h:58
Field * field(uint id)
Definition fieldlist.h:76
virtual void clear()
Definition fieldlist.cpp:61
Meta-data for a field.
Definition field.h:73
TQPtrListIterator< Field > ListIterator
iterator for list of fields
Definition field.h:77
virtual TableSchema * table() const
Definition field.cpp:300
Provides information about database index that can be created for a database table.
Definition indexschema.h:44
Relationship::List m_master_rels
void detachRelationship(Relationship *rel)
TableSchema * table() const
void setUnique(bool set)
bool isPrimaryKey() const
Relationship::List m_master_owned_rels
table on that index is built
void setForeignKey(bool set)
void setAutoGenerated(bool set)
bool isAutoGenerated() const
Relationship::List m_details_rels
bool isUnique() const
IndexSchema(TableSchema *tableSchema)
virtual FieldList & addField(Field *field)
void setPrimaryKey(bool set)
void attachRelationship(Relationship *rel)
virtual TQString debugString()
virtual TQString schemaDataDebugString() const
High-level database connectivity library with database backend drivers.
@ IndexObjectType
special
Definition global.h:157