summaryrefslogtreecommitdiff
path: root/libopie2/opiedb
Unidiff
Diffstat (limited to 'libopie2/opiedb') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opiedb/osqlitedriver.cpp24
1 files changed, 7 insertions, 17 deletions
diff --git a/libopie2/opiedb/osqlitedriver.cpp b/libopie2/opiedb/osqlitedriver.cpp
index 69eddfe..b9a491e 100644
--- a/libopie2/opiedb/osqlitedriver.cpp
+++ b/libopie2/opiedb/osqlitedriver.cpp
@@ -1,238 +1,228 @@
1/* 1/*
2                 This file is part of the Opie Project 2                 This file is part of the Opie Project
3 3
4 =. 4 =.
5 .=l. 5 .=l.
6           .>+-= 6           .>+-=
7 _;:,     .>    :=|. This program is free software; you can 7 _;:,     .>    :=|. This program is free software; you can
8.> <`_,   >  .   <= redistribute it and/or modify it under 8.> <`_,   >  .   <= redistribute it and/or modify it under
9:`=1 )Y*s>-.--   : the terms of the GNU Library General Public 9:`=1 )Y*s>-.--   : the terms of the GNU Library General Public
10.="- .-=="i,     .._ License as published by the Free Software 10.="- .-=="i,     .._ License as published by the Free Software
11 - .   .-<_>     .<> Foundation; either version 2 of the License, 11 - .   .-<_>     .<> Foundation; either version 2 of the License,
12     ._= =}       : or (at your option) any later version. 12     ._= =}       : or (at your option) any later version.
13    .%`+i>       _;_. 13    .%`+i>       _;_.
14    .i_,=:_.      -<s. This program is distributed in the hope that 14    .i_,=:_.      -<s. This program is distributed in the hope that
15     +  .  -:.       = it will be useful, but WITHOUT ANY WARRANTY; 15     +  .  -:.       = it will be useful, but WITHOUT ANY WARRANTY;
16    : ..    .:,     . . . without even the implied warranty of 16    : ..    .:,     . . . without even the implied warranty of
17    =_        +     =;=|` MERCHANTABILITY or FITNESS FOR A 17    =_        +     =;=|` MERCHANTABILITY or FITNESS FOR A
18  _.=:.       :    :=>`: PARTICULAR PURPOSE. See the GNU 18  _.=:.       :    :=>`: PARTICULAR PURPOSE. See the GNU
19..}^=.=       =       ; Library General Public License for more 19..}^=.=       =       ; Library General Public License for more
20++=   -.     .`     .: details. 20++=   -.     .`     .: details.
21 :     =  ...= . :.=- 21 :     =  ...= . :.=-
22 -.   .:....=;==+<; You should have received a copy of the GNU 22 -.   .:....=;==+<; You should have received a copy of the GNU
23  -_. . .   )=.  = Library General Public License along with 23  -_. . .   )=.  = Library General Public License along with
24    --        :-=` this library; see the file COPYING.LIB. 24    --        :-=` this library; see the file COPYING.LIB.
25 If not, write to the Free Software Foundation, 25 If not, write to the Free Software Foundation,
26 Inc., 59 Temple Place - Suite 330, 26 Inc., 59 Temple Place - Suite 330,
27 Boston, MA 02111-1307, USA. 27 Boston, MA 02111-1307, USA.
28 28
29*/ 29*/
30 30
31#include "osqlquery.h" 31#include "osqlquery.h"
32#include "osqlitedriver.h" 32#include "osqlitedriver.h"
33 33
34#include <opie2/odebug.h> 34#include <opie2/odebug.h>
35 35
36#include <stdlib.h> 36#include <stdlib.h>
37#include <stdio.h> 37#include <stdio.h>
38 38
39// fromLocal8Bit() does not work as expected. Thus it 39namespace Opie {
40// is replaced by fromLatin1() (eilers) 40namespace DB {
41#define __BUGGY_LOCAL8BIT_
42
43namespace Opie {
44namespace DB {
45namespace Internal { 41namespace Internal {
46 42
47namespace { 43namespace {
48 struct Query { 44 struct Query {
49 OSQLError::ValueList errors; 45 OSQLError::ValueList errors;
50 OSQLResultItem::ValueList items; 46 OSQLResultItem::ValueList items;
51 OSQLiteDriver *driver; 47 OSQLiteDriver *driver;
52 }; 48 };
53} 49}
54 50
55 51
56OSQLiteDriver::OSQLiteDriver( QLibrary *lib ) 52OSQLiteDriver::OSQLiteDriver( QLibrary *lib )
57 : OSQLDriver( lib ) 53 : OSQLDriver( lib )
58{ 54{
59 m_sqlite = 0l; 55 m_sqlite = 0l;
60} 56}
61 57
62 58
63OSQLiteDriver::~OSQLiteDriver() { 59OSQLiteDriver::~OSQLiteDriver() {
64 close(); 60 close();
65} 61}
66 62
67 63
68QString OSQLiteDriver::id()const { 64QString OSQLiteDriver::id()const {
69 return QString::fromLatin1("SQLite"); 65 return QString::fromLatin1("SQLite");
70} 66}
71 67
72void OSQLiteDriver::setUserName( const QString& ) {} 68void OSQLiteDriver::setUserName( const QString& ) {}
73 69
74 70
75void OSQLiteDriver::setPassword( const QString& ) {} 71void OSQLiteDriver::setPassword( const QString& ) {}
76 72
77 73
78void OSQLiteDriver::setUrl( const QString& url ) { 74void OSQLiteDriver::setUrl( const QString& url ) {
79 m_url = url; 75 m_url = url;
80} 76}
81 77
82 78
83void OSQLiteDriver::setOptions( const QStringList& ) { 79void OSQLiteDriver::setOptions( const QStringList& ) {
84} 80}
85 81
86/* 82/*
87 * Functions to patch a regex search into sqlite 83 * Functions to patch a regex search into sqlite
88 */ 84 */
89int sqliteRlikeCompare(const char *zPattern, const char *zString, sqregex *reg){ 85int sqliteRlikeCompare(const char *zPattern, const char *zString, sqregex *reg){
90 int res; 86 int res;
91 if (reg->regex_raw == NULL || (strcmp (zPattern, reg->regex_raw) != 0)){ 87 if (reg->regex_raw == NULL || (strcmp (zPattern, reg->regex_raw) != 0)){
92 if (reg->regex_raw != NULL) { 88 if (reg->regex_raw != NULL) {
93 free(reg->regex_raw); 89 free(reg->regex_raw);
94 regfree(&reg->regex_c); 90 regfree(&reg->regex_c);
95 } 91 }
96 reg->regex_raw = (char *)malloc(strlen(zPattern)+1); 92 reg->regex_raw = (char *)malloc(strlen(zPattern)+1);
97 strncpy(reg->regex_raw, zPattern, strlen(zPattern)+1); 93 strncpy(reg->regex_raw, zPattern, strlen(zPattern)+1);
98 res = regcomp(&reg->regex_c, zPattern, REG_EXTENDED); 94 res = regcomp(&reg->regex_c, zPattern, REG_EXTENDED);
99 if ( res != 0 ) { 95 if ( res != 0 ) {
100 printf("Regcomp failed with code %u on string %s\n",res,zPattern); 96 printf("Regcomp failed with code %u on string %s\n",res,zPattern);
101 free(reg->regex_raw); 97 free(reg->regex_raw);
102 reg->regex_raw=NULL; 98 reg->regex_raw=NULL;
103 return 0; 99 return 0;
104 } 100 }
105 } 101 }
106 res = (regexec(&reg->regex_c, zString, 0, NULL, 0)==0); 102 res = (regexec(&reg->regex_c, zString, 0, NULL, 0)==0);
107 return res; 103 return res;
108} 104}
109 105
110void rlikeFunc(sqlite_func *context, int arg, const char **argv){ 106void rlikeFunc(sqlite_func *context, int arg, const char **argv){
111 if( argv[0]==0 || argv[1]==0 ){ 107 if( argv[0]==0 || argv[1]==0 ){
112 printf("One of arguments Null!!\n"); 108 printf("One of arguments Null!!\n");
113 return; 109 return;
114 } 110 }
115 sqlite_set_result_int(context, 111 sqlite_set_result_int(context,
116 sqliteRlikeCompare((const char*)argv[0], 112 sqliteRlikeCompare((const char*)argv[0],
117 (const char*)argv[1], (sqregex *)sqlite_user_data(context) )); 113 (const char*)argv[1], (sqregex *)sqlite_user_data(context) ));
118} 114}
119 115
120/* 116/*
121 * try to open a db specified via setUrl 117 * try to open a db specified via setUrl
122 * and options 118 * and options
123 */ 119 */
124bool OSQLiteDriver::open() { 120bool OSQLiteDriver::open() {
125 char *error; 121 char *error;
126 122
127 odebug << "OSQLiteDriver::open: about to open" << oendl; 123 odebug << "OSQLiteDriver::open: about to open" << oendl;
128 m_sqlite = sqlite_open(m_url.local8Bit(), 124 m_sqlite = sqlite_open(m_url.local8Bit(),
129 0, 125 0,
130 &error ); 126 &error );
131 127
132 /* failed to open */ 128 /* failed to open */
133 if (m_sqlite == 0l ) { 129 if (m_sqlite == 0l ) {
134 // FIXME set the last error 130 // FIXME set the last error
135 owarn << "OSQLiteDriver::open: " << error << "" << oendl; 131 owarn << "OSQLiteDriver::open: " << error << "" << oendl;
136 free( error ); 132 free( error );
137 return false; 133 return false;
138 } 134 }
139 if (sqlite_create_function(m_sqlite,"rlike",2,rlikeFunc,&sqreg) != 0) 135 if (sqlite_create_function(m_sqlite,"rlike",2,rlikeFunc,&sqreg) != 0)
140 odebug << "Unable to create user defined function!" << oendl; 136 odebug << "Unable to create user defined function!" << oendl;
141 if (sqlite_function_type(m_sqlite,"rlike",SQLITE_NUMERIC) != 0) 137 if (sqlite_function_type(m_sqlite,"rlike",SQLITE_NUMERIC) != 0)
142 odebug << "Unable to set rlike function result type!" << oendl; 138 odebug << "Unable to set rlike function result type!" << oendl;
143 sqreg.regex_raw = NULL; 139 sqreg.regex_raw = NULL;
144 return true; 140 return true;
145} 141}
146 142
147 143
148/* close the db 144/* close the db
149 * sqlite closes them without 145 * sqlite closes them without
150 * telling failure or success 146 * telling failure or success
151 */ 147 */
152bool OSQLiteDriver::close() { 148bool OSQLiteDriver::close() {
153 if (m_sqlite ) 149 if (m_sqlite )
154 sqlite_close( m_sqlite ), m_sqlite=0l; 150 sqlite_close( m_sqlite ), m_sqlite=0l;
155 if (sqreg.regex_raw != NULL){ 151 if (sqreg.regex_raw != NULL){
156 odebug << "Freeing regex on close" << oendl; 152 odebug << "Freeing regex on close" << oendl;
157 free(sqreg.regex_raw); 153 free(sqreg.regex_raw);
158 sqreg.regex_raw=NULL; 154 sqreg.regex_raw=NULL;
159 regfree(&sqreg.regex_c); 155 regfree(&sqreg.regex_c);
160 } 156 }
161 return true; 157 return true;
162} 158}
163 159
164 160
165/* Query */ 161/* Query */
166OSQLResult OSQLiteDriver::query( OSQLQuery* qu) { 162OSQLResult OSQLiteDriver::query( OSQLQuery* qu) {
167 if ( !m_sqlite ) { 163 if ( !m_sqlite ) {
168 // FIXME set error code 164 // FIXME set error code
169 OSQLResult result( OSQLResult::Failure ); 165 OSQLResult result( OSQLResult::Failure );
170 return result; 166 return result;
171 } 167 }
172 Query query; 168 Query query;
173 query.driver = this; 169 query.driver = this;
174 char *err; 170 char *err;
175 /* SQLITE_OK 0 if return code > 0 == failure */ 171 /* SQLITE_OK 0 if return code > 0 == failure */
176 if ( sqlite_exec(m_sqlite, qu->query(),&call_back, &query, &err) > 0 ) { 172 if ( sqlite_exec(m_sqlite, qu->query().utf8(),&call_back, &query, &err) > 0 ) {
177 owarn << "OSQLiteDriver::query: Error while executing " << err << "" << oendl; 173 qWarning("OSQLiteDriver::query: Error while executing %s",err);
178 free(err ); 174 free(err );
179 // FixMe Errors 175 // FixMe Errors
180 } 176 }
181 177
182 OSQLResult result(OSQLResult::Success, 178 OSQLResult result(OSQLResult::Success,
183 query.items, 179 query.items,
184 query.errors ); 180 query.errors );
185 return result; 181 return result;
186} 182}
187 183
188 184
189OSQLTable::ValueList OSQLiteDriver::tables() const { 185OSQLTable::ValueList OSQLiteDriver::tables() const {
190 186
191} 187}
192 188
193 189
194OSQLError OSQLiteDriver::lastError() { 190OSQLError OSQLiteDriver::lastError() {
195 OSQLError error; 191 OSQLError error;
196 return error; 192 return error;
197}; 193};
198 194
199 195
200/* handle a callback add the row to the global 196/* handle a callback add the row to the global
201 * OSQLResultItem 197 * OSQLResultItem
202 */ 198 */
203int OSQLiteDriver::handleCallBack( int, char**, char** ) { 199int OSQLiteDriver::handleCallBack( int, char**, char** ) {
204 return 0; 200 return 0;
205} 201}
206 202
207 203
208/* callback_handler add the values to the list*/ 204/* callback_handler add the values to the list*/
209int OSQLiteDriver::call_back( void* voi, int argc, 205int OSQLiteDriver::call_back( void* voi, int argc,
210 char** argv, char** columns) { 206 char** argv, char** columns) {
211 Query* qu = (Query*)voi; 207 Query* qu = (Query*)voi;
212 208
213 //copy them over to a OSQLResultItem 209 //copy them over to a OSQLResultItem
214 QMap<QString, QString> tableString; 210 QMap<QString, QString> tableString;
215 QMap<int, QString> tableInt; 211 QMap<int, QString> tableInt;
216 for (int i = 0; i < argc; i++ ) { 212 for (int i = 0; i < argc; i++ ) {
217 213
218#ifdef __BUGGY_LOCAL8BIT_ 214 tableInt.insert( i, QString::fromUtf8( argv[i] ) );
219 tableInt.insert( i, QString::fromLatin1( argv[i] ) ); 215 tableString.insert( QString::fromUtf8( columns[i] ),
220 tableString.insert( QString::fromLatin1( columns[i] ), 216 QString::fromUtf8( argv[i] ) );
221 QString::fromLatin1( argv[i] ) );
222#else
223 tableInt.insert( i, QString::fromLocal8Bit( argv[i] ) );
224 tableString.insert( QString::fromLocal8Bit( columns[i] ),
225 QString::fromLocal8Bit( argv[i] ) );
226#endif
227 } 217 }
228 OSQLResultItem item( tableString, tableInt ); 218 OSQLResultItem item( tableString, tableInt );
229 qu->items.append( item ); 219 qu->items.append( item );
230 220
231 return ((Query*)voi)->driver->handleCallBack( argc, 221 return ((Query*)voi)->driver->handleCallBack( argc,
232 argv, 222 argv,
233 columns ); 223 columns );
234 224
235 225
236} 226}
237 227
238}}} // namespace OPIE::DB::Internal 228}}} // namespace OPIE::DB::Internal