From 0eb3a5026ed4fa91659613036369efe0ff6a1658 Mon Sep 17 00:00:00 2001 From: Konsta Vesterinen Date: Fri, 4 Oct 2013 10:21:04 +0300 Subject: [PATCH] Added catalog parameter support for match_tsquery operator --- CHANGES.rst | 7 +++++++ setup.py | 2 +- sqlalchemy_utils/__init__.py | 2 +- sqlalchemy_utils/types/ts_vector.py | 16 +++++++++++++--- tests/types/test_tsvector.py | 13 +++++++++++-- 5 files changed, 33 insertions(+), 7 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 12ccb63..dc6d090 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,6 +4,13 @@ Changelog Here you can see the full list of changes between each SQLAlchemy-Utils release. +0.16.24 (2013-10-04) +^^^^^^^^^^^^^^^^^^^^ + +- Renamed match operator of TSVectorType to match_tsquery in order to avoid confusion with existing match operator +- Added catalog parameter support for match_tsquery operator + + 0.16.23 (2013-10-04) ^^^^^^^^^^^^^^^^^^^^ diff --git a/setup.py b/setup.py index 59c9780..c2549a0 100644 --- a/setup.py +++ b/setup.py @@ -56,7 +56,7 @@ for name, requirements in extras_require.items(): setup( name='SQLAlchemy-Utils', - version='0.16.23', + version='0.16.24', url='https://github.com/kvesteri/sqlalchemy-utils', license='BSD', author='Konsta Vesterinen, Ryan Leckey, Janne Vanhala, Vesa Uimonen', diff --git a/sqlalchemy_utils/__init__.py b/sqlalchemy_utils/__init__.py index 0286a07..255218b 100644 --- a/sqlalchemy_utils/__init__.py +++ b/sqlalchemy_utils/__init__.py @@ -38,7 +38,7 @@ from .types import ( ) -__version__ = '0.16.23' +__version__ = '0.16.24' __all__ = ( diff --git a/sqlalchemy_utils/types/ts_vector.py b/sqlalchemy_utils/types/ts_vector.py index 5a3befd..8734910 100644 --- a/sqlalchemy_utils/types/ts_vector.py +++ b/sqlalchemy_utils/types/ts_vector.py @@ -4,10 +4,20 @@ from sqlalchemy.dialects.postgresql.base import ischema_names class TSVectorType(sa.types.UserDefinedType): class comparator_factory(sa.types.TypeEngine.Comparator): - def match(self, other): - from sqlalchemy_utils.expressions import tsvector_match + def match_tsquery(self, other, catalog=None): + from sqlalchemy_utils.expressions import tsvector_match, to_tsquery - return tsvector_match(self.expr, other) + args = [] + if catalog: + args.append(catalog) + elif self.type.options.get('catalog'): + args.append(self.type.options.get('catalog')) + args.append(other) + + return tsvector_match( + self.expr, + to_tsquery(*args) + ) def __init__(self, *args, **kwargs): """ diff --git a/tests/types/test_tsvector.py b/tests/types/test_tsvector.py index 1efb6c1..58b22e6 100644 --- a/tests/types/test_tsvector.py +++ b/tests/types/test_tsvector.py @@ -38,6 +38,15 @@ class TestTSVector(TestCase): assert type_.options['catalog'] == 'pg_catalog.simple' def test_match(self): - assert six.text_type(self.User.search_index.match(u'something')) == ( - '("user".search_index) @@ :tsvector_match_1' + expr = self.User.search_index.match_tsquery(u'something') + assert six.text_type(expr) == ( + '("user".search_index) @@ to_tsquery(:to_tsquery_1)' + ) + + def test_match_with_catalog(self): + expr = self.User.search_index.match_tsquery( + u'something', catalog='pg_catalog.simple' + ) + assert six.text_type(expr) == ( + '("user".search_index) @@ to_tsquery(:to_tsquery_1, :to_tsquery_2)' )