kexi

fieldlist.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2003-2005 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/fieldlist.h>
00021 #include <kexidb/object.h>
00022 
00023 #include <kdebug.h>
00024 
00025 #include <assert.h>
00026 
00027 using namespace KexiDB;
00028 
00029 FieldList::FieldList(bool owner)
00030  //reasonable sizes: TODO
00031  : m_fields_by_name(101, false)
00032 {
00033     m_fields.setAutoDelete( owner );
00034     m_fields_by_name.setAutoDelete( false );
00035     m_autoinc_fields = 0;
00036 }
00037 
00038 FieldList::FieldList(const FieldList& fl)
00039  : m_fields_by_name( fl.m_fields_by_name.size() )
00040 {
00041     m_fields.setAutoDelete( fl.m_fields.autoDelete() );
00042     m_fields_by_name.setAutoDelete( false );
00043     m_autoinc_fields = 0;
00044 
00045     //deep copy for the fields
00046     for (Field::ListIterator f_it(fl.m_fields); f_it.current(); ++f_it) {
00047         Field *f = new Field( *f_it.current() );
00048         f->m_parent = this;
00049         addField( f );
00050     }
00051 }
00052 
00053 FieldList::~FieldList()
00054 {
00055     delete m_autoinc_fields;
00056 }
00057 
00058 void FieldList::clear()
00059 {
00060 //  m_name = QString::null;
00061     m_fields.clear();
00062     m_fields_by_name.clear();
00063     m_sqlFields = QString::null;
00064     delete m_autoinc_fields;
00065     m_autoinc_fields = 0;
00066 }
00067 
00068 FieldList& FieldList::insertField(uint index, KexiDB::Field *field)
00069 {
00070     assert(field);
00071     if (!field)
00072         return *this;
00073     if (index>m_fields.count()) {
00074         KexiDBFatal << "FieldList::insertField(): index (" << index << ") out of range" << endl;
00075         return *this;
00076     }
00077     if (!m_fields.insert(index, field))
00078         return *this;
00079     if (!field->name().isEmpty())
00080         m_fields_by_name.insert(field->name().lower(),field);
00081     m_sqlFields = QString::null;
00082     return *this;
00083 }
00084 
00085 void FieldList::renameField(const QString& oldName, const QString& newName)
00086 {
00087     renameField( m_fields_by_name[ oldName ], newName );
00088 }
00089 
00090 void FieldList::renameField(KexiDB::Field *field, const QString& newName)
00091 {
00092     if (!field || field != m_fields_by_name[ field->name() ]) {
00093         KexiDBFatal << "FieldList::renameField() no field found " 
00094             << (field ? QString("\"%1\"").arg(field->name()) : QString::null) << endl;
00095         return;
00096     }
00097     m_fields_by_name.take( field->name() );
00098     field->setName( newName );
00099     m_fields_by_name.insert( field->name(), field );
00100 }
00101 
00102 
00103 FieldList& FieldList::addField(KexiDB::Field *field)
00104 {
00105     return insertField(m_fields.count(), field);
00106 }
00107 
00108 void FieldList::removeField(KexiDB::Field *field)
00109 {
00110     assert(field);
00111     if (!field)
00112         return;
00113     m_fields_by_name.remove(field->name());
00114     m_fields.remove(field);
00115     m_sqlFields = QString::null;
00116 }
00117 
00118 Field* FieldList::field(const QString& name)
00119 {
00120     return m_fields_by_name[name];
00121 }
00122 
00123 QString FieldList::debugString()
00124 {
00125     QString dbg;
00126     dbg.reserve(512);
00127     Field::ListIterator it( m_fields );
00128     Field *field;
00129     bool start = true;
00130     if (!it.current())
00131         dbg = "<NO FIELDS>";
00132     for (; (field = it.current())!=0; ++it) {
00133         if (!start)
00134             dbg += ",\n";
00135         else
00136             start = false;
00137         dbg += "  ";
00138         dbg += field->debugString();
00139     }
00140     return dbg;
00141 }
00142 
00143 void FieldList::debug()
00144 {
00145     KexiDBDbg << debugString() << endl;
00146 }
00147 
00148 #define _ADD_FIELD(fname) \
00149 { \
00150     if (fname.isEmpty()) return fl; \
00151     f = m_fields_by_name[fname]; \
00152     if (!f) { delete fl; return 0; } \
00153     fl->addField(f); \
00154 }
00155 
00156 FieldList* FieldList::subList(const QString& n1, const QString& n2, 
00157     const QString& n3, const QString& n4,
00158     const QString& n5, const QString& n6,
00159     const QString& n7, const QString& n8,
00160     const QString& n9, const QString& n10,
00161     const QString& n11, const QString& n12,
00162     const QString& n13, const QString& n14,
00163     const QString& n15, const QString& n16,
00164     const QString& n17, const QString& n18)
00165 {
00166     if (n1.isEmpty())
00167         return 0;
00168     Field *f;
00169     FieldList *fl = new FieldList(false);
00170     _ADD_FIELD(n1);
00171     _ADD_FIELD(n2);
00172     _ADD_FIELD(n3);
00173     _ADD_FIELD(n4);
00174     _ADD_FIELD(n5);
00175     _ADD_FIELD(n6);
00176     _ADD_FIELD(n7);
00177     _ADD_FIELD(n8);
00178     _ADD_FIELD(n9);
00179     _ADD_FIELD(n10);
00180     _ADD_FIELD(n11);
00181     _ADD_FIELD(n12);
00182     _ADD_FIELD(n13);
00183     _ADD_FIELD(n14);
00184     _ADD_FIELD(n15);
00185     _ADD_FIELD(n16);
00186     _ADD_FIELD(n17);
00187     _ADD_FIELD(n18);
00188     return fl;
00189 }
00190 
00191 FieldList* FieldList::subList(const QStringList& list)
00192 {
00193         Field *f;
00194     FieldList *fl = new FieldList(false);
00195     for(QStringList::ConstIterator it = list.constBegin(); it != list.constEnd(); ++it) {
00196         _ADD_FIELD( (*it) );
00197     }
00198     return fl;
00199 }
00200 
00201 QStringList FieldList::names() const
00202 {
00203     QStringList r;
00204 //  for (QDictIterator<Field> it(m_fields_by_name);it.current();++it) {
00205 //      r += it.currentKey().lower();
00206 //  }
00207     for (Field::ListIterator it(m_fields); it.current(); ++it) {
00208         r += it.current()->name().lower();
00209     }
00210     return r;
00211 }
00212 
00213 QString FieldList::sqlFieldsList(Field::List* list, Driver *driver)
00214 {
00215     if (!list)
00216         return QString::null;
00217     QString result;
00218     result.reserve(256);
00219     Field::ListIterator it( *list );
00220     bool start = true;
00221     for (; it.current(); ++it) {
00222         if (!start)
00223             result += ",";
00224         else
00225             start = false;
00226         result += driver->escapeIdentifier( it.current()->name() );
00227     }
00228     return result;
00229 }
00230 
00231 QString FieldList::sqlFieldsList(Driver *driver)
00232 {
00233     if (!m_sqlFields.isEmpty())
00234         return m_sqlFields;
00235 
00236     m_sqlFields = FieldList::sqlFieldsList( &m_fields, driver );
00237     return m_sqlFields;
00238 }
00239 
00240 Field::List* FieldList::autoIncrementFields()
00241 {
00242     if (!m_autoinc_fields) {
00243         m_autoinc_fields = new Field::List();
00244         Field *f;
00245         for (Field::ListIterator f_it(m_fields); (f = f_it.current()); ++f_it) {
00246             if (f->isAutoIncrement()) {
00247                 m_autoinc_fields->append( f_it.current() );
00248             }
00249         }
00250     }
00251     return m_autoinc_fields;
00252 }
KDE Home | KDE Accessibility Home | Description of Access Keys