We now have a current svn snapshot of libqxt in our contrib dir, and
[quassel.git] / src / contrib / libqxt-2007-10-24 / src / sql / qxtsqlpackage.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) Qxt Foundation. Some rights reserved.
4 **
5 ** This file is part of the QxtSql module of the Qt eXTension library
6 **
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
9 ** IBM.
10 **
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.
15 **
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.
20 **
21 ** <http://libqxt.sourceforge.net>  <foundation@libqxt.org>
22 **
23 ****************************************************************************/
24
25 #include "qxtsqlpackage.h"
26 #include <QBuffer>
27 #include <QDataStream>
28
29 QxtSqlPackage::QxtSqlPackage(QObject *parent) : QObject(parent)
30 {
31     record = -1;
32 }
33
34 QxtSqlPackage::QxtSqlPackage(const QxtSqlPackage & other, QObject *parent) : QObject(parent)
35 {
36     record = -1;
37
38     setData(other.data());
39 }
40
41 bool QxtSqlPackage::isValid()
42 {
43     if ((record >= 0) && (record < map.count()))
44         return true;
45     else
46         return false;
47 }
48
49 int QxtSqlPackage::at()
50 {
51     return record;
52 }
53
54 bool QxtSqlPackage::next()
55 {
56     record++;
57     if (record > (map.count()-1))
58     {
59         last();
60         return false;
61     }
62
63     return true;
64 }
65
66 bool QxtSqlPackage::last()
67 {
68     record=map.count()-1;
69     if (record >= 0)
70         return true;
71     else
72         return false;
73 }
74
75 bool QxtSqlPackage::first()
76 {
77     if (map.count())
78     {
79         record=0;
80         return true;
81     }
82     else
83     {
84         record=-1;
85         return false;
86     }
87 }
88
89 QString QxtSqlPackage::value(const QString& key)
90 {
91     if ((record<0) || !map.count()) return QString();
92
93     return map.at(record).value(key);
94 }
95
96
97
98 void QxtSqlPackage::insert(QSqlQuery query)
99 {
100     map.clear();
101     record=-1;
102
103     /*query will be invalid if next is not called first*/
104     if (!query.isValid())
105         query.next();
106
107     QSqlRecord infoRecord = query.record();
108     int iNumCols = infoRecord.count();
109     QVector<QString> tableMap = QVector<QString>(iNumCols);
110
111     /*first create a map of index->colname pairs*/
112     for (int iLoop = 0; iLoop < iNumCols; iLoop++)
113     {
114         tableMap[iLoop] = infoRecord.fieldName(iLoop);
115     }
116
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)
120      */
121     do
122     {
123         QHash<QString,QString> hash;
124         for (int iColLoop = 0; iColLoop < iNumCols; iColLoop++)
125         {
126             hash[tableMap[iColLoop]] = query.value(iColLoop).toString();
127         }
128         map.append(hash);
129
130     }
131     while (query.next());
132 }
133
134
135 int QxtSqlPackage::count() const
136 {
137     return map.count();
138 }
139
140
141 QByteArray QxtSqlPackage::data() const
142 {
143     QBuffer buff;
144     buff.open(QBuffer::WriteOnly);
145     QDataStream stream(&buff);
146
147     stream<<count();
148     for (int i=0; i < count();i++)
149         stream << map.at(i);
150
151     buff.close();
152     return buff.data();
153 }
154
155 void QxtSqlPackage::setData(const QByteArray& data)
156 {
157     map.clear();
158     record=-1;
159
160     QBuffer buff;
161     buff.setData(data);
162     buff.open(QBuffer::ReadOnly);
163     QDataStream stream(&buff);
164
165     int c;
166     stream >> c;
167
168     for (int i=0; i<c;i++)
169     {
170         QHash<QString,QString> hash;
171         stream >> hash;
172         map.append(hash);
173     }
174 }
175
176 QHash<QString,QString> QxtSqlPackage::hash(int index)
177 {
178     if (index > count()) return QHash<QString,QString>();
179     return map.at(index);
180 }
181
182
183 QHash<QString,QString> QxtSqlPackage::hash()
184 {
185     qDebug()<<record;
186     return map.at(record);
187 }
188
189
190 QxtSqlPackage& QxtSqlPackage::operator= ( const QxtSqlPackage & other )
191 {
192     setData(other.data());
193     return *this;
194 }
195