From 3c2503fc79dfb6e652444a2a47162df13938ad54 Mon Sep 17 00:00:00 2001 From: Nick Hall Date: Tue, 19 Apr 2016 22:04:55 +0100 Subject: [PATCH] Enable REGEXP operator --- gramps/gen/db/base.py | 5 +++++ gramps/plugins/database/dbapi_support/sqlite.py | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/gramps/gen/db/base.py b/gramps/gen/db/base.py index 7240c8e1e..fd0c19dd5 100644 --- a/gramps/gen/db/base.py +++ b/gramps/gen/db/base.py @@ -1318,6 +1318,11 @@ class DbReadBase(object): matched = re.match("^" + value + "$", v, re.MULTILINE) else: matched = False + elif op == "REGEXP": + if value and v: + matched = re.search(value, v, re.MULTILINE) is not None + else: + matched = False else: raise Exception("invalid select operator: '%s'" % op) return True if matched else False diff --git a/gramps/plugins/database/dbapi_support/sqlite.py b/gramps/plugins/database/dbapi_support/sqlite.py index f40f19d42..07edb535a 100644 --- a/gramps/plugins/database/dbapi_support/sqlite.py +++ b/gramps/plugins/database/dbapi_support/sqlite.py @@ -1,6 +1,7 @@ import os import sqlite3 import logging +import re sqlite3.paramstyle = 'qmark' @@ -10,6 +11,7 @@ class Sqlite(object): self.connection = sqlite3.connect(*args, **kwargs) self.cursor = self.connection.cursor() self.queries = {} + self.connection.create_function("regexp", 2, regexp) def execute(self, *args, **kwargs): self.log.debug(args) @@ -42,3 +44,6 @@ class Sqlite(object): def close(self): self.log.debug("closing database...") self.connection.close() + +def regexp(expr, value): + return re.search(expr, value, re.MULTILINE) is not None