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         kdWarning() << "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 FieldList& FieldList::addField(KexiDB::Field *field)
00086 {
00087     return insertField(m_fields.count(), field);
00088 }
00089 
00090 void FieldList::removeField(KexiDB::Field *field)
00091 {
00092     assert(field);
00093     if (!field)
00094         return;
00095     m_fields_by_name.remove(field->name());
00096     m_fields.remove(field);
00097     m_sqlFields = QString::null;
00098 }
00099 
00100 Field* FieldList::field(const QString& name)
00101 {
00102     return m_fields_by_name[name];
00103 }
00104 
00105 QString FieldList::debugString()
00106 {
00107     QString dbg;
00108     dbg.reserve(512);
00109     Field::ListIterator it( m_fields );
00110     Field *field;
00111     bool start = true;
00112     if (!it.current())
00113         dbg = "<NO FIELDS>";
00114     for (; (field = it.current())!=0; ++it) {
00115         if (!start)
00116             dbg += ",\n";
00117         else
00118             start = false;
00119         dbg += "  ";
00120         dbg += field->debugString();
00121     }
00122     return dbg;
00123 }
00124 
00125 void FieldList::debug()
00126 {
00127     KexiDBDbg << debugString() << endl;
00128 }
00129 
00130 #define _ADD_FIELD(fname) \
00131 { \
00132     if (fname.isEmpty()) return fl; \
00133     f = m_fields_by_name[fname]; \
00134     if (!f) { delete fl; return 0; } \
00135     fl->addField(f); \
00136 }
00137 
00138 FieldList* FieldList::subList(const QString& n1, const QString& n2, 
00139     const QString& n3, const QString& n4,
00140     const QString& n5, const QString& n6,
00141     const QString& n7, const QString& n8,
00142     const QString& n9, const QString& n10,
00143     const QString& n11, const QString& n12,
00144     const QString& n13, const QString& n14,
00145     const QString& n15, const QString& n16,
00146     const QString& n17, const QString& n18)
00147 {
00148     if (n1.isEmpty())
00149         return 0;
00150     Field *f;
00151     FieldList *fl = new FieldList(false);
00152     _ADD_FIELD(n1);
00153     _ADD_FIELD(n2);
00154     _ADD_FIELD(n3);
00155     _ADD_FIELD(n4);
00156     _ADD_FIELD(n5);
00157     _ADD_FIELD(n6);
00158     _ADD_FIELD(n7);
00159     _ADD_FIELD(n8);
00160     _ADD_FIELD(n9);
00161     _ADD_FIELD(n10);
00162     _ADD_FIELD(n11);
00163     _ADD_FIELD(n12);
00164     _ADD_FIELD(n13);
00165     _ADD_FIELD(n14);
00166     _ADD_FIELD(n15);
00167     _ADD_FIELD(n16);
00168     _ADD_FIELD(n17);
00169     _ADD_FIELD(n18);
00170     return fl;
00171 }
00172 
00173 FieldList* FieldList::subList(const QStringList& list)
00174 {
00175         Field *f;
00176     FieldList *fl = new FieldList(false);
00177     for(QStringList::ConstIterator it = list.constBegin(); it != list.constEnd(); ++it) {
00178         _ADD_FIELD( (*it) );
00179     }
00180     return fl;
00181 }
00182 
00183 QStringList FieldList::names() const
00184 {
00185     QStringList r;
00186 //  for (QDictIterator<Field> it(m_fields_by_name);it.current();++it) {
00187 //      r += it.currentKey().lower();
00188 //  }
00189     for (Field::ListIterator it(m_fields); it.current(); ++it) {
00190         r += it.current()->name().lower();
00191     }
00192     return r;
00193 }
00194 
00195 QString FieldList::sqlFieldsList(Field::List* list, Driver *driver)
00196 {
00197     if (!list)
00198         return QString::null;
00199     QString result;
00200     result.reserve(256);
00201     Field::ListIterator it( *list );
00202     bool start = true;
00203     for (; it.current(); ++it) {
00204         if (!start)
00205             result += ",";
00206         else
00207             start = false;
00208         result += driver->escapeIdentifier( it.current()->name() );
00209     }
00210     return result;
00211 }
00212 
00213 QString FieldList::sqlFieldsList(Driver *driver)
00214 {
00215     if (!m_sqlFields.isEmpty())
00216         return m_sqlFields;
00217 
00218     m_sqlFields = FieldList::sqlFieldsList( &m_fields, driver );
00219     return m_sqlFields;
00220 }
00221 
00222 Field::List* FieldList::autoIncrementFields()
00223 {
00224     if (!m_autoinc_fields) {
00225         m_autoinc_fields = new Field::List();
00226         Field *f;
00227         for (Field::ListIterator f_it(m_fields); (f = f_it.current()); ++f_it) {
00228             if (f->isAutoIncrement()) {
00229                 m_autoinc_fields->append( f_it.current() );
00230             }
00231         }
00232     }
00233     return m_autoinc_fields;
00234 }
KDE Home | KDE Accessibility Home | Description of Access Keys