1 /****************************************************************************
3 ** Copyright (C) Qxt Foundation. Some rights reserved.
5 ** This file is part of the QxtSql module of the Qt eXTension library
7 ** This library is free software; you can redistribute it and/or modify it
8 ** under the terms of th Common Public License, version 1.0, as published by
11 ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
12 ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
13 ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
14 ** FITNESS FOR A PARTICULAR PURPOSE.
16 ** You should have received a copy of the CPL along with this file.
17 ** See the LICENSE file and the cpl1.0.txt file included with the source
18 ** distribution for more information. If you did not receive a copy of the
19 ** license, contact the Qxt Foundation.
21 ** <http://libqxt.sourceforge.net> <foundation@libqxt.org>
23 ****************************************************************************/
25 #include "qxtsqlpackage.h"
27 #include <QDataStream>
29 QxtSqlPackage::QxtSqlPackage(QObject *parent) : QObject(parent)
34 QxtSqlPackage::QxtSqlPackage(const QxtSqlPackage & other, QObject *parent) : QObject(parent)
38 setData(other.data());
41 bool QxtSqlPackage::isValid()
43 if ((record >= 0) && (record < map.count()))
49 int QxtSqlPackage::at()
54 bool QxtSqlPackage::next()
57 if (record > (map.count()-1))
66 bool QxtSqlPackage::last()
75 bool QxtSqlPackage::first()
89 QString QxtSqlPackage::value(const QString& key)
91 if ((record<0) || !map.count()) return QString();
93 return map.at(record).value(key);
98 void QxtSqlPackage::insert(QSqlQuery query)
103 /*query will be invalid if next is not called first*/
104 if (!query.isValid())
107 QSqlRecord infoRecord = query.record();
108 int iNumCols = infoRecord.count();
109 QVector<QString> tableMap = QVector<QString>(iNumCols);
111 /*first create a map of index->colname pairs*/
112 for (int iLoop = 0; iLoop < iNumCols; iLoop++)
114 tableMap[iLoop] = infoRecord.fieldName(iLoop);
117 /*now use this created map to get column names
118 *this should be faster than querying the QSqlRecord every time
119 *but that depends on the databasetype and size of the table (number of rows and cols)
123 QHash<QString,QString> hash;
124 for (int iColLoop = 0; iColLoop < iNumCols; iColLoop++)
126 hash[tableMap[iColLoop]] = query.value(iColLoop).toString();
131 while (query.next());
135 int QxtSqlPackage::count() const
141 QByteArray QxtSqlPackage::data() const
144 buff.open(QBuffer::WriteOnly);
145 QDataStream stream(&buff);
148 for (int i=0; i < count();i++)
155 void QxtSqlPackage::setData(const QByteArray& data)
162 buff.open(QBuffer::ReadOnly);
163 QDataStream stream(&buff);
168 for (int i=0; i<c;i++)
170 QHash<QString,QString> hash;
176 QHash<QString,QString> QxtSqlPackage::hash(int index)
178 if (index > count()) return QHash<QString,QString>();
179 return map.at(index);
183 QHash<QString,QString> QxtSqlPackage::hash()
186 return map.at(record);
190 QxtSqlPackage& QxtSqlPackage::operator= ( const QxtSqlPackage & other )
192 setData(other.data());