-
-
Notifications
You must be signed in to change notification settings - Fork 355
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from typing import Dict, Optional, Tuple | ||
|
||
from pypika.terms import Term, ValueWrapper | ||
|
||
from tortoise.indexes import PartialIndex | ||
|
||
|
||
class SqliteIndex(PartialIndex): | ||
INDEX_CREATE_TEMPLATE = PartialIndex.INDEX_CREATE_TEMPLATE.replace("INDEX", "INDEX {exists} ") | ||
|
||
|
||
class SqliteUniqueIndex(SqliteIndex): | ||
INDEX_TYPE = "unique".upper() | ||
|
||
def __init__( | ||
self, | ||
*expressions: Term, | ||
fields: Optional[Tuple[str, ...]] = None, | ||
name: Optional[str] = None, | ||
condition: Optional[Dict[str, str]] = None, | ||
where_expre: Optional[str] = None, | ||
): | ||
_condition = condition | ||
if condition: | ||
condition = None | ||
super().__init__(*expressions, fields=fields, name=name, condition=condition) | ||
if _condition: | ||
# TODO: what is the best practice to inject where expression? | ||
self.extra = where_expre or self._gen_condition(_condition) | ||
|
||
@classmethod | ||
def _gen_field_cond(cls, kv: tuple): | ||
key, cond = kv | ||
op = ( | ||
"" | ||
if isinstance(cond, str) | ||
and cond.strip().lower().startswith(("is ", "isnot ", "<", ">", "=", "!=")) | ||
else "=" | ||
) | ||
if op == "=": | ||
cond = ValueWrapper(cond) | ||
return str(f"{key} {op} {cond}") | ||
|
||
def _gen_condition(self, conditions: Dict[str, str]): | ||
conditions = " AND ".join(tuple(map(self._gen_field_cond, conditions.items()))) | ||
return f" where {conditions}" |