sqlite.patch 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. Index: Makefile.in
  2. --- Makefile.in.orig 2012-05-14 05:12:27.000000000 +0200
  3. +++ Makefile.in 2012-05-14 20:54:52.000000000 +0200
  4. @@ -188,6 +188,21 @@
  5. #
  6. LIBOBJ = $(LIBOBJS$(USE_AMALGAMATION))
  7. +# FTS3 support
  8. +ifdef FTS3
  9. +TCC += -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS -I$(TOP)/ext/fts3
  10. +endif
  11. +
  12. +# RTREE support
  13. +ifdef RTREE
  14. +TCC += -DSQLITE_ENABLE_RTREE -I$(TOP)/ext/rtree
  15. +endif
  16. +
  17. +# REGEXP support
  18. +ifdef REGEXP
  19. +TCC += -DSQLITE_ENABLE_REGEXP
  20. +LIBOBJ += regexp.lo
  21. +endif
  22. # All of the source code files.
  23. #
  24. @@ -324,6 +339,8 @@
  25. SRC += \
  26. $(TOP)/ext/rtree/rtree.h \
  27. $(TOP)/ext/rtree/rtree.c
  28. +SRC += \
  29. + $(TOP)/ext/regexp/regexp.c
  30. # Generated source code files
  31. @@ -862,6 +879,8 @@
  32. rtree.lo: $(TOP)/ext/rtree/rtree.c $(HDR) $(EXTHDR)
  33. $(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/rtree/rtree.c
  34. +regexp.lo: $(TOP)/ext/regexp/regexp.c $(HDR) $(EXTHDR)
  35. + $(LTCOMPILE) -DSQLITE_CORE -c $(TOP)/ext/regexp/regexp.c
  36. # Rules to build the 'testfixture' application.
  37. #
  38. Index: configure
  39. --- configure.orig 2012-05-14 05:12:27.000000000 +0200
  40. +++ configure 2012-05-14 20:54:38.000000000 +0200
  41. @@ -6008,11 +6008,7 @@
  42. if $ac_preproc_ok; then
  43. :
  44. else
  45. - { { $as_echo "$as_me:$LINENO: error: C preprocessor \"$CPP\" fails sanity check
  46. -See \`config.log' for more details." >&5
  47. -$as_echo "$as_me: error: C preprocessor \"$CPP\" fails sanity check
  48. -See \`config.log' for more details." >&2;}
  49. - { (exit 1); exit 1; }; }
  50. + :
  51. fi
  52. ac_ext=c
  53. Index: ext/regexp/regexp.c
  54. --- ext/regexp/regexp.c.orig 2012-05-14 20:54:38.000000000 +0200
  55. +++ ext/regexp/regexp.c 2012-05-14 20:54:38.000000000 +0200
  56. @@ -0,0 +1,147 @@
  57. +/*
  58. + * SQLite REGEXP(regex, string) function
  59. + * Copyright (c) 2010 by Ralf S. Engelschall <rse@engelschall.com>
  60. + *
  61. + * Based on Public Domain code from 2007 by Alexey Tourbin
  62. + * <at@altlinux.org> which added plain PCRE based REGEXP function to
  63. + * SQLite. In order to not require the SQLite _library_ to require the
  64. + * external PCRE library, the code was adapted to the regex(3) API as
  65. + * standardized by IEEE Std 1003.2 ("POSIX.2"), sections 2.8 (Regular
  66. + * Expression Notation) and B.5 (C Binding for Regular Expression
  67. + * Matching) and which is usually provided already by any modern Unix
  68. + * system in libc.
  69. + */
  70. +
  71. +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_REGEXP)
  72. +
  73. +/* standard includes */
  74. +#include <assert.h>
  75. +#include <stdlib.h>
  76. +#include <string.h>
  77. +#include <regex.h>
  78. +
  79. +/* SQLite includes */
  80. +#ifndef SQLITE_CORE
  81. +#include "sqlite3ext.h"
  82. +SQLITE_EXTENSION_INIT1
  83. +#else
  84. +#include "sqlite3.h"
  85. +#endif
  86. +
  87. +/* regex cache configuration */
  88. +#ifndef CACHE_SIZE
  89. +#define CACHE_SIZE 32
  90. +#endif
  91. +typedef struct {
  92. + char *str;
  93. + regex_t re;
  94. +} cache_entry;
  95. +
  96. +/* the SQL REGEXP(regex, string) function C implementation */
  97. +static void regexp(sqlite3_context *ctx, int argc, sqlite3_value **argv)
  98. +{
  99. + const char *re_str, *str;
  100. + regex_t *re;
  101. + int i;
  102. + int rc;
  103. + int found;
  104. + cache_entry *cache;
  105. + cache_entry c;
  106. + char err[1024];
  107. +
  108. + /* sanity check arguments */
  109. + if (argc != 2) {
  110. + sqlite3_result_error(ctx, "invalid number of arguments to REGEXP function", -1);
  111. + return;
  112. + }
  113. + if ((re_str = (const char *)sqlite3_value_text(argv[0])) == NULL) {
  114. + sqlite3_result_error(ctx, "no regexp argument given to REGEXP function", -1);
  115. + return;
  116. + }
  117. + if ((str = (const char *)sqlite3_value_text(argv[1])) == NULL) {
  118. + sqlite3_result_error(ctx, "no string argument given to REGEXP function", -1);
  119. + return;
  120. + }
  121. +
  122. + /* simple regex LRU caching */
  123. + if ((cache = sqlite3_user_data(ctx)) == NULL) {
  124. + sqlite3_result_error(ctx, "no regex cache available", -1);
  125. + return;
  126. + }
  127. + found = 0;
  128. + for (i = 0; i < CACHE_SIZE && cache[i].str != NULL; i++) {
  129. + if (strcmp(re_str, cache[i].str) == 0) {
  130. + found = 1;
  131. + break;
  132. + }
  133. + }
  134. + if (found) {
  135. + if (i > 0) {
  136. + /* move cache[i] element to front for faster searching next time */
  137. + c = cache[i];
  138. + memmove(cache + 1, cache, i * sizeof(cache_entry));
  139. + cache[0] = c;
  140. + }
  141. + }
  142. + else {
  143. + /* compile and store regular expression */
  144. + int rc = regcomp(&(c.re), re_str, REG_EXTENDED|REG_NOSUB);
  145. + if (rc != 0) {
  146. + regerror(rc, &(c.re), err, sizeof(err));
  147. + char *e2 = sqlite3_mprintf("regex \"%s\" failed to compile: %s", re_str, err);
  148. + sqlite3_result_error(ctx, e2, -1);
  149. + sqlite3_free(e2);
  150. + return;
  151. + }
  152. + if ((c.str = strdup(re_str)) == NULL) {
  153. + sqlite3_result_error(ctx, "strdup: ENOMEM", -1);
  154. + regfree(&(c.re));
  155. + return;
  156. + }
  157. +
  158. + /* insert regex into cache (at first position) */
  159. + i = CACHE_SIZE - 1;
  160. + if (cache[i].str != NULL) {
  161. + /* expire oldest cache entry */
  162. + free(cache[i].str);
  163. + regfree(&(cache[i].re));
  164. + }
  165. + memmove(cache + 1, cache, i * sizeof(cache_entry));
  166. + cache[0] = c;
  167. + }
  168. +
  169. + /* take compiled regular expression (either found old one or created new one) */
  170. + re = &(cache[0].re);
  171. +
  172. + /* apply regular expression onto given string and report matching result */
  173. + rc = regexec(re, str, 0, NULL, 0);
  174. + sqlite3_result_int(ctx, rc == 0);
  175. +
  176. + return;
  177. +}
  178. +
  179. +/* SQLITE_CORE (built-in) entry point */
  180. +int sqlite3RegexpInit(sqlite3 *);
  181. +int sqlite3RegexpInit(sqlite3 *db)
  182. +{
  183. + cache_entry *cache = NULL;
  184. + int rc = SQLITE_OK;
  185. +
  186. + if ((cache = calloc(CACHE_SIZE, sizeof(cache_entry))) == NULL)
  187. + return SQLITE_NOMEM;
  188. + rc = sqlite3_create_function(db, "REGEXP", 2, SQLITE_UTF8, cache, regexp, NULL, NULL);
  189. + return rc;
  190. +}
  191. +
  192. +#ifndef SQLITE_CORE
  193. +/* DSO entry point */
  194. +int sqlite3_extension_init(sqlite3 *, char **, const sqlite3_api_routines *);
  195. +int sqlite3_extension_init(sqlite3 *db, char **err, const sqlite3_api_routines *api)
  196. +{
  197. + SQLITE_EXTENSION_INIT2(api)
  198. + return sqlite3RegexpInit(db);
  199. +}
  200. +#endif
  201. +
  202. +#endif
  203. +
  204. Index: sqlite3.pc.in
  205. --- sqlite3.pc.in.orig 2012-05-14 05:12:27.000000000 +0200
  206. +++ sqlite3.pc.in 2012-05-14 20:54:38.000000000 +0200
  207. @@ -8,6 +8,5 @@
  208. Name: SQLite
  209. Description: SQL database engine
  210. Version: @RELEASE@
  211. -Libs: -L${libdir} -lsqlite3
  212. -Libs.private: @LIBS@
  213. +Libs: -L${libdir} -lsqlite3 @LIBS@
  214. Cflags: -I${includedir}
  215. Index: src/main.c
  216. --- src/main.c.orig 2012-05-14 05:12:27.000000000 +0200
  217. +++ src/main.c 2012-05-14 20:54:38.000000000 +0200
  218. @@ -2298,6 +2298,13 @@
  219. }
  220. #endif
  221. +#ifdef SQLITE_ENABLE_REGEXP
  222. + if( !db->mallocFailed && rc==SQLITE_OK){
  223. + extern int sqlite3RegexpInit(sqlite3*);
  224. + rc = sqlite3RegexpInit(db);
  225. + }
  226. +#endif
  227. +
  228. sqlite3Error(db, rc, 0);
  229. /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
  230. Index: src/mem1.c
  231. --- src/mem1.c.orig 2012-05-14 05:12:27.000000000 +0200
  232. +++ src/mem1.c 2012-05-14 20:54:38.000000000 +0200
  233. @@ -87,7 +87,7 @@
  234. #if (defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE)) \
  235. || (defined(HAVE_MALLOC_H) && defined(HAVE_MALLOC_USABLE_SIZE))
  236. -# include <malloc.h> /* Needed for malloc_usable_size on linux */
  237. +# include <stdlib.h> /* Needed for malloc_usable_size on linux */
  238. #endif
  239. #ifdef HAVE_MALLOC_USABLE_SIZE
  240. # ifndef SQLITE_MALLOCSIZE