summaryrefslogtreecommitdiff
path: root/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp
Side-by-side diff
Diffstat (limited to 'libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp180
1 files changed, 123 insertions, 57 deletions
diff --git a/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp b/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp
index 1ea35a8..3142f75 100644
--- a/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp
+++ b/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp
@@ -41,2 +41,3 @@
+#include <opie2/opimcontact.h>
#include <opie2/opimcontactfields.h>
@@ -48,2 +49,3 @@
+using namespace Opie;
using namespace Opie::DB;
@@ -52,4 +54,3 @@ using namespace Opie::DB;
/*
- * Implementation of used query types
- * CREATE query
+ * Implementation of used query types * CREATE query
* LOAD query
@@ -59,3 +60,3 @@ using namespace Opie::DB;
*/
-namespace Opie {
+namespace {
/**
@@ -152,3 +153,3 @@ namespace Opie {
- // We using three tables to store the information:
+ // We using two tables to store the information:
// 1. addressbook : It contains General information about the contact (non custom)
@@ -170,3 +171,3 @@ namespace Opie {
- qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR, priority INTEGER, value VARCHAR, PRIMARY KEY /* identifier */ (uid, id) );";
+ qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR(10), priority INTEGER, value VARCHAR(10), PRIMARY KEY /* identifier */ (uid, id) );";
@@ -223,25 +224,17 @@ namespace Opie {
switch ( id ){
- case Qtopia::Birthday:{
- // These entries should stored in a special format
- // year-month-day
- QDate day = m_contact.birthday();
- if ( day.isValid() ){
- qu += QString(",\"%1-%2-%3\"")
- .arg( day.year() )
- .arg( day.month() )
- .arg( day.day() );
+ case Qtopia::Birthday:
+ case Qtopia::Anniversary:{
+ QDate day;
+ if ( id == Qtopia::Birthday ){
+ day = m_contact.birthday();
} else {
- qu += ",\"\"";
+ day = m_contact.anniversary();
}
- }
- break;
- case Qtopia::Anniversary:{
// These entries should stored in a special format
// year-month-day
- QDate day = m_contact.anniversary();
if ( day.isValid() ){
qu += QString(",\"%1-%2-%3\"")
- .arg( day.year() )
- .arg( day.month() )
- .arg( day.day() );
+ .arg( QString::number( day.year() ).rightJustify( 4, '0' ) )
+ .arg( QString::number( day.month() ).rightJustify( 2, '0' ) )
+ .arg( QString::number( day.day() ).rightJustify( 2, '0' ) );
} else {
@@ -251,3 +244,2 @@ namespace Opie {
break;
-
default:
@@ -270,3 +262,3 @@ namespace Opie {
+ ",'"
- + it.key() //.latin1()
+ + it.key()
+ "',"
@@ -274,3 +266,3 @@ namespace Opie {
+ ",'"
- + it.data() //.latin1()
+ + it.data()
+ "');";
@@ -278,3 +270,3 @@ namespace Opie {
// qu += "commit;";
- qWarning("add %s", qu.latin1() );
+ qDebug("add %s", qu.latin1() );
return qu;
@@ -360,3 +352,3 @@ OPimContactAccessBackend_SQL::OPimContactAccessBackend_SQL ( const QString& /* a
{
- qWarning("C'tor OPimContactAccessBackend_SQL starts");
+ qDebug("C'tor OPimContactAccessBackend_SQL starts");
QTime t;
@@ -377,3 +369,3 @@ OPimContactAccessBackend_SQL::OPimContactAccessBackend_SQL ( const QString& /* a
- qWarning("C'tor OPimContactAccessBackend_SQL ends: %d ms", t.elapsed() );
+ qDebug("C'tor OPimContactAccessBackend_SQL ends: %d ms", t.elapsed() );
}
@@ -478,3 +470,3 @@ OPimContact OPimContactAccessBackend_SQL::find ( int uid ) const
{
- qWarning("OPimContactAccessBackend_SQL::find()");
+ qDebug("OPimContactAccessBackend_SQL::find()");
QTime t;
@@ -485,3 +477,3 @@ OPimContact OPimContactAccessBackend_SQL::find ( int uid ) const
- qWarning("OPimContactAccessBackend_SQL::find() needed: %d ms", t.elapsed() );
+ qDebug("OPimContactAccessBackend_SQL::find() needed: %d ms", t.elapsed() );
return retContact;
@@ -491,5 +483,14 @@ OPimContact OPimContactAccessBackend_SQL::find ( int uid ) const
-QArray<int> OPimContactAccessBackend_SQL::queryByExample ( const OPimContact &query, int settings, const QDateTime& d = QDateTime() )
+QArray<int> OPimContactAccessBackend_SQL::queryByExample ( const OPimContact &query, int settings, const QDateTime& qd )
{
QString qu = "SELECT uid FROM addressbook WHERE";
+ QString searchQuery ="";
+
+ QDate startDate;
+
+ if ( qd.isValid() )
+ startDate = qd.date();
+ else
+ startDate = QDate::currentDate();
+
@@ -500,9 +501,67 @@ QArray<int> OPimContactAccessBackend_SQL::queryByExample ( const OPimContact &qu
// Convert every filled field to a SQL-Query
- bool isAnyFieldSelected = false;
+// bool isAnyFieldSelected = false;
for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){
+
int id = translate[*it];
QString queryStr = queryFields[id];
+ QDate* endDate = 0l;
+
if ( !queryStr.isEmpty() ){
- isAnyFieldSelected = true;
+ // If something is alredy stored in the query, add an "AND"
+ // to the end of the string to prepare for the next ..
+ if ( !searchQuery.isEmpty() )
+ searchQuery += " AND";
+
+// isAnyFieldSelected = true;
switch( id ){
+ case Qtopia::Birthday:
+ endDate = new QDate( query.birthday() );
+ // Fall through !
+ case Qtopia::Anniversary:
+ if ( endDate == 0l )
+ endDate = new QDate( query.anniversary() );
+
+ if ( settings & OPimContactAccess::DateDiff ) {
+ searchQuery += QString( " (\"%1\" <= '%2-%3-%4\' AND \"%5\" >= '%6-%7-%8')" )
+ .arg( *it )
+ .arg( QString::number( endDate->year() ).rightJustify( 4, '0' ) )
+ .arg( QString::number( endDate->month() ).rightJustify( 2, '0' ) )
+ .arg( QString::number( endDate->day() ).rightJustify( 2, '0' ) )
+ .arg( *it )
+ .arg( QString::number( startDate.year() ).rightJustify( 4, '0' ) )
+ .arg( QString::number( startDate.month() ).rightJustify( 2, '0' ) )
+ .arg( QString::number( startDate.day() ).rightJustify( 2, '0' ) ) ;
+ }
+
+ if ( settings & OPimContactAccess::DateYear ){
+ if ( settings & OPimContactAccess::DateDiff )
+ searchQuery += " AND";
+
+ searchQuery += QString( " (\"%1\" LIKE '%2-%')" )
+ .arg( *it )
+ .arg( QString::number( endDate->year() ).rightJustify( 4, '0' ) );
+ }
+
+ if ( settings & OPimContactAccess::DateMonth ){
+ if ( ( settings & OPimContactAccess::DateDiff )
+ || ( settings & OPimContactAccess::DateYear ) )
+ searchQuery += " AND";
+
+ searchQuery += QString( " (\"%1\" LIKE '%-%2-%')" )
+ .arg( *it )
+ .arg( QString::number( endDate->month() ).rightJustify( 2, '0' ) );
+ }
+
+ if ( settings & OPimContactAccess::DateDay ){
+ if ( ( settings & OPimContactAccess::DateDiff )
+ || ( settings & OPimContactAccess::DateYear )
+ || ( settings & OPimContactAccess::DateMonth ) )
+ searchQuery += " AND";
+
+ searchQuery += QString( " (\"%1\" LIKE '%-%-%2')" )
+ .arg( *it )
+ .arg( QString::number( endDate->day() ).rightJustify( 2, '0' ) );
+ }
+
+ break;
default:
@@ -512,7 +571,7 @@ QArray<int> OPimContactAccessBackend_SQL::queryByExample ( const OPimContact &qu
if ( settings & OPimContactAccess::IgnoreCase )
- qu += "(\"" + *it + "\"" + " LIKE " + "'"
- + queryStr.replace(QRegExp("\\*"),"%") + "'" + ") AND ";
+ searchQuery += "(\"" + *it + "\"" + " LIKE " + "'"
+ + queryStr.replace(QRegExp("\\*"),"%") + "'" + ")";
else
- qu += "(\"" + *it + "\"" + " GLOB " + "'"
- + queryStr + "'" + ") AND ";
+ searchQuery += "(\"" + *it + "\"" + " GLOB " + "'"
+ + queryStr + "'" + ")";
@@ -522,6 +581,8 @@ QArray<int> OPimContactAccessBackend_SQL::queryByExample ( const OPimContact &qu
// Skip trailing "AND"
- if ( isAnyFieldSelected )
- qu = qu.left( qu.length() - 4 );
+// if ( isAnyFieldSelected )
+// qu = qu.left( qu.length() - 4 );
+
+ qu += searchQuery;
- qWarning( "queryByExample query: %s", qu.latin1() );
+ qDebug( "queryByExample query: %s", qu.latin1() );
@@ -549,3 +610,8 @@ const uint OPimContactAccessBackend_SQL::querySettings()
return OPimContactAccess::IgnoreCase
- || OPimContactAccess::WildCards;
+ || OPimContactAccess::WildCards
+ || OPimContactAccess::DateDiff
+ || OPimContactAccess::DateYear
+ || OPimContactAccess::DateMonth
+ || OPimContactAccess::DateDay
+ ;
}
@@ -563,6 +629,6 @@ bool OPimContactAccessBackend_SQL::hasQuerySettings (uint querySettings) const
| OPimContactAccess::WildCards
-// | OPimContactAccess::DateDiff
-// | OPimContactAccess::DateYear
-// | OPimContactAccess::DateMonth
-// | OPimContactAccess::DateDay
+ | OPimContactAccess::DateDiff
+ | OPimContactAccess::DateYear
+ | OPimContactAccess::DateMonth
+ | OPimContactAccess::DateDay
// | OPimContactAccess::RegExp
@@ -611,3 +677,3 @@ QArray<int> OPimContactAccessBackend_SQL::sorted( bool asc, int , int , int )
- // qWarning("sorted query is: %s", query.latin1() );
+ // qDebug("sorted query is: %s", query.latin1() );
@@ -622,3 +688,3 @@ QArray<int> OPimContactAccessBackend_SQL::sorted( bool asc, int , int , int )
- qWarning("sorted needed %d ms!", t.elapsed() );
+ qDebug("sorted needed %d ms!", t.elapsed() );
return list;
@@ -629,3 +695,3 @@ void OPimContactAccessBackend_SQL::update()
{
- qWarning("Update starts");
+ qDebug("Update starts");
QTime t;
@@ -645,3 +711,3 @@ void OPimContactAccessBackend_SQL::update()
- qWarning("Update ends %d ms", t.elapsed() );
+ qDebug("Update ends %d ms", t.elapsed() );
}
@@ -650,3 +716,3 @@ QArray<int> OPimContactAccessBackend_SQL::extractUids( OSQLResult& res ) const
{
- qWarning("extractUids");
+ qDebug("extractUids");
QTime t;
@@ -656,3 +722,3 @@ QArray<int> OPimContactAccessBackend_SQL::extractUids( OSQLResult& res ) const
QArray<int> ints(list.count() );
- qWarning(" count = %d", list.count() );
+ qDebug(" count = %d", list.count() );
@@ -663,3 +729,3 @@ QArray<int> OPimContactAccessBackend_SQL::extractUids( OSQLResult& res ) const
}
- qWarning("extractUids ready: count2 = %d needs %d ms", i, t.elapsed() );
+ qDebug("extractUids ready: count2 = %d needs %d ms", i, t.elapsed() );
@@ -698,3 +764,3 @@ QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) co
- // qWarning("Reading %s... found: %s", (*it).latin1(), value.latin1() );
+ // qDebug("Reading %s... found: %s", (*it).latin1(), value.latin1() );
@@ -716,3 +782,3 @@ QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) co
case Qtopia::AddressCategory:
- qWarning("Category is: %s", value.latin1() );
+ qDebug("Category is: %s", value.latin1() );
default:
@@ -726,4 +792,4 @@ QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) co
- // qWarning("Adding UID: %s", resItem.data( "uid" ).latin1() );
- qWarning("RequestNonCustom needed: insg.:%d ms, query: %d ms, mapping: %d ms",
+ // qDebug("Adding UID: %s", resItem.data( "uid" ).latin1() );
+ qDebug("RequestNonCustom needed: insg.:%d ms, query: %d ms, mapping: %d ms",
t.elapsed(), t2needed, t3needed );
@@ -755,3 +821,3 @@ QMap<QString, QString> OPimContactAccessBackend_SQL::requestCustom( int uid ) c
- qWarning("RequestCustom needed: %d ms", t.elapsed() );
+ qDebug("RequestCustom needed: %d ms", t.elapsed() );
return customMap;