File indexing completed on 2024-11-10 04:15:56
0001 /* This file is part of the KDE project 0002 Copyright (C) 2003-2011 Jarosław Staniek <staniek@kde.org> 0003 0004 This library is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU Library General Public 0006 License as published by the Free Software Foundation; either 0007 version 2 of the License, or (at your option) any later version. 0008 0009 This library is distributed in the hope that it will be useful, 0010 but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 Library General Public License for more details. 0013 0014 You should have received a copy of the GNU Library General Public License 0015 along with this library; see the file COPYING.LIB. If not, write to 0016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 * Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include "KDbRecordEditBuffer.h" 0021 #include "KDb.h" 0022 #include "KDbQueryColumnInfo.h" 0023 #include "kdb_debug.h" 0024 0025 KDbRecordEditBuffer::KDbRecordEditBuffer(bool dbAwareBuffer) 0026 : m_simpleBuffer(dbAwareBuffer ? nullptr : new SimpleMap()) 0027 , m_simpleBufferIt(dbAwareBuffer ? nullptr : new SimpleMap::ConstIterator()) 0028 , m_dbBuffer(dbAwareBuffer ? new DbHash() : nullptr) 0029 , m_dbBufferIt(dbAwareBuffer ? new DbHash::Iterator() : nullptr) 0030 , m_defaultValuesDbBuffer(dbAwareBuffer ? new QMap<KDbQueryColumnInfo*, bool>() : nullptr) 0031 , m_defaultValuesDbBufferIt(dbAwareBuffer ? new QMap<KDbQueryColumnInfo*, bool>::ConstIterator() : nullptr) 0032 { 0033 } 0034 0035 KDbRecordEditBuffer::~KDbRecordEditBuffer() 0036 { 0037 delete m_simpleBuffer; 0038 delete m_simpleBufferIt; 0039 delete m_dbBuffer; 0040 delete m_dbBufferIt; 0041 delete m_defaultValuesDbBuffer; 0042 delete m_defaultValuesDbBufferIt; 0043 } 0044 0045 bool KDbRecordEditBuffer::isDBAware() const 0046 { 0047 return m_dbBuffer != nullptr; 0048 } 0049 0050 const QVariant* KDbRecordEditBuffer::at(KDbQueryColumnInfo* ci, bool useDefaultValueIfPossible) const 0051 { 0052 if (!ci) { 0053 kdbWarning() << "no KDbQueryColumnInfo provided"; 0054 return nullptr; 0055 } 0056 if (!m_dbBuffer) { 0057 kdbWarning() << "not db-aware buffer!"; 0058 return nullptr; 0059 } 0060 *m_dbBufferIt = m_dbBuffer->find(ci); 0061 QVariant* result = nullptr; 0062 if (*m_dbBufferIt != m_dbBuffer->end()) 0063 result = &(*m_dbBufferIt).value(); 0064 if (useDefaultValueIfPossible 0065 && (!result || result->isNull()) 0066 && ci->field() && !ci->field()->defaultValue().isNull() && KDb::isDefaultValueAllowed(*ci->field()) 0067 && !hasDefaultValueAt(*ci)) { 0068 //no buffered or stored value: try to get a default value declared in a field, so user can modify it 0069 if (!result) 0070 m_dbBuffer->insert(ci, ci->field()->defaultValue()); 0071 result = &(*m_dbBuffer)[ ci ]; 0072 m_defaultValuesDbBuffer->insert(ci, true); 0073 } 0074 return (const QVariant*)result; 0075 } 0076 0077 const QVariant* KDbRecordEditBuffer::at(const KDbField &field) const 0078 { 0079 if (!m_simpleBuffer) { 0080 kdbWarning() << "this is db-aware buffer!"; 0081 return nullptr; 0082 } 0083 *m_simpleBufferIt = m_simpleBuffer->constFind(field.name()); 0084 if (*m_simpleBufferIt == m_simpleBuffer->constEnd()) { 0085 //kdbWarning() << "no such field:" << field; 0086 return nullptr; 0087 } 0088 return &(*m_simpleBufferIt).value(); 0089 } 0090 0091 const QVariant* KDbRecordEditBuffer::at(const QString& fname) const 0092 { 0093 if (!m_simpleBuffer) { 0094 kdbWarning() << "this is db-aware buffer!"; 0095 return nullptr; 0096 } 0097 *m_simpleBufferIt = m_simpleBuffer->constFind(fname); 0098 if (*m_simpleBufferIt == m_simpleBuffer->constEnd()) { 0099 //kdbWarning() << "no such field:" << fname; 0100 return nullptr; 0101 } 0102 return &(*m_simpleBufferIt).value(); 0103 } 0104 0105 void KDbRecordEditBuffer::removeAt(const KDbQueryColumnInfo& ci) 0106 { 0107 if (!m_dbBuffer) { 0108 kdbWarning() << "not db-aware buffer!"; 0109 return; 0110 } 0111 m_dbBuffer->remove(const_cast<KDbQueryColumnInfo*>(&ci)); // const_cast ok here, we won't modify ci 0112 } 0113 0114 void KDbRecordEditBuffer::removeAt(const KDbField& field) 0115 { 0116 if (!m_simpleBuffer) { 0117 kdbWarning() << "this is db-aware buffer!"; 0118 return; 0119 } 0120 m_simpleBuffer->remove(field.name()); 0121 } 0122 0123 void KDbRecordEditBuffer::removeAt(const QString& fname) 0124 { 0125 if (!m_simpleBuffer) { 0126 kdbWarning() << "this is db-aware buffer!"; 0127 return; 0128 } 0129 m_simpleBuffer->remove(fname); 0130 } 0131 0132 void KDbRecordEditBuffer::clear() 0133 { 0134 if (m_dbBuffer) { 0135 m_dbBuffer->clear(); 0136 m_defaultValuesDbBuffer->clear(); 0137 } 0138 if (m_simpleBuffer) 0139 m_simpleBuffer->clear(); 0140 } 0141 0142 bool KDbRecordEditBuffer::isEmpty() const 0143 { 0144 if (m_dbBuffer) 0145 return m_dbBuffer->isEmpty(); 0146 if (m_simpleBuffer) 0147 return m_simpleBuffer->isEmpty(); 0148 return true; 0149 } 0150 0151 void KDbRecordEditBuffer::insert(KDbQueryColumnInfo *ci, const QVariant &val) 0152 { 0153 if (ci && m_dbBuffer) { 0154 m_dbBuffer->insert(ci, val); 0155 m_defaultValuesDbBuffer->remove(ci); 0156 } 0157 } 0158 0159 void KDbRecordEditBuffer::insert(const QString &fname, const QVariant &val) 0160 { 0161 if (m_simpleBuffer) { 0162 m_simpleBuffer->insert(fname, val); 0163 } 0164 } 0165 0166 bool KDbRecordEditBuffer::hasDefaultValueAt(const KDbQueryColumnInfo &ci) const 0167 { 0168 return m_defaultValuesDbBuffer->value(const_cast<KDbQueryColumnInfo*>(&ci), false); 0169 } 0170 0171 KDbRecordEditBuffer::SimpleMap KDbRecordEditBuffer::simpleBuffer() const 0172 { 0173 return *m_simpleBuffer; 0174 } 0175 0176 KDbRecordEditBuffer::DbHash KDbRecordEditBuffer::dbBuffer() const 0177 { 0178 return *m_dbBuffer; 0179 } 0180 0181 KDB_EXPORT QDebug operator<<(QDebug dbg, const KDbRecordEditBuffer& buffer) 0182 { 0183 if (buffer.isDBAware()) { 0184 const KDbRecordEditBuffer::DbHash buf(buffer.dbBuffer()); 0185 dbg.space() << "RecordEditBuffer type=DB-AWARE," << buf.count() << "field(s):\n"; 0186 for (KDbRecordEditBuffer::DbHash::ConstIterator it = buf.constBegin(); 0187 it != buf.constEnd(); ++it) 0188 { 0189 dbg.nospace() << "* field name=" << qPrintable(it.key()->field()->name()) << " val=" 0190 << (it.value().isNull() ? QLatin1String("<NULL>") : it.value().toString()) 0191 << (buffer.hasDefaultValueAt(*it.key()) ? " DEFAULT\n" : "\n"); 0192 } 0193 return dbg.space(); 0194 } 0195 const KDbRecordEditBuffer::SimpleMap map(buffer.simpleBuffer()); 0196 dbg.space() << "RecordEditBuffer type=SIMPLE," << map.count() << "field(s):\n"; 0197 for (KDbRecordEditBuffer::SimpleMap::ConstIterator it = map.constBegin(); 0198 it != map.constEnd(); ++it) 0199 { 0200 dbg.nospace() << "* field name=" << qPrintable(it.key()) << " val=" 0201 << (it.value().isNull() ? QLatin1String("<NULL>") : it.value().toString()) << "\n"; 0202 } 0203 return dbg.space(); 0204 }