0.3.0: initial commit + Dockerfile + rewrite
This commit is contained in:
commit
e679de5991
77 changed files with 4147 additions and 0 deletions
25
freak/search.py
Normal file
25
freak/search.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
|
||||
|
||||
|
||||
from typing import Iterable
|
||||
from sqlalchemy import Column, Select, select, or_
|
||||
|
||||
|
||||
class SearchQuery:
|
||||
keywords: Iterable[str]
|
||||
|
||||
def __init__(self, keywords: str | Iterable[str]):
|
||||
if isinstance(keywords, str):
|
||||
keywords = keywords.split()
|
||||
self.keywords = keywords
|
||||
def select(self, table: type, attrs: Iterable[Column]) -> Select:
|
||||
if not attrs:
|
||||
raise TypeError
|
||||
sq: Select = select(table)
|
||||
for kw in self.keywords:
|
||||
or_cond = []
|
||||
for attr in attrs:
|
||||
or_cond.append(attr.ilike(f"%{kw.replace('%', r'\%')}%"))
|
||||
sq = sq.where(or_(*or_cond) if len(or_cond) > 1 else or_cond[0])
|
||||
return sq
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue