summaryrefslogtreecommitdiff
authorbrad <brad>2004-04-06 10:38:36 (UTC)
committer brad <brad>2004-04-06 10:38:36 (UTC)
commit4738d39e8168e24cbaae8a7ea3c71f00552d5dac (patch) (unidiff)
tree3404a879fb4275d899af2939bfb54f751e3cacfe
parent8981e35697647315d88cdf95e912b0fe2f9d5375 (diff)
downloadopie-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)
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opiedb/osqlitedriver.cpp44
-rw-r--r--libopie2/opiedb/osqlitedriver.h8
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
@@ -32,27 +32,24 @@
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
47using namespace Opie::DB; 44using namespace Opie::DB;
48using namespace Opie::DB::Internal; 45using namespace Opie::DB::Internal;
49 46
50namespace { 47namespace {
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
@@ -80,85 +77,92 @@ void OSQLiteDriver::setPassword( const QString& ) {}
80 77
81void OSQLiteDriver::setUrl( const QString& url ) { 78void OSQLiteDriver::setUrl( const QString& url ) {
82 m_url = url; 79 m_url = url;
83} 80}
84 81
85 82
86void OSQLiteDriver::setOptions( const QStringList& ) { 83void 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 */
92int sqliteRlikeCompare(const char *zPattern, const char *zString){ 89int 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(&regex_c); 94 free(reg->regex_raw);
95 regfree(&reg->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(&regex_c, zPattern, REG_EXTENDED); 99 res = regcomp(&reg->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(&regex_c, zString, 0, NULL, 0)==0); 107 res = (regexec(&reg->regex_c, zString, 0, NULL, 0)==0);
110 return res; 108 return res;
111} 109}
112 110
113void rlikeFunc(sqlite_func *context, int arg, const char **argv){ 111void 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 */
124bool OSQLiteDriver::open() { 125bool 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 */
147bool OSQLiteDriver::close() { 149bool 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(&regex_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 */
158OSQLResult OSQLiteDriver::query( OSQLQuery* qu) { 162OSQLResult 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;
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
10namespace Opie { 11namespace Opie {
11namespace DB { 12namespace DB {
12namespace Internal { 13namespace Internal {
13 14
15struct sqregex {
16 char *regex_raw;
17 regex_t regex_c;
18};
19
14class OSQLiteDriver : public OSQLDriver { 20class OSQLiteDriver : public OSQLDriver {
15 Q_OBJECT 21 Q_OBJECT
16public: 22public:
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
29private: 36private:
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