/
lookups.py
265 lines (232 loc) · 10.8 KB
/
lookups.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# Copyright 2020 Google LLC
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd
from django.db.models.lookups import (
Contains,
EndsWith,
Exact,
GreaterThan,
GreaterThanOrEqual,
IContains,
IEndsWith,
IExact,
IRegex,
IStartsWith,
LessThan,
LessThanOrEqual,
Regex,
StartsWith,
)
def contains(self, compiler, connection):
"""A method to extend Django Contains and IContains classes.
:type self: :class:`~django.db.models.lookups.Contains` or
:class:`~django.db.models.lookups.IContains`
:param self: the instance of the class that owns this method.
:type compiler: :class:`~django_spanner.compiler.SQLCompilerst`
:param compiler: The query compiler responsible for generating the query.
Must have a compile method, returning a (sql, [params])
tuple. Calling compiler(value) will return a quoted
`value`.
:type connection: :class:`~google.cloud.spanner_dbapi.connection.Connection`
:param connection: The Spanner database connection used for the current
query.
:rtype: tuple[str, str]
:returns: A tuple of the SQL request and parameters.
"""
lhs_sql, params = self.process_lhs(compiler, connection)
rhs_sql, rhs_params = self.process_rhs(compiler, connection)
params.extend(rhs_params)
is_icontains = self.lookup_name.startswith("i")
if self.rhs_is_direct_value() and params and not self.bilateral_transforms:
rhs_sql = self.get_rhs_op(connection, rhs_sql)
# Chop the leading and trailing percent signs that Django adds to the
# param since this isn't a LIKE query as Django expects.
params[0] = params[0][1:-1]
# Add the case insensitive flag for icontains.
if is_icontains:
params[0] = "(?i)" + params[0]
# rhs_sql is REGEXP_CONTAINS(%s, %%s), and lhs_sql is the column name.
return rhs_sql % lhs_sql, params
else:
# rhs_sql is the expression/column to use as the base of the regular
# expression.
if is_icontains:
rhs_sql = "CONCAT('(?i)', " + rhs_sql + ")"
return (
"REGEXP_CONTAINS(%s, %s)"
% (lhs_sql, connection.pattern_esc.format(rhs_sql)),
params,
)
def iexact(self, compiler, connection):
"""A method to extend Django IExact class. Case-insensitive exact match.
If the value provided for comparison is None, it will be interpreted as
an SQL NULL.
:type self: :class:`~django.db.models.lookups.IExact`
:param self: the instance of the class that owns this method.
:type compiler: :class:`~django_spanner.compiler.SQLCompilerst`
:param compiler: The query compiler responsible for generating the query.
Must have a compile method, returning a (sql, [params])
tuple. Calling compiler(value) will return a quoted
`value`.
:type connection: :class:`~google.cloud.spanner_dbapi.connection.Connection`
:param connection: The Spanner database connection used for the current
query.
:rtype: tuple[str, str]
:returns: A tuple of the SQL request and parameters.
"""
lhs_sql, params = self.process_lhs(compiler, connection)
rhs_sql, rhs_params = self.process_rhs(compiler, connection)
params.extend(rhs_params)
rhs_sql = self.get_rhs_op(connection, rhs_sql)
# Wrap the parameter in ^ and $ to restrict the regex to an exact match.
if self.rhs_is_direct_value() and params and not self.bilateral_transforms:
params[0] = "^(?i)%s$" % params[0]
else:
# lhs_sql is the expression/column to use as the regular expression.
# Use concat to make the value case-insensitive.
lhs_sql = "CONCAT('^(?i)', " + lhs_sql + ", '$')"
if not self.rhs_is_direct_value() and not params:
# If rhs is not a direct value and parameter is not present we want
# to have only 1 formatable argument in rhs_sql else we need 2.
rhs_sql = rhs_sql.replace("%%s", "%s")
# rhs_sql is REGEXP_CONTAINS(%s, %%s), and lhs_sql is the column name.
return rhs_sql % lhs_sql, params
def regex(self, compiler, connection):
"""A method to extend Django Regex and IRegex classes.
:type self: :class:`~django.db.models.lookups.Regex` or
:class:`~django.db.models.lookups.IRegex`
:param self: the instance of the class that owns this method.
:type compiler: :class:`~django_spanner.compiler.SQLCompilerst`
:param compiler: The query compiler responsible for generating the query.
Must have a compile method, returning a (sql, [params])
tuple. Calling compiler(value) will return a quoted
`value`.
:type connection: :class:`~google.cloud.spanner_dbapi.connection.Connection`
:param connection: The Spanner database connection used for the current
query.
:rtype: tuple[str, str]
:returns: A tuple of the SQL request and parameters.
"""
lhs_sql, params = self.process_lhs(compiler, connection)
rhs_sql, rhs_params = self.process_rhs(compiler, connection)
params.extend(rhs_params)
is_iregex = self.lookup_name.startswith("i")
if self.rhs_is_direct_value() and params and not self.bilateral_transforms:
rhs_sql = self.get_rhs_op(connection, rhs_sql)
if is_iregex:
params[0] = "(?i)%s" % params[0]
else:
params[0] = str(params[0])
# rhs_sql is REGEXP_CONTAINS(%s, %%s), and lhs_sql is the column name.
return rhs_sql % lhs_sql, params
else:
# rhs_sql is the expression/column to use as the base of the regular
# expression.
if is_iregex:
rhs_sql = "CONCAT('(?i)', " + rhs_sql + ")"
return "REGEXP_CONTAINS(%s, %s)" % (lhs_sql, rhs_sql), params
def startswith_endswith(self, compiler, connection):
"""A method to extend Django StartsWith, IStartsWith, EndsWith, and
IEndsWith classes.
:type self: :class:`~django.db.models.lookups.StartsWith` or
:class:`~django.db.models.lookups.IStartsWith` or
:class:`~django.db.models.lookups.EndsWith` or
:class:`~django.db.models.lookups.IEndsWith`
:param self: the instance of the class that owns this method.
:type compiler: :class:`~django_spanner.compiler.SQLCompilerst`
:param compiler: The query compiler responsible for generating the query.
Must have a compile method, returning a (sql, [params])
tuple. Calling compiler(value) will return a quoted
`value`.
:type connection: :class:`~google.cloud.spanner_dbapi.connection.Connection`
:param connection: The Spanner database connection used for the current
query.
:rtype: tuple[str, str]
:returns: A tuple of the SQL request and parameters.
"""
lhs_sql, params = self.process_lhs(compiler, connection)
rhs_sql, rhs_params = self.process_rhs(compiler, connection)
params.extend(rhs_params)
is_startswith = "startswith" in self.lookup_name
is_endswith = "endswith" in self.lookup_name
is_insensitive = self.lookup_name.startswith("i")
# Chop the leading (endswith) or trailing (startswith) percent sign that
# Django adds to the param since this isn't a LIKE query as Django expects.
if self.rhs_is_direct_value() and params and not self.bilateral_transforms:
rhs_sql = self.get_rhs_op(connection, rhs_sql)
if is_endswith:
params[0] = str(params[0][1:]) + "$"
else:
params[0] = "^" + str(params[0][:-1])
# Add the case insensitive flag for istartswith or iendswith.
if is_insensitive:
params[0] = "(?i)" + params[0]
# rhs_sql is REGEXP_CONTAINS(%s, %%s), and lhs_sql is the column name.
return rhs_sql % lhs_sql, params
else:
# rhs_sql is the expression/column to use as the base of the regular
# expression.
sql = "CONCAT('"
if is_startswith:
sql += "^"
if is_insensitive:
sql += "(?i)"
sql += "', " + rhs_sql
if is_endswith:
sql += ", '$'"
sql += ")"
return (
"REGEXP_CONTAINS(%s, %s)"
% (lhs_sql, connection.pattern_esc.format(sql)),
params,
)
def cast_param_to_float(self, compiler, connection):
"""A method to extend Django Exact, GreaterThan, GreaterThanOrEqual,
LessThan, and LessThanOrEqual classes.
:type self: :class:`~django.db.models.lookups.Exact` or
:class:`~django.db.models.lookups.GreaterThan` or
:class:`~django.db.models.lookups.GreaterThanOrEqual` or
:class:`~django.db.models.lookups.LessThan` or
:class:`~django.db.models.lookups.LessThanOrEqual`
:param self: the instance of the class that owns this method.
:type compiler: :class:`~django_spanner.compiler.SQLCompilerst`
:param compiler: The query compiler responsible for generating the query.
Must have a compile method, returning a (sql, [params])
tuple. Calling compiler(value) will return a quoted
`value`.
:type connection: :class:`~google.cloud.spanner_dbapi.connection.Connection`
:param connection: The Spanner database connection used for the current
query.
:rtype: tuple[str, str]
:returns: A tuple of the SQL request and float parameters.
"""
sql, params = self.as_sql(compiler, connection)
if params:
# Cast remote field lookups that must be integer but come in as string.
if hasattr(self.lhs.output_field, "get_path_info"):
for i, field in enumerate(
self.lhs.output_field.get_path_info()[-1].target_fields
):
if field.rel_db_type(connection) == "INT64" and isinstance(
params[i], str
):
params[i] = int(params[i])
return sql, params
def register_lookups():
"""Registers the above methods with the corersponding Django classes."""
Contains.as_spanner = contains
IContains.as_spanner = contains
IExact.as_spanner = iexact
Regex.as_spanner = regex
IRegex.as_spanner = regex
EndsWith.as_spanner = startswith_endswith
IEndsWith.as_spanner = startswith_endswith
StartsWith.as_spanner = startswith_endswith
IStartsWith.as_spanner = startswith_endswith
Exact.as_spanner = cast_param_to_float
GreaterThan.as_spanner = cast_param_to_float
GreaterThanOrEqual.as_spanner = cast_param_to_float
LessThan.as_spanner = cast_param_to_float
LessThanOrEqual.as_spanner = cast_param_to_float