|
6 | 6 |
|
7 | 7 | Numeric = int | float | Decimal |
8 | 8 |
|
9 | | -QueryValue = str | bool | dt.date | dt.datetime | Numeric |
| 9 | +QueryValue = str | bool | dt.date | dt.datetime | Numeric # noqa: WPS221 |
| 10 | + |
| 11 | + |
| 12 | +class Property: |
| 13 | + """Wrapper for model properties in RQL queries.""" |
| 14 | + |
| 15 | + def __init__(self, value: str): |
| 16 | + self.value = value |
| 17 | + |
| 18 | + @override |
| 19 | + def __str__(self) -> str: |
| 20 | + return self.value |
| 21 | + |
| 22 | + |
| 23 | +class Value: |
| 24 | + """Wrapper for literal values in RQL queries.""" |
| 25 | + |
| 26 | + def __init__(self, value: QueryValue): |
| 27 | + self.value = value |
| 28 | + |
| 29 | + @override |
| 30 | + def __str__(self) -> str: |
| 31 | + if isinstance(self.value, str): |
| 32 | + return f"'{self.value}'" |
| 33 | + if isinstance(self.value, bool): |
| 34 | + return "true" if self.value else "false" |
| 35 | + |
| 36 | + if isinstance(self.value, dt.date | dt.datetime): |
| 37 | + str_time = self.value.isoformat() |
| 38 | + return f"'{str_time}'" |
| 39 | + |
| 40 | + # Matching: if isinstance(value, int | float | Decimal): |
| 41 | + return str(self.value) |
10 | 42 |
|
11 | 43 |
|
12 | 44 | def parse_kwargs(query_dict: dict[str, QueryValue]) -> list[str]: # noqa: WPS231 |
@@ -62,16 +94,14 @@ def parse_kwargs(query_dict: dict[str, QueryValue]) -> list[str]: # noqa: WPS23 |
62 | 94 | return query |
63 | 95 |
|
64 | 96 |
|
65 | | -def query_value_str(value: QueryValue) -> str: |
| 97 | +def query_value_str(value: Any) -> str: |
66 | 98 | """Converts a value to string for use in RQL queries.""" |
67 | | - if isinstance(value, str): |
68 | | - return value |
69 | | - if isinstance(value, bool): |
70 | | - return "true" if value else "false" |
71 | | - |
72 | | - if isinstance(value, dt.date | dt.datetime): |
73 | | - return value.isoformat() |
74 | | - # Matching: if isinstance(value, int | float | Decimal): |
| 99 | + if isinstance(value, QueryValue): |
| 100 | + value = Value(value) |
| 101 | + if isinstance(value, Value): |
| 102 | + return str(value) |
| 103 | + if isinstance(value, Property): |
| 104 | + return str(value) |
75 | 105 | return str(value) |
76 | 106 |
|
77 | 107 |
|
@@ -104,7 +134,9 @@ def rql_encode(op: str, value: Any) -> str: |
104 | 134 | rql_encode('in', ['a', 'b', 'c']) |
105 | 135 | 'a,b,c' |
106 | 136 | """ |
107 | | - if op not in constants.LIST and isinstance(value, QueryValue): |
| 137 | + if op not in constants.LIST and isinstance(value, QueryValue | Value | Property): |
| 138 | + return query_value_str(value) |
| 139 | + if op in constants.COMP and isinstance(value, list | tuple | set): |
108 | 140 | return query_value_str(value) |
109 | 141 | if op in constants.LIST and isinstance(value, list | tuple | set): |
110 | 142 | return ",".join(str(el) for el in value) |
|
0 commit comments