00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 #include <stdio.h>
00045 #include <stdlib.h>
00046
00047 #include <qarray.h>
00048 #include <qstringlist.h>
00049
00050 #include <qpe/global.h>
00051
00052 #include <opie2/osqldriver.h>
00053 #include <opie2/osqlmanager.h>
00054 #include <opie2/osqlquery.h>
00055
00056 #include "orecur.h"
00057 #include "odatebookaccessbackend_sql.h"
00058
00059 using namespace Opie::DB;
00060
00061
00062 ODateBookAccessBackend_SQL::ODateBookAccessBackend_SQL( const QString& ,
00063 const QString& fileName )
00064 : ODateBookAccessBackend(), m_driver( NULL )
00065 {
00066 m_fileName = fileName.isEmpty() ? Global::applicationFileName( "datebook", "datebook.db" ) : fileName;
00067
00068
00069 OSQLManager man;
00070 m_driver = man.standard();
00071 m_driver->setUrl( m_fileName );
00072
00073 initFields();
00074
00075 load();
00076 }
00077
00078 ODateBookAccessBackend_SQL::~ODateBookAccessBackend_SQL() {
00079 if( m_driver )
00080 delete m_driver;
00081 }
00082
00083 void ODateBookAccessBackend_SQL::initFields()
00084 {
00085
00086
00087
00088 m_fieldMap.insert( OEvent::FUid, "uid" );
00089 m_fieldMap.insert( OEvent::FCategories, "Categories" );
00090 m_fieldMap.insert( OEvent::FDescription, "Description" );
00091 m_fieldMap.insert( OEvent::FLocation, "Location" );
00092 m_fieldMap.insert( OEvent::FType, "Type" );
00093 m_fieldMap.insert( OEvent::FAlarm, "Alarm" );
00094 m_fieldMap.insert( OEvent::FSound, "Sound" );
00095 m_fieldMap.insert( OEvent::FRType, "RType" );
00096 m_fieldMap.insert( OEvent::FRWeekdays, "RWeekdays" );
00097 m_fieldMap.insert( OEvent::FRPosition, "RPosition" );
00098 m_fieldMap.insert( OEvent::FRFreq, "RFreq" );
00099 m_fieldMap.insert( OEvent::FRHasEndDate, "RHasEndDate" );
00100 m_fieldMap.insert( OEvent::FREndDate, "REndDate" );
00101 m_fieldMap.insert( OEvent::FRCreated, "RCreated" );
00102 m_fieldMap.insert( OEvent::FRExceptions, "RExceptions" );
00103 m_fieldMap.insert( OEvent::FStart, "Start" );
00104 m_fieldMap.insert( OEvent::FEnd, "End" );
00105 m_fieldMap.insert( OEvent::FNote, "Note" );
00106 m_fieldMap.insert( OEvent::FTimeZone, "TimeZone" );
00107 m_fieldMap.insert( OEvent::FRecParent, "RecParent" );
00108 m_fieldMap.insert( OEvent::FRecChildren, "Recchildren" );
00109
00110
00111 QMapConstIterator<int, QString> it;
00112 for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){
00113 m_reverseFieldMap.insert( it.data(), it.key() );
00114 }
00115
00116 }
00117
00118 bool ODateBookAccessBackend_SQL::load()
00119 {
00120 if (!m_driver->open() )
00121 return false;
00122
00123
00124
00125
00126 QString qu = "create table datebook( uid INTEGER PRIMARY KEY ";
00127
00128 QMap<int, QString>::Iterator it;
00129 for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){
00130 qu += QString( ",%1 VARCHAR(10)" ).arg( it.data() );
00131 }
00132 qu += " );";
00133
00134 qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR, priority INTEGER, value VARCHAR, PRIMARY KEY /* identifier */ (uid, id) );";
00135
00136 qWarning( "command: %s", qu.latin1() );
00137
00138 OSQLRawQuery raw( qu );
00139 OSQLResult res = m_driver->query( &raw );
00140 if ( res.state() != OSQLResult::Success )
00141 return false;
00142
00143 update();
00144
00145 return true;
00146 }
00147
00148 void ODateBookAccessBackend_SQL::update()
00149 {
00150
00151 QString qu = "select uid from datebook";
00152 OSQLRawQuery raw( qu );
00153 OSQLResult res = m_driver->query( &raw );
00154 if ( res.state() != OSQLResult::Success ){
00155
00156 return;
00157 }
00158
00159 m_uids = extractUids( res );
00160
00161 }
00162
00163 bool ODateBookAccessBackend_SQL::reload()
00164 {
00165 return load();
00166 }
00167
00168 bool ODateBookAccessBackend_SQL::save()
00169 {
00170 return m_driver->close();
00171 }
00172
00173 QArray<int> ODateBookAccessBackend_SQL::allRecords()const
00174 {
00175 return m_uids;
00176 }
00177
00178 QArray<int> ODateBookAccessBackend_SQL::queryByExample(const OEvent&, int, const QDateTime& ) {
00179 return QArray<int>();
00180 }
00181
00182 void ODateBookAccessBackend_SQL::clear()
00183 {
00184 QString qu = "drop table datebook;";
00185 qu += "drop table custom_data;";
00186
00187 OSQLRawQuery raw( qu );
00188 OSQLResult res = m_driver->query( &raw );
00189
00190 reload();
00191 }
00192
00193
00194 OEvent ODateBookAccessBackend_SQL::find( int uid ) const{
00195 QString qu = "select *";
00196 qu += "from datebook where uid = " + QString::number(uid);
00197
00198 OSQLRawQuery raw( qu );
00199 OSQLResult res = m_driver->query( &raw );
00200
00201 OSQLResultItem resItem = res.first();
00202
00203
00204 QMap<int,QString> dateEventMap;
00205 dateEventMap.insert( OEvent::FUid, QString::number( uid ) );
00206
00207
00208 QMapConstIterator<int, QString> it;
00209 for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){
00210 dateEventMap.insert( m_reverseFieldMap[*it], resItem.data( *it ) );
00211 }
00212
00213
00214 OEvent retDate( dateEventMap );
00215
00216 return retDate;
00217 }
00218
00219
00220 bool ODateBookAccessBackend_SQL::add( const OEvent& ev )
00221 {
00222 QMap<int,QString> eventMap = ev.toMap();
00223
00224 QString qu = "insert into datebook VALUES( " + QString::number( ev.uid() );
00225 QMap<int, QString>::Iterator it;
00226 for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){
00227 if ( !eventMap[it.key()].isEmpty() )
00228 qu += QString( ",\"%1\"" ).arg( eventMap[it.key()] );
00229 else
00230 qu += QString( ",\"\"" );
00231 }
00232 qu += " );";
00233
00234
00235 int id = 0;
00236 QMap<QString, QString> customMap = ev.toExtraMap();
00237 for( QMap<QString, QString>::Iterator it = customMap.begin();
00238 it != customMap.end(); ++it ){
00239 qu += "insert into custom_data VALUES("
00240 + QString::number( ev.uid() )
00241 + ","
00242 + QString::number( id++ )
00243 + ",'"
00244 + it.key()
00245 + "',"
00246 + "0"
00247 + ",'"
00248 + it.data()
00249 + "');";
00250 }
00251 qWarning("add %s", qu.latin1() );
00252
00253 OSQLRawQuery raw( qu );
00254 OSQLResult res = m_driver->query( &raw );
00255 if ( res.state() != OSQLResult::Success ){
00256 return false;
00257 }
00258
00259
00260 update();
00261
00262 return true;
00263 }
00264
00265
00266 bool ODateBookAccessBackend_SQL::remove( int uid )
00267 {
00268 QString qu = "DELETE from datebook where uid = "
00269 + QString::number( uid ) + ";";
00270 qu += "DELETE from custom_data where uid = "
00271 + QString::number( uid ) + ";";
00272
00273 OSQLRawQuery raw( qu );
00274 OSQLResult res = m_driver->query( &raw );
00275 if ( res.state() != OSQLResult::Success ){
00276 return false;
00277 }
00278
00279
00280 update();
00281
00282 return true;
00283 }
00284
00285 bool ODateBookAccessBackend_SQL::replace( const OEvent& ev )
00286 {
00287 remove( ev.uid() );
00288 return add( ev );
00289 }
00290
00291 QArray<int> ODateBookAccessBackend_SQL::rawEvents()const
00292 {
00293 return allRecords();
00294 }
00295
00296 QArray<int> ODateBookAccessBackend_SQL::rawRepeats()const
00297 {
00298 QString qu = "select uid from datebook where RType!=\"\" AND RType!=\"NoRepeat\"";
00299 OSQLRawQuery raw( qu );
00300 OSQLResult res = m_driver->query( &raw );
00301 if ( res.state() != OSQLResult::Success ){
00302 QArray<int> nix;
00303 return nix;
00304 }
00305
00306 return extractUids( res );
00307 }
00308
00309 QArray<int> ODateBookAccessBackend_SQL::nonRepeats()const
00310 {
00311 QString qu = "select uid from datebook where RType=\"\" or RType=\"NoRepeat\"";
00312 OSQLRawQuery raw( qu );
00313 OSQLResult res = m_driver->query( &raw );
00314 if ( res.state() != OSQLResult::Success ){
00315 QArray<int> nix;
00316 return nix;
00317 }
00318
00319 return extractUids( res );
00320 }
00321
00322 OEvent::ValueList ODateBookAccessBackend_SQL::directNonRepeats()
00323 {
00324 QArray<int> nonRepUids = nonRepeats();
00325 OEvent::ValueList list;
00326
00327 for (uint i = 0; i < nonRepUids.count(); ++i ){
00328 list.append( find( nonRepUids[i] ) );
00329 }
00330
00331 return list;
00332
00333 }
00334 OEvent::ValueList ODateBookAccessBackend_SQL::directRawRepeats()
00335 {
00336 QArray<int> rawRepUids = rawRepeats();
00337 OEvent::ValueList list;
00338
00339 for (uint i = 0; i < rawRepUids.count(); ++i ){
00340 list.append( find( rawRepUids[i] ) );
00341 }
00342
00343 return list;
00344 }
00345
00346
00347 QArray<int> ODateBookAccessBackend_SQL::matchRegexp( const QRegExp &r ) const
00348 {
00349 QArray<int> null;
00350 return null;
00351 }
00352
00353
00354
00355 QArray<int> ODateBookAccessBackend_SQL::extractUids( OSQLResult& res ) const
00356 {
00357 qWarning("extractUids");
00358 QTime t;
00359 t.start();
00360 OSQLResultItem::ValueList list = res.results();
00361 OSQLResultItem::ValueList::Iterator it;
00362 QArray<int> ints(list.count() );
00363 qWarning(" count = %d", list.count() );
00364
00365 int i = 0;
00366 for (it = list.begin(); it != list.end(); ++it ) {
00367 ints[i] = (*it).data("uid").toInt();
00368 i++;
00369 }
00370 qWarning("extractUids ready: count2 = %d needs %d ms", i, t.elapsed() );
00371
00372 return ints;
00373
00374 }