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