36 lines
1.1 KiB
Python
36 lines
1.1 KiB
Python
from typing import Any, Optional, Dict
|
|
|
|
|
|
def _convert_type(type_: Optional[type], type_overload: Dict[Optional[type], str]) -> str:
|
|
"""
|
|
Given a Python type, return the str name of its
|
|
SQLlite equivalent.
|
|
:param type_: A Python type, or None.
|
|
:param type_overload: A type table to overload the custom type table.
|
|
:return: The str name of the sql type.
|
|
>>> _convert_type(int)
|
|
"INTEGER"
|
|
"""
|
|
try:
|
|
return type_overload[type_]
|
|
except KeyError:
|
|
raise TypeError("Requested type not in the default or overloaded type table.")
|
|
|
|
|
|
def _convert_sql_format(value: Any) -> str:
|
|
"""
|
|
Given a Python value, convert to string representation
|
|
of the equivalent SQL datatype.
|
|
:param value: A value, ie: a literal, a variable etc.
|
|
:return: The string representation of the SQL equivalent.
|
|
>>> _convert_sql_format(1)
|
|
"1"
|
|
>>> _convert_sql_format("John Smith")
|
|
'"John Smith"'
|
|
"""
|
|
if isinstance(value, str):
|
|
return f'"{value}"'
|
|
elif isinstance(value, bytes):
|
|
return '"' + str(value).replace("b'", "")[:-1] + '"'
|
|
else:
|
|
return str(value) |