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.0.1, created at 2022-12-25 06:11 +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 

9 

10 

11class match(Generative, elements.BinaryExpression): 

12 """Produce a ``MATCH (X, Y) AGAINST ('TEXT')`` clause. 

13 

14 E.g.:: 

15 

16 from sqlalchemy import desc 

17 from sqlalchemy.dialects.mysql import match 

18 

19 match_expr = match( 

20 users_table.c.firstname, 

21 users_table.c.lastname, 

22 against="Firstname Lastname", 

23 ) 

24 

25 stmt = ( 

26 select(users_table) 

27 .where(match_expr.in_boolean_mode()) 

28 .order_by(desc(match_expr)) 

29 ) 

30 

31 Would produce SQL resembling:: 

32 

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 

37 

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 

42 

43 :param cols: column expressions to match against 

44 

45 :param against: expression to be compared towards 

46 

47 :param in_boolean_mode: boolean, set "boolean mode" to true 

48 

49 :param in_natural_language_mode: boolean , set "natural language" to true 

50 

51 :param with_query_expansion: boolean, set "query expansion" to true 

52 

53 .. versionadded:: 1.4.19 

54 

55 .. seealso:: 

56 

57 :meth:`_expression.ColumnElement.match` 

58 

59 """ 

60 

61 __visit_name__ = "mysql_match" 

62 

63 inherit_cache = True 

64 

65 def __init__(self, *cols, **kw): 

66 if not cols: 

67 raise exc.ArgumentError("columns are required") 

68 

69 against = kw.pop("against", None) 

70 

71 if against is None: 

72 raise exc.ArgumentError("against is required") 

73 against = coercions.expect( 

74 roles.ExpressionElementRole, 

75 against, 

76 ) 

77 

78 left = elements.BooleanClauseList._construct_raw( 

79 operators.comma_op, 

80 clauses=cols, 

81 ) 

82 left.group = False 

83 

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 ) 

93 

94 if kw: 

95 raise exc.ArgumentError("unknown arguments: %s" % (", ".join(kw))) 

96 

97 super(match, self).__init__( 

98 left, against, operators.match_op, modifiers=flags 

99 ) 

100 

101 @_generative 

102 def in_boolean_mode(self): 

103 """Apply the "IN BOOLEAN MODE" modifier to the MATCH expression. 

104 

105 :return: a new :class:`_mysql.match` instance with modifications 

106 applied. 

107 """ 

108 

109 self.modifiers = self.modifiers.union({"mysql_boolean_mode": True}) 

110 

111 @_generative 

112 def in_natural_language_mode(self): 

113 """Apply the "IN NATURAL LANGUAGE MODE" modifier to the MATCH 

114 expression. 

115 

116 :return: a new :class:`_mysql.match` instance with modifications 

117 applied. 

118 """ 

119 

120 self.modifiers = self.modifiers.union({"mysql_natural_language": True}) 

121 

122 @_generative 

123 def with_query_expansion(self): 

124 """Apply the "WITH QUERY EXPANSION" modifier to the MATCH expression. 

125 

126 :return: a new :class:`_mysql.match` instance with modifications 

127 applied. 

128 """ 

129 

130 self.modifiers = self.modifiers.union({"mysql_query_expansion": True})