Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/sqlalchemy/dialects/mysql/expression.py: 55%
33 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:35 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:35 +0000
1from ... import exc
2from ... import util
3from ...sql import coercions
4from ...sql import elements
5from ...sql import operators
6from ...sql import roles
7from ...sql.base import _generative
8from ...sql.base import Generative
11class match(Generative, elements.BinaryExpression):
12 """Produce a ``MATCH (X, Y) AGAINST ('TEXT')`` clause.
14 E.g.::
16 from sqlalchemy import desc
17 from sqlalchemy.dialects.mysql import match
19 match_expr = match(
20 users_table.c.firstname,
21 users_table.c.lastname,
22 against="Firstname Lastname",
23 )
25 stmt = (
26 select(users_table)
27 .where(match_expr.in_boolean_mode())
28 .order_by(desc(match_expr))
29 )
31 Would produce SQL resembling::
33 SELECT id, firstname, lastname
34 FROM user
35 WHERE MATCH(firstname, lastname) AGAINST (:param_1 IN BOOLEAN MODE)
36 ORDER BY MATCH(firstname, lastname) AGAINST (:param_2) DESC
38 The :func:`_mysql.match` function is a standalone version of the
39 :meth:`_sql.ColumnElement.match` method available on all
40 SQL expressions, as when :meth:`_expression.ColumnElement.match` is
41 used, but allows to pass multiple columns
43 :param cols: column expressions to match against
45 :param against: expression to be compared towards
47 :param in_boolean_mode: boolean, set "boolean mode" to true
49 :param in_natural_language_mode: boolean , set "natural language" to true
51 :param with_query_expansion: boolean, set "query expansion" to true
53 .. versionadded:: 1.4.19
55 .. seealso::
57 :meth:`_expression.ColumnElement.match`
59 """
61 __visit_name__ = "mysql_match"
63 inherit_cache = True
65 def __init__(self, *cols, **kw):
66 if not cols:
67 raise exc.ArgumentError("columns are required")
69 against = kw.pop("against", None)
71 if against is None:
72 raise exc.ArgumentError("against is required")
73 against = coercions.expect(
74 roles.ExpressionElementRole,
75 against,
76 )
78 left = elements.BooleanClauseList._construct_raw(
79 operators.comma_op,
80 clauses=cols,
81 )
82 left.group = False
84 flags = util.immutabledict(
85 {
86 "mysql_boolean_mode": kw.pop("in_boolean_mode", False),
87 "mysql_natural_language": kw.pop(
88 "in_natural_language_mode", False
89 ),
90 "mysql_query_expansion": kw.pop("with_query_expansion", False),
91 }
92 )
94 if kw:
95 raise exc.ArgumentError("unknown arguments: %s" % (", ".join(kw)))
97 super(match, self).__init__(
98 left, against, operators.match_op, modifiers=flags
99 )
101 @_generative
102 def in_boolean_mode(self):
103 """Apply the "IN BOOLEAN MODE" modifier to the MATCH expression.
105 :return: a new :class:`_mysql.match` instance with modifications
106 applied.
107 """
109 self.modifiers = self.modifiers.union({"mysql_boolean_mode": True})
111 @_generative
112 def in_natural_language_mode(self):
113 """Apply the "IN NATURAL LANGUAGE MODE" modifier to the MATCH
114 expression.
116 :return: a new :class:`_mysql.match` instance with modifications
117 applied.
118 """
120 self.modifiers = self.modifiers.union({"mysql_natural_language": True})
122 @_generative
123 def with_query_expansion(self):
124 """Apply the "WITH QUERY EXPANSION" modifier to the MATCH expression.
126 :return: a new :class:`_mysql.match` instance with modifications
127 applied.
128 """
130 self.modifiers = self.modifiers.union({"mysql_query_expansion": True})