Diffstat (limited to 'libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp b/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp index 132c9fc..dd9dbde 100644 --- a/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp +++ b/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp | |||
@@ -1,80 +1,83 @@ | |||
1 | /* | 1 | /* |
2 | * SQL Backend for the OPIE-Contact Database. | 2 | * SQL Backend for the OPIE-Contact Database. |
3 | * | 3 | * |
4 | * Copyright (c) 2002 by Stefan Eilers (Eilers.Stefan@epost.de) | 4 | * Copyright (c) 2002 by Stefan Eilers (Eilers.Stefan@epost.de) |
5 | * | 5 | * |
6 | * ===================================================================== | 6 | * ===================================================================== |
7 | *This program is free software; you can redistribute it and/or | 7 | *This program is free software; you can redistribute it and/or |
8 | *modify it under the terms of the GNU Library General Public | 8 | *modify it under the terms of the GNU Library General Public |
9 | * License as published by the Free Software Foundation; either | 9 | * License as published by the Free Software Foundation; either |
10 | * version 2 of the License, or (at your option) any later version. | 10 | * version 2 of the License, or (at your option) any later version. |
11 | * ===================================================================== | 11 | * ===================================================================== |
12 | * ===================================================================== | 12 | * ===================================================================== |
13 | * Version: $Id$ | 13 | * Version: $Id$ |
14 | * ===================================================================== | 14 | * ===================================================================== |
15 | * History: | 15 | * History: |
16 | * $Log$ | 16 | * $Log$ |
17 | * Revision 1.3 2003/12/08 15:18:10 eilers | ||
18 | * Committing unfinished sql implementation before merging to libopie2 starts.. | ||
19 | * | ||
17 | * Revision 1.2 2003/09/29 07:44:26 eilers | 20 | * Revision 1.2 2003/09/29 07:44:26 eilers |
18 | * Improvement of PIM-SQL Databases, but search queries are still limited. | 21 | * Improvement of PIM-SQL Databases, but search queries are still limited. |
19 | * Addressbook: Changed table layout. Now, we just need 1/3 of disk-space. | 22 | * Addressbook: Changed table layout. Now, we just need 1/3 of disk-space. |
20 | * Todo: Started to add new attributes. Some type conversions missing. | 23 | * Todo: Started to add new attributes. Some type conversions missing. |
21 | * | 24 | * |
22 | * Revision 1.1 2003/09/22 14:31:16 eilers | 25 | * Revision 1.1 2003/09/22 14:31:16 eilers |
23 | * Added first experimental incarnation of sql-backend for addressbook. | 26 | * Added first experimental incarnation of sql-backend for addressbook. |
24 | * Some modifications to be able to compile the todo sql-backend. | 27 | * Some modifications to be able to compile the todo sql-backend. |
25 | * A lot of changes fill follow... | 28 | * A lot of changes fill follow... |
26 | * | 29 | * |
27 | */ | 30 | */ |
28 | 31 | ||
29 | #include "ocontactaccessbackend_sql.h" | 32 | #include "ocontactaccessbackend_sql.h" |
30 | 33 | ||
31 | #include <qarray.h> | 34 | #include <qarray.h> |
32 | #include <qdatetime.h> | 35 | #include <qdatetime.h> |
33 | #include <qstringlist.h> | 36 | #include <qstringlist.h> |
34 | 37 | ||
35 | #include <qpe/global.h> | 38 | #include <qpe/global.h> |
36 | #include <qpe/recordfields.h> | 39 | #include <qpe/recordfields.h> |
37 | 40 | ||
38 | #include <opie/ocontactfields.h> | 41 | #include <opie/ocontactfields.h> |
39 | #include <opie/oconversion.h> | 42 | #include <opie/oconversion.h> |
40 | #include <opie2/osqldriver.h> | 43 | #include <opie2/osqldriver.h> |
41 | #include <opie2/osqlresult.h> | 44 | #include <opie2/osqlresult.h> |
42 | #include <opie2/osqlmanager.h> | 45 | #include <opie2/osqlmanager.h> |
43 | #include <opie2/osqlquery.h> | 46 | #include <opie2/osqlquery.h> |
44 | 47 | ||
45 | 48 | ||
46 | 49 | ||
47 | 50 | ||
48 | // If defined, we use a horizontal table ( uid, attr1, attr2, attr3, ..., attrn ) instead | 51 | // If defined, we use a horizontal table ( uid, attr1, attr2, attr3, ..., attrn ) instead |
49 | // vertical like "uid, type, value". | 52 | // vertical like "uid, type, value". |
50 | // DON'T DEACTIVATE THIS DEFINE IN PRODUCTIVE ENVIRONMENTS !! | 53 | // DON'T DEACTIVATE THIS DEFINE IN PRODUCTIVE ENVIRONMENTS !! |
51 | #define __STORE_HORIZONTAL_ | 54 | #define __STORE_HORIZONTAL_ |
52 | 55 | ||
53 | // Distinct loading is not very fast. If I expect that every person has just | 56 | // Distinct loading is not very fast. If I expect that every person has just |
54 | // one (and always one) 'Last Name', I can request all uid's for existing lastnames, | 57 | // one (and always one) 'Last Name', I can request all uid's for existing lastnames, |
55 | // which is faster.. | 58 | // which is faster.. |
56 | // But this may not be true for all entries, like company contacts.. | 59 | // But this may not be true for all entries, like company contacts.. |
57 | // The current AddressBook application handles this problem, but other may not.. (eilers) | 60 | // The current AddressBook application handles this problem, but other may not.. (eilers) |
58 | #define __USE_SUPERFAST_LOADQUERY | 61 | #define __USE_SUPERFAST_LOADQUERY |
59 | 62 | ||
60 | 63 | ||
61 | /* | 64 | /* |
62 | * Implementation of used query types | 65 | * Implementation of used query types |
63 | * CREATE query | 66 | * CREATE query |
64 | * LOAD query | 67 | * LOAD query |
65 | * INSERT | 68 | * INSERT |
66 | * REMOVE | 69 | * REMOVE |
67 | * CLEAR | 70 | * CLEAR |
68 | */ | 71 | */ |
69 | namespace { | 72 | namespace { |
70 | /** | 73 | /** |
71 | * CreateQuery for the Todolist Table | 74 | * CreateQuery for the Todolist Table |
72 | */ | 75 | */ |
73 | class CreateQuery : public OSQLQuery { | 76 | class CreateQuery : public OSQLQuery { |
74 | public: | 77 | public: |
75 | CreateQuery(); | 78 | CreateQuery(); |
76 | ~CreateQuery(); | 79 | ~CreateQuery(); |
77 | QString query()const; | 80 | QString query()const; |
78 | }; | 81 | }; |
79 | 82 | ||
80 | /** | 83 | /** |
@@ -393,150 +396,156 @@ namespace { | |||
393 | } | 396 | } |
394 | FindQuery::FindQuery(const QArray<int>& ints) | 397 | FindQuery::FindQuery(const QArray<int>& ints) |
395 | : OSQLQuery(), m_uids( ints ){ | 398 | : OSQLQuery(), m_uids( ints ){ |
396 | } | 399 | } |
397 | FindQuery::~FindQuery() { | 400 | FindQuery::~FindQuery() { |
398 | } | 401 | } |
399 | QString FindQuery::query()const{ | 402 | QString FindQuery::query()const{ |
400 | // if ( m_uids.count() == 0 ) | 403 | // if ( m_uids.count() == 0 ) |
401 | return single(); | 404 | return single(); |
402 | } | 405 | } |
403 | /* | 406 | /* |
404 | else | 407 | else |
405 | return multi(); | 408 | return multi(); |
406 | } | 409 | } |
407 | QString FindQuery::multi()const { | 410 | QString FindQuery::multi()const { |
408 | QString qu = "select uid, type, value from addressbook where"; | 411 | QString qu = "select uid, type, value from addressbook where"; |
409 | for (uint i = 0; i < m_uids.count(); i++ ) { | 412 | for (uint i = 0; i < m_uids.count(); i++ ) { |
410 | qu += " UID = " + QString::number( m_uids[i] ) + " OR"; | 413 | qu += " UID = " + QString::number( m_uids[i] ) + " OR"; |
411 | } | 414 | } |
412 | qu.remove( qu.length()-2, 2 ); // Hmmmm.. | 415 | qu.remove( qu.length()-2, 2 ); // Hmmmm.. |
413 | return qu; | 416 | return qu; |
414 | } | 417 | } |
415 | */ | 418 | */ |
416 | #ifdef __STORE_HORIZONTAL_ | 419 | #ifdef __STORE_HORIZONTAL_ |
417 | QString FindQuery::single()const{ | 420 | QString FindQuery::single()const{ |
418 | QString qu = "select *"; | 421 | QString qu = "select *"; |
419 | qu += " from addressbook where uid = " + QString::number(m_uid); | 422 | qu += " from addressbook where uid = " + QString::number(m_uid); |
420 | 423 | ||
421 | // qWarning("find query: %s", qu.latin1() ); | 424 | // qWarning("find query: %s", qu.latin1() ); |
422 | return qu; | 425 | return qu; |
423 | } | 426 | } |
424 | #else | 427 | #else |
425 | QString FindQuery::single()const{ | 428 | QString FindQuery::single()const{ |
426 | QString qu = "select uid, type, value from addressbook where uid = "; | 429 | QString qu = "select uid, type, value from addressbook where uid = "; |
427 | qu += QString::number(m_uid); | 430 | qu += QString::number(m_uid); |
428 | return qu; | 431 | return qu; |
429 | } | 432 | } |
430 | #endif | 433 | #endif |
431 | 434 | ||
432 | 435 | ||
433 | FindCustomQuery::FindCustomQuery(int uid) | 436 | FindCustomQuery::FindCustomQuery(int uid) |
434 | : OSQLQuery(), m_uid( uid ) { | 437 | : OSQLQuery(), m_uid( uid ) { |
435 | } | 438 | } |
436 | FindCustomQuery::FindCustomQuery(const QArray<int>& ints) | 439 | FindCustomQuery::FindCustomQuery(const QArray<int>& ints) |
437 | : OSQLQuery(), m_uids( ints ){ | 440 | : OSQLQuery(), m_uids( ints ){ |
438 | } | 441 | } |
439 | FindCustomQuery::~FindCustomQuery() { | 442 | FindCustomQuery::~FindCustomQuery() { |
440 | } | 443 | } |
441 | QString FindCustomQuery::query()const{ | 444 | QString FindCustomQuery::query()const{ |
442 | // if ( m_uids.count() == 0 ) | 445 | // if ( m_uids.count() == 0 ) |
443 | return single(); | 446 | return single(); |
444 | } | 447 | } |
445 | QString FindCustomQuery::single()const{ | 448 | QString FindCustomQuery::single()const{ |
446 | QString qu = "select uid, type, value from custom_data where uid = "; | 449 | QString qu = "select uid, type, value from custom_data where uid = "; |
447 | qu += QString::number(m_uid); | 450 | qu += QString::number(m_uid); |
448 | return qu; | 451 | return qu; |
449 | } | 452 | } |
450 | 453 | ||
451 | }; | 454 | }; |
452 | 455 | ||
453 | 456 | ||
454 | /* --------------------------------------------------------------------------- */ | 457 | /* --------------------------------------------------------------------------- */ |
455 | 458 | ||
456 | OContactAccessBackend_SQL::OContactAccessBackend_SQL ( const QString& /* appname */, | 459 | OContactAccessBackend_SQL::OContactAccessBackend_SQL ( const QString& /* appname */, |
457 | const QString& filename ): m_changed(false) | 460 | const QString& filename ): |
461 | OContactAccessBackend(), m_changed(false), m_driver( NULL ) | ||
458 | { | 462 | { |
459 | qWarning("C'tor OContactAccessBackend_SQL starts"); | 463 | qWarning("C'tor OContactAccessBackend_SQL starts"); |
460 | QTime t; | 464 | QTime t; |
461 | t.start(); | 465 | t.start(); |
462 | 466 | ||
463 | /* Expecting to access the default filename if nothing else is set */ | 467 | /* Expecting to access the default filename if nothing else is set */ |
464 | if ( filename.isEmpty() ){ | 468 | if ( filename.isEmpty() ){ |
465 | m_fileName = Global::applicationFileName( "addressbook","addressbook.db" ); | 469 | m_fileName = Global::applicationFileName( "addressbook","addressbook.db" ); |
466 | } else | 470 | } else |
467 | m_fileName = filename; | 471 | m_fileName = filename; |
468 | 472 | ||
469 | // Get the standart sql-driver from the OSQLManager.. | 473 | // Get the standart sql-driver from the OSQLManager.. |
470 | OSQLManager man; | 474 | OSQLManager man; |
471 | m_driver = man.standard(); | 475 | m_driver = man.standard(); |
472 | m_driver->setUrl( m_fileName ); | 476 | m_driver->setUrl( m_fileName ); |
473 | 477 | ||
474 | load(); | 478 | load(); |
475 | 479 | ||
476 | qWarning("C'tor OContactAccessBackend_SQL ends: %d ms", t.elapsed() ); | 480 | qWarning("C'tor OContactAccessBackend_SQL ends: %d ms", t.elapsed() ); |
477 | } | 481 | } |
478 | 482 | ||
483 | OContactAccessBackend_SQL::~OContactAccessBackend_SQL () | ||
484 | { | ||
485 | if( m_driver ) | ||
486 | delete m_driver; | ||
487 | } | ||
479 | 488 | ||
480 | bool OContactAccessBackend_SQL::load () | 489 | bool OContactAccessBackend_SQL::load () |
481 | { | 490 | { |
482 | if (!m_driver->open() ) | 491 | if (!m_driver->open() ) |
483 | return false; | 492 | return false; |
484 | 493 | ||
485 | // Don't expect that the database exists. | 494 | // Don't expect that the database exists. |
486 | // It is save here to create the table, even if it | 495 | // It is save here to create the table, even if it |
487 | // do exist. ( Is that correct for all databases ?? ) | 496 | // do exist. ( Is that correct for all databases ?? ) |
488 | CreateQuery creat; | 497 | CreateQuery creat; |
489 | OSQLResult res = m_driver->query( &creat ); | 498 | OSQLResult res = m_driver->query( &creat ); |
490 | 499 | ||
491 | update(); | 500 | update(); |
492 | 501 | ||
493 | return true; | 502 | return true; |
494 | 503 | ||
495 | } | 504 | } |
496 | 505 | ||
497 | bool OContactAccessBackend_SQL::reload() | 506 | bool OContactAccessBackend_SQL::reload() |
498 | { | 507 | { |
499 | return load(); | 508 | return load(); |
500 | } | 509 | } |
501 | 510 | ||
502 | bool OContactAccessBackend_SQL::save() | 511 | bool OContactAccessBackend_SQL::save() |
503 | { | 512 | { |
504 | return m_driver->close(); | 513 | return m_driver->close(); |
505 | } | 514 | } |
506 | 515 | ||
507 | 516 | ||
508 | void OContactAccessBackend_SQL::clear () | 517 | void OContactAccessBackend_SQL::clear () |
509 | { | 518 | { |
510 | ClearQuery cle; | 519 | ClearQuery cle; |
511 | OSQLResult res = m_driver->query( &cle ); | 520 | OSQLResult res = m_driver->query( &cle ); |
512 | CreateQuery qu; | 521 | CreateQuery qu; |
513 | res = m_driver->query(&qu); | 522 | res = m_driver->query(&qu); |
514 | } | 523 | } |
515 | 524 | ||
516 | bool OContactAccessBackend_SQL::wasChangedExternally() | 525 | bool OContactAccessBackend_SQL::wasChangedExternally() |
517 | { | 526 | { |
518 | return false; | 527 | return false; |
519 | } | 528 | } |
520 | 529 | ||
521 | QArray<int> OContactAccessBackend_SQL::allRecords() const | 530 | QArray<int> OContactAccessBackend_SQL::allRecords() const |
522 | { | 531 | { |
523 | 532 | ||
524 | // FIXME: Think about cute handling of changed tables.. | 533 | // FIXME: Think about cute handling of changed tables.. |
525 | // Thus, we don't have to call update here... | 534 | // Thus, we don't have to call update here... |
526 | if ( m_changed ) | 535 | if ( m_changed ) |
527 | ((OContactAccessBackend_SQL*)this)->update(); | 536 | ((OContactAccessBackend_SQL*)this)->update(); |
528 | 537 | ||
529 | return m_uids; | 538 | return m_uids; |
530 | } | 539 | } |
531 | 540 | ||
532 | bool OContactAccessBackend_SQL::add ( const OContact &newcontact ) | 541 | bool OContactAccessBackend_SQL::add ( const OContact &newcontact ) |
533 | { | 542 | { |
534 | InsertQuery ins( newcontact ); | 543 | InsertQuery ins( newcontact ); |
535 | OSQLResult res = m_driver->query( &ins ); | 544 | OSQLResult res = m_driver->query( &ins ); |
536 | 545 | ||
537 | if ( res.state() == OSQLResult::Failure ) | 546 | if ( res.state() == OSQLResult::Failure ) |
538 | return false; | 547 | return false; |
539 | 548 | ||
540 | int c = m_uids.count(); | 549 | int c = m_uids.count(); |
541 | m_uids.resize( c+1 ); | 550 | m_uids.resize( c+1 ); |
542 | m_uids[c] = newcontact.uid(); | 551 | m_uids[c] = newcontact.uid(); |