-rw-r--r-- | libopie2/opiepim/backend/odatebookaccessbackend_sql.cpp | 24 | ||||
-rw-r--r-- | libopie2/opiepim/backend/otodoaccesssql.cpp | 75 |
2 files changed, 80 insertions, 19 deletions
diff --git a/libopie2/opiepim/backend/odatebookaccessbackend_sql.cpp b/libopie2/opiepim/backend/odatebookaccessbackend_sql.cpp index 8a8cb0b..495a8dd 100644 --- a/libopie2/opiepim/backend/odatebookaccessbackend_sql.cpp +++ b/libopie2/opiepim/backend/odatebookaccessbackend_sql.cpp @@ -160,149 +160,153 @@ bool ODateBookAccessBackend_SQL::load() } qu += " );"; qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR(10), priority INTEGER, value VARCHAR(10), PRIMARY KEY /* identifier */ (uid, id) );"; qWarning( "command: %s", qu.latin1() ); OSQLRawQuery raw( qu ); OSQLResult res = m_driver->query( &raw ); if ( res.state() != OSQLResult::Success ) return false; update(); return true; } void ODateBookAccessBackend_SQL::update() { QString qu = "select uid from datebook"; OSQLRawQuery raw( qu ); OSQLResult res = m_driver->query( &raw ); if ( res.state() != OSQLResult::Success ){ // m_uids.clear(); return; } m_uids = extractUids( res ); } bool ODateBookAccessBackend_SQL::reload() { return load(); } bool ODateBookAccessBackend_SQL::save() { return m_driver->close(); // Shouldn't m_driver->sync be better than close ? (eilers) } QArray<int> ODateBookAccessBackend_SQL::allRecords()const { return m_uids; } QArray<int> ODateBookAccessBackend_SQL::queryByExample(const OPimEvent&, int, const QDateTime& ) { return QArray<int>(); } void ODateBookAccessBackend_SQL::clear() { QString qu = "drop table datebook;"; qu += "drop table custom_data;"; OSQLRawQuery raw( qu ); OSQLResult res = m_driver->query( &raw ); reload(); } OPimEvent ODateBookAccessBackend_SQL::find( int uid ) const{ + qDebug( "ODateBookAccessBackend_SQL::find( %d )", uid ); + QString qu = "select *"; qu += "from datebook where uid = " + QString::number(uid); OSQLRawQuery raw( qu ); OSQLResult res = m_driver->query( &raw ); OSQLResultItem resItem = res.first(); // Create Map for date event and insert UID QMap<int,QString> dateEventMap; dateEventMap.insert( OPimEvent::FUid, QString::number( uid ) ); // Now insert the data out of the columns into the map. QMapConstIterator<int, QString> it; for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){ dateEventMap.insert( m_reverseFieldMap[*it], resItem.data( *it ) ); } // Last step: Put map into date event, add custom map and return it OPimEvent retDate( dateEventMap ); retDate.setExtraMap( requestCustom( uid ) ); + + qDebug( "ODateBookAccessBackend_SQL::find() end", uid ); return retDate; } // FIXME: Speed up update of uid's.. bool ODateBookAccessBackend_SQL::add( const OPimEvent& ev ) { QMap<int,QString> eventMap = ev.toMap(); QString qu = "insert into datebook VALUES( " + QString::number( ev.uid() ); QMap<int, QString>::Iterator it; for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){ if ( !eventMap[it.key()].isEmpty() ) qu += QString( ",\"%1\"" ).arg( eventMap[it.key()] ); else qu += QString( ",\"\"" ); } qu += " );"; // Add custom entries int id = 0; QMap<QString, QString> customMap = ev.toExtraMap(); for( QMap<QString, QString>::Iterator it = customMap.begin(); it != customMap.end(); ++it ){ qu += "insert into custom_data VALUES(" + QString::number( ev.uid() ) + "," + QString::number( id++ ) + ",'" + it.key() //.latin1() + "'," + "0" // Priority for future enhancements + ",'" + it.data() //.latin1() + "');"; } qWarning("add %s", qu.latin1() ); OSQLRawQuery raw( qu ); OSQLResult res = m_driver->query( &raw ); if ( res.state() != OSQLResult::Success ){ return false; } // Update list of uid's update(); return true; } // FIXME: Speed up update of uid's.. bool ODateBookAccessBackend_SQL::remove( int uid ) { QString qu = "DELETE from datebook where uid = " + QString::number( uid ) + ";"; qu += "DELETE from custom_data where uid = " + QString::number( uid ) + ";"; OSQLRawQuery raw( qu ); OSQLResult res = m_driver->query( &raw ); if ( res.state() != OSQLResult::Success ){ return false; } // Update list of uid's @@ -314,119 +318,135 @@ bool ODateBookAccessBackend_SQL::remove( int uid ) bool ODateBookAccessBackend_SQL::replace( const OPimEvent& ev ) { remove( ev.uid() ); return add( ev ); } QArray<int> ODateBookAccessBackend_SQL::rawEvents()const { return allRecords(); } QArray<int> ODateBookAccessBackend_SQL::rawRepeats()const { QString qu = "select uid from datebook where RType!=\"\" AND RType!=\"NoRepeat\""; OSQLRawQuery raw( qu ); OSQLResult res = m_driver->query( &raw ); if ( res.state() != OSQLResult::Success ){ QArray<int> nix; return nix; } return extractUids( res ); } QArray<int> ODateBookAccessBackend_SQL::nonRepeats()const { QString qu = "select uid from datebook where RType=\"\" or RType=\"NoRepeat\""; OSQLRawQuery raw( qu ); OSQLResult res = m_driver->query( &raw ); if ( res.state() != OSQLResult::Success ){ QArray<int> nix; return nix; } return extractUids( res ); } OPimEvent::ValueList ODateBookAccessBackend_SQL::directNonRepeats() { QArray<int> nonRepUids = nonRepeats(); OPimEvent::ValueList list; for (uint i = 0; i < nonRepUids.count(); ++i ){ list.append( find( nonRepUids[i] ) ); } return list; } OPimEvent::ValueList ODateBookAccessBackend_SQL::directRawRepeats() { QArray<int> rawRepUids = rawRepeats(); OPimEvent::ValueList list; for (uint i = 0; i < rawRepUids.count(); ++i ){ list.append( find( rawRepUids[i] ) ); } return list; } QArray<int> ODateBookAccessBackend_SQL::matchRegexp( const QRegExp &r ) const { - QArray<int> null; - return null; + + QString qu = "SELECT uid FROM datebook WHERE ("; + + // Do it make sense to search other fields, too ? + qu += " rlike(\""+ r.pattern() + "\", Location ) OR"; + qu += " rlike(\""+ r.pattern() + "\", Note )"; + + qu += " )"; + + qDebug( "query: %s", qu.latin1() ); + + OSQLRawQuery raw( qu ); + OSQLResult res = m_driver->query( &raw ); + + return extractUids( res ); + + + } /* ===== Private Functions ========================================== */ QArray<int> ODateBookAccessBackend_SQL::extractUids( OSQLResult& res ) const { qWarning("extractUids"); QTime t; t.start(); OSQLResultItem::ValueList list = res.results(); OSQLResultItem::ValueList::Iterator it; QArray<int> ints(list.count() ); qWarning(" count = %d", list.count() ); int i = 0; for (it = list.begin(); it != list.end(); ++it ) { ints[i] = (*it).data("uid").toInt(); i++; } qWarning("extractUids ready: count2 = %d needs %d ms", i, t.elapsed() ); return ints; } QMap<QString, QString> ODateBookAccessBackend_SQL::requestCustom( int uid ) const { QTime t; t.start(); QMap<QString, QString> customMap; FindCustomQuery query( uid ); OSQLResult res_custom = m_driver->query( &query ); if ( res_custom.state() == OSQLResult::Failure ) { qWarning("OSQLResult::Failure in find query !!"); QMap<QString, QString> empty; return empty; } OSQLResultItem::ValueList list = res_custom.results(); OSQLResultItem::ValueList::Iterator it = list.begin(); for ( ; it != list.end(); ++it ) { customMap.insert( (*it).data( "type" ), (*it).data( "value" ) ); } qDebug("RequestCustom needed: %d ms", t.elapsed() ); return customMap; } } diff --git a/libopie2/opiepim/backend/otodoaccesssql.cpp b/libopie2/opiepim/backend/otodoaccesssql.cpp index b4170fc..ef036d5 100644 --- a/libopie2/opiepim/backend/otodoaccesssql.cpp +++ b/libopie2/opiepim/backend/otodoaccesssql.cpp @@ -238,129 +238,130 @@ namespace { + "'" + m_todo.description() + "'" + "," + "'" + m_todo.summary() + "'" + "," + QString::number(m_todo.priority() ) + "," + "'" + QString::number(year).rightJustify( 4, '0' ) + "-" + QString::number(month).rightJustify( 2, '0' ) + "-" + QString::number( day ).rightJustify( 2, '0' )+ "'" + "," + QString::number( m_todo.progress() ) + "," + QString::number( m_todo.state().state() ) + "," + "'" + recMap[ OPimRecurrence::RType ] + "'" + "," + "'" + recMap[ OPimRecurrence::RWeekdays ] + "'" + "," + "'" + recMap[ OPimRecurrence::RPosition ] + "'" + "," + "'" + recMap[ OPimRecurrence::RFreq ] + "'" + "," + "'" + recMap[ OPimRecurrence::RHasEndDate ] + "'" + "," + "'" + recMap[ OPimRecurrence::EndDate ] + "'" + "," + "'" + recMap[ OPimRecurrence::Created ] + "'" + "," + "'" + recMap[ OPimRecurrence::Exceptions ] + "'" + ","; if ( m_todo.hasNotifiers() ) { OPimNotifyManager manager = m_todo.notifiers(); qu += "'" + manager.remindersToString() + "'" + "," + "'" + manager.alarmsToString() + "'" + ","; } else{ qu += QString( "''" ) + "," + "''" + ","; } qu += QString( "''" ) + QString( "," ) // Maintainers (cur. not supported !) + "'" + QString::number(sYear).rightJustify( 4, '0' ) + "-" + QString::number(sMonth).rightJustify( 2, '0' ) + "-" + QString::number(sDay).rightJustify( 2, '0' )+ "'" + "," + "'" + QString::number(eYear).rightJustify( 4, '0' ) + "-" + QString::number(eMonth).rightJustify( 2, '0' ) + "-"+QString::number(eDay).rightJustify( 2, '0' ) + "'" + ")"; // Save custom Entries: int id = 0; id = 0; QMap<QString, QString> customMap = m_todo.toExtraMap(); for( QMap<QString, QString>::Iterator it = customMap.begin(); it != customMap.end(); ++it ){ qu += "insert into custom_data VALUES(" + QString::number( m_todo.uid() ) + "," + QString::number( id++ ) + ",'" + it.key() + "'," + "0" // Priority for future enhancements + ",'" + it.data() + "');"; } qDebug("add %s", qu.latin1() ); return qu; } RemoveQuery::RemoveQuery(int uid ) : OSQLQuery(), m_uid( uid ) {} RemoveQuery::~RemoveQuery() {} QString RemoveQuery::query()const { - QString qu = "DELETE from todolist where uid = " + QString::number(m_uid); + QString qu = "DELETE FROM todolist WHERE uid = " + QString::number(m_uid) + " ;"; + qu += "DELETE FROM custom_data WHERE uid = " + QString::number(m_uid); return qu; } ClearQuery::ClearQuery() : OSQLQuery() {} ClearQuery::~ClearQuery() {} QString ClearQuery::query()const { QString qu = "drop table todolist"; return qu; } FindQuery::FindQuery(int uid) : OSQLQuery(), m_uid(uid ) { } FindQuery::FindQuery(const QArray<int>& ints) : OSQLQuery(), m_uids(ints){ } FindQuery::~FindQuery() { } QString FindQuery::query()const{ if (m_uids.count() == 0 ) return single(); else return multi(); } QString FindQuery::single()const{ QString qu = "select * from todolist where uid = " + QString::number(m_uid); return qu; } QString FindQuery::multi()const { QString qu = "select * from todolist where "; for (uint i = 0; i < m_uids.count(); i++ ) { qu += " UID = " + QString::number( m_uids[i] ) + " OR"; } qu.remove( qu.length()-2, 2 ); return qu; } OverDueQuery::OverDueQuery(): OSQLQuery() {} OverDueQuery::~OverDueQuery() {} QString OverDueQuery::query()const { QDate date = QDate::currentDate(); QString str; str = QString("select uid from todolist where DueDate ='%1-%2-%3'") .arg( QString::number( date.year() ).rightJustify( 4, '0' ) ) .arg( QString::number( date.month() ).rightJustify( 2, '0' ) ) .arg( QString::number( date.day() ) .rightJustify( 2, '0' ) ); return str; } EffQuery::EffQuery( const QDate& start, const QDate& end, bool inc ) : OSQLQuery(), m_start( start ), m_end( end ),m_inc(inc) {} EffQuery::~EffQuery() {} QString EffQuery::query()const { return m_inc ? with() : out(); } QString EffQuery::with()const { QString str; str = QString("select uid from todolist where ( DueDate >= '%1-%2-%3' AND DueDate <= '%4-%5-%6' ) OR DueDate = '0-0-0' ") .arg( QString::number( m_start.year() ).rightJustify( 4, '0' ) ) .arg( QString::number( m_start.month() ).rightJustify( 2, '0' ) ) .arg( QString::number( m_start.day() ).rightJustify( 2, '0' ) ) @@ -701,139 +702,179 @@ OPimTodo OPimTodoAccessBackendSQL::todo( int uid )const { FindQuery find( uid ); return todo( m_driver->query(&find) ); } /* * update the dict */ void OPimTodoAccessBackendSQL::fillDict() { #if 0 /* initialize dict */ /* * UPDATE dict if you change anything!!! * FIXME: Isn't this dict obsolete ? (eilers) */ m_dict.setAutoDelete( TRUE ); m_dict.insert("Categories" , new int(OPimTodo::Category) ); m_dict.insert("Uid" , new int(OPimTodo::Uid) ); m_dict.insert("HasDate" , new int(OPimTodo::HasDate) ); m_dict.insert("Completed" , new int(OPimTodo::Completed) ); m_dict.insert("Description" , new int(OPimTodo::Description) ); m_dict.insert("Summary" , new int(OPimTodo::Summary) ); m_dict.insert("Priority" , new int(OPimTodo::Priority) ); m_dict.insert("DateDay" , new int(OPimTodo::DateDay) ); m_dict.insert("DateMonth" , new int(OPimTodo::DateMonth) ); m_dict.insert("DateYear" , new int(OPimTodo::DateYear) ); m_dict.insert("Progress" , new int(OPimTodo::Progress) ); m_dict.insert("Completed", new int(OPimTodo::Completed) ); // Why twice ? (eilers) m_dict.insert("CrossReference", new int(OPimTodo::CrossReference) ); // m_dict.insert("HasAlarmDateTime",new int(OPimTodo::HasAlarmDateTime) ); // old stuff (eilers) // m_dict.insert("AlarmDateTime", new int(OPimTodo::AlarmDateTime) ); // old stuff (eilers) #endif } /* * need to be const so let's fool the * compiler :( */ void OPimTodoAccessBackendSQL::update()const { ((OPimTodoAccessBackendSQL*)this)->m_dirty = false; LoadQuery lo; OSQLResult res = m_driver->query(&lo); if ( res.state() != OSQLResult::Success ) return; ((OPimTodoAccessBackendSQL*)this)->m_uids = uids( res ); } QArray<int> OPimTodoAccessBackendSQL::uids( const OSQLResult& res) const{ OSQLResultItem::ValueList list = res.results(); OSQLResultItem::ValueList::Iterator it; QArray<int> ints(list.count() ); qDebug(" count = %d", list.count() ); int i = 0; for (it = list.begin(); it != list.end(); ++it ) { ints[i] = (*it).data("uid").toInt(); i++; } return ints; } QArray<int> OPimTodoAccessBackendSQL::matchRegexp( const QRegExp &r ) const { -#warning OPimTodoAccessBackendSQL::matchRegexp() not implemented !! - #if 0 + QArray<int> empty; + return empty; - Copied from xml-backend by not adapted to sql (eilers) +#else + QString qu = "SELECT uid FROM todolist WHERE ("; - QArray<int> m_currentQuery( m_events.count() ); - uint arraycounter = 0; + // Do it make sense to search other fields, too ? + qu += " rlike(\""+ r.pattern() + "\",\"description\") OR"; + qu += " rlike(\""+ r.pattern() + "\",\"summary\")"; + + qu += ")"; + qDebug( "query: %s", qu.latin1() ); + OSQLRawQuery raw( qu ); + OSQLResult res = m_driver->query( &raw ); - QMap<int, OPimTodo>::ConstIterator it; - for (it = m_events.begin(); it != m_events.end(); ++it ) { - if ( it.data().match( r ) ) - m_currentQuery[arraycounter++] = it.data().uid(); + return uids( res ); - } - // Shrink to fit.. - m_currentQuery.resize(arraycounter); - return m_currentQuery; #endif - QArray<int> empty; - return empty; + } QBitArray OPimTodoAccessBackendSQL::supports()const { return sup(); } QBitArray OPimTodoAccessBackendSQL::sup() const{ QBitArray ar( OPimTodo::CompletedDate + 1 ); ar.fill( true ); ar[OPimTodo::CrossReference] = false; ar[OPimTodo::State ] = false; ar[OPimTodo::Reminders] = false; ar[OPimTodo::Notifiers] = false; ar[OPimTodo::Maintainer] = false; return ar; } void OPimTodoAccessBackendSQL::removeAllCompleted(){ -#warning OPimTodoAccessBackendSQL::removeAllCompleted() not implemented !! + // First we need the uids from all entries which are + // completed. Then, we just have to remove them... + + QString qu = "SELECT uid FROM todolist WHERE completed = 1"; + + OSQLRawQuery raw( qu ); + OSQLResult res = m_driver->query( &raw ); + + QArray<int> completed_uids = uids( res ); + qDebug( "Number of completed: %d", completed_uids.size() ); + + if ( completed_uids.size() == 0 ) + return; + + qu = "DELETE FROM todolist WHERE ("; + QString query; + + for ( int i = 0; i < completed_uids.size(); i++ ){ + if ( !query.isEmpty() ) + query += " OR "; + query += QString( "uid = %1" ).arg( completed_uids[i] ); + } + qu += query + " );"; + + // Put remove of custom entries in this query to speed up.. + qu += "DELETE FORM custom_data WHERE ("; + query = ""; + + for ( int i = 0; i < completed_uids.size(); i++ ){ + if ( !query.isEmpty() ) + query += " OR "; + query += QString( "uid = %1" ).arg( completed_uids[i] ); + } + qu += query + " );"; + + qDebug( "query: %s", qu.latin1() ); + + OSQLRawQuery raw2( qu ); + res = m_driver->query( &raw2 ); + if ( res.state() == OSQLResult::Failure ) { + qWarning("OPimTodoAccessBackendSQL::removeAllCompleted():Failure in query: %s", qu.latin1() ); + } } QMap<QString, QString> OPimTodoAccessBackendSQL::requestCustom( int uid ) const { QMap<QString, QString> customMap; FindCustomQuery query( uid ); OSQLResult res_custom = m_driver->query( &query ); if ( res_custom.state() == OSQLResult::Failure ) { qWarning("OSQLResult::Failure in find query !!"); QMap<QString, QString> empty; return empty; } OSQLResultItem::ValueList list = res_custom.results(); OSQLResultItem::ValueList::Iterator it = list.begin(); for ( ; it != list.end(); ++it ) { customMap.insert( (*it).data( "type" ), (*it).data( "value" ) ); } return customMap; } } |