kexi

roweditbuffer.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2003,2006 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 "roweditbuffer.h"
00021 #include "utils.h"
00022 
00023 #include <kdebug.h>
00024 
00025 using namespace KexiDB;
00026 
00027 
00028 RowEditBuffer::RowEditBuffer(bool dbAwareBuffer)
00029 : m_simpleBuffer(dbAwareBuffer ? 0 : new SimpleMap())
00030 , m_simpleBufferIt(dbAwareBuffer ? 0 : new SimpleMap::ConstIterator())
00031 , m_dbBuffer(dbAwareBuffer ? new DBMap() : 0)
00032 , m_dbBufferIt(dbAwareBuffer ? new DBMap::Iterator() : 0)
00033 , m_defaultValuesDbBuffer(dbAwareBuffer ? new QMap<QueryColumnInfo*,bool>() : 0)
00034 , m_defaultValuesDbBufferIt(dbAwareBuffer ? new QMap<QueryColumnInfo*,bool>::ConstIterator() : 0)
00035 {
00036 }
00037 
00038 RowEditBuffer::~RowEditBuffer()
00039 {
00040     delete m_simpleBuffer;
00041     delete m_simpleBufferIt;
00042     delete m_dbBuffer;
00043     delete m_defaultValuesDbBuffer;
00044     delete m_dbBufferIt;
00045 }
00046 
00047 const QVariant* RowEditBuffer::at( QueryColumnInfo& ci, bool useDefaultValueIfPossible ) const
00048 {
00049     if (!m_dbBuffer) {
00050         KexiDBWarn << "RowEditBuffer::at(QueryColumnInfo&): not db-aware buffer!" << endl;
00051         return 0;
00052     }
00053     *m_dbBufferIt = m_dbBuffer->find( &ci );
00054     QVariant* result = 0;
00055     if (*m_dbBufferIt!=m_dbBuffer->end())
00056         result = &(*m_dbBufferIt).data();
00057     if ( useDefaultValueIfPossible 
00058         && (!result || result->isNull()) 
00059         && ci.field && !ci.field->defaultValue().isNull() && KexiDB::isDefaultValueAllowed(ci.field)
00060         && !hasDefaultValueAt(ci) )
00061     {
00062         //no buffered or stored value: try to get a default value declared in a field, so user can modify it
00063         if (!result)
00064             m_dbBuffer->insert(&ci, ci.field->defaultValue() );
00065         result = &(*m_dbBuffer)[ &ci ];
00066         m_defaultValuesDbBuffer->insert(&ci, true);
00067     }
00068     return (const QVariant*)result;
00069 }
00070 
00071 const QVariant* RowEditBuffer::at( Field& f ) const
00072 {
00073     if (!m_simpleBuffer) {
00074         KexiDBWarn << "RowEditBuffer::at(Field&): this is db-aware buffer!" << endl;
00075         return 0;
00076     }
00077     *m_simpleBufferIt = m_simpleBuffer->find( f.name() );
00078     if (*m_simpleBufferIt==m_simpleBuffer->constEnd())
00079         return 0;
00080     return &(*m_simpleBufferIt).data();
00081 }
00082 
00083 const QVariant* RowEditBuffer::at( const QString& fname ) const
00084 {
00085     if (!m_simpleBuffer) {
00086         KexiDBWarn << "RowEditBuffer::at(Field&): this is db-aware buffer!" << endl;
00087         return 0;
00088     }
00089     *m_simpleBufferIt = m_simpleBuffer->find( fname );
00090     if (*m_simpleBufferIt==m_simpleBuffer->constEnd())
00091         return 0;
00092     return &(*m_simpleBufferIt).data();
00093 }
00094 
00095 void RowEditBuffer::clear() {
00096     if (m_dbBuffer) {
00097         m_dbBuffer->clear(); 
00098         m_defaultValuesDbBuffer->clear();
00099     }
00100     if (m_simpleBuffer)
00101         m_simpleBuffer->clear();
00102 }
00103 
00104 bool RowEditBuffer::isEmpty() const
00105 {
00106     if (m_dbBuffer)
00107         return m_dbBuffer->isEmpty(); 
00108     if (m_simpleBuffer)
00109         return m_simpleBuffer->isEmpty();
00110     return true;
00111 }
00112 
00113 void RowEditBuffer::debug()
00114 {
00115     if (isDBAware()) {
00116         KexiDBDbg << "RowEditBuffer type=DB-AWARE, " << m_dbBuffer->count() <<" items"<< endl;
00117         for (DBMap::ConstIterator it = m_dbBuffer->constBegin(); it!=m_dbBuffer->constEnd(); ++it) {
00118             KexiDBDbg << "* field name=" <<it.key()->field->name()<<" val="
00119                 << (it.data().isNull() ? QString("<NULL>") : it.data().toString()) 
00120                 << (hasDefaultValueAt(*it.key()) ? " DEFAULT" : "") <<endl;
00121         }
00122         return;
00123     }
00124     KexiDBDbg << "RowEditBuffer type=SIMPLE, " << m_simpleBuffer->count() <<" items"<< endl;
00125     for (SimpleMap::ConstIterator it = m_simpleBuffer->constBegin(); it!=m_simpleBuffer->constEnd(); ++it) {
00126         KexiDBDbg << "* field name=" <<it.key()<<" val="
00127             << (it.data().isNull() ? QString("<NULL>") : it.data().toString()) <<endl;
00128     }
00129 }
KDE Home | KDE Accessibility Home | Description of Access Keys