summaryrefslogtreecommitdiff
path: root/libopie/pim/odatebookaccessbackend_sql.cpp
Unidiff
Diffstat (limited to 'libopie/pim/odatebookaccessbackend_sql.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie/pim/odatebookaccessbackend_sql.cpp221
1 files changed, 221 insertions, 0 deletions
diff --git a/libopie/pim/odatebookaccessbackend_sql.cpp b/libopie/pim/odatebookaccessbackend_sql.cpp
new file mode 100644
index 0000000..9769bf7
--- a/dev/null
+++ b/libopie/pim/odatebookaccessbackend_sql.cpp
@@ -0,0 +1,221 @@
1/*
2 * SQL Backend for the OPIE-Calender Database.
3 *
4 * Copyright (c) 2003 by Stefan Eilers (Eilers.Stefan@epost.de)
5 *
6 * =====================================================================
7 *This program is free software; you can redistribute it and/or
8 *modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 * =====================================================================
12 * =====================================================================
13 * Version: $Id$
14 * =====================================================================
15 * History:
16 * $Log$
17 * Revision 1.1 2003/12/08 15:18:12 eilers
18 * Committing unfinished sql implementation before merging to libopie2 starts..
19 *
20 *
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25
26#include <qarray.h>
27#include <qstringlist.h>
28
29#include "orecur.h"
30#include "odatebookaccessbackend_sql.h"
31
32#include <opie2/osqldriver.h>
33#include <opie2/osqlresult.h>
34#include <opie2/osqlmanager.h>
35#include <opie2/osqlquery.h>
36
37namespace {
38
39
40
41};
42
43ODateBookAccessBackend_SQL::ODateBookAccessBackend_SQL( const QString& ,
44 const QString& fileName )
45 : ODateBookAccessBackend(), m_driver( NULL )
46{
47 m_fileName = fileName.isEmpty() ? Global::applicationFileName( "datebook", "datebook.db" ) : fileName;
48
49 // Get the standart sql-driver from the OSQLManager..
50 OSQLManager man;
51 m_driver = man.standard();
52 m_driver->setUrl( m_fileName );
53
54 initFields();
55
56 load();
57}
58
59ODateBookAccessBackend_SQL::~ODateBookAccessBackend_SQL() {
60}
61
62void ODateBookAccessBackend_SQL::initFields()
63{
64
65 // This map contains the translation of the fieldtype id's to
66 // the names of the table columns
67 m_fieldMap.insert( OEvent::FUid, "uid" );
68 m_fieldMap.insert( OEvent::FCategories, "Categories" );
69 m_fieldMap.insert( OEvent::FDescription, "Description" );
70 m_fieldMap.insert( OEvent::FLocation, "Location" );
71 m_fieldMap.insert( OEvent::FType, "Type" );
72 m_fieldMap.insert( OEvent::FAlarm, "Alarm" );
73 m_fieldMap.insert( OEvent::FSound, "Sound" );
74 m_fieldMap.insert( OEvent::FRType, "RType" );
75 m_fieldMap.insert( OEvent::FRWeekdays, "RWeekdays" );
76 m_fieldMap.insert( OEvent::FRPosition, "RPosition" );
77 m_fieldMap.insert( OEvent::FRFreq, "RFreq" );
78 m_fieldMap.insert( OEvent::FRHasEndDate, "RHasEndDate" );
79 m_fieldMap.insert( OEvent::FREndDate, "REndDate" );
80 m_fieldMap.insert( OEvent::FRCreated, "RCreated" );
81 m_fieldMap.insert( OEvent::FRExeptions, "RExceptions" );
82 m_fieldMap.insert( OEvent::FStart, "Start" );
83 m_fieldMap.insert( OEvent::FEnd, "End" );
84 m_fieldMap.insert( OEvent::FNote, "Note" );
85 m_fieldMap.insert( OEvent::FTimeZone, "TimeZone" );
86 m_fieldMap.insert( OEvent::FRecParent, "RecParent" );
87 m_fieldMap.insert( OEvent::FRecChildren, "Recchildren" );
88}
89
90bool ODateBookAccessBackend_SQL::load()
91{
92 if (!m_driver->open() )
93 return false;
94
95 // Don't expect that the database exists.
96 // It is save here to create the table, even if it
97 // do exist. ( Is that correct for all databases ?? )
98 QStringqu = "create table datebook( uid INTEGER PRIMARY KEY ";
99
100 QMap<int, QString>::Iterator it;
101 for ( it = ++m_fieldMap.begin(); it != m_fieldMap.end(); ++it ){
102 qu += QString( ",\"%1\" VARCHAR(10)" ).arg( it.data() );
103 }
104 qu += " );";
105
106 qu += "create table custom_data( uid INTEGER, id INTEGER, type VARCHAR, priority INTEGER, value VARCHAR, PRIMARY KEY /* identifier */ (uid, id) );";
107
108 OSQLRawQuery raw( qu );
109 OSQLResult res = m_driver->query( &raw );
110 if ( res.state() != OSQLResult::Success )
111 return false;
112
113 update();
114
115 return true;
116}
117
118void ODateBookAccessBackend_SQL::update()
119{
120
121 QString qu = "select uid from datebook";
122 OSQLRawQuery raw( qu );
123 OSQLResult res = m_driver->query( &raw );
124 if ( res.state() != OSQLResult::Success ){
125 m_uids.clear();
126 return;
127 }
128
129 m_uids = extractUids( res );
130
131}
132
133QArray<int> ODateBookAccessBackend_SQL::extractUids( OSQLResult& res ) const
134{
135 qWarning("extractUids");
136
137 OSQLResultItem::ValueList list = res.results();
138 OSQLResultItem::ValueList::Iterator it;
139 QArray<int> ints(list.count() );
140 qWarning(" count = %d", list.count() );
141
142 int i = 0;
143 for (it = list.begin(); it != list.end(); ++it ) {
144 ints[i] = (*it).data("uid").toInt();
145 i++;
146 }
147
148 return ints;
149
150}
151
152bool ODateBookAccessBackend_SQL::reload()
153{
154 return load();
155}
156
157bool ODateBookAccessBackend_SQL::save()
158{
159 return m_driver->close();
160}
161
162QArray<int> ODateBookAccessBackend_SQL::allRecords()const
163{
164 return m_uids;
165}
166
167QArray<int> ODateBookAccessBackend_SQL::queryByExample(const OEvent&, int, const QDateTime& ) {
168 return QArray<int>();
169}
170
171void ODateBookAccessBackend_SQL::clear()
172{
173 QString qu = "drop table datebook;";
174 qu += "drop table custom_data;";
175
176 OSQLRawQuery raw( qu );
177 OSQLResult res = m_driver->query( &raw );
178
179}
180
181
182OEvent ODateBookAccessBackend_SQL::find( int uid ) const{
183}
184
185bool ODateBookAccessBackend_SQL::add( const OEvent& ev ) {
186 return true;
187}
188bool ODateBookAccessBackend_SQL::remove( int uid ) {
189
190 return true;
191}
192bool ODateBookAccessBackend_SQL::replace( const OEvent& ev ) {
193 remove( ev.uid() );
194 return add( ev );
195}
196QArray<int> ODateBookAccessBackend_SQL::rawEvents()const {
197 return allRecords();
198}
199QArray<int> ODateBookAccessBackend_SQL::rawRepeats()const {
200
201 return ints;
202}
203QArray<int> ODateBookAccessBackend_SQL::nonRepeats()const {
204
205 return ints;
206}
207OEvent::ValueList ODateBookAccessBackend_SQL::directNonRepeats() {
208
209 return list;
210}
211OEvent::ValueList ODateBookAccessBackend_SQL::directRawRepeats() {
212
213 return list;
214}
215
216
217QArray<int> ODateBookAccessBackend_SQL::matchRegexp( const QRegExp &r ) const
218{
219
220 return m_currentQuery;
221}