From 0b481957a2eebf28b05d9803780d05ad4232aa00 Mon Sep 17 00:00:00 2001 From: brad Date: Mon, 05 Apr 2004 10:58:32 +0000 Subject: Regex support for sqlite (finally) --- diff --git a/libopie2/opiedb/osqlitedriver.cpp b/libopie2/opiedb/osqlitedriver.cpp index 2c53248..eea1093 100644 --- a/libopie2/opiedb/osqlitedriver.cpp +++ b/libopie2/opiedb/osqlitedriver.cpp @@ -34,6 +34,8 @@ #include #include +#include +#include // fromLocal8Bit() does not work as expected. Thus it // is replaced by fromLatin1() (eilers) @@ -82,6 +84,33 @@ void OSQLiteDriver::setUrl( const QString& url ) { void OSQLiteDriver::setOptions( const QStringList& ) { } +/*------------------------------------------------------------------/* + * Functions to patch a regex search into sqlite + * -----------------------------------------------------------------*/ +int sqliteRlikeCompare(const char *zPattern, const char *zString){ + regex_t regex; + int res; + if ( zPattern==NULL || zString==NULL ) { + printf("One of the args was null!\n"); + return 0; + } + res = regcomp(®ex, zPattern, REG_EXTENDED); + if ( res != 0 ) { + printf("Regcomp failed with code %u on string %s\n",res,zPattern); + + return 0; + } + res = (regexec(®ex, zString, 0, NULL, 0)==0); + regfree(®ex); + return res; +} + +void rlikeFunc(sqlite_func *context, int arg, const char **argv){ + if( argv[0]==0 || argv[1]==0 ) return; + sqlite_set_result_int(context, + sqliteRlikeCompare((const char*)argv[0], + (const char*)argv[1])); +} /* * try to open a db specified via setUrl @@ -101,6 +130,7 @@ bool OSQLiteDriver::open() { free( error ); return false; } + sqlite_create_function(m_sqlite,"rlike",2,rlikeFunc,NULL); return true; } -- cgit v0.9.0.2