kexi

indexschema.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2003-2004 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
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)//fields are not owned by IndexSchema object
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); //rels at the master-side are owned
00042 }
00043 
00044 IndexSchema::IndexSchema(const IndexSchema& idx, TableSchema& parentTable)
00045 //  : FieldList(static_cast<const FieldList&>(idx))//fields are not owned by IndexSchema object
00046     : FieldList(false)//fields are not owned by IndexSchema object
00047     , SchemaData(static_cast<const SchemaData&>(idx))
00048     , m_tableSchema(&parentTable)
00049     , m_primary( idx.m_primary )
00050     , m_unique( idx.m_unique )
00051     , m_isAutoGenerated( idx.m_isAutoGenerated )
00052     , m_isForeignKey( idx.m_isForeignKey )
00053 {
00054     m_master_owned_rels.setAutoDelete(true); //rels at the master-side are owned
00055 
00056     //deep copy of the fields
00057     for (Field::ListIterator f_it(idx.m_fields); f_it.current(); ++f_it) {
00058         Field *parentTableField = parentTable.field( f_it.current()->name() );
00059         if (!parentTableField) {
00060             KexiDBWarn << "IndexSchema::IndexSchema(const IndexSchema& idx, const TableSchema& parentTable): "
00061                 "cannot find field '" << f_it.current()->name() << " in parentTable. Empty index will be created!" << endl;
00062             FieldList::clear();
00063             break;
00064         }
00065         addField( parentTableField );
00066     }
00067 
00068 //js TODO: copy relationships!
00069 //  Reference::List m_refs_to; //! list of references to table (of this index)
00070 //  Reference::List m_refs_from; //! list of references from the table (of this index),
00071 //                               //! this index is foreign key for these references
00072 //                               //! and therefore - owner of these
00073 }
00074 
00075 IndexSchema::~IndexSchema()
00076 {
00077     /* It's a list of relationships to the table (of this index), i.e. any such relationship in which
00078      the table is at 'master' side will be cleared and relationships will be destroyed.
00079      So, we need to detach all these relationships from details-side, corresponding indices.
00080     */
00081 
00082     QPtrListIterator<Relationship> it(m_master_owned_rels);
00083     for (;it.current();++it) {
00084         if (it.current()->detailsIndex()) {
00085             it.current()->detailsIndex()->detachRelationship(it.current());
00086         }
00087     }
00088     //ok, now m_master_owned_rels will be just cleared automatically
00089 }
00090 
00091 FieldList& IndexSchema::addField(Field *field)
00092 {
00093     if (field->table() != m_tableSchema) {
00094         KexiDBDbg << "IndexSchema::addField(" << (field ? field->name() : 0) 
00095          << "): WARNING: field doas not belong to the same table '"
00096          << (field && field->table() ? field->table()->name() : 0) 
00097          << "'as index!" << endl;
00098         return *this;
00099     }
00100     return FieldList::addField(field);
00101 }
00102 
00103 
00104 KexiDB::TableSchema* IndexSchema::table() const
00105 {
00106     return m_tableSchema;
00107 }
00108 
00109 bool IndexSchema::isAutoGenerated() const
00110 {
00111     return m_isAutoGenerated;
00112 }
00113 
00114 void IndexSchema::setAutoGenerated(bool set)
00115 {
00116     m_isAutoGenerated = set;
00117 }
00118 
00119 bool IndexSchema::isPrimaryKey() const
00120 {
00121     return m_primary;
00122 }
00123 
00124 void IndexSchema::setPrimaryKey(bool set)
00125 {
00126     m_primary = set;
00127     if (m_primary)
00128         m_unique = true;
00129 }
00130 
00131 bool IndexSchema::isUnique() const
00132 {
00133     return m_unique;
00134 }
00135 
00136 void IndexSchema::setUnique(bool set)
00137 {
00138     m_unique=set;
00139     if (!m_unique)
00140         m_primary=false;
00141 }
00142 
00143 void IndexSchema::setForeignKey(bool set)
00144 {
00145     m_isForeignKey = set;
00146     if (m_isForeignKey) {
00147         setUnique(false);
00148     }
00149     if (fieldCount()==1) {
00150         m_fields.first()->setForeignKey(true);
00151     }
00152 }
00153 
00154 QString IndexSchema::debugString()
00155 {
00156     return QString("INDEX ") + schemaDataDebugString() + "\n"
00157         + (m_isForeignKey ? "FOREIGN KEY " : "")
00158         + (m_isAutoGenerated ? "AUTOGENERATED " : "")
00159         + (m_primary ? "PRIMARY " : "")
00160         + ((!m_primary) && m_unique ? "UNIQUE " : "")
00161         + FieldList::debugString();
00162 }
00163 
00164 void IndexSchema::attachRelationship(Relationship *rel)
00165 {
00166     attachRelationship(rel, true);
00167 }
00168 
00169 void IndexSchema::attachRelationship(Relationship *rel, bool ownedByMaster)
00170 {
00171     if (!rel)
00172         return;
00173     if (rel->masterIndex()==this) {
00174         if (ownedByMaster) {
00175             if (m_master_owned_rels.findRef(rel)==-1) {
00176                 m_master_owned_rels.append(rel);
00177             }
00178         }
00179         else {//not owned
00180             if (m_master_rels.findRef(rel)==-1) {
00181                 m_master_rels.append(rel);
00182             }
00183         }
00184     }
00185     else if (rel->detailsIndex()==this) {
00186         if (m_details_rels.findRef(rel)==-1) {
00187             m_details_rels.append(rel);
00188         }
00189     }
00190 }
00191 
00192 void IndexSchema::detachRelationship(Relationship *rel)
00193 {
00194     if (!rel)
00195         return;
00196     m_master_owned_rels.take( m_master_owned_rels.findRef(rel) ); //for sanity
00197     m_master_rels.take( m_master_rels.findRef(rel) ); //for sanity
00198     m_details_rels.take( m_details_rels.findRef(rel) ); //for sanity
00199 }
KDE Home | KDE Accessibility Home | Description of Access Keys