summaryrefslogtreecommitdiff
path: root/noncore/apps/tableviewer/db/common.cpp
Unidiff
Diffstat (limited to 'noncore/apps/tableviewer/db/common.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/apps/tableviewer/db/common.cpp28
1 files changed, 18 insertions, 10 deletions
diff --git a/noncore/apps/tableviewer/db/common.cpp b/noncore/apps/tableviewer/db/common.cpp
index dbf9370..6e544ba 100644
--- a/noncore/apps/tableviewer/db/common.cpp
+++ b/noncore/apps/tableviewer/db/common.cpp
@@ -1,76 +1,84 @@
1/********************************************************************** 1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved. 2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3** 3**
4** This file is part of Qtopia Environment. 4** This file is part of Qtopia Environment.
5** 5**
6** This file may be distributed and/or modified under the terms of the 6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software 7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the 8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file. 9** packaging of this file.
10** 10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13** 13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information. 14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15** 15**
16** Contact info@trolltech.com if any conditions of this licensing are 16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you. 17** not clear to you.
18** 18**
19**********************************************************************/ 19**********************************************************************/
20#include <stdlib.h> 20#include "common.h"
21#include "datacache.h"
22
23/* OPIE */
24#include <opie2/odebug.h>
25#include <qpe/timestring.h>
26using namespace Opie::Core;
27
28/* QT */
21#include <qstring.h> 29#include <qstring.h>
22#include <qheader.h> 30#include <qheader.h>
23#include <qvector.h> 31#include <qvector.h>
24#include <qdatetime.h> 32#include <qdatetime.h>
25#include <qpe/timestring.h> 33
26#include "common.h" 34/* STD */
27#include "datacache.h"
28#include <assert.h> 35#include <assert.h>
36#include <stdlib.h>
29 37
30static const int del_flag = 0x1; 38static const int del_flag = 0x1;
31static const int new_flag = 0x2; 39static const int new_flag = 0x2;
32 40
33/* Helper function */ 41/* Helper function */
34 42
35int parseNextNumber(QString *q) { 43int parseNextNumber(QString *q) {
36 QChar c; 44 QChar c;
37 uint i; 45 uint i;
38 int result = 0; 46 int result = 0;
39 47
40 bool found_digits = FALSE; 48 bool found_digits = FALSE;
41 for(i = 0; i < q->length(); i++) { 49 for(i = 0; i < q->length(); i++) {
42 c = q->at(i); 50 c = q->at(i);
43 if (c.isDigit()) { 51 if (c.isDigit()) {
44 if (found_digits) 52 if (found_digits)
45 result *= 10; 53 result *= 10;
46 found_digits = TRUE; 54 found_digits = TRUE;
47 result += c.digitValue(); 55 result += c.digitValue();
48 } else { 56 } else {
49 if (found_digits) 57 if (found_digits)
50 break; 58 break;
51 /* just skip this char */ 59 /* just skip this char */
52 } 60 }
53 } 61 }
54 /* now truncate q */ 62 /* now truncate q */
55 if (found_digits) 63 if (found_digits)
56 q->remove(0, i); 64 q->remove(0, i);
57 return result; 65 return result;
58} 66}
59 67
60/*! 68/*!
61 \class QStringVector 69 \class QStringVector
62 \brief A Vector of QStrings that can be sorted and searched 70 \brief A Vector of QStrings that can be sorted and searched
63 71
64 Implmented in order to allow reverse lookup on the string name 72 Implmented in order to allow reverse lookup on the string name
65 73
66*/ 74*/
67 75
68/*! 76/*!
69 This function implements the compare function in order to allow the 77 This function implements the compare function in order to allow the
70 searching and sorting of the QStringVector to occur 78 searching and sorting of the QStringVector to occur
71 79
72 \returns an int which is either 80 \returns an int which is either
73 <UL> 81 <UL>
74 <LI> < 0 if the first string is smaller than the second,</LI> 82 <LI> < 0 if the first string is smaller than the second,</LI>
75 <LI> > 0 if the first string is bigger then the second,</LI> 83 <LI> > 0 if the first string is bigger then the second,</LI>
76 <LI> == 0 if the first string is equal to the second.</LI> 84 <LI> == 0 if the first string is equal to the second.</LI>
@@ -267,97 +275,97 @@ TVVariant::KeyType TVVariant::nameToType(const QString &name)
267 return Date; 275 return Date;
268 if(!qstrcmp("Time", name)) 276 if(!qstrcmp("Time", name))
269 return Time; 277 return Time;
270 if(!qstrcmp("Int", name)) 278 if(!qstrcmp("Int", name))
271 return Int; 279 return Int;
272 280
273 return Invalid; 281 return Invalid;
274} 282}
275 283
276void TVVariant::load(QDataStream &s ) 284void TVVariant::load(QDataStream &s )
277{ 285{
278 KeyType t; 286 KeyType t;
279 s >> t; 287 s >> t;
280 288
281 d->typ = t; 289 d->typ = t;
282 switch(t) { 290 switch(t) {
283 case Invalid: 291 case Invalid:
284 d->typ = t; 292 d->typ = t;
285 break; 293 break;
286 case String: 294 case String:
287 { 295 {
288 QString *x = new QString; 296 QString *x = new QString;
289 s >> *x; 297 s >> *x;
290 d->value.ptr = x; 298 d->value.ptr = x;
291 } 299 }
292 break; 300 break;
293 case Time: 301 case Time:
294 { 302 {
295 QTime *x = new QTime; 303 QTime *x = new QTime;
296 s >> *x; 304 s >> *x;
297 d->value.ptr = x; 305 d->value.ptr = x;
298 } 306 }
299 break; 307 break;
300 case Date: 308 case Date:
301 { 309 {
302 QDate *x = new QDate; 310 QDate *x = new QDate;
303 s >> *x; 311 s >> *x;
304 d->value.ptr = x; 312 d->value.ptr = x;
305 } 313 }
306 break; 314 break;
307 case Int: 315 case Int:
308 { 316 {
309 int x; 317 int x;
310 s >> x; 318 s >> x;
311 d->value.i = x; 319 d->value.i = x;
312 } 320 }
313 break; 321 break;
314 default: 322 default:
315 qFatal("Unrecognized data type"); 323 ofatal << "Unrecognized data type" << oendl;
316 } 324 }
317} 325}
318 326
319void TVVariant::save( QDataStream &s ) const 327void TVVariant::save( QDataStream &s ) const
320{ 328{
321 s << type(); 329 s << type();
322 330
323 switch( d->typ ) { 331 switch( d->typ ) {
324 case String: 332 case String:
325 s << *((QString *)d->value.ptr); 333 s << *((QString *)d->value.ptr);
326 break; 334 break;
327 case Date: 335 case Date:
328 s << *((QDate *)d->value.ptr); 336 s << *((QDate *)d->value.ptr);
329 break; 337 break;
330 case Time: 338 case Time:
331 s << *((QTime *)d->value.ptr); 339 s << *((QTime *)d->value.ptr);
332 break; 340 break;
333 case Int: 341 case Int:
334 s << d->value.i; 342 s << d->value.i;
335 break; 343 break;
336 case Invalid: 344 case Invalid:
337 break; 345 break;
338 } 346 }
339} 347}
340 348
341QDataStream& operator>>(QDataStream& s, TVVariant& p) 349QDataStream& operator>>(QDataStream& s, TVVariant& p)
342{ 350{
343 p.load( s ); 351 p.load( s );
344 return s; 352 return s;
345} 353}
346 354
347QDataStream& operator<<(QDataStream &s, const TVVariant& p) 355QDataStream& operator<<(QDataStream &s, const TVVariant& p)
348{ 356{
349 p.save( s ); 357 p.save( s );
350 return s; 358 return s;
351} 359}
352 360
353QDataStream& operator>> (QDataStream &s, TVVariant::KeyType& p) 361QDataStream& operator>> (QDataStream &s, TVVariant::KeyType& p)
354{ 362{
355 Q_UINT8 u = 0; 363 Q_UINT8 u = 0;
356 s >> u; 364 s >> u;
357 p = (TVVariant::KeyType) u; 365 p = (TVVariant::KeyType) u;
358 366
359 return s; 367 return s;
360} 368}
361 369
362QDataStream& operator<< (QDataStream& s, const TVVariant::KeyType& p) 370QDataStream& operator<< (QDataStream& s, const TVVariant::KeyType& p)
363{ 371{
@@ -1033,97 +1041,97 @@ QDataStream &operator>>( QDataStream &s, KeyList &k)
1033 1041
1034/*! 1042/*!
1035 Constructs a DataElem. This function needs a container because the 1043 Constructs a DataElem. This function needs a container because the
1036 size, types of keys and primary key are all defined by the containing 1044 size, types of keys and primary key are all defined by the containing
1037 database 1045 database
1038*/ 1046*/
1039DataElem::DataElem(DBStore *c) : values(20) 1047DataElem::DataElem(DBStore *c) : values(20)
1040{ 1048{
1041 int size; 1049 int size;
1042 contained = c; 1050 contained = c;
1043 size = c->getNumFields(); 1051 size = c->getNumFields();
1044 values.setAutoDelete(TRUE); 1052 values.setAutoDelete(TRUE);
1045} 1053}
1046 1054
1047/*! 1055/*!
1048 Destroys a DataElem and frees memory used by the DataElem 1056 Destroys a DataElem and frees memory used by the DataElem
1049*/ 1057*/
1050DataElem::~DataElem() { 1058DataElem::~DataElem() {
1051} 1059}
1052 1060
1053 1061
1054 1062
1055QDataStream &operator<<( QDataStream &s, const DataElem &d) 1063QDataStream &operator<<( QDataStream &s, const DataElem &d)
1056{ 1064{
1057 int size = d.getNumFields(); 1065 int size = d.getNumFields();
1058 1066
1059 s << size; /* redundent data but makes streaming easier */ 1067 s << size; /* redundent data but makes streaming easier */
1060 KeyList k = d.getKeys(); 1068 KeyList k = d.getKeys();
1061 1069
1062 KeyListIterator it(k); 1070 KeyListIterator it(k);
1063 1071
1064 while(it.current()) { 1072 while(it.current()) {
1065 s << (Q_UINT16)it.currentKey(); 1073 s << (Q_UINT16)it.currentKey();
1066 s << d.getField(it.currentKey()); 1074 s << d.getField(it.currentKey());
1067 ++it; 1075 ++it;
1068 } 1076 }
1069 return s; 1077 return s;
1070} 1078}
1071 1079
1072QDataStream &operator>>( QDataStream &s, DataElem &d) 1080QDataStream &operator>>( QDataStream &s, DataElem &d)
1073{ 1081{
1074 int i; 1082 int i;
1075 int size; 1083 int size;
1076 TVVariant t; 1084 TVVariant t;
1077 int index = 0; 1085 int index = 0;
1078 1086
1079 s >> size; /* redundent data but makes streaming easier */ 1087 s >> size; /* redundent data but makes streaming easier */
1080 if (size != d.getNumFields()) { 1088 if (size != d.getNumFields()) {
1081 qWarning("DataSize mis-match"); 1089 owarn << "DataSize mis-match" << oendl;
1082 return s; /* sanity check failed.. don't load */ 1090 return s; /* sanity check failed.. don't load */
1083 } 1091 }
1084 1092
1085 for(i = 0; i < size; i++) { 1093 for(i = 0; i < size; i++) {
1086 s >> (Q_UINT16)index; 1094 s >> (Q_UINT16)index;
1087 s >> t; 1095 s >> t;
1088 d.setField(index, t); 1096 d.setField(index, t);
1089 } 1097 }
1090 return s; 1098 return s;
1091} 1099}
1092 1100
1093/*! Returns the number of possible (not valid) fields in the data element */ 1101/*! Returns the number of possible (not valid) fields in the data element */
1094int DataElem::getNumFields() const 1102int DataElem::getNumFields() const
1095{ 1103{
1096 return contained->getNumFields(); 1104 return contained->getNumFields();
1097} 1105}
1098 1106
1099KeyList DataElem::getKeys() const 1107KeyList DataElem::getKeys() const
1100{ 1108{
1101 return *(contained->getKeys()); 1109 return *(contained->getKeys());
1102} 1110}
1103 1111
1104/*! 1112/*!
1105 This function determines whether field index i of the element has been 1113 This function determines whether field index i of the element has been
1106 set yet. 1114 set yet.
1107 1115
1108 \return A boolean value that is TRUE if the specfied field of this 1116 \return A boolean value that is TRUE if the specfied field of this
1109 element has been set and FALSE if the field has not yet been set 1117 element has been set and FALSE if the field has not yet been set
1110*/ 1118*/
1111bool DataElem::hasValidValue(int i) const 1119bool DataElem::hasValidValue(int i) const
1112{ 1120{
1113 if(!values.find(i)) 1121 if(!values.find(i))
1114 return FALSE; 1122 return FALSE;
1115 if(!contained->getKeys()->validIndex(i)) 1123 if(!contained->getKeys()->validIndex(i))
1116 return FALSE; 1124 return FALSE;
1117 return values.find(i)->isValid(); 1125 return values.find(i)->isValid();
1118} 1126}
1119 1127
1120/*! 1128/*!
1121 This function determines whether field name qs of the element has been 1129 This function determines whether field name qs of the element has been
1122 set yet. 1130 set yet.
1123 1131
1124 \return A boolean value that is TRUE if the specfied field of this 1132 \return A boolean value that is TRUE if the specfied field of this
1125 element has been set and FALSE if the field has not yet been set 1133 element has been set and FALSE if the field has not yet been set
1126*/ 1134*/
1127bool DataElem::hasValidValue(QString qs) const 1135bool DataElem::hasValidValue(QString qs) const
1128{ 1136{
1129 int i = contained->getKeyIndex(qs); 1137 int i = contained->getKeyIndex(qs);
@@ -1332,139 +1340,139 @@ bool DataElem::lessThan(int i, TVVariant v) const
1332 if (getField(i).type() != v.type()) 1340 if (getField(i).type() != v.type())
1333 return FALSE; 1341 return FALSE;
1334 1342
1335 return (getField(i) < v); 1343 return (getField(i) < v);
1336} 1344}
1337 1345
1338bool DataElem::moreThan(int i, TVVariant v) const 1346bool DataElem::moreThan(int i, TVVariant v) const
1339{ 1347{
1340 if (!hasValidValue(i)) return FALSE; 1348 if (!hasValidValue(i)) return FALSE;
1341 1349
1342 if (getField(i).type() != v.type()) 1350 if (getField(i).type() != v.type())
1343 return FALSE; 1351 return FALSE;
1344 1352
1345 return (getField(i) > v); 1353 return (getField(i) > v);
1346} 1354}
1347 1355
1348bool DataElem::equalTo(int i, TVVariant v) const 1356bool DataElem::equalTo(int i, TVVariant v) const
1349{ 1357{
1350 if (!hasValidValue(i)) return FALSE; 1358 if (!hasValidValue(i)) return FALSE;
1351 1359
1352 if (getField(i).type() != v.type()) 1360 if (getField(i).type() != v.type())
1353 return FALSE; 1361 return FALSE;
1354 1362
1355 return (getField(i) == v); 1363 return (getField(i) == v);
1356} 1364}
1357 1365
1358bool DataElem::contains(int i, TVVariant v) const 1366bool DataElem::contains(int i, TVVariant v) const
1359{ 1367{
1360 if (!hasValidValue(i)) return FALSE; 1368 if (!hasValidValue(i)) return FALSE;
1361 1369
1362 if (getField(i).type() != v.type()) 1370 if (getField(i).type() != v.type())
1363 return FALSE; 1371 return FALSE;
1364 1372
1365 switch(getField(i).type()) { 1373 switch(getField(i).type()) {
1366 case TVVariant::String: { 1374 case TVVariant::String: {
1367 QString qs1 = getField(i).toString().lower(); 1375 QString qs1 = getField(i).toString().lower();
1368 QString qs2 = v.toString().lower(); 1376 QString qs2 = v.toString().lower();
1369 if (qs1.contains(qs2) > 0) return TRUE; 1377 if (qs1.contains(qs2) > 0) return TRUE;
1370 break; 1378 break;
1371 } 1379 }
1372 /* meaningless for ints */ 1380 /* meaningless for ints */
1373 /* meaningless for time */ 1381 /* meaningless for time */
1374 /* meaningless for dates */ 1382 /* meaningless for dates */
1375 case TVVariant::Int: 1383 case TVVariant::Int:
1376 case TVVariant::Time: 1384 case TVVariant::Time:
1377 case TVVariant::Date: 1385 case TVVariant::Date:
1378 break; 1386 break;
1379 default: 1387 default:
1380 qWarning("Tried to compare unknown data type"); 1388 owarn << "Tried to compare unknown data type" << oendl;
1381 } 1389 }
1382 return FALSE; 1390 return FALSE;
1383} 1391}
1384 1392
1385bool DataElem::startsWith(int i, TVVariant v) const 1393bool DataElem::startsWith(int i, TVVariant v) const
1386{ 1394{
1387 if (!hasValidValue(i)) return FALSE; 1395 if (!hasValidValue(i)) return FALSE;
1388 1396
1389 if (getField(i).type() != v.type()) 1397 if (getField(i).type() != v.type())
1390 return FALSE; 1398 return FALSE;
1391 1399
1392 switch(getField(i).type()) { 1400 switch(getField(i).type()) {
1393 case TVVariant::String: { 1401 case TVVariant::String: {
1394 QString qs1 = getField(i).toString().lower(); 1402 QString qs1 = getField(i).toString().lower();
1395 QString qs2 = v.toString().lower(); 1403 QString qs2 = v.toString().lower();
1396 return qs1.startsWith(qs2); 1404 return qs1.startsWith(qs2);
1397 } 1405 }
1398 /* meaningless for ints */ 1406 /* meaningless for ints */
1399 /* meaningless for time */ 1407 /* meaningless for time */
1400 /* meaningless for dates */ 1408 /* meaningless for dates */
1401 case TVVariant::Int: 1409 case TVVariant::Int:
1402 case TVVariant::Time: 1410 case TVVariant::Time:
1403 case TVVariant::Date: 1411 case TVVariant::Date:
1404 return FALSE; 1412 return FALSE;
1405 default: 1413 default:
1406 qWarning("Tried to compare unknown data type"); 1414 owarn << "Tried to compare unknown data type" << oendl;
1407 } 1415 }
1408 return FALSE; 1416 return FALSE;
1409} 1417}
1410 1418
1411bool DataElem::endsWith(int i, TVVariant v) const 1419bool DataElem::endsWith(int i, TVVariant v) const
1412{ 1420{
1413 if (!hasValidValue(i)) return FALSE; 1421 if (!hasValidValue(i)) return FALSE;
1414 1422
1415 if (getField(i).type() != v.type()) 1423 if (getField(i).type() != v.type())
1416 return FALSE; 1424 return FALSE;
1417 1425
1418 switch(getField(i).type()) { 1426 switch(getField(i).type()) {
1419 case TVVariant::String: { 1427 case TVVariant::String: {
1420 QString qs1 = getField(i).toString().lower(); 1428 QString qs1 = getField(i).toString().lower();
1421 QString qs2 = v.toString().lower(); 1429 QString qs2 = v.toString().lower();
1422 return qs1.startsWith(qs2); 1430 return qs1.startsWith(qs2);
1423 } 1431 }
1424 /* meaningless for ints */ 1432 /* meaningless for ints */
1425 /* meaningless for time */ 1433 /* meaningless for time */
1426 /* meaningless for dates */ 1434 /* meaningless for dates */
1427 case TVVariant::Int: 1435 case TVVariant::Int:
1428 case TVVariant::Time: 1436 case TVVariant::Time:
1429 case TVVariant::Date: 1437 case TVVariant::Date:
1430 return FALSE; 1438 return FALSE;
1431 default: 1439 default:
1432 qWarning("Tried to compare unknown data type"); 1440 owarn << "Tried to compare unknown data type" << oendl;
1433 } 1441 }
1434 return FALSE; 1442 return FALSE;
1435} 1443}
1436 1444
1437/*! 1445/*!
1438 Determins which of the first to parameters are closer to the third, target 1446 Determins which of the first to parameters are closer to the third, target
1439 parameter. 1447 parameter.
1440 1448
1441 \return 1449 \return
1442 <UL> 1450 <UL>
1443 <LI>TRUE if the first element is a closer match to the target than the 1451 <LI>TRUE if the first element is a closer match to the target than the
1444 second element</LI> 1452 second element</LI>
1445 <LI>FALSE if the first element is not a closer match to the target than 1453 <LI>FALSE if the first element is not a closer match to the target than
1446 the second element</LI> 1454 the second element</LI>
1447 </UL> 1455 </UL>
1448*/ 1456*/
1449bool DataElem::closer(DataElem*d1, DataElem *d2, TVVariant target, int column) 1457bool DataElem::closer(DataElem*d1, DataElem *d2, TVVariant target, int column)
1450{ 1458{
1451 int type; 1459 int type;
1452 1460
1453 if(!d1) return FALSE; 1461 if(!d1) return FALSE;
1454 1462
1455 if (!d1->hasValidValue(column)) return FALSE; 1463 if (!d1->hasValidValue(column)) return FALSE;
1456 1464
1457 if(!target.isValid()) return FALSE; 1465 if(!target.isValid()) return FALSE;
1458 1466
1459 type = d1->getField(column).type(); 1467 type = d1->getField(column).type();
1460 1468
1461 if(d2) { 1469 if(d2) {
1462 if (type != d2->getField(column).type()) { 1470 if (type != d2->getField(column).type()) {
1463 /* can't do compare */ 1471 /* can't do compare */
1464 qWarning("Tried to compare two incompatable types"); 1472 owarn << "Tried to compare two incompatable types" << oendl;
1465 return FALSE; 1473 return FALSE;
1466 } 1474 }
1467 return target.closer(d1->getField(column), d2->getField(column)); 1475 return target.closer(d1->getField(column), d2->getField(column));
1468 } 1476 }
1469 return target.close(d1->getField(column)); 1477 return target.close(d1->getField(column));
1470} 1478}