Merge lp:~stefanor/ibid/regexp-595423 into lp:~ibid-core/ibid/old-release-0.1-1.6

Proposed by Stefano Rivera
Status: Merged
Approved by: Stefano Rivera
Approved revision: 909
Merged at revision: 974
Proposed branch: lp:~stefanor/ibid/regexp-595423
Merge into: lp:~ibid-core/ibid/old-release-0.1-1.6
Diff against target: 60 lines (+14/-5)
2 files modified
ibid/db/__init__.py (+8/-1)
ibid/plugins/factoid.py (+6/-4)
To merge this branch: bzr merge lp:~stefanor/ibid/regexp-595423
Reviewer Review Type Date Requested Status
Jonathan Hitchcock Approve
marcog (community) Approve
Max Rabkin Approve
Review via email: mp+47244@code.launchpad.net

Commit message

Add function get_regexp_op to ibid.db that returns a REGEXP op that works on Postgres too.

Description of the change

I wasn't entirely sure where that helper function should go. It's UGLY. I'll see if I can get an upstream SQLAlchemy fix for this (long term)...

To post a comment you must log in.
Revision history for this message
Max Rabkin (max-rabkin) :
review: Approve
Revision history for this message
marcog (marco-gallotta) :
review: Approve
Revision history for this message
Jonathan Hitchcock (vhata) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ibid/db/__init__.py'
2--- ibid/db/__init__.py 2010-02-05 15:58:06 +0000
3+++ ibid/db/__init__.py 2011-01-24 11:43:14 +0000
4@@ -1,4 +1,4 @@
5-# Copyright (c) 2009-2010, Stefano Rivera
6+# Copyright (c) 2009-2011, Stefano Rivera
7 # Released under terms of the MIT/X/Expat Licence. See COPYING for details.
8 import warnings as _warnings
9
10@@ -22,4 +22,11 @@
11 # We use SQLAlchemy 0.4 compatible .save_or_update() functions
12 _warnings.filterwarnings('ignore', 'Use session.add\(\)', SADeprecationWarning)
13
14+def get_regexp_op(session):
15+ "Return a regexp operator"
16+ if session.bind.engine.name in ('postgres', 'postgresql'):
17+ return lambda x, y: x.op('~')(y)
18+ else:
19+ return lambda x, y: x.op('REGEXP')(y)
20+
21 # vi: set et sta sw=4 ts=4:
22
23=== modified file 'ibid/plugins/factoid.py'
24--- ibid/plugins/factoid.py 2011-01-12 14:16:31 +0000
25+++ ibid/plugins/factoid.py 2011-01-24 11:43:14 +0000
26@@ -1,4 +1,4 @@
27-# Copyright (c) 2009-2010, Michael Gorven, Stefano Rivera
28+# Copyright (c) 2009-2011, Michael Gorven, Stefano Rivera
29 # Released under terms of the MIT/X/Expat Licence. See COPYING for details.
30
31 from datetime import datetime
32@@ -14,7 +14,8 @@
33 from ibid.db import IbidUnicode, IbidUnicodeText, Boolean, Integer, DateTime, \
34 Table, Column, ForeignKey, PassiveDefault, \
35 relation, synonym, func, or_, \
36- Base, VersionedSchema
37+ Base, VersionedSchema, \
38+ get_regexp_op
39 from ibid.plugins.identity import get_identities
40 from ibid.utils import format_date
41
42@@ -238,7 +239,8 @@
43
44 if pattern:
45 if is_regex:
46- query = query.filter(FactoidValue.value.op('REGEXP')(pattern))
47+ op = get_regexp_op(session)
48+ query = query.filter(op(FactoidValue.value, pattern))
49 else:
50 pattern = '%%%s%%' % escape_like_re.sub(r'\\\1', pattern)
51 # http://www.sqlalchemy.org/trac/ticket/1400:
52@@ -418,7 +420,7 @@
53 filter_on = (FactoidName.name, FactoidValue.value)
54
55 if is_regex:
56- filter_op = lambda x, y: x.op('REGEXP')(y)
57+ filter_op = get_regexp_op(event.session)
58 else:
59 pattern = "%%%s%%" % escape_like_re.sub(r'\\\1', pattern)
60 filter_op = lambda x, y: x.like(y)

Subscribers

People subscribed via source and target branches