KDb

KDbRecordEditBuffer.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2003-2011 Jarosław Staniek <staniek@kde.org>
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 "KDbRecordEditBuffer.h"
21#include "KDb.h"
22#include "KDbQueryColumnInfo.h"
23#include "kdb_debug.h"
24
25KDbRecordEditBuffer::KDbRecordEditBuffer(bool dbAwareBuffer)
26 : m_simpleBuffer(dbAwareBuffer ? nullptr : new SimpleMap())
27 , m_simpleBufferIt(dbAwareBuffer ? nullptr : new SimpleMap::ConstIterator())
28 , m_dbBuffer(dbAwareBuffer ? new DbHash() : nullptr)
29 , m_dbBufferIt(dbAwareBuffer ? new DbHash::Iterator() : nullptr)
30 , m_defaultValuesDbBuffer(dbAwareBuffer ? new QMap<KDbQueryColumnInfo*, bool>() : nullptr)
31 , m_defaultValuesDbBufferIt(dbAwareBuffer ? new QMap<KDbQueryColumnInfo*, bool>::ConstIterator() : nullptr)
32{
33}
34
35KDbRecordEditBuffer::~KDbRecordEditBuffer()
36{
37 delete m_simpleBuffer;
38 delete m_simpleBufferIt;
39 delete m_dbBuffer;
40 delete m_dbBufferIt;
41 delete m_defaultValuesDbBuffer;
42 delete m_defaultValuesDbBufferIt;
43}
44
45bool KDbRecordEditBuffer::isDBAware() const
46{
47 return m_dbBuffer != nullptr;
48}
49
50const QVariant* KDbRecordEditBuffer::at(KDbQueryColumnInfo* ci, bool useDefaultValueIfPossible) const
51{
52 if (!ci) {
53 kdbWarning() << "no KDbQueryColumnInfo provided";
54 return nullptr;
55 }
56 if (!m_dbBuffer) {
57 kdbWarning() << "not db-aware buffer!";
58 return nullptr;
59 }
60 *m_dbBufferIt = m_dbBuffer->find(ci);
61 QVariant* result = nullptr;
62 if (*m_dbBufferIt != m_dbBuffer->end())
63 result = &(*m_dbBufferIt).value();
64 if (useDefaultValueIfPossible
65 && (!result || result->isNull())
66 && ci->field() && !ci->field()->defaultValue().isNull() && KDb::isDefaultValueAllowed(*ci->field())
67 && !hasDefaultValueAt(*ci)) {
68 //no buffered or stored value: try to get a default value declared in a field, so user can modify it
69 if (!result)
70 m_dbBuffer->insert(ci, ci->field()->defaultValue());
71 result = &(*m_dbBuffer)[ ci ];
72 m_defaultValuesDbBuffer->insert(ci, true);
73 }
74 return (const QVariant*)result;
75}
76
77const QVariant* KDbRecordEditBuffer::at(const KDbField &field) const
78{
79 if (!m_simpleBuffer) {
80 kdbWarning() << "this is db-aware buffer!";
81 return nullptr;
82 }
83 *m_simpleBufferIt = m_simpleBuffer->constFind(field.name());
84 if (*m_simpleBufferIt == m_simpleBuffer->constEnd()) {
85 //kdbWarning() << "no such field:" << field;
86 return nullptr;
87 }
88 return &(*m_simpleBufferIt).value();
89}
90
91const QVariant* KDbRecordEditBuffer::at(const QString& fname) const
92{
93 if (!m_simpleBuffer) {
94 kdbWarning() << "this is db-aware buffer!";
95 return nullptr;
96 }
97 *m_simpleBufferIt = m_simpleBuffer->constFind(fname);
98 if (*m_simpleBufferIt == m_simpleBuffer->constEnd()) {
99 //kdbWarning() << "no such field:" << fname;
100 return nullptr;
101 }
102 return &(*m_simpleBufferIt).value();
103}
104
106{
107 if (!m_dbBuffer) {
108 kdbWarning() << "not db-aware buffer!";
109 return;
110 }
111 m_dbBuffer->remove(const_cast<KDbQueryColumnInfo*>(&ci)); // const_cast ok here, we won't modify ci
112}
113
115{
116 if (!m_simpleBuffer) {
117 kdbWarning() << "this is db-aware buffer!";
118 return;
119 }
120 m_simpleBuffer->remove(field.name());
121}
122
124{
125 if (!m_simpleBuffer) {
126 kdbWarning() << "this is db-aware buffer!";
127 return;
128 }
129 m_simpleBuffer->remove(fname);
130}
131
132void KDbRecordEditBuffer::clear()
133{
134 if (m_dbBuffer) {
135 m_dbBuffer->clear();
136 m_defaultValuesDbBuffer->clear();
137 }
138 if (m_simpleBuffer)
139 m_simpleBuffer->clear();
140}
141
142bool KDbRecordEditBuffer::isEmpty() const
143{
144 if (m_dbBuffer)
145 return m_dbBuffer->isEmpty();
146 if (m_simpleBuffer)
147 return m_simpleBuffer->isEmpty();
148 return true;
149}
150
152{
153 if (ci && m_dbBuffer) {
154 m_dbBuffer->insert(ci, val);
155 m_defaultValuesDbBuffer->remove(ci);
156 }
157}
158
159void KDbRecordEditBuffer::insert(const QString &fname, const QVariant &val)
160{
161 if (m_simpleBuffer) {
162 m_simpleBuffer->insert(fname, val);
163 }
164}
165
167{
168 return m_defaultValuesDbBuffer->value(const_cast<KDbQueryColumnInfo*>(&ci), false);
169}
170
171KDbRecordEditBuffer::SimpleMap KDbRecordEditBuffer::simpleBuffer() const
172{
173 return *m_simpleBuffer;
174}
175
176KDbRecordEditBuffer::DbHash KDbRecordEditBuffer::dbBuffer() const
177{
178 return *m_dbBuffer;
179}
180
181KDB_EXPORT QDebug operator<<(QDebug dbg, const KDbRecordEditBuffer& buffer)
182{
183 if (buffer.isDBAware()) {
184 const KDbRecordEditBuffer::DbHash buf(buffer.dbBuffer());
185 dbg.space() << "RecordEditBuffer type=DB-AWARE," << buf.count() << "field(s):\n";
186 for (KDbRecordEditBuffer::DbHash::ConstIterator it = buf.constBegin();
187 it != buf.constEnd(); ++it)
188 {
189 dbg.nospace() << "* field name=" << qPrintable(it.key()->field()->name()) << " val="
190 << (it.value().isNull() ? QLatin1String("<NULL>") : it.value().toString())
191 << (buffer.hasDefaultValueAt(*it.key()) ? " DEFAULT\n" : "\n");
192 }
193 return dbg.space();
194 }
195 const KDbRecordEditBuffer::SimpleMap map(buffer.simpleBuffer());
196 dbg.space() << "RecordEditBuffer type=SIMPLE," << map.count() << "field(s):\n";
198 it != map.constEnd(); ++it)
199 {
200 dbg.nospace() << "* field name=" << qPrintable(it.key()) << " val="
201 << (it.value().isNull() ? QLatin1String("<NULL>") : it.value().toString()) << "\n";
202 }
203 return dbg.space();
204}
Meta-data for a field.
Definition KDbField.h:72
QString name() const
Definition KDbField.cpp:256
QVariant defaultValue() const
Definition KDbField.cpp:281
Helper class that assigns additional information for the column in a query.
provides data for single edited database record
void removeAt(const KDbQueryColumnInfo &ci)
Removes value from db-aware buffer's column ci.
const QVariant * at(KDbQueryColumnInfo *ci, bool useDefaultValueIfPossible=true) const
bool hasDefaultValueAt(const KDbQueryColumnInfo &ci) const
Useful only for db-aware buffer:
void insert(KDbQueryColumnInfo *ci, const QVariant &val)
Inserts value val for db-aware buffer's column ci Does nothing if ci is nullptr.
KCALENDARCORE_EXPORT QDataStream & operator<<(QDataStream &out, const KCalendarCore::Alarm::Ptr &)
KDB_EXPORT bool isDefaultValueAllowed(const KDbField &field)
QDebug & nospace()
QDebug & space()
void clear()
iterator end()
iterator find(const Key &key)
iterator insert(const Key &key, const T &value)
bool isEmpty() const const
bool remove(const Key &key)
ConstIterator
void clear()
const_iterator constEnd() const const
const_iterator constFind(const Key &key) const const
iterator insert(const Key &key, const T &value)
bool isEmpty() const const
size_type remove(const Key &key)
T value(const Key &key, const T &defaultValue) const const
QFuture< void > map(Iterator begin, Iterator end, MapFunctor &&function)
bool isNull() const const
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:38:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.