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