1
/* This file is part of the KDE project
2
Copyright (C) 2003,2006 Jarosław Staniek <staniek@kde.org>
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.
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.
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.
20
#include "roweditbuffer.h"
25
using namespace KexiDB;
28
RowEditBuffer::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 QMap<QueryColumnInfo*, bool>() : 0)
34
, m_defaultValuesDbBufferIt(dbAwareBuffer ? new QMap<QueryColumnInfo*, bool>::ConstIterator() : 0)
38
RowEditBuffer::~RowEditBuffer()
40
delete m_simpleBuffer;
41
delete m_simpleBufferIt;
44
delete m_defaultValuesDbBuffer;
45
delete m_defaultValuesDbBufferIt;
48
const QVariant* RowEditBuffer::at(QueryColumnInfo& ci, bool useDefaultValueIfPossible) const
51
KexiDBWarn << "RowEditBuffer::at(QueryColumnInfo&): not db-aware buffer!";
54
*m_dbBufferIt = m_dbBuffer->find(&ci);
56
if (*m_dbBufferIt != m_dbBuffer->end())
57
result = &(*m_dbBufferIt).value();
58
if (useDefaultValueIfPossible
59
&& (!result || result->isNull())
60
&& ci.field && !ci.field->defaultValue().isNull() && KexiDB::isDefaultValueAllowed(ci.field)
61
&& !hasDefaultValueAt(ci)) {
62
//no buffered or stored value: try to get a default value declared in a field, so user can modify it
64
m_dbBuffer->insert(&ci, ci.field->defaultValue());
65
result = &(*m_dbBuffer)[ &ci ];
66
m_defaultValuesDbBuffer->insert(&ci, true);
68
return (const QVariant*)result;
71
const QVariant* RowEditBuffer::at(Field& f) const
73
if (!m_simpleBuffer) {
74
KexiDBWarn << "RowEditBuffer::at(Field&): this is db-aware buffer!";
77
*m_simpleBufferIt = m_simpleBuffer->constFind(f.name());
78
if (*m_simpleBufferIt == m_simpleBuffer->constEnd())
80
return &(*m_simpleBufferIt).value();
83
const QVariant* RowEditBuffer::at(const QString& fname) const
85
if (!m_simpleBuffer) {
86
KexiDBWarn << "RowEditBuffer::at(Field&): this is db-aware buffer!";
89
*m_simpleBufferIt = m_simpleBuffer->constFind(fname);
90
if (*m_simpleBufferIt == m_simpleBuffer->constEnd())
92
return &(*m_simpleBufferIt).value();
95
void RowEditBuffer::removeAt(QueryColumnInfo& ci)
98
KexiDBWarn << "not db-aware buffer!";
101
m_dbBuffer->remove(&ci);
104
void RowEditBuffer::removeAt(Field& f)
106
if (!m_simpleBuffer) {
107
KexiDBWarn << "this is db-aware buffer!";
110
m_simpleBuffer->remove(f.name());
113
void RowEditBuffer::removeAt(const QString& fname)
115
if (!m_simpleBuffer) {
116
KexiDBWarn << "this is db-aware buffer!";
119
m_simpleBuffer->remove(fname);
122
void RowEditBuffer::clear()
126
m_defaultValuesDbBuffer->clear();
129
m_simpleBuffer->clear();
132
bool RowEditBuffer::isEmpty() const
135
return m_dbBuffer->isEmpty();
137
return m_simpleBuffer->isEmpty();
141
void RowEditBuffer::debug()
144
KexiDBDbg << "RowEditBuffer type=DB-AWARE, " << m_dbBuffer->count() << " items";
145
for (DBMap::ConstIterator it = m_dbBuffer->constBegin(); it != m_dbBuffer->constEnd(); ++it) {
146
KexiDBDbg << "* field name=" << it.key()->field->name() << " val="
147
<< (it.value().isNull() ? QString("<NULL>") : it.value().toString())
148
<< (hasDefaultValueAt(*it.key()) ? " DEFAULT" : "");
152
KexiDBDbg << "RowEditBuffer type=SIMPLE, " << m_simpleBuffer->count() << " items";
153
for (SimpleMap::ConstIterator it = m_simpleBuffer->constBegin(); it != m_simpleBuffer->constEnd(); ++it) {
154
KexiDBDbg << "* field name=" << it.key() << " val="
155
<< (it.value().isNull() ? QString("<NULL>") : it.value().toString());