summaryrefslogtreecommitdiffstats
path: root/kexi/kexidb/indexschema.cpp
blob: 20dc9e82945d5635d07c5959c6910dbf75dd7f13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* This file is part of the KDE project
   Copyright (C) 2003-2004 Jaroslaw Staniek <js@iidea.pl>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include <kexidb/indexschema.h>

#include <kexidb/driver.h>
#include <kexidb/connection.h>
#include <kexidb/tableschema.h>

#include <assert.h>

#include <kdebug.h>

using namespace KexiDB;

IndexSchema::IndexSchema(TableSchema *tableSchema)
	: FieldList(false)//fields are not owned by IndexSchema object
	, SchemaData(KexiDB::IndexObjectType)
	, m_tableSchema(tableSchema)
	, m_primary( false )
	, m_unique( false )
	, m_isAutoGenerated( false )
	, m_isForeignKey( false )
{
	m_master_owned_rels.setAutoDelete(true); //rels at the master-side are owned
}

IndexSchema::IndexSchema(const IndexSchema& idx, TableSchema& parentTable)
//	: FieldList(static_cast<const FieldList&>(idx))//fields are not owned by IndexSchema object
	: FieldList(false)//fields are not owned by IndexSchema object
	, SchemaData(static_cast<const SchemaData&>(idx))
	, m_tableSchema(&parentTable)
	, m_primary( idx.m_primary )
	, m_unique( idx.m_unique )
	, m_isAutoGenerated( idx.m_isAutoGenerated )
	, m_isForeignKey( idx.m_isForeignKey )
{
	m_master_owned_rels.setAutoDelete(true); //rels at the master-side are owned

	//deep copy of the fields
	for (Field::ListIterator f_it(idx.m_fields); f_it.current(); ++f_it) {
		Field *parentTableField = parentTable.field( f_it.current()->name() );
		if (!parentTableField) {
			KexiDBWarn << "IndexSchema::IndexSchema(const IndexSchema& idx, const TableSchema& parentTable): "
				"cannot find field '" << f_it.current()->name() << " in parentTable. Empty index will be created!" << endl;
			FieldList::clear();
			break;
		}
		addField( parentTableField );
	}

//js TODO: copy relationships!
//	Reference::List m_refs_to; //! list of references to table (of this index)
//	Reference::List m_refs_from; //! list of references from the table (of this index),
//								 //! this index is foreign key for these references
//								 //! and therefore - owner of these
}

IndexSchema::~IndexSchema()
{
	/* It's a list of relationships to the table (of this index), i.e. any such relationship in which
	 the table is at 'master' side will be cleared and relationships will be destroyed.
	 So, we need to detach all these relationships from details-side, corresponding indices.
	*/

	QPtrListIterator<Relationship> it(m_master_owned_rels);
	for (;it.current();++it) {
		if (it.current()->detailsIndex()) {
			it.current()->detailsIndex()->detachRelationship(it.current());
		}
	}
	//ok, now m_master_owned_rels will be just cleared automatically
}

FieldList& IndexSchema::addField(Field *field)
{
	if (field->table() != m_tableSchema) {
		KexiDBDbg << "IndexSchema::addField(" << (field ? field->name() : 0) 
		 << "): WARNING: field doas not belong to the same table '"
		 << (field && field->table() ? field->table()->name() : 0) 
		 << "'as index!" << endl;
		return *this;
	}
	return FieldList::addField(field);
}


KexiDB::TableSchema* IndexSchema::table() const
{
	return m_tableSchema;
}

bool IndexSchema::isAutoGenerated() const
{
	return m_isAutoGenerated;
}

void IndexSchema::setAutoGenerated(bool set)
{
	m_isAutoGenerated = set;
}

bool IndexSchema::isPrimaryKey() const
{
	return m_primary;
}

void IndexSchema::setPrimaryKey(bool set)
{
	m_primary = set;
	if (m_primary)
		m_unique = true;
}

bool IndexSchema::isUnique() const
{
	return m_unique;
}

void IndexSchema::setUnique(bool set)
{
	m_unique=set;
	if (!m_unique)
		m_primary=false;
}

void IndexSchema::setForeignKey(bool set)
{
	m_isForeignKey = set;
	if (m_isForeignKey) {
		setUnique(false);
	}
	if (fieldCount()==1) {
		m_fields.first()->setForeignKey(true);
	}
}

QString IndexSchema::debugString()
{
	return QString("INDEX ") + schemaDataDebugString() + "\n"
		+ (m_isForeignKey ? "FOREIGN KEY " : "")
		+ (m_isAutoGenerated ? "AUTOGENERATED " : "")
		+ (m_primary ? "PRIMARY " : "")
		+ ((!m_primary) && m_unique ? "UNIQUE " : "")
		+ FieldList::debugString();
}

void IndexSchema::attachRelationship(Relationship *rel)
{
	attachRelationship(rel, true);
}

void IndexSchema::attachRelationship(Relationship *rel, bool ownedByMaster)
{
	if (!rel)
		return;
	if (rel->masterIndex()==this) {
		if (ownedByMaster) {
			if (m_master_owned_rels.findRef(rel)==-1) {
				m_master_owned_rels.append(rel);
			}
		}
		else {//not owned
			if (m_master_rels.findRef(rel)==-1) {
				m_master_rels.append(rel);
			}
		}
	}
	else if (rel->detailsIndex()==this) {
		if (m_details_rels.findRef(rel)==-1) {
			m_details_rels.append(rel);
		}
	}
}

void IndexSchema::detachRelationship(Relationship *rel)
{
	if (!rel)
		return;
	m_master_owned_rels.take( m_master_owned_rels.findRef(rel) ); //for sanity
	m_master_rels.take( m_master_rels.findRef(rel) ); //for sanity
	m_details_rels.take( m_details_rels.findRef(rel) ); //for sanity
}