Skip to content

Commit d1a7e2e

Browse files
committed
feat: add new function wrap_backticks
1 parent 186abe0 commit d1a7e2e

File tree

6 files changed

+24
-2
lines changed

6 files changed

+24
-2
lines changed

examples/service/routers/account.py

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ async def query_by_id_view(
4545
aid: int = Query(...),
4646
):
4747
meta = AccountMgr.model._meta
48+
print(AccountMgr.table, type(AccountMgr.table))
4849
print(dir(meta))
4950
print("db_complex_fields", meta.db_complex_fields, "", sep="\n")
5051
print("db_default_fields", meta.db_default_fields, "", sep="\n")

fastapi_esql/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
Singleton,
2121
convert_dicts,
2222
timing,
23+
wrap_backticks,
2324
)
2425

2526
__version__ = "0.0.11"
@@ -39,6 +40,7 @@
3940
"convert_dicts",
4041
"escape_string",
4142
"timing",
43+
"wrap_backticks",
4244
]
4345

4446
dictConfig({

fastapi_esql/utils/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .converter import convert_dicts
1+
from .converter import convert_dicts, wrap_backticks
22
from .cursor_handler import CursorHandler
33
from .decorator import timing
44
from .metaclass import Singleton

fastapi_esql/utils/converter.py

+5
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@ def convert_dicts(dicts, converters: Dict[str, Callable]):
1818
d[field] = converter(value)
1919
except Exception as e:
2020
logger.warning(f"Converting value `{value}` by `{converter.__name__}` failed => {e}")
21+
22+
23+
def wrap_backticks(identifier: str):
24+
assert isinstance(identifier, str), f"identifier({identifier}) must be instance of `str`"
25+
return f"`{str(identifier).strip('`')}`"

fastapi_esql/utils/sqlizer.py

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from tortoise.query_utils import QueryModifier
88

99
from ..const.error import QsParsingError, WrongParamsError
10+
from .converter import wrap_backticks
1011

1112
logger = getLogger(__name__)
1213
# To ensure the functionality of the RawSQL

tests/test_converter.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from json import loads
33
from unittest import TestCase
44

5-
from fastapi_esql import convert_dicts, escape_string
5+
from fastapi_esql import convert_dicts, escape_string, wrap_backticks
66

77

88
class TestConvertDicts(TestCase):
@@ -31,3 +31,16 @@ def test_normal(self):
3131
assert escape_string("'") == "\\'"
3232
assert escape_string('"') == '\\"'
3333
assert escape_string('\\') == '\\\\'
34+
35+
36+
class TestWrapBackticks(TestCase):
37+
38+
def test_wrong_input(self):
39+
with self.assertRaises(AssertionError):
40+
wrap_backticks(1024)
41+
42+
def test_without_backticks(self):
43+
assert wrap_backticks("order") == "`order`"
44+
45+
def test_with_backticks(self):
46+
assert wrap_backticks("`order`") == "`order`"

0 commit comments

Comments
 (0)