kexi

roweditbuffer.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2003,2006 Jaroslaw Staniek <js@iidea.pl>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "roweditbuffer.h"
21#include "utils.h"
22
23#include <kdebug.h>
24
25using namespace KexiDB;
26
27
28RowEditBuffer::RowEditBuffer(bool dbAwareBuffer)
29: m_simpleBuffer(dbAwareBuffer ? 0 : new SimpleMap())
30, m_simpleBufferIt(dbAwareBuffer ? 0 : new SimpleMap::ConstIterator())
31, m_dbBuffer(dbAwareBuffer ? new DBMap() : 0)
32, m_dbBufferIt(dbAwareBuffer ? new DBMap::Iterator() : 0)
33, m_defaultValuesDbBuffer(dbAwareBuffer ? new TQMap<QueryColumnInfo*,bool>() : 0)
34, m_defaultValuesDbBufferIt(dbAwareBuffer ? new TQMap<QueryColumnInfo*,bool>::ConstIterator() : 0)
35{
36}
37
38RowEditBuffer::~RowEditBuffer()
39{
40 delete m_simpleBuffer;
41 delete m_simpleBufferIt;
42 delete m_dbBuffer;
43 delete m_defaultValuesDbBuffer;
44 delete m_dbBufferIt;
45}
46
47const TQVariant* RowEditBuffer::at( QueryColumnInfo& ci, bool useDefaultValueIfPossible ) const
48{
49 if (!m_dbBuffer) {
50 KexiDBWarn << "RowEditBuffer::at(QueryColumnInfo&): not db-aware buffer!" << endl;
51 return 0;
52 }
53 *m_dbBufferIt = m_dbBuffer->find( &ci );
54 TQVariant* result = 0;
55 if (*m_dbBufferIt!=m_dbBuffer->end())
56 result = &(*m_dbBufferIt).data();
57 if ( useDefaultValueIfPossible
58 && (!result || result->isNull())
59 && ci.field && !ci.field->defaultValue().isNull() && KexiDB::isDefaultValueAllowed(ci.field)
60 && !hasDefaultValueAt(ci) )
61 {
62 //no buffered or stored value: try to get a default value declared in a field, so user can modify it
63 if (!result)
64 m_dbBuffer->insert(&ci, ci.field->defaultValue() );
65 result = &(*m_dbBuffer)[ &ci ];
66 m_defaultValuesDbBuffer->insert(&ci, true);
67 }
68 return (const TQVariant*)result;
69}
70
71const TQVariant* RowEditBuffer::at( Field& f ) const
72{
73 if (!m_simpleBuffer) {
74 KexiDBWarn << "RowEditBuffer::at(Field&): this is db-aware buffer!" << endl;
75 return 0;
76 }
77 *m_simpleBufferIt = m_simpleBuffer->find( f.name() );
78 if (*m_simpleBufferIt==m_simpleBuffer->constEnd())
79 return 0;
80 return &(*m_simpleBufferIt).data();
81}
82
83const TQVariant* RowEditBuffer::at( const TQString& fname ) const
84{
85 if (!m_simpleBuffer) {
86 KexiDBWarn << "RowEditBuffer::at(Field&): this is db-aware buffer!" << endl;
87 return 0;
88 }
89 *m_simpleBufferIt = m_simpleBuffer->find( fname );
90 if (*m_simpleBufferIt==m_simpleBuffer->constEnd())
91 return 0;
92 return &(*m_simpleBufferIt).data();
93}
94
95void RowEditBuffer::clear() {
96 if (m_dbBuffer) {
97 m_dbBuffer->clear();
98 m_defaultValuesDbBuffer->clear();
99 }
100 if (m_simpleBuffer)
101 m_simpleBuffer->clear();
102}
103
104bool RowEditBuffer::isEmpty() const
105{
106 if (m_dbBuffer)
107 return m_dbBuffer->isEmpty();
108 if (m_simpleBuffer)
109 return m_simpleBuffer->isEmpty();
110 return true;
111}
112
114{
115 if (isDBAware()) {
116 KexiDBDbg << "RowEditBuffer type=DB-AWARE, " << m_dbBuffer->count() <<" items"<< endl;
117 for (DBMap::ConstIterator it = m_dbBuffer->constBegin(); it!=m_dbBuffer->constEnd(); ++it) {
118 KexiDBDbg << "* field name=" <<it.key()->field->name()<<" val="
119 << (it.data().isNull() ? TQString("<NULL>") : it.data().toString())
120 << (hasDefaultValueAt(*it.key()) ? " DEFAULT" : "") <<endl;
121 }
122 return;
123 }
124 KexiDBDbg << "RowEditBuffer type=SIMPLE, " << m_simpleBuffer->count() <<" items"<< endl;
125 for (SimpleMap::ConstIterator it = m_simpleBuffer->constBegin(); it!=m_simpleBuffer->constEnd(); ++it) {
126 KexiDBDbg << "* field name=" <<it.key()<<" val="
127 << (it.data().isNull() ? TQString("<NULL>") : it.data().toString()) <<endl;
128 }
129}
Meta-data for a field.
Definition field.h:73
TQVariant defaultValue() const
Definition field.h:326
Helper class that assigns additional information for the column in a query.
Definition queryschema.h:49
const TQVariant * at(QueryColumnInfo &ci, bool useDefaultValueIfPossible=true) const
bool hasDefaultValueAt(QueryColumnInfo &ci) const
Useful only for db-aware buffer:
void debug()
For debugging purposes.
High-level database connectivity library with database backend drivers.
KEXI_DB_EXPORT bool isDefaultValueAllowed(Field *field)