Diffstat (limited to 'libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp | 140 |
1 files changed, 118 insertions, 22 deletions
diff --git a/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp b/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp index 3d284f7..9375f43 100644 --- a/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp +++ b/libopie2/opiepim/backend/ocontactaccessbackend_sql.cpp @@ -118,47 +118,47 @@ namespace { ~RemoveQuery(); QString query()const; private: int m_uid; }; /** * a find query for noncustom elements */ class FindQuery : public OSQLQuery { public: FindQuery(int uid); - FindQuery(const QArray<int>& ); + FindQuery(const UIDArray& ); ~FindQuery(); QString query()const; private: QString single()const; QString multi()const; - QArray<int> m_uids; + UIDArray m_uids; int m_uid; }; /** * a find query for custom elements */ class FindCustomQuery : public OSQLQuery { public: FindCustomQuery(int uid); - FindCustomQuery(const QArray<int>& ); + FindCustomQuery(const UIDArray& ); ~FindCustomQuery(); QString query()const; private: QString single()const; QString multi()const; - QArray<int> m_uids; + UIDArray m_uids; int m_uid; }; // We using two tables to store the information: // 1. addressbook : It contains General information about the contact (non custom) // 2. custom_data : Not official supported entries // All tables are connected by the uid of the contact. // Maybe I should add a table for meta-information ? CreateQuery::CreateQuery() : OSQLQuery() {} CreateQuery::~CreateQuery() {} @@ -285,25 +285,25 @@ namespace { + QString::number(m_uid) + ";"; qu += "DELETE from custom_data where uid = " + QString::number(m_uid) + ";"; return qu; } FindQuery::FindQuery(int uid) : OSQLQuery(), m_uid( uid ) { } - FindQuery::FindQuery(const QArray<int>& ints) + FindQuery::FindQuery(const UIDArray& ints) : OSQLQuery(), m_uids( ints ){ } FindQuery::~FindQuery() { } QString FindQuery::query()const{ if ( m_uids.count() == 0 ) return single(); else return multi(); } QString FindQuery::multi()const { @@ -320,25 +320,25 @@ namespace { QString FindQuery::single()const{ QString qu = "select *"; qu += " from addressbook where uid = " + QString::number(m_uid); // owarn << "find query: " << qu << "" << oendl; return qu; } FindCustomQuery::FindCustomQuery(int uid) : OSQLQuery(), m_uid( uid ) { } - FindCustomQuery::FindCustomQuery(const QArray<int>& ints) + FindCustomQuery::FindCustomQuery(const UIDArray& ints) : OSQLQuery(), m_uids( ints ){ } FindCustomQuery::~FindCustomQuery() { } QString FindCustomQuery::query()const{ // if ( m_uids.count() == 0 ) return single(); } QString FindCustomQuery::single()const{ QString qu = "select uid, type, value from custom_data where uid = "; qu += QString::number(m_uid); return qu; @@ -413,25 +413,25 @@ void OPimContactAccessBackend_SQL::clear () { ClearQuery cle; OSQLResult res = m_driver->query( &cle ); reload(); } bool OPimContactAccessBackend_SQL::wasChangedExternally() { return false; } -QArray<int> OPimContactAccessBackend_SQL::allRecords() const +UIDArray OPimContactAccessBackend_SQL::allRecords() const { // FIXME: Think about cute handling of changed tables.. // Thus, we don't have to call update here... if ( m_changed ) ((OPimContactAccessBackend_SQL*)this)->update(); return m_uids; } bool OPimContactAccessBackend_SQL::add ( const OPimContact &newcontact ) { @@ -476,25 +476,25 @@ OPimContact OPimContactAccessBackend_SQL::find ( int uid ) const { odebug << "OPimContactAccessBackend_SQL::find(" << uid << ")" << oendl; QTime t; t.start(); OPimContact retContact( requestNonCustom( uid ) ); retContact.setExtraMap( requestCustom( uid ) ); odebug << "OPimContactAccessBackend_SQL::find() needed: " << t.elapsed() << " ms" << oendl; return retContact; } -OPimContact OPimContactAccessBackend_SQL::find( int uid, const QArray<int>& queryUids, uint current, Frontend::CacheDirection direction ) const +OPimContact OPimContactAccessBackend_SQL::find( int uid, const UIDArray& queryUids, uint current, Frontend::CacheDirection direction ) const { odebug << "OPimContactAccessBackend_SQL::find( ..multi.. )" << oendl; odebug << "searching for " << uid << "" << oendl; QTime t; t.start(); uint numReadAhead = readAhead(); QArray<int> searchList( numReadAhead ); uint size =0; @@ -519,25 +519,26 @@ OPimContact OPimContactAccessBackend_SQL::find( int uid, const QArray<int>& quer } //Shrink to real size.. searchList.resize( size ); OPimContact retContact( requestContactsAndCache( uid, searchList ) ); odebug << "OPimContactAccessBackend_SQL::find( ..multi.. ) needed: " << t.elapsed() << " ms" << oendl; return retContact; } -QArray<int> OPimContactAccessBackend_SQL::queryByExample ( const OPimContact &query, int settings, const QDateTime& qd ) +UIDArray OPimContactAccessBackend_SQL::queryByExample ( const OPimContact &query, int settings, + const QDateTime& qd ) const { QString qu = "SELECT uid FROM addressbook WHERE"; QString searchQuery =""; QDate startDate; if ( qd.isValid() ) startDate = qd.date(); else startDate = QDate::currentDate(); @@ -630,34 +631,34 @@ QArray<int> OPimContactAccessBackend_SQL::queryByExample ( const OPimContact &qu // Skip trailing "AND" // if ( isAnyFieldSelected ) // qu = qu.left( qu.length() - 4 ); qu += searchQuery; odebug << "queryByExample query: " << qu << "" << oendl; // Execute query and return the received uid's OSQLRawQuery raw( qu ); OSQLResult res = m_driver->query( &raw ); if ( res.state() != OSQLResult::Success ){ - QArray<int> empty; + UIDArray empty; return empty; } - QArray<int> list = extractUids( res ); + UIDArray list = extractUids( res ); return list; } -QArray<int> OPimContactAccessBackend_SQL::matchRegexp( const QRegExp &r ) const +UIDArray OPimContactAccessBackend_SQL::matchRegexp( const QRegExp &r ) const { #if 0 QArray<int> nix(0); return nix; #else QString qu = "SELECT uid FROM addressbook WHERE ("; QString searchlist; QStringList fieldList = OPimContactFields::untrfields( false ); // QMap<QString, int> translate = OPimContactFields::untrFieldsToId(); for ( QStringList::Iterator it = ++fieldList.begin(); it != fieldList.end(); ++it ){ @@ -670,25 +671,25 @@ QArray<int> OPimContactAccessBackend_SQL::matchRegexp( const QRegExp &r ) const odebug << "query: " << qu << "" << oendl; OSQLRawQuery raw( qu ); OSQLResult res = m_driver->query( &raw ); return extractUids( res ); #endif } -const uint OPimContactAccessBackend_SQL::querySettings() +const uint OPimContactAccessBackend_SQL::querySettings() const { return OPimContactAccess::IgnoreCase | OPimContactAccess::WildCards | OPimContactAccess::DateDiff | OPimContactAccess::DateYear | OPimContactAccess::DateMonth | OPimContactAccess::DateDay ; } bool OPimContactAccessBackend_SQL::hasQuerySettings (uint querySettings) const { @@ -729,45 +730,140 @@ bool OPimContactAccessBackend_SQL::hasQuerySettings (uint querySettings) const case OPimContactAccess::WildCards: return ( true ); case OPimContactAccess::ExactMatch: return ( true ); case 0: // one of the upper removed bits were set.. return ( true ); default: return ( false ); } } -QArray<int> OPimContactAccessBackend_SQL::sorted( bool asc, int , int , int ) +UIDArray OPimContactAccessBackend_SQL::sorted( const UIDArray& ar, bool asc, int sortOrder, + int filter, const QArray<int>& categories )const { QTime t; t.start(); QString query = "SELECT uid FROM addressbook "; - query += "ORDER BY \"Last Name\" "; + + query += " WHERE ("; + for ( uint i = 0; i < ar.count(); i++ ) { + query += " uid = " + QString::number( ar[i] ) + " OR"; + } + query.remove( query.length()-2, 2 ); // Hmmmm.. + query += ")"; + + + if ( filter != OPimBase::FilterOff ){ + if ( filter & OPimContactAccess::DoNotShowWithCategory ){ + query += " AND ( \"Categories\" == '' )"; + } else if ( filter & OPimBase::FilterCategory ){ + query += " AND ("; + for ( uint i = 0; i < categories.count(); i++ ){ + query += "\"Categories\" LIKE"; + query += QString( " '%" ) + QString::number( categories[i] ) + "%' OR"; + } + query.remove( query.length()-2, 2 ); // Hmmmm.. + query += ")"; + } + + if ( filter & OPimContactAccess::DoNotShowWithoutChildren ){ + query += " AND ( \"Children\" != '' )"; + } + + if ( filter & OPimContactAccess::DoNotShowWithoutAnniversary ){ + query += " AND ( \"Anniversary\" != '' )"; + } + + if ( filter & OPimContactAccess::DoNotShowWithoutBirthday ){ + query += " AND ( \"Birthday\" != '' )"; + } + + if ( filter & OPimContactAccess::DoNotShowWithoutHomeAddress ){ + // Expect that no Street means no Address, too! (eilers) + query += " AND ( \"Home Street\" != '' )"; + } + + if ( filter & OPimContactAccess::DoNotShowWithoutBusinessAddress ){ + // Expect that no Street means no Address, too! (eilers) + query += " AND ( \"Business Street\" != '' )"; + } + + } + + query += " ORDER BY"; + + switch ( sortOrder ) { + case OPimContactAccess::SortSummary: + query += " \"Notes\""; + break; + case OPimContactAccess::SortByCategory: + query += " \"Categories\""; + break; + case OPimContactAccess::SortByDate: + query += " \"\""; + break; + case OPimContactAccess::SortTitle: + query += " \"Name Title\""; + break; + case OPimContactAccess::SortFirstName: + query += " \"First Name\""; + break; + case OPimContactAccess::SortMiddleName: + query += " \"Middle Name\""; + break; + case OPimContactAccess::SortLastName: + query += " \"Last Name\""; + break; + case OPimContactAccess::SortFileAsName: + query += " \"File As\""; + break; + case OPimContactAccess::SortSuffix: + query += " \"Suffix\""; + break; + case OPimContactAccess::SortEmail: + query += " \"Default Email\""; + break; + case OPimContactAccess::SortNickname: + query += " \"Nickname\""; + break; + case OPimContactAccess::SortAnniversary: + query += " \"Anniversary\""; + break; + case OPimContactAccess::SortBirthday: + query += " \"Birthday\""; + break; + case OPimContactAccess::SortGender: + query += " \"Gender\""; + break; + default: + query += " \"Last Name\""; + } if ( !asc ) query += "DESC"; - // odebug << "sorted query is: " << query << "" << oendl; + + odebug << "sorted query is: " << query << "" << oendl; OSQLRawQuery raw( query ); OSQLResult res = m_driver->query( &raw ); if ( res.state() != OSQLResult::Success ){ - QArray<int> empty; + UIDArray empty; return empty; } - QArray<int> list = extractUids( res ); + UIDArray list = extractUids( res ); odebug << "sorted needed " << t.elapsed() << " ms!" << oendl; return list; } void OPimContactAccessBackend_SQL::update() { odebug << "Update starts" << oendl; QTime t; t.start(); @@ -777,32 +873,32 @@ void OPimContactAccessBackend_SQL::update() LoadQuery lo; OSQLResult res = m_driver->query(&lo); if ( res.state() != OSQLResult::Success ) return; m_uids = extractUids( res ); m_changed = false; odebug << "Update ends " << t.elapsed() << " ms" << oendl; } -QArray<int> OPimContactAccessBackend_SQL::extractUids( OSQLResult& res ) const +UIDArray OPimContactAccessBackend_SQL::extractUids( OSQLResult& res ) const { odebug << "extractUids" << oendl; QTime t; t.start(); OSQLResultItem::ValueList list = res.results(); OSQLResultItem::ValueList::Iterator it; - QArray<int> ints(list.count() ); + UIDArray ints(list.count() ); odebug << " count = " << list.count() << "" << oendl; int i = 0; for (it = list.begin(); it != list.end(); ++it ) { ints[i] = (*it).data("uid").toInt(); i++; } odebug << "extractUids ready: count2 = " << i << " needs " << t.elapsed() << " ms" << oendl; return ints; } @@ -828,31 +924,31 @@ QMap<int, QString> OPimContactAccessBackend_SQL::requestNonCustom( int uid ) co nonCustomMap = fillNonCustomMap( resItem ); t3needed = t3.elapsed(); // odebug << "Adding UID: " << resItem.data( "uid" ) << "" << oendl; odebug << "RequestNonCustom needed: insg.:" << t.elapsed() << " ms, query: " << t2needed << " ms, mapping: " << t3needed << " ms" << oendl; return nonCustomMap; } /* Returns contact requested by uid and fills cache with contacts requested by uids in the cachelist */ -OPimContact OPimContactAccessBackend_SQL::requestContactsAndCache( int uid, const QArray<int>& uidlist )const +OPimContact OPimContactAccessBackend_SQL::requestContactsAndCache( int uid, const UIDArray& uidlist )const { // We want to get all contacts with one query. // We don't have to add the given uid to the uidlist, it is expected to be there already (see opimrecordlist.h). // All contacts will be stored in the cache, afterwards the contact with the user id "uid" will be returned // by using the cache.. - QArray<int> cachelist = uidlist; + UIDArray cachelist = uidlist; OPimContact retContact; odebug << "Reqest and cache" << cachelist.size() << "elements !" << oendl; QTime t; t.start(); int t2needed = 0; int t3needed = 0; QTime t2; t2.start(); FindQuery query( cachelist ); |