| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -68,6 +68,7 @@ | |||
| 68 | 68 | StringArray = field.StringArray | |
| 69 | 69 | Timestamp = field.Timestamp | |
| 70 | 70 | ||
| 71 | + contains = condition.contains | ||
| 71 | 72 | equal_to = condition.equal_to | |
| 72 | 73 | force_index = condition.force_index | |
| 73 | 74 | greater_than = condition.greater_than | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -571,6 +571,34 @@ def columns_equal(origin_column: str, dest_model_class: Type[Any], | |||
| 571 | 571 | return ColumnsEqualCondition(origin_column, dest_model_class, dest_column) | |
| 572 | 572 | ||
| 573 | 573 | ||
| 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 | + | ||
| 574 | 602 | def equal_to(column: Union[field.Field, str], value: Any) -> EqualityCondition: | |
| 575 | 603 | """Condition where the specified column is equal to the given value. | |
| 576 | 604 | ||
| Original file line number | Diff line number | Diff 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() | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments