Add SQLModel core code

This commit is contained in:
Sebastián Ramírez
2021-08-24 14:41:53 +02:00
commit fcff2050e6
17 changed files with 1867 additions and 0 deletions

0
sqlmodel/sql/__init__.py Normal file
View File

11
sqlmodel/sql/base.py Normal file
View File

@@ -0,0 +1,11 @@
from typing import Generic, TypeVar
from sqlalchemy.sql.base import Executable as _Executable
_T = TypeVar("_T")
class Executable(_Executable, Generic[_T]):
def __init__(self, *args, **kwargs):
self.__dict__["_exec_options"] = kwargs.pop("_exec_options", None)
super(_Executable, self).__init__(*args, **kwargs)

459
sqlmodel/sql/expression.py Normal file
View File

@@ -0,0 +1,459 @@
# WARNING: do not modify this code, it is generated by expression.py.jinja2
import sys
from datetime import datetime
from typing import (
TYPE_CHECKING,
Any,
Generic,
Mapping,
Sequence,
Tuple,
Type,
TypeVar,
Union,
cast,
overload,
)
from uuid import UUID
from sqlalchemy import Column
from sqlalchemy.orm import InstrumentedAttribute
from sqlalchemy.sql.elements import ColumnClause
from sqlalchemy.sql.expression import Select as _Select
_TSelect = TypeVar("_TSelect")
# Workaround Generics incompatibility in Python 3.6
# Ref: https://github.com/python/typing/issues/449#issuecomment-316061322
if sys.version_info.minor >= 7:
class Select(_Select, Generic[_TSelect]):
pass
# This is not comparable to sqlalchemy.sql.selectable.ScalarSelect, that has a different
# purpose. This is the same as a normal SQLAlchemy Select class where there's only one
# entity, so the result will be converted to a scalar by default. This way writing
# for loops on the results will feel natural.
class SelectOfScalar(_Select, Generic[_TSelect]):
pass
else:
from typing import GenericMeta # type: ignore
class GenericSelectMeta(GenericMeta, _Select.__class__): # type: ignore
pass
class _Py36Select(_Select, Generic[_TSelect], metaclass=GenericSelectMeta):
pass
class _Py36SelectOfScalar(_Select, Generic[_TSelect], metaclass=GenericSelectMeta):
pass
# Cast them for editors to work correctly, from several tricks tried, this works
# for both VS Code and PyCharm
Select = cast("Select", _Py36Select) # type: ignore
SelectOfScalar = cast("SelectOfScalar", _Py36SelectOfScalar) # type: ignore
if TYPE_CHECKING: # pragma: no cover
from ..main import SQLModel
# Generated TypeVars start
_TScalar_0 = TypeVar(
"_TScalar_0",
Column,
Sequence,
Mapping,
UUID,
datetime,
float,
int,
bool,
bytes,
str,
None,
)
_TModel_0 = TypeVar("_TModel_0", bound="SQLModel")
_TScalar_1 = TypeVar(
"_TScalar_1",
Column,
Sequence,
Mapping,
UUID,
datetime,
float,
int,
bool,
bytes,
str,
None,
)
_TModel_1 = TypeVar("_TModel_1", bound="SQLModel")
_TScalar_2 = TypeVar(
"_TScalar_2",
Column,
Sequence,
Mapping,
UUID,
datetime,
float,
int,
bool,
bytes,
str,
None,
)
_TModel_2 = TypeVar("_TModel_2", bound="SQLModel")
_TScalar_3 = TypeVar(
"_TScalar_3",
Column,
Sequence,
Mapping,
UUID,
datetime,
float,
int,
bool,
bytes,
str,
None,
)
_TModel_3 = TypeVar("_TModel_3", bound="SQLModel")
# Generated TypeVars end
@overload
def select(entity_0: _TScalar_0, **kw: Any) -> SelectOfScalar[_TScalar_0]: # type: ignore
...
@overload
def select(entity_0: Type[_TModel_0], **kw: Any) -> SelectOfScalar[_TModel_0]: # type: ignore
...
# Generated overloads start
@overload
def select( # type: ignore
entity_0: _TScalar_0,
entity_1: _TScalar_1,
**kw: Any,
) -> Select[Tuple[_TScalar_0, _TScalar_1]]:
...
@overload
def select( # type: ignore
entity_0: _TScalar_0,
entity_1: Type[_TModel_1],
**kw: Any,
) -> Select[Tuple[_TScalar_0, _TModel_1]]:
...
@overload
def select( # type: ignore
entity_0: Type[_TModel_0],
entity_1: _TScalar_1,
**kw: Any,
) -> Select[Tuple[_TModel_0, _TScalar_1]]:
...
@overload
def select( # type: ignore
entity_0: Type[_TModel_0],
entity_1: Type[_TModel_1],
**kw: Any,
) -> Select[Tuple[_TModel_0, _TModel_1]]:
...
@overload
def select( # type: ignore
entity_0: _TScalar_0,
entity_1: _TScalar_1,
entity_2: _TScalar_2,
**kw: Any,
) -> Select[Tuple[_TScalar_0, _TScalar_1, _TScalar_2]]:
...
@overload
def select( # type: ignore
entity_0: _TScalar_0,
entity_1: _TScalar_1,
entity_2: Type[_TModel_2],
**kw: Any,
) -> Select[Tuple[_TScalar_0, _TScalar_1, _TModel_2]]:
...
@overload
def select( # type: ignore
entity_0: _TScalar_0,
entity_1: Type[_TModel_1],
entity_2: _TScalar_2,
**kw: Any,
) -> Select[Tuple[_TScalar_0, _TModel_1, _TScalar_2]]:
...
@overload
def select( # type: ignore
entity_0: _TScalar_0,
entity_1: Type[_TModel_1],
entity_2: Type[_TModel_2],
**kw: Any,
) -> Select[Tuple[_TScalar_0, _TModel_1, _TModel_2]]:
...
@overload
def select( # type: ignore
entity_0: Type[_TModel_0],
entity_1: _TScalar_1,
entity_2: _TScalar_2,
**kw: Any,
) -> Select[Tuple[_TModel_0, _TScalar_1, _TScalar_2]]:
...
@overload
def select( # type: ignore
entity_0: Type[_TModel_0],
entity_1: _TScalar_1,
entity_2: Type[_TModel_2],
**kw: Any,
) -> Select[Tuple[_TModel_0, _TScalar_1, _TModel_2]]:
...
@overload
def select( # type: ignore
entity_0: Type[_TModel_0],
entity_1: Type[_TModel_1],
entity_2: _TScalar_2,
**kw: Any,
) -> Select[Tuple[_TModel_0, _TModel_1, _TScalar_2]]:
...
@overload
def select( # type: ignore
entity_0: Type[_TModel_0],
entity_1: Type[_TModel_1],
entity_2: Type[_TModel_2],
**kw: Any,
) -> Select[Tuple[_TModel_0, _TModel_1, _TModel_2]]:
...
@overload
def select( # type: ignore
entity_0: _TScalar_0,
entity_1: _TScalar_1,
entity_2: _TScalar_2,
entity_3: _TScalar_3,
**kw: Any,
) -> Select[Tuple[_TScalar_0, _TScalar_1, _TScalar_2, _TScalar_3]]:
...
@overload
def select( # type: ignore
entity_0: _TScalar_0,
entity_1: _TScalar_1,
entity_2: _TScalar_2,
entity_3: Type[_TModel_3],
**kw: Any,
) -> Select[Tuple[_TScalar_0, _TScalar_1, _TScalar_2, _TModel_3]]:
...
@overload
def select( # type: ignore
entity_0: _TScalar_0,
entity_1: _TScalar_1,
entity_2: Type[_TModel_2],
entity_3: _TScalar_3,
**kw: Any,
) -> Select[Tuple[_TScalar_0, _TScalar_1, _TModel_2, _TScalar_3]]:
...
@overload
def select( # type: ignore
entity_0: _TScalar_0,
entity_1: _TScalar_1,
entity_2: Type[_TModel_2],
entity_3: Type[_TModel_3],
**kw: Any,
) -> Select[Tuple[_TScalar_0, _TScalar_1, _TModel_2, _TModel_3]]:
...
@overload
def select( # type: ignore
entity_0: _TScalar_0,
entity_1: Type[_TModel_1],
entity_2: _TScalar_2,
entity_3: _TScalar_3,
**kw: Any,
) -> Select[Tuple[_TScalar_0, _TModel_1, _TScalar_2, _TScalar_3]]:
...
@overload
def select( # type: ignore
entity_0: _TScalar_0,
entity_1: Type[_TModel_1],
entity_2: _TScalar_2,
entity_3: Type[_TModel_3],
**kw: Any,
) -> Select[Tuple[_TScalar_0, _TModel_1, _TScalar_2, _TModel_3]]:
...
@overload
def select( # type: ignore
entity_0: _TScalar_0,
entity_1: Type[_TModel_1],
entity_2: Type[_TModel_2],
entity_3: _TScalar_3,
**kw: Any,
) -> Select[Tuple[_TScalar_0, _TModel_1, _TModel_2, _TScalar_3]]:
...
@overload
def select( # type: ignore
entity_0: _TScalar_0,
entity_1: Type[_TModel_1],
entity_2: Type[_TModel_2],
entity_3: Type[_TModel_3],
**kw: Any,
) -> Select[Tuple[_TScalar_0, _TModel_1, _TModel_2, _TModel_3]]:
...
@overload
def select( # type: ignore
entity_0: Type[_TModel_0],
entity_1: _TScalar_1,
entity_2: _TScalar_2,
entity_3: _TScalar_3,
**kw: Any,
) -> Select[Tuple[_TModel_0, _TScalar_1, _TScalar_2, _TScalar_3]]:
...
@overload
def select( # type: ignore
entity_0: Type[_TModel_0],
entity_1: _TScalar_1,
entity_2: _TScalar_2,
entity_3: Type[_TModel_3],
**kw: Any,
) -> Select[Tuple[_TModel_0, _TScalar_1, _TScalar_2, _TModel_3]]:
...
@overload
def select( # type: ignore
entity_0: Type[_TModel_0],
entity_1: _TScalar_1,
entity_2: Type[_TModel_2],
entity_3: _TScalar_3,
**kw: Any,
) -> Select[Tuple[_TModel_0, _TScalar_1, _TModel_2, _TScalar_3]]:
...
@overload
def select( # type: ignore
entity_0: Type[_TModel_0],
entity_1: _TScalar_1,
entity_2: Type[_TModel_2],
entity_3: Type[_TModel_3],
**kw: Any,
) -> Select[Tuple[_TModel_0, _TScalar_1, _TModel_2, _TModel_3]]:
...
@overload
def select( # type: ignore
entity_0: Type[_TModel_0],
entity_1: Type[_TModel_1],
entity_2: _TScalar_2,
entity_3: _TScalar_3,
**kw: Any,
) -> Select[Tuple[_TModel_0, _TModel_1, _TScalar_2, _TScalar_3]]:
...
@overload
def select( # type: ignore
entity_0: Type[_TModel_0],
entity_1: Type[_TModel_1],
entity_2: _TScalar_2,
entity_3: Type[_TModel_3],
**kw: Any,
) -> Select[Tuple[_TModel_0, _TModel_1, _TScalar_2, _TModel_3]]:
...
@overload
def select( # type: ignore
entity_0: Type[_TModel_0],
entity_1: Type[_TModel_1],
entity_2: Type[_TModel_2],
entity_3: _TScalar_3,
**kw: Any,
) -> Select[Tuple[_TModel_0, _TModel_1, _TModel_2, _TScalar_3]]:
...
@overload
def select( # type: ignore
entity_0: Type[_TModel_0],
entity_1: Type[_TModel_1],
entity_2: Type[_TModel_2],
entity_3: Type[_TModel_3],
**kw: Any,
) -> Select[Tuple[_TModel_0, _TModel_1, _TModel_2, _TModel_3]]:
...
# Generated overloads end
def select(*entities: Any, **kw: Any) -> Union[Select, SelectOfScalar]:
if len(entities) == 1:
return SelectOfScalar._create(*entities, **kw) # type: ignore
return Select._create(*entities, **kw) # type: ignore
# TODO: add several @overload from Python types to SQLAlchemy equivalents
def col(column_expression: Any) -> ColumnClause:
if not isinstance(column_expression, (ColumnClause, Column, InstrumentedAttribute)):
raise RuntimeError(f"Not a SQLAlchemy column: {column_expression}")
return column_expression

View File

@@ -0,0 +1,119 @@
import sys
from datetime import datetime
from typing import (
TYPE_CHECKING,
Any,
Generic,
Mapping,
Sequence,
Tuple,
Type,
TypeVar,
Union,
cast,
overload,
)
from uuid import UUID
from sqlalchemy import Column
from sqlalchemy.orm import InstrumentedAttribute
from sqlalchemy.sql.elements import ColumnClause
from sqlalchemy.sql.expression import Select as _Select
_TSelect = TypeVar("_TSelect")
# Workaround Generics incompatibility in Python 3.6
# Ref: https://github.com/python/typing/issues/449#issuecomment-316061322
if sys.version_info.minor >= 7:
class Select(_Select, Generic[_TSelect]):
pass
# This is not comparable to sqlalchemy.sql.selectable.ScalarSelect, that has a different
# purpose. This is the same as a normal SQLAlchemy Select class where there's only one
# entity, so the result will be converted to a scalar by default. This way writing
# for loops on the results will feel natural.
class SelectOfScalar(_Select, Generic[_TSelect]):
pass
else:
from typing import GenericMeta # type: ignore
class GenericSelectMeta(GenericMeta, _Select.__class__): # type: ignore
pass
class _Py36Select(_Select, Generic[_TSelect], metaclass=GenericSelectMeta):
pass
class _Py36SelectOfScalar(_Select, Generic[_TSelect], metaclass=GenericSelectMeta):
pass
# Cast them for editors to work correctly, from several tricks tried, this works
# for both VS Code and PyCharm
Select = cast("Select", _Py36Select) # type: ignore
SelectOfScalar = cast("SelectOfScalar", _Py36SelectOfScalar) # type: ignore
if TYPE_CHECKING: # pragma: no cover
from ..main import SQLModel
# Generated TypeVars start
{% for i in range(number_of_types) %}
_TScalar_{{ i }} = TypeVar(
"_TScalar_{{ i }}",
Column,
Sequence,
Mapping,
UUID,
datetime,
float,
int,
bool,
bytes,
str,
None,
)
_TModel_{{ i }} = TypeVar("_TModel_{{ i }}", bound="SQLModel")
{% endfor %}
# Generated TypeVars end
@overload
def select(entity_0: _TScalar_0, **kw: Any) -> SelectOfScalar[_TScalar_0]: # type: ignore
...
@overload
def select(entity_0: Type[_TModel_0], **kw: Any) -> SelectOfScalar[_TModel_0]: # type: ignore
...
# Generated overloads start
{% for signature in signatures %}
@overload
def select( # type: ignore
{% for arg in signature[0] %}{{ arg.name }}: {{ arg.annotation }}, {% endfor %}**kw: Any,
) -> Select[Tuple[{%for ret in signature[1] %}{{ ret }} {% if not loop.last %}, {% endif %}{% endfor %}]]:
...
{% endfor %}
# Generated overloads end
def select(*entities: Any, **kw: Any) -> Union[Select, SelectOfScalar]:
if len(entities) == 1:
return SelectOfScalar._create(*entities, **kw) # type: ignore
return Select._create(*entities, **kw)
# TODO: add several @overload from Python types to SQLAlchemy equivalents
def col(column_expression: Any) -> ColumnClause:
if not isinstance(column_expression, (ColumnClause, Column, InstrumentedAttribute)):
raise RuntimeError(f"Not a SQLAlchemy column: {column_expression}")
return column_expression

60
sqlmodel/sql/sqltypes.py Normal file
View File

@@ -0,0 +1,60 @@
import uuid
from typing import Any, cast
from sqlalchemy import types
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.engine.interfaces import Dialect
from sqlalchemy.types import CHAR, TypeDecorator
class AutoString(types.TypeDecorator):
impl = types.String
cache_ok = True
mysql_default_length = 255
def load_dialect_impl(self, dialect: Dialect) -> "types.TypeEngine[Any]":
impl = cast(types.String, self.impl)
if impl.length is None and dialect.name == "mysql":
return dialect.type_descriptor(types.String(self.mysql_default_length)) # type: ignore
return super().load_dialect_impl(dialect)
# Reference form SQLAlchemy docs: https://docs.sqlalchemy.org/en/14/core/custom_types.html#backend-agnostic-guid-type
# with small modifications
class GUID(TypeDecorator):
"""Platform-independent GUID type.
Uses PostgreSQL's UUID type, otherwise uses
CHAR(32), storing as stringified hex values.
"""
impl = CHAR
cache_ok = True
def load_dialect_impl(self, dialect):
if dialect.name == "postgresql":
return dialect.type_descriptor(UUID())
else:
return dialect.type_descriptor(CHAR(32))
def process_bind_param(self, value, dialect):
if value is None:
return value
elif dialect.name == "postgresql":
return str(value)
else:
if not isinstance(value, uuid.UUID):
return f"{uuid.UUID(value).int:x}"
else:
# hexstring
return f"{value.int:x}"
def process_result_value(self, value, dialect):
if value is None:
return value
else:
if not isinstance(value, uuid.UUID):
value = uuid.UUID(value)
return value