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)
00045     : FieldList(static_cast<const FieldList&>(idx))//fields are not owned by IndexSchema object
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); //rels at the master-side are owned
00054 //js TODO: copy relationships!
00055 //  Reference::List m_refs_to; //! list of references to table (of this index)
00056 //  Reference::List m_refs_from; //! list of references from the table (of this index),
00057 //                               //! this index is foreign key for these references
00058 //                               //! and therefore - owner of these
00059 }
00060 
00061 IndexSchema::~IndexSchema()
00062 {
00063     /* It's a list of relationships to the table (of this index), i.e. any such relationship in which
00064      the table is at 'master' side will be cleared and relationships will be destroyed.
00065      So, we need to detach all these relationships from details-side, corresponding indices.
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     //ok, now m_master_owned_rels will be just cleared automatically
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 {//not owned
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) ); //for sanity
00183     m_master_rels.take( m_master_rels.findRef(rel) ); //for sanity
00184     m_details_rels.take( m_details_rels.findRef(rel) ); //for sanity
00185 }
KDE Home | KDE Accessibility Home | Description of Access Keys