summaryrefslogtreecommitdiff
authorbrad <brad>2004-04-06 11:58:24 (UTC)
committer brad <brad>2004-04-06 11:58:24 (UTC)
commit74c9cbe70b2a2530aad5e5a3cc8e0c946c560e95 (patch) (unidiff)
treeaf55d362e359414b2b18cef0c979d7e71e6a7d96
parent52b7f68dd9435286a2d0b53fc8649b4323b8af38 (diff)
downloadopie-74c9cbe70b2a2530aad5e5a3cc8e0c946c560e95.zip
opie-74c9cbe70b2a2530aad5e5a3cc8e0c946c560e95.tar.gz
opie-74c9cbe70b2a2530aad5e5a3cc8e0c946c560e95.tar.bz2
Fix memleak
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opiedb/osqlitedriver.cpp9
-rw-r--r--libopie2/opiedb/osqlitedriver.h2
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
@@ -75,145 +75,144 @@ void OSQLiteDriver::setUserName( const QString& ) {}
75void OSQLiteDriver::setPassword( const QString& ) {} 75void OSQLiteDriver::setPassword( const QString& ) {}
76 76
77 77
78void OSQLiteDriver::setUrl( const QString& url ) { 78void OSQLiteDriver::setUrl( const QString& url ) {
79 m_url = url; 79 m_url = url;
80} 80}
81 81
82 82
83void OSQLiteDriver::setOptions( const QStringList& ) { 83void 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 */
89int sqliteRlikeCompare(const char *zPattern, const char *zString, sqregex *reg){ 89int 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(&reg->regex_c); 95 regfree(&reg->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(&reg->regex_c, zPattern, REG_EXTENDED); 99 res = regcomp(&reg->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(&reg->regex_c, zString, 0, NULL, 0)==0); 107 res = (regexec(&reg->regex_c, zString, 0, NULL, 0)==0);
108 return res; 108 return res;
109} 109}
110 110
111void rlikeFunc(sqlite_func *context, int arg, const char **argv){ 111void 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 */
125bool OSQLiteDriver::open() { 125bool 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 */
149bool OSQLiteDriver::close() { 148bool 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 */
162OSQLResult OSQLiteDriver::query( OSQLQuery* qu) { 161OSQLResult 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
185OSQLTable::ValueList OSQLiteDriver::tables() const { 184OSQLTable::ValueList OSQLiteDriver::tables() const {
186 185
187} 186}
188 187
189 188
190OSQLError OSQLiteDriver::lastError() { 189OSQLError 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 */
199int OSQLiteDriver::handleCallBack( int, char**, char** ) { 198int 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*/
205int OSQLiteDriver::call_back( void* voi, int argc, 204int 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] ) );
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
11namespace Opie { 11namespace Opie {
12namespace DB { 12namespace DB {
13namespace Internal { 13namespace Internal {
14 14
15struct sqregex { 15struct sqregex {
16 char *regex_raw; 16 char *regex_raw;
17 regex_t regex_c; 17 regex_t regex_c;
18}; 18};
19 19
20class OSQLiteDriver : public OSQLDriver { 20class OSQLiteDriver : public OSQLDriver {
21 Q_OBJECT 21 Q_OBJECT
22public: 22public:
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
36private: 36private:
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