-rw-r--r-- | libopie2/opiepim/backend/otodoaccesssql.cpp | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/libopie2/opiepim/backend/otodoaccesssql.cpp b/libopie2/opiepim/backend/otodoaccesssql.cpp index 6f65c48..12a8bea 100644 --- a/libopie2/opiepim/backend/otodoaccesssql.cpp +++ b/libopie2/opiepim/backend/otodoaccesssql.cpp | |||
@@ -332,290 +332,295 @@ namespace { | |||
332 | } | 332 | } |
333 | QString FindQuery::single()const{ | 333 | QString FindQuery::single()const{ |
334 | QString qu = "select * from todolist where uid = " + QString::number(m_uid); | 334 | QString qu = "select * from todolist where uid = " + QString::number(m_uid); |
335 | return qu; | 335 | return qu; |
336 | } | 336 | } |
337 | QString FindQuery::multi()const { | 337 | QString FindQuery::multi()const { |
338 | QString qu = "select * from todolist where "; | 338 | QString qu = "select * from todolist where "; |
339 | for (uint i = 0; i < m_uids.count(); i++ ) { | 339 | for (uint i = 0; i < m_uids.count(); i++ ) { |
340 | qu += " UID = " + QString::number( m_uids[i] ) + " OR"; | 340 | qu += " UID = " + QString::number( m_uids[i] ) + " OR"; |
341 | } | 341 | } |
342 | qu.remove( qu.length()-2, 2 ); | 342 | qu.remove( qu.length()-2, 2 ); |
343 | return qu; | 343 | return qu; |
344 | } | 344 | } |
345 | 345 | ||
346 | OverDueQuery::OverDueQuery(): OSQLQuery() {} | 346 | OverDueQuery::OverDueQuery(): OSQLQuery() {} |
347 | OverDueQuery::~OverDueQuery() {} | 347 | OverDueQuery::~OverDueQuery() {} |
348 | QString OverDueQuery::query()const { | 348 | QString OverDueQuery::query()const { |
349 | QDate date = QDate::currentDate(); | 349 | QDate date = QDate::currentDate(); |
350 | QString str; | 350 | QString str; |
351 | str = QString("select uid from todolist where DueDate ='%1-%2-%3'") | 351 | str = QString("select uid from todolist where DueDate ='%1-%2-%3'") |
352 | .arg( QString::number( date.year() ).rightJustify( 4, '0' ) ) | 352 | .arg( QString::number( date.year() ).rightJustify( 4, '0' ) ) |
353 | .arg( QString::number( date.month() ).rightJustify( 2, '0' ) ) | 353 | .arg( QString::number( date.month() ).rightJustify( 2, '0' ) ) |
354 | .arg( QString::number( date.day() ) .rightJustify( 2, '0' ) ); | 354 | .arg( QString::number( date.day() ) .rightJustify( 2, '0' ) ); |
355 | 355 | ||
356 | return str; | 356 | return str; |
357 | } | 357 | } |
358 | 358 | ||
359 | 359 | ||
360 | EffQuery::EffQuery( const QDate& start, const QDate& end, bool inc ) | 360 | EffQuery::EffQuery( const QDate& start, const QDate& end, bool inc ) |
361 | : OSQLQuery(), m_start( start ), m_end( end ),m_inc(inc) {} | 361 | : OSQLQuery(), m_start( start ), m_end( end ),m_inc(inc) {} |
362 | EffQuery::~EffQuery() {} | 362 | EffQuery::~EffQuery() {} |
363 | QString EffQuery::query()const { | 363 | QString EffQuery::query()const { |
364 | return m_inc ? with() : out(); | 364 | return m_inc ? with() : out(); |
365 | } | 365 | } |
366 | QString EffQuery::with()const { | 366 | QString EffQuery::with()const { |
367 | QString str; | 367 | QString str; |
368 | str = QString("select uid from todolist where ( DueDate >= '%1-%2-%3' AND DueDate <= '%4-%5-%6' ) OR DueDate = '0-0-0' ") | 368 | str = QString("select uid from todolist where ( DueDate >= '%1-%2-%3' AND DueDate <= '%4-%5-%6' ) OR DueDate = '0-0-0' ") |
369 | .arg( QString::number( m_start.year() ).rightJustify( 4, '0' ) ) | 369 | .arg( QString::number( m_start.year() ).rightJustify( 4, '0' ) ) |
370 | .arg( QString::number( m_start.month() ).rightJustify( 2, '0' ) ) | 370 | .arg( QString::number( m_start.month() ).rightJustify( 2, '0' ) ) |
371 | .arg( QString::number( m_start.day() ).rightJustify( 2, '0' ) ) | 371 | .arg( QString::number( m_start.day() ).rightJustify( 2, '0' ) ) |
372 | .arg( QString::number( m_end.year() ).rightJustify( 4, '0' ) ) | 372 | .arg( QString::number( m_end.year() ).rightJustify( 4, '0' ) ) |
373 | .arg( QString::number( m_end.month() ).rightJustify( 2, '0' ) ) | 373 | .arg( QString::number( m_end.month() ).rightJustify( 2, '0' ) ) |
374 | .arg( QString::number( m_end.day() ).rightJustify( 2, '0' ) ); | 374 | .arg( QString::number( m_end.day() ).rightJustify( 2, '0' ) ); |
375 | return str; | 375 | return str; |
376 | } | 376 | } |
377 | QString EffQuery::out()const { | 377 | QString EffQuery::out()const { |
378 | QString str; | 378 | QString str; |
379 | str = QString("select uid from todolist where DueDate >= '%1-%2-%3' AND DueDate <= '%4-%5-%6'") | 379 | str = QString("select uid from todolist where DueDate >= '%1-%2-%3' AND DueDate <= '%4-%5-%6'") |
380 | .arg( QString::number( m_start.year() ).rightJustify( 4, '0' ) ) | 380 | .arg( QString::number( m_start.year() ).rightJustify( 4, '0' ) ) |
381 | .arg( QString::number( m_start.month() ).rightJustify( 2, '0' ) ) | 381 | .arg( QString::number( m_start.month() ).rightJustify( 2, '0' ) ) |
382 | .arg( QString::number( m_start.day() ).rightJustify( 2, '0' ) ) | 382 | .arg( QString::number( m_start.day() ).rightJustify( 2, '0' ) ) |
383 | .arg( QString::number( m_end.year() ).rightJustify( 4, '0' ) ) | 383 | .arg( QString::number( m_end.year() ).rightJustify( 4, '0' ) ) |
384 | .arg( QString::number( m_end.month() ).rightJustify( 2, '0' ) ) | 384 | .arg( QString::number( m_end.month() ).rightJustify( 2, '0' ) ) |
385 | .arg( QString::number( m_end.day() ).rightJustify( 2, '0' ) ); | 385 | .arg( QString::number( m_end.day() ).rightJustify( 2, '0' ) ); |
386 | 386 | ||
387 | return str; | 387 | return str; |
388 | } | 388 | } |
389 | 389 | ||
390 | FindCustomQuery::FindCustomQuery(int uid) | 390 | FindCustomQuery::FindCustomQuery(int uid) |
391 | : OSQLQuery(), m_uid( uid ) { | 391 | : OSQLQuery(), m_uid( uid ) { |
392 | } | 392 | } |
393 | FindCustomQuery::FindCustomQuery(const QArray<int>& ints) | 393 | FindCustomQuery::FindCustomQuery(const QArray<int>& ints) |
394 | : OSQLQuery(), m_uids( ints ){ | 394 | : OSQLQuery(), m_uids( ints ){ |
395 | } | 395 | } |
396 | FindCustomQuery::~FindCustomQuery() { | 396 | FindCustomQuery::~FindCustomQuery() { |
397 | } | 397 | } |
398 | QString FindCustomQuery::query()const{ | 398 | QString FindCustomQuery::query()const{ |
399 | return single(); // Multiple requests not supported ! | 399 | return single(); // Multiple requests not supported ! |
400 | } | 400 | } |
401 | QString FindCustomQuery::single()const{ | 401 | QString FindCustomQuery::single()const{ |
402 | QString qu = "select uid, type, value from custom_data where uid = "; | 402 | QString qu = "select uid, type, value from custom_data where uid = "; |
403 | qu += QString::number(m_uid); | 403 | qu += QString::number(m_uid); |
404 | return qu; | 404 | return qu; |
405 | } | 405 | } |
406 | 406 | ||
407 | }; | 407 | }; |
408 | 408 | ||
409 | 409 | ||
410 | namespace Opie { | 410 | namespace Opie { |
411 | OPimTodoAccessBackendSQL::OPimTodoAccessBackendSQL( const QString& file ) | 411 | OPimTodoAccessBackendSQL::OPimTodoAccessBackendSQL( const QString& file ) |
412 | : OPimTodoAccessBackend(),/* m_dict(15),*/ m_driver(NULL), m_dirty(true) | 412 | : OPimTodoAccessBackend(),/* m_dict(15),*/ m_driver(NULL), m_dirty(true) |
413 | { | 413 | { |
414 | QString fi = file; | 414 | QString fi = file; |
415 | if ( fi.isEmpty() ) | 415 | if ( fi.isEmpty() ) |
416 | fi = Global::applicationFileName( "todolist", "todolist.db" ); | 416 | fi = Global::applicationFileName( "todolist", "todolist.db" ); |
417 | OSQLManager man; | 417 | OSQLManager man; |
418 | m_driver = man.standard(); | 418 | m_driver = man.standard(); |
419 | m_driver->setUrl(fi); | 419 | m_driver->setUrl(fi); |
420 | // fillDict(); | 420 | // fillDict(); |
421 | } | 421 | } |
422 | 422 | ||
423 | OPimTodoAccessBackendSQL::~OPimTodoAccessBackendSQL(){ | 423 | OPimTodoAccessBackendSQL::~OPimTodoAccessBackendSQL(){ |
424 | if( m_driver ) | 424 | if( m_driver ) |
425 | delete m_driver; | 425 | delete m_driver; |
426 | } | 426 | } |
427 | 427 | ||
428 | bool OPimTodoAccessBackendSQL::load(){ | 428 | bool OPimTodoAccessBackendSQL::load(){ |
429 | if (!m_driver->open() ) | 429 | if (!m_driver->open() ) |
430 | return false; | 430 | return false; |
431 | 431 | ||
432 | CreateQuery creat; | 432 | CreateQuery creat; |
433 | OSQLResult res = m_driver->query(&creat ); | 433 | OSQLResult res = m_driver->query(&creat ); |
434 | 434 | ||
435 | m_dirty = true; | 435 | m_dirty = true; |
436 | return true; | 436 | return true; |
437 | } | 437 | } |
438 | bool OPimTodoAccessBackendSQL::reload(){ | 438 | bool OPimTodoAccessBackendSQL::reload(){ |
439 | return load(); | 439 | return load(); |
440 | } | 440 | } |
441 | 441 | ||
442 | bool OPimTodoAccessBackendSQL::save(){ | 442 | bool OPimTodoAccessBackendSQL::save(){ |
443 | return m_driver->close(); // Shouldn't m_driver->sync be better than close ? (eilers) | 443 | return m_driver->close(); // Shouldn't m_driver->sync be better than close ? (eilers) |
444 | } | 444 | } |
445 | QArray<int> OPimTodoAccessBackendSQL::allRecords()const { | 445 | QArray<int> OPimTodoAccessBackendSQL::allRecords()const { |
446 | if (m_dirty ) | 446 | if (m_dirty ) |
447 | update(); | 447 | update(); |
448 | 448 | ||
449 | return m_uids; | 449 | return m_uids; |
450 | } | 450 | } |
451 | QArray<int> OPimTodoAccessBackendSQL::queryByExample( const OPimTodo& , int, const QDateTime& ){ | 451 | QArray<int> OPimTodoAccessBackendSQL::queryByExample( const OPimTodo& , int, const QDateTime& ){ |
452 | QArray<int> ints(0); | 452 | QArray<int> ints(0); |
453 | return ints; | 453 | return ints; |
454 | } | 454 | } |
455 | OPimTodo OPimTodoAccessBackendSQL::find(int uid ) const{ | 455 | OPimTodo OPimTodoAccessBackendSQL::find(int uid ) const{ |
456 | FindQuery query( uid ); | 456 | FindQuery query( uid ); |
457 | return todo( m_driver->query(&query) ); | 457 | return todo( m_driver->query(&query) ); |
458 | 458 | ||
459 | } | 459 | } |
460 | |||
461 | // Remember: uid is already in the list of uids, called ints ! | ||
460 | OPimTodo OPimTodoAccessBackendSQL::find( int uid, const QArray<int>& ints, | 462 | OPimTodo OPimTodoAccessBackendSQL::find( int uid, const QArray<int>& ints, |
461 | uint cur, Frontend::CacheDirection dir ) const{ | 463 | uint cur, Frontend::CacheDirection dir ) const{ |
462 | uint CACHE = readAhead(); | 464 | uint CACHE = readAhead(); |
463 | odebug << "searching for " << uid << "" << oendl; | 465 | odebug << "searching for " << uid << "" << oendl; |
464 | QArray<int> search( CACHE ); | 466 | QArray<int> search( CACHE ); |
465 | uint size =0; | 467 | uint size =0; |
466 | OPimTodo to; | 468 | OPimTodo to; |
467 | 469 | ||
468 | // we try to cache CACHE items | 470 | // we try to cache CACHE items |
469 | switch( dir ) { | 471 | switch( dir ) { |
470 | /* forward */ | 472 | /* forward */ |
471 | case 0: // FIXME: Not a good style to use magic numbers here (eilers) | 473 | case Frontend::Forward: |
472 | for (uint i = cur; i < ints.count() && size < CACHE; i++ ) { | 474 | for (uint i = cur; i < ints.count() && size < CACHE; i++ ) { |
473 | odebug << "size " << size << " " << ints[i] << "" << oendl; | 475 | odebug << "size " << size << " " << ints[i] << "" << oendl; |
474 | search[size] = ints[i]; | 476 | search[size] = ints[i]; |
475 | size++; | 477 | size++; |
476 | } | 478 | } |
477 | break; | 479 | break; |
478 | /* reverse */ | 480 | /* reverse */ |
479 | case 1: // FIXME: Not a good style to use magic numbers here (eilers) | 481 | case Frontend::Reverse: |
480 | for (uint i = cur; i != 0 && size < CACHE; i-- ) { | 482 | for (uint i = cur; i != 0 && size < CACHE; i-- ) { |
481 | search[size] = ints[i]; | 483 | search[size] = ints[i]; |
482 | size++; | 484 | size++; |
483 | } | 485 | } |
484 | break; | 486 | break; |
485 | } | 487 | } |
488 | |||
486 | search.resize( size ); | 489 | search.resize( size ); |
487 | FindQuery query( search ); | 490 | FindQuery query( search ); |
488 | OSQLResult res = m_driver->query( &query ); | 491 | OSQLResult res = m_driver->query( &query ); |
489 | if ( res.state() != OSQLResult::Success ) | 492 | if ( res.state() != OSQLResult::Success ) |
490 | return to; | 493 | return to; |
491 | 494 | ||
492 | return todo( res ); | 495 | todo( res ); //FIXME: Don't like polymorphism here. It makes the code hard to read here..(eilers) |
496 | return cacheFind( uid ); | ||
493 | } | 497 | } |
498 | |||
494 | void OPimTodoAccessBackendSQL::clear() { | 499 | void OPimTodoAccessBackendSQL::clear() { |
495 | ClearQuery cle; | 500 | ClearQuery cle; |
496 | OSQLResult res = m_driver->query( &cle ); | 501 | OSQLResult res = m_driver->query( &cle ); |
497 | CreateQuery qu; | 502 | CreateQuery qu; |
498 | res = m_driver->query(&qu); | 503 | res = m_driver->query(&qu); |
499 | } | 504 | } |
500 | bool OPimTodoAccessBackendSQL::add( const OPimTodo& t) { | 505 | bool OPimTodoAccessBackendSQL::add( const OPimTodo& t) { |
501 | InsertQuery ins( t ); | 506 | InsertQuery ins( t ); |
502 | OSQLResult res = m_driver->query( &ins ); | 507 | OSQLResult res = m_driver->query( &ins ); |
503 | 508 | ||
504 | if ( res.state() == OSQLResult::Failure ) | 509 | if ( res.state() == OSQLResult::Failure ) |
505 | return false; | 510 | return false; |
506 | int c = m_uids.count(); | 511 | int c = m_uids.count(); |
507 | m_uids.resize( c+1 ); | 512 | m_uids.resize( c+1 ); |
508 | m_uids[c] = t.uid(); | 513 | m_uids[c] = t.uid(); |
509 | 514 | ||
510 | return true; | 515 | return true; |
511 | } | 516 | } |
512 | bool OPimTodoAccessBackendSQL::remove( int uid ) { | 517 | bool OPimTodoAccessBackendSQL::remove( int uid ) { |
513 | RemoveQuery rem( uid ); | 518 | RemoveQuery rem( uid ); |
514 | OSQLResult res = m_driver->query(&rem ); | 519 | OSQLResult res = m_driver->query(&rem ); |
515 | 520 | ||
516 | if ( res.state() == OSQLResult::Failure ) | 521 | if ( res.state() == OSQLResult::Failure ) |
517 | return false; | 522 | return false; |
518 | 523 | ||
519 | m_dirty = true; | 524 | m_dirty = true; |
520 | return true; | 525 | return true; |
521 | } | 526 | } |
522 | /* | 527 | /* |
523 | * FIXME better set query | 528 | * FIXME better set query |
524 | * but we need the cache for that | 529 | * but we need the cache for that |
525 | * now we remove | 530 | * now we remove |
526 | */ | 531 | */ |
527 | bool OPimTodoAccessBackendSQL::replace( const OPimTodo& t) { | 532 | bool OPimTodoAccessBackendSQL::replace( const OPimTodo& t) { |
528 | remove( t.uid() ); | 533 | remove( t.uid() ); |
529 | bool b= add(t); | 534 | bool b= add(t); |
530 | m_dirty = false; // we changed some stuff but the UID stayed the same | 535 | m_dirty = false; // we changed some stuff but the UID stayed the same |
531 | return b; | 536 | return b; |
532 | } | 537 | } |
533 | QArray<int> OPimTodoAccessBackendSQL::overDue() { | 538 | QArray<int> OPimTodoAccessBackendSQL::overDue() { |
534 | OverDueQuery qu; | 539 | OverDueQuery qu; |
535 | return uids( m_driver->query(&qu ) ); | 540 | return uids( m_driver->query(&qu ) ); |
536 | } | 541 | } |
537 | QArray<int> OPimTodoAccessBackendSQL::effectiveToDos( const QDate& s, | 542 | QArray<int> OPimTodoAccessBackendSQL::effectiveToDos( const QDate& s, |
538 | const QDate& t, | 543 | const QDate& t, |
539 | bool u) { | 544 | bool u) { |
540 | EffQuery ef(s, t, u ); | 545 | EffQuery ef(s, t, u ); |
541 | return uids (m_driver->query(&ef) ); | 546 | return uids (m_driver->query(&ef) ); |
542 | } | 547 | } |
543 | /* | 548 | /* |
544 | * | 549 | * |
545 | */ | 550 | */ |
546 | QArray<int> OPimTodoAccessBackendSQL::sorted( bool asc, int sortOrder, | 551 | QArray<int> OPimTodoAccessBackendSQL::sorted( bool asc, int sortOrder, |
547 | int sortFilter, int cat ) { | 552 | int sortFilter, int cat ) { |
548 | odebug << "sorted " << asc << ", " << sortOrder << "" << oendl; | 553 | odebug << "sorted " << asc << ", " << sortOrder << "" << oendl; |
549 | QString query; | 554 | QString query; |
550 | query = "select uid from todolist WHERE "; | 555 | query = "select uid from todolist WHERE "; |
551 | 556 | ||
552 | /* | 557 | /* |
553 | * Sort Filter stuff | 558 | * Sort Filter stuff |
554 | * not that straight forward | 559 | * not that straight forward |
555 | * FIXME: Replace magic numbers | 560 | * FIXME: Replace magic numbers |
556 | * | 561 | * |
557 | */ | 562 | */ |
558 | /* Category */ | 563 | /* Category */ |
559 | if ( sortFilter & 1 ) { | 564 | if ( sortFilter & 1 ) { |
560 | QString str; | 565 | QString str; |
561 | if (cat != 0 ) str = QString::number( cat ); | 566 | if (cat != 0 ) str = QString::number( cat ); |
562 | query += " categories like '%" +str+"%' AND"; | 567 | query += " categories like '%" +str+"%' AND"; |
563 | } | 568 | } |
564 | /* Show only overdue */ | 569 | /* Show only overdue */ |
565 | if ( sortFilter & 2 ) { | 570 | if ( sortFilter & 2 ) { |
566 | QDate date = QDate::currentDate(); | 571 | QDate date = QDate::currentDate(); |
567 | QString due; | 572 | QString due; |
568 | QString base; | 573 | QString base; |
569 | base = QString("DueDate <= '%1-%2-%3' AND completed = 0") | 574 | base = QString("DueDate <= '%1-%2-%3' AND completed = 0") |
570 | .arg( QString::number( date.year() ).rightJustify( 4, '0' ) ) | 575 | .arg( QString::number( date.year() ).rightJustify( 4, '0' ) ) |
571 | .arg( QString::number( date.month() ).rightJustify( 2, '0' ) ) | 576 | .arg( QString::number( date.month() ).rightJustify( 2, '0' ) ) |
572 | .arg( QString::number( date.day() ).rightJustify( 2, '0' ) ); | 577 | .arg( QString::number( date.day() ).rightJustify( 2, '0' ) ); |
573 | query += " " + base + " AND"; | 578 | query += " " + base + " AND"; |
574 | } | 579 | } |
575 | /* not show completed */ | 580 | /* not show completed */ |
576 | if ( sortFilter & 4 ) { | 581 | if ( sortFilter & 4 ) { |
577 | query += " completed = 0 AND"; | 582 | query += " completed = 0 AND"; |
578 | }else{ | 583 | }else{ |
579 | query += " ( completed = 1 OR completed = 0) AND"; | 584 | query += " ( completed = 1 OR completed = 0) AND"; |
580 | } | 585 | } |
581 | /* strip the end */ | 586 | /* strip the end */ |
582 | query = query.remove( query.length()-3, 3 ); | 587 | query = query.remove( query.length()-3, 3 ); |
583 | 588 | ||
584 | 589 | ||
585 | /* | 590 | /* |
586 | * sort order stuff | 591 | * sort order stuff |
587 | * quite straight forward | 592 | * quite straight forward |
588 | */ | 593 | */ |
589 | query += "ORDER BY "; | 594 | query += "ORDER BY "; |
590 | switch( sortOrder ) { | 595 | switch( sortOrder ) { |
591 | /* completed */ | 596 | /* completed */ |
592 | case 0: | 597 | case 0: |
593 | query += "completed"; | 598 | query += "completed"; |
594 | break; | 599 | break; |
595 | case 1: | 600 | case 1: |
596 | query += "priority"; | 601 | query += "priority"; |
597 | break; | 602 | break; |
598 | case 2: | 603 | case 2: |
599 | query += "summary"; | 604 | query += "summary"; |
600 | break; | 605 | break; |
601 | case 3: | 606 | case 3: |
602 | query += "DueDate"; | 607 | query += "DueDate"; |
603 | break; | 608 | break; |
604 | } | 609 | } |
605 | 610 | ||
606 | if ( !asc ) { | 611 | if ( !asc ) { |
607 | odebug << "not ascending!" << oendl; | 612 | odebug << "not ascending!" << oendl; |
608 | query += " DESC"; | 613 | query += " DESC"; |
609 | } | 614 | } |
610 | 615 | ||
611 | odebug << query << oendl; | 616 | odebug << query << oendl; |
612 | OSQLRawQuery raw(query ); | 617 | OSQLRawQuery raw(query ); |
613 | return uids( m_driver->query(&raw) ); | 618 | return uids( m_driver->query(&raw) ); |
614 | } | 619 | } |
615 | bool OPimTodoAccessBackendSQL::date( QDate& da, const QString& str ) const{ | 620 | bool OPimTodoAccessBackendSQL::date( QDate& da, const QString& str ) const{ |
616 | if ( str == "0-0-0" ) | 621 | if ( str == "0-0-0" ) |
617 | return false; | 622 | return false; |
618 | else{ | 623 | else{ |
619 | int day, year, month; | 624 | int day, year, month; |
620 | QStringList list = QStringList::split("-", str ); | 625 | QStringList list = QStringList::split("-", str ); |
621 | year = list[0].toInt(); | 626 | year = list[0].toInt(); |