Skip to content

Commit d5a86d8

Browse files
committed
Merge branch 'tsvector' of https://bitbucket.org/nibrahim/sqlalchemy/branch/tsvector into tsvector
2 parents 111c615 + d3b65cd commit d5a86d8

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

doc/build/dialects/postgresql.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ they originate from :mod:`sqlalchemy.types` or from the local dialect::
1717
DOUBLE_PRECISION, ENUM, FLOAT, HSTORE, INET, INTEGER, \
1818
INTERVAL, MACADDR, NUMERIC, REAL, SMALLINT, TEXT, TIME, \
1919
TIMESTAMP, UUID, VARCHAR, INT4RANGE, INT8RANGE, NUMRANGE, \
20-
DATERANGE, TSRANGE, TSTZRANGE
20+
DATERANGE, TSRANGE, TSTZRANGE, TSVECTOR
2121

2222
Types which are specific to PostgreSQL, or have PostgreSQL-specific
2323
construction arguments, are as follows:
@@ -77,6 +77,8 @@ construction arguments, are as follows:
7777
.. autoclass:: REAL
7878
:members: __init__
7979

80+
.. autoclass:: TSVECTOR
81+
:members: __init__
8082

8183
.. autoclass:: UUID
8284
:members: __init__

lib/sqlalchemy/dialects/postgresql/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
from .base import \
1212
INTEGER, BIGINT, SMALLINT, VARCHAR, CHAR, TEXT, NUMERIC, FLOAT, REAL, \
1313
INET, CIDR, UUID, BIT, MACADDR, DOUBLE_PRECISION, TIMESTAMP, TIME, \
14-
DATE, BYTEA, BOOLEAN, INTERVAL, ARRAY, ENUM, dialect, array, Any, All
14+
DATE, BYTEA, BOOLEAN, INTERVAL, ARRAY, ENUM, dialect, array, Any, All, \
15+
TSVECTOR
1516
from .constraints import ExcludeConstraint
1617
from .hstore import HSTORE, hstore
1718
from .ranges import INT4RANGE, INT8RANGE, NUMRANGE, DATERANGE, TSRANGE, \
@@ -23,5 +24,5 @@
2324
'DOUBLE_PRECISION', 'TIMESTAMP', 'TIME', 'DATE', 'BYTEA', 'BOOLEAN',
2425
'INTERVAL', 'ARRAY', 'ENUM', 'dialect', 'Any', 'All', 'array', 'HSTORE',
2526
'hstore', 'INT4RANGE', 'INT8RANGE', 'NUMRANGE', 'DATERANGE',
26-
'TSRANGE', 'TSTZRANGE'
27+
'TSRANGE', 'TSTZRANGE', 'TSVECTOR'
2728
)

lib/sqlalchemy/dialects/postgresql/base.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,35 @@ def process(value):
368368

369369
PGUuid = UUID
370370

371+
class TSVECTOR(sqltypes.TypeEngine):
372+
"""The TSVECTOR type implements the Postgresql text search type
373+
TSVECTOR.
374+
375+
It can be used to do full text queries on natural language
376+
*documents*.
377+
378+
Search queries are performed using the ``@@`` operator in
379+
postgresql. This is made available with the ``match`` method
380+
available on the column.
381+
382+
This means that if you have a table ``Example`` with a column
383+
``text`` of type ``TSVECTOR``, you can create a search clause like
384+
so
385+
386+
::
387+
388+
Example.text.match("search string")
389+
390+
which will be compiled to
391+
392+
::
393+
394+
text @@ to_tsquery('search string')
395+
396+
"""
397+
__visit_name__ = 'TSVECTOR'
398+
399+
371400

372401
class _Slice(expression.ColumnElement):
373402
__visit_name__ = 'slice'
@@ -913,6 +942,7 @@ def _on_metadata_drop(self, target, bind, checkfirst, **kw):
913942
'interval': INTERVAL,
914943
'interval year to month': INTERVAL,
915944
'interval day to second': INTERVAL,
945+
'tsvector' : TSVECTOR
916946
}
917947

918948

@@ -1163,6 +1193,9 @@ def visit_exclude_constraint(self, constraint):
11631193

11641194

11651195
class PGTypeCompiler(compiler.GenericTypeCompiler):
1196+
def visit_TSVECTOR(self, type):
1197+
return "TSVECTOR"
1198+
11661199
def visit_INET(self, type_):
11671200
return "INET"
11681201

test/dialect/postgresql/test_types.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,8 @@ def get_col_spec(self):
836836
Column('plain_interval', postgresql.INTERVAL),
837837
Column('year_interval', y2m()),
838838
Column('month_interval', d2s()),
839-
Column('precision_interval', postgresql.INTERVAL(precision=3))
839+
Column('precision_interval', postgresql.INTERVAL(precision=3)),
840+
Column('tsvector_document', postgresql.TSVECTOR)
840841
)
841842

842843
metadata.create_all()

0 commit comments

Comments
 (0)