summaryrefslogtreecommitdiff
path: root/libopie2
Unidiff
Diffstat (limited to 'libopie2') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opiedb/osqlitedriver.cpp30
1 files changed, 30 insertions, 0 deletions
diff --git a/libopie2/opiedb/osqlitedriver.cpp b/libopie2/opiedb/osqlitedriver.cpp
index 2c53248..eea1093 100644
--- a/libopie2/opiedb/osqlitedriver.cpp
+++ b/libopie2/opiedb/osqlitedriver.cpp
@@ -1,191 +1,221 @@
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 <regex.h>
38#include <stdio.h>
37 39
38// fromLocal8Bit() does not work as expected. Thus it 40// fromLocal8Bit() does not work as expected. Thus it
39// is replaced by fromLatin1() (eilers) 41// is replaced by fromLatin1() (eilers)
40#define __BUGGY_LOCAL8BIT_ 42#define __BUGGY_LOCAL8BIT_
41 43
42 44
43using namespace Opie::DB; 45using namespace Opie::DB;
44using namespace Opie::DB::Internal; 46using namespace Opie::DB::Internal;
45 47
46namespace { 48namespace {
47 struct Query { 49 struct Query {
48 OSQLError::ValueList errors; 50 OSQLError::ValueList errors;
49 OSQLResultItem::ValueList items; 51 OSQLResultItem::ValueList items;
50 OSQLiteDriver *driver; 52 OSQLiteDriver *driver;
51 }; 53 };
52} 54}
53 55
54 56
55OSQLiteDriver::OSQLiteDriver( QLibrary *lib ) 57OSQLiteDriver::OSQLiteDriver( QLibrary *lib )
56 : OSQLDriver( lib ) 58 : OSQLDriver( lib )
57{ 59{
58 m_sqlite = 0l; 60 m_sqlite = 0l;
59} 61}
60 62
61 63
62OSQLiteDriver::~OSQLiteDriver() { 64OSQLiteDriver::~OSQLiteDriver() {
63 close(); 65 close();
64} 66}
65 67
66 68
67QString OSQLiteDriver::id()const { 69QString OSQLiteDriver::id()const {
68 return QString::fromLatin1("SQLite"); 70 return QString::fromLatin1("SQLite");
69} 71}
70 72
71void OSQLiteDriver::setUserName( const QString& ) {} 73void OSQLiteDriver::setUserName( const QString& ) {}
72 74
73 75
74void OSQLiteDriver::setPassword( const QString& ) {} 76void OSQLiteDriver::setPassword( const QString& ) {}
75 77
76 78
77void OSQLiteDriver::setUrl( const QString& url ) { 79void OSQLiteDriver::setUrl( const QString& url ) {
78 m_url = url; 80 m_url = url;
79} 81}
80 82
81 83
82void OSQLiteDriver::setOptions( const QStringList& ) { 84void OSQLiteDriver::setOptions( const QStringList& ) {
83} 85}
84 86
87/*------------------------------------------------------------------/*
88 * Functions to patch a regex search into sqlite
89 * -----------------------------------------------------------------*/
90int sqliteRlikeCompare(const char *zPattern, const char *zString){
91 regex_t regex;
92 int res;
93 if ( zPattern==NULL || zString==NULL ) {
94 printf("One of the args was null!\n");
95 return 0;
96 }
97 res = regcomp(&regex, zPattern, REG_EXTENDED);
98 if ( res != 0 ) {
99 printf("Regcomp failed with code %u on string %s\n",res,zPattern);
100
101 return 0;
102 }
103 res = (regexec(&regex, zString, 0, NULL, 0)==0);
104 regfree(&regex);
105 return res;
106}
107
108void rlikeFunc(sqlite_func *context, int arg, const char **argv){
109 if( argv[0]==0 || argv[1]==0 ) return;
110 sqlite_set_result_int(context,
111 sqliteRlikeCompare((const char*)argv[0],
112 (const char*)argv[1]));
113}
85 114
86/* 115/*
87 * try to open a db specified via setUrl 116 * try to open a db specified via setUrl
88 * and options 117 * and options
89 */ 118 */
90bool OSQLiteDriver::open() { 119bool OSQLiteDriver::open() {
91 char *error; 120 char *error;
92 qDebug("OSQLiteDriver::open: about to open"); 121 qDebug("OSQLiteDriver::open: about to open");
93 m_sqlite = sqlite_open(m_url.local8Bit(), 122 m_sqlite = sqlite_open(m_url.local8Bit(),
94 0, 123 0,
95 &error ); 124 &error );
96 125
97 /* failed to open */ 126 /* failed to open */
98 if (m_sqlite == 0l ) { 127 if (m_sqlite == 0l ) {
99 // FIXME set the last error 128 // FIXME set the last error
100 qWarning("OSQLiteDriver::open: %s", error ); 129 qWarning("OSQLiteDriver::open: %s", error );
101 free( error ); 130 free( error );
102 return false; 131 return false;
103 } 132 }
133 sqlite_create_function(m_sqlite,"rlike",2,rlikeFunc,NULL);
104 return true; 134 return true;
105} 135}
106 136
107 137
108/* close the db 138/* close the db
109 * sqlite closes them without 139 * sqlite closes them without
110 * telling failure or success 140 * telling failure or success
111 */ 141 */
112bool OSQLiteDriver::close() { 142bool OSQLiteDriver::close() {
113 if (m_sqlite ) 143 if (m_sqlite )
114 sqlite_close( m_sqlite ), m_sqlite=0l; 144 sqlite_close( m_sqlite ), m_sqlite=0l;
115 145
116 return true; 146 return true;
117} 147}
118 148
119 149
120/* Query */ 150/* Query */
121OSQLResult OSQLiteDriver::query( OSQLQuery* qu) { 151OSQLResult OSQLiteDriver::query( OSQLQuery* qu) {
122 if ( !m_sqlite ) { 152 if ( !m_sqlite ) {
123 // FIXME set error code 153 // FIXME set error code
124 OSQLResult result( OSQLResult::Failure ); 154 OSQLResult result( OSQLResult::Failure );
125 return result; 155 return result;
126 } 156 }
127 Query query; 157 Query query;
128 query.driver = this; 158 query.driver = this;
129 char *err; 159 char *err;
130 /* SQLITE_OK 0 if return code > 0 == failure */ 160 /* SQLITE_OK 0 if return code > 0 == failure */
131 if ( sqlite_exec(m_sqlite, qu->query(),&call_back, &query, &err) > 0 ) { 161 if ( sqlite_exec(m_sqlite, qu->query(),&call_back, &query, &err) > 0 ) {
132 qWarning("OSQLiteDriver::query: Error while executing"); 162 qWarning("OSQLiteDriver::query: Error while executing");
133 free(err ); 163 free(err );
134 // FixMe Errors 164 // FixMe Errors
135 } 165 }
136 166
137 OSQLResult result(OSQLResult::Success, 167 OSQLResult result(OSQLResult::Success,
138 query.items, 168 query.items,
139 query.errors ); 169 query.errors );
140 return result; 170 return result;
141} 171}
142 172
143 173
144OSQLTable::ValueList OSQLiteDriver::tables() const { 174OSQLTable::ValueList OSQLiteDriver::tables() const {
145 175
146} 176}
147 177
148 178
149OSQLError OSQLiteDriver::lastError() { 179OSQLError OSQLiteDriver::lastError() {
150 OSQLError error; 180 OSQLError error;
151 return error; 181 return error;
152}; 182};
153 183
154 184
155/* handle a callback add the row to the global 185/* handle a callback add the row to the global
156 * OSQLResultItem 186 * OSQLResultItem
157 */ 187 */
158int OSQLiteDriver::handleCallBack( int, char**, char** ) { 188int OSQLiteDriver::handleCallBack( int, char**, char** ) {
159 return 0; 189 return 0;
160} 190}
161 191
162 192
163/* callback_handler add the values to the list*/ 193/* callback_handler add the values to the list*/
164int OSQLiteDriver::call_back( void* voi, int argc, 194int OSQLiteDriver::call_back( void* voi, int argc,
165 char** argv, char** columns) { 195 char** argv, char** columns) {
166 Query* qu = (Query*)voi; 196 Query* qu = (Query*)voi;
167 197
168 //copy them over to a OSQLResultItem 198 //copy them over to a OSQLResultItem
169 QMap<QString, QString> tableString; 199 QMap<QString, QString> tableString;
170 QMap<int, QString> tableInt; 200 QMap<int, QString> tableInt;
171 for (int i = 0; i < argc; i++ ) { 201 for (int i = 0; i < argc; i++ ) {
172 202
173#ifdef __BUGGY_LOCAL8BIT_ 203#ifdef __BUGGY_LOCAL8BIT_
174 tableInt.insert( i, QString::fromLatin1( argv[i] ) ); 204 tableInt.insert( i, QString::fromLatin1( argv[i] ) );
175 tableString.insert( QString::fromLatin1( columns[i] ), 205 tableString.insert( QString::fromLatin1( columns[i] ),
176 QString::fromLatin1( argv[i] ) ); 206 QString::fromLatin1( argv[i] ) );
177#else 207#else
178 tableInt.insert( i, QString::fromLocal8Bit( argv[i] ) ); 208 tableInt.insert( i, QString::fromLocal8Bit( argv[i] ) );
179 tableString.insert( QString::fromLocal8Bit( columns[i] ), 209 tableString.insert( QString::fromLocal8Bit( columns[i] ),
180 QString::fromLocal8Bit( argv[i] ) ); 210 QString::fromLocal8Bit( argv[i] ) );
181#endif 211#endif
182 } 212 }
183 OSQLResultItem item( tableString, tableInt ); 213 OSQLResultItem item( tableString, tableInt );
184 qu->items.append( item ); 214 qu->items.append( item );
185 215
186 return ((Query*)voi)->driver->handleCallBack( argc, 216 return ((Query*)voi)->driver->handleCallBack( argc,
187 argv, 217 argv,
188 columns ); 218 columns );
189 219
190 220
191} 221}