kexi
indexschema.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <kexidb/indexschema.h>
00021
00022 #include <kexidb/driver.h>
00023 #include <kexidb/connection.h>
00024 #include <kexidb/tableschema.h>
00025
00026 #include <assert.h>
00027
00028 #include <kdebug.h>
00029
00030 using namespace KexiDB;
00031
00032 IndexSchema::IndexSchema(TableSchema *tableSchema)
00033 : FieldList(false)
00034 , SchemaData(KexiDB::IndexObjectType)
00035 , m_tableSchema(tableSchema)
00036 , m_primary( false )
00037 , m_unique( false )
00038 , m_isAutoGenerated( false )
00039 , m_isForeignKey( false )
00040 {
00041 m_master_owned_rels.setAutoDelete(true);
00042 }
00043
00044 IndexSchema::IndexSchema(const IndexSchema& idx)
00045 : FieldList(static_cast<const FieldList&>(idx))
00046 , SchemaData(static_cast<const SchemaData&>(idx))
00047 , m_tableSchema(idx.m_tableSchema)
00048 , m_primary( idx.m_primary )
00049 , m_unique( idx.m_unique )
00050 , m_isAutoGenerated( idx.m_isAutoGenerated )
00051 , m_isForeignKey( false )
00052 {
00053 m_master_owned_rels.setAutoDelete(true);
00054
00055
00056
00057
00058
00059 }
00060
00061 IndexSchema::~IndexSchema()
00062 {
00063
00064
00065
00066
00067
00068 QPtrListIterator<Relationship> it(m_master_owned_rels);
00069 for (;it.current();++it) {
00070 if (it.current()->detailsIndex()) {
00071 it.current()->detailsIndex()->detachRelationship(it.current());
00072 }
00073 }
00074
00075 }
00076
00077 FieldList& IndexSchema::addField(Field *field)
00078 {
00079 if (field->table() != m_tableSchema) {
00080 KexiDBDbg << "IndexSchema::addField(" << (field ? field->name() : 0)
00081 << "): WARNING: field doas not belong to the same table '"
00082 << (field && field->table() ? field->table()->name() : 0)
00083 << "'as index!" << endl;
00084 return *this;
00085 }
00086 return FieldList::addField(field);
00087 }
00088
00089
00090 KexiDB::TableSchema* IndexSchema::table() const
00091 {
00092 return m_tableSchema;
00093 }
00094
00095 bool IndexSchema::isAutoGenerated() const
00096 {
00097 return m_isAutoGenerated;
00098 }
00099
00100 void IndexSchema::setAutoGenerated(bool set)
00101 {
00102 m_isAutoGenerated = set;
00103 }
00104
00105 bool IndexSchema::isPrimaryKey() const
00106 {
00107 return m_primary;
00108 }
00109
00110 void IndexSchema::setPrimaryKey(bool set)
00111 {
00112 m_primary = set;
00113 if (m_primary)
00114 m_unique = true;
00115 }
00116
00117 bool IndexSchema::isUnique() const
00118 {
00119 return m_unique;
00120 }
00121
00122 void IndexSchema::setUnique(bool set)
00123 {
00124 m_unique=set;
00125 if (!m_unique)
00126 m_primary=false;
00127 }
00128
00129 void IndexSchema::setForeignKey(bool set)
00130 {
00131 m_isForeignKey = set;
00132 if (m_isForeignKey) {
00133 setUnique(false);
00134 }
00135 if (fieldCount()==1) {
00136 m_fields.first()->setForeignKey(true);
00137 }
00138 }
00139
00140 QString IndexSchema::debugString()
00141 {
00142 return QString("INDEX ") + schemaDataDebugString() + "\n"
00143 + (m_isForeignKey ? "FOREIGN KEY " : "")
00144 + (m_isAutoGenerated ? "AUTOGENERATED " : "")
00145 + (m_primary ? "PRIMARY " : "")
00146 + ((!m_primary) && m_unique ? "UNIQUE " : "")
00147 + FieldList::debugString();
00148 }
00149
00150 void IndexSchema::attachRelationship(Relationship *rel)
00151 {
00152 attachRelationship(rel, true);
00153 }
00154
00155 void IndexSchema::attachRelationship(Relationship *rel, bool ownedByMaster)
00156 {
00157 if (!rel)
00158 return;
00159 if (rel->masterIndex()==this) {
00160 if (ownedByMaster) {
00161 if (m_master_owned_rels.findRef(rel)==-1) {
00162 m_master_owned_rels.append(rel);
00163 }
00164 }
00165 else {
00166 if (m_master_rels.findRef(rel)==-1) {
00167 m_master_rels.append(rel);
00168 }
00169 }
00170 }
00171 else if (rel->detailsIndex()==this) {
00172 if (m_details_rels.findRef(rel)==-1) {
00173 m_details_rels.append(rel);
00174 }
00175 }
00176 }
00177
00178 void IndexSchema::detachRelationship(Relationship *rel)
00179 {
00180 if (!rel)
00181 return;
00182 m_master_owned_rels.take( m_master_owned_rels.findRef(rel) );
00183 m_master_rels.take( m_master_rels.findRef(rel) );
00184 m_details_rels.take( m_details_rels.findRef(rel) );
00185 }
|