Skip to content

Commit d7316e3

Browse files
committed
added 'not_like' where clause
1 parent 5212450 commit d7316e3

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

piccolo/columns/base.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
Like,
1111
NotEqual,
1212
NotIn,
13+
NotLike
1314
)
1415
from ..custom_types import Iterable
1516
from .combination import Where
@@ -45,6 +46,11 @@ def like(self, value: str) -> Where:
4546
raise ValueError('% is required for like operators')
4647
return Where(column=self, value=value, operator=Like)
4748

49+
def not_like(self, value: str) -> Where:
50+
if '%' not in value:
51+
raise ValueError('% is required for like operators')
52+
return Where(column=self, value=value, operator=NotLike)
53+
4854
def format_value(self, value):
4955
"""
5056
Takes the raw Python value and return a string usable in the database

piccolo/columns/operators.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ class Like(Operator):
2222
template = "{name} LIKE {value}"
2323

2424

25+
class NotLike(Operator):
26+
template = "{name} NOT LIKE {value}"
27+
28+
2529
class GreaterThan(Operator):
2630
# Add permitted types???
2731
template = "{name} > {value}"

tests/table/test_select.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,24 @@ def test_where_like(self):
4747
[{'name': 'Pythonistas'}]
4848
)
4949

50+
def test_where_not_like(self):
51+
self.insert_rows()
52+
53+
response = Band.select.columns(
54+
Band.name
55+
).where(
56+
Band.name.not_like('Python%')
57+
).order_by(
58+
Band.name
59+
).run_sync()
60+
61+
print(f'response = {response}')
62+
63+
self.assertEqual(
64+
response,
65+
[{'name': 'CSharps'}, {'name': 'Rustaceans'}]
66+
)
67+
5068
def test_where_greater_than(self):
5169
self.insert_rows()
5270

0 commit comments

Comments
 (0)