File indexing completed on 2024-04-21 15:30:19

0001 /* This file is part of the KDE project
0002    Copyright (C) 2002   Lucijan Busch <lucijan@gmx.at>
0003    Copyright (C) 2003   Daniel Molkentin <molkentin@kde.org>
0004    Copyright (C) 2003-2016 Jarosław Staniek <staniek@kde.org>
0005 
0006    This program is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU Library General Public
0008    License as published by the Free Software Foundation; either
0009    version 2 of the License, or (at your option) any later version.
0010 
0011    This program is distributed in the hope that it will be useful,
0012    but WITHOUT ANY WARRANTY; without even the implied warranty of
0013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014    Library General Public License for more details.
0015 
0016    You should have received a copy of the GNU Library General Public License
0017    along with this program; see the file COPYING.  If not, write to
0018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019  * Boston, MA 02110-1301, USA.
0020 
0021    Original Author:  Till Busch <till@bux.at>
0022    Original Project: buX (www.bux.at)
0023 */
0024 
0025 #include "KDbRecordData.h"
0026 #include "KDbGlobal.h"
0027 #include "KDbUtils.h"
0028 #include "kdb_debug.h"
0029 
0030 QVariant KDbRecordData::s_null;
0031 
0032 QDebug operator<<(QDebug dbg, const KDbRecordData& data)
0033 {
0034     if (data.isEmpty()) {
0035         dbg.nospace() << QLatin1String("EMPTY RECORD DATA");
0036     }
0037     else {
0038         dbg.nospace() << "RECORD DATA (" << data.size() << " COLUMNS):";
0039         for (int i = 0; i < data.size(); i++) {
0040             dbg.nospace()
0041                 << " " << i << ":" << KDbUtils::squeezedValue(data[i]);
0042         }
0043     }
0044     return dbg.space();
0045 }
0046 
0047 void KDbRecordData::clear()
0048 {
0049     if (m_numCols > 0) {
0050         for (int i = 0; i < m_numCols; i++)
0051             free(m_data[i]);
0052         free(m_data);
0053         m_data = nullptr;
0054         m_numCols = 0;
0055     }
0056 }
0057 
0058 void KDbRecordData::resize(int numCols)
0059 {
0060     if (m_numCols == numCols)
0061         return;
0062     else if (m_numCols < numCols) { // grow
0063         m_data = (QVariant **)realloc(m_data, numCols * sizeof(QVariant *));
0064         memset(m_data + m_numCols, 0, (numCols - m_numCols) * sizeof(QVariant *));
0065         m_numCols = numCols;
0066     } else { // shrink
0067         for (int i = numCols; i < m_numCols; i++)
0068             delete m_data[i];
0069         m_data = (QVariant **)realloc(m_data, numCols * sizeof(QVariant *));
0070         m_numCols = numCols;
0071     }
0072 }
0073 
0074 void KDbRecordData::clearValues()
0075 {
0076     for (int i = 0; i < m_numCols; i++) {
0077         delete m_data[i];
0078         m_data[i] = nullptr;
0079     }
0080 }
0081 
0082 QList<QVariant> KDbRecordData::toList() const
0083 {
0084     QList<QVariant> list;
0085     list.reserve(m_numCols);
0086     for (int i = 0; i < m_numCols; ++i) {
0087         list.append(*m_data[i]);
0088     }
0089     return list;
0090 }