FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add condition for substring matching. · 7dracoder/python-spanner-orm@c8502fc · GitHub

Commit c8502fc

Browse files
committed
Add condition for substring matching.
1 parent 5324407 commit c8502fc

3 files changed

Lines changed: 63 additions & 0 deletions

File tree

‎spanner_orm/__init__.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
StringArray = field.StringArray
6969
Timestamp = field.Timestamp
7070

71+
contains = condition.contains
7172
equal_to = condition.equal_to
7273
force_index = condition.force_index
7374
greater_than = condition.greater_than

‎spanner_orm/condition.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,34 @@ def columns_equal(origin_column: str, dest_model_class: Type[Any],
571571
return ColumnsEqualCondition(origin_column, dest_model_class, dest_column)
572572

573573

574+
def contains(
575+
column: Union[field.Field, str],
576+
value: str,
577+
) -> ComparisonCondition:
578+
"""Condition where the specified column contains the given substring.
579+
580+
Args:
581+
column: Name of the column on the origin model or the Field on the origin
582+
model class to compare from
583+
value: The value to compare against
584+
585+
Returns:
586+
A Condition subclass that will be used in the query
587+
"""
588+
value_escaped = value.translate(
589+
str.maketrans({
590+
# https://cloud.google.com/spanner/docs/functions-and-operators#comparison_operators
591+
'%': r'\%',
592+
'_': r'\_',
593+
'\\': '\\\\',
594+
}))
595+
return ComparisonCondition(
596+
operator='LIKE',
597+
field_or_name=column,
598+
value=f'%{value_escaped}%',
599+
)
600+
601+
574602
def equal_to(column: Union[field.Field, str], value: Any) -> EqualityCondition:
575603
"""Condition where the specified column is equal to the given value.
576604
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Lint as: python3
2+
# Copyright 2020 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# https://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
"""Tests for spanner_orm.condition."""
16+
17+
import logging
18+
import unittest
19+
20+
import spanner_orm
21+
22+
23+
class ConditionTest(unittest.TestCase):
24+
25+
def test_contains(self):
26+
contains = spanner_orm.contains('some_column', r'a%b_c\d')
27+
self.assertEqual('some_column', contains.column)
28+
self.assertEqual('LIKE', contains.operator)
29+
self.assertEqual(r'%a\%b\_c\\d%', contains.value)
30+
31+
32+
if __name__ == '__main__':
33+
logging.basicConfig()
34+
unittest.main()

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL