-rw-r--r-- | libopie2/opiedb/osqlitedriver.cpp | 46 | ||||
-rw-r--r-- | libopie2/opiedb/osqlitedriver.h | 8 |
2 files changed, 33 insertions, 21 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 | |||
@@ -41,9 +41,6 @@ | |||
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 | ||
@@ -89,32 +86,36 @@ void OSQLiteDriver::setOptions( const QStringList& ) { | |||
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); |
98 | } | 95 | regfree(®->regex_c); |
99 | regex_raw = (char *)malloc(strlen(zPattern)+1); | 96 | } |
100 | strncpy(regex_raw, zPattern, strlen(zPattern)+1); | 97 | reg->regex_raw = (char *)malloc(strlen(zPattern)+1); |
101 | res = regcomp(®ex_c, zPattern, REG_EXTENDED); | 98 | strncpy(reg->regex_raw, zPattern, strlen(zPattern)+1); |
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 | /* |
@@ -135,7 +136,8 @@ bool OSQLiteDriver::open() { | |||
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 | ||
@@ -147,9 +149,11 @@ bool OSQLiteDriver::open() { | |||
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 | ||
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 | |||
@@ -2,6 +2,7 @@ | |||
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" |
@@ -11,6 +12,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: |
@@ -26,6 +32,7 @@ public: | |||
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; |
@@ -34,6 +41,7 @@ private: | |||
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 | } |