ormlambda 1.0.1__py3-none-any.whl → 1.1.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. ormlambda/__init__.py +8 -2
  2. ormlambda/common/__init__.py +0 -1
  3. ormlambda/common/abstract_classes/__init__.py +1 -1
  4. ormlambda/common/abstract_classes/abstract_model.py +30 -13
  5. ormlambda/common/abstract_classes/non_query_base.py +7 -5
  6. ormlambda/common/abstract_classes/query_base.py +5 -2
  7. ormlambda/common/enums/__init__.py +1 -1
  8. ormlambda/common/enums/join_type.py +2 -1
  9. ormlambda/common/interfaces/IQueryCommand.py +2 -1
  10. ormlambda/common/interfaces/IStatements.py +25 -14
  11. ormlambda/common/interfaces/__init__.py +1 -1
  12. ormlambda/components/delete/abstract_delete.py +6 -3
  13. ormlambda/components/insert/IInsert.py +1 -1
  14. ormlambda/components/insert/abstract_insert.py +8 -4
  15. ormlambda/components/select/ISelect.py +1 -1
  16. ormlambda/components/select/table_column.py +5 -1
  17. ormlambda/components/update/IUpdate.py +1 -1
  18. ormlambda/components/update/__init__.py +1 -1
  19. ormlambda/components/update/abstract_update.py +9 -5
  20. ormlambda/components/upsert/__init__.py +1 -1
  21. ormlambda/components/upsert/abstract_upsert.py +8 -4
  22. ormlambda/components/where/abstract_where.py +6 -2
  23. ormlambda/databases/my_sql/clauses/__init__.py +1 -0
  24. ormlambda/databases/my_sql/clauses/count.py +35 -0
  25. ormlambda/databases/my_sql/clauses/create_database.py +17 -10
  26. ormlambda/databases/my_sql/clauses/delete.py +7 -4
  27. ormlambda/databases/my_sql/clauses/drop_database.py +1 -1
  28. ormlambda/databases/my_sql/clauses/drop_table.py +1 -1
  29. ormlambda/databases/my_sql/clauses/insert.py +4 -3
  30. ormlambda/databases/my_sql/clauses/joins.py +8 -7
  31. ormlambda/databases/my_sql/clauses/limit.py +1 -1
  32. ormlambda/databases/my_sql/clauses/offset.py +1 -1
  33. ormlambda/databases/my_sql/clauses/order.py +3 -3
  34. ormlambda/databases/my_sql/clauses/select.py +5 -5
  35. ormlambda/databases/my_sql/clauses/update.py +3 -3
  36. ormlambda/databases/my_sql/clauses/upsert.py +3 -3
  37. ormlambda/databases/my_sql/clauses/where_condition.py +5 -5
  38. ormlambda/databases/my_sql/repository.py +26 -4
  39. ormlambda/databases/my_sql/statements.py +14 -5
  40. ormlambda/model_base.py +0 -2
  41. ormlambda/utils/column.py +4 -3
  42. ormlambda/utils/dtypes.py +2 -17
  43. ormlambda/utils/foreign_key.py +55 -10
  44. ormlambda/utils/lambda_disassembler/__init__.py +1 -1
  45. ormlambda/utils/lambda_disassembler/dis_types.py +22 -25
  46. ormlambda/utils/lambda_disassembler/tree_instruction.py +1 -1
  47. ormlambda/utils/module_tree/dynamic_module.py +6 -4
  48. ormlambda/utils/table_constructor.py +39 -22
  49. {ormlambda-1.0.1.dist-info → ormlambda-1.1.3.dist-info}/METADATA +8 -8
  50. ormlambda-1.1.3.dist-info/RECORD +71 -0
  51. ormlambda-1.0.1.dist-info/RECORD +0 -70
  52. {ormlambda-1.0.1.dist-info → ormlambda-1.1.3.dist-info}/LICENSE +0 -0
  53. {ormlambda-1.0.1.dist-info → ormlambda-1.1.3.dist-info}/WHEEL +0 -0
ormlambda/__init__.py CHANGED
@@ -1,4 +1,10 @@
1
- from .model_base import BaseModel # noqa: F401
1
+ # enums
2
+ from .common.enums import ( # noqa: F401
3
+ JoinType,
4
+ ConditionType,
5
+ )
6
+
7
+ from .common.interfaces import IRepositoryBase # noqa: F401
2
8
  from .utils import Table, Column, ForeignKey # noqa: F401
3
9
  from .utils.lambda_disassembler import Disassembler, nameof # noqa: F401
4
- from .common.interfaces import IRepositoryBase # noqa: F401
10
+ from .model_base import BaseModel # noqa: F401 # COMMENT: to avoid relative import we need to import BaseModel after import Table,Column, ForeignKey, IRepositoryBase and Disassembler
@@ -1,2 +1 @@
1
1
 
2
-
@@ -1,3 +1,3 @@
1
1
  from .non_query_base import NonQueryBase # noqa: F401
2
2
  from .abstract_model import AbstractSQLStatements # noqa: F401
3
- from .query_base import QueryBase # noqa: F401
3
+ from .query_base import QueryBase # noqa: F401
@@ -1,21 +1,27 @@
1
- from typing import Any, Callable, Optional, Type, override, Iterable, Literal
1
+ from __future__ import annotations
2
+ from typing import Any, Callable, Optional, Type, override, Iterable, Literal, TYPE_CHECKING
2
3
  from enum import Enum
3
4
  from collections import defaultdict
4
5
  from abc import abstractmethod
5
6
  import inspect
6
7
 
7
- from ...utils import ForeignKey, Table
8
+ from ormlambda.utils import ForeignKey, Table
8
9
 
9
- from ..interfaces import IQuery, IRepositoryBase, IStatements_two_generic
10
- from ..interfaces.IStatements import OrderType
10
+ from ormlambda.common.interfaces import IQuery, IRepositoryBase, IStatements_two_generic
11
11
 
12
- from ...components.update import UpdateQueryBase
13
- from ...components.select import ISelect
14
- from ...components.delete import DeleteQueryBase
15
- from ...components.upsert import UpsertQueryBase
16
- from ...components.select import TableColumn
17
- from ...components.insert import InsertQueryBase
18
- from ...components.where.abstract_where import AbstractWhere
12
+ if TYPE_CHECKING:
13
+ from ormlambda.common.interfaces.IStatements import OrderType
14
+
15
+ from ormlambda.components.update import UpdateQueryBase
16
+ from ormlambda.components.select import ISelect
17
+ from ormlambda.components.delete import DeleteQueryBase
18
+ from ormlambda.components.upsert import UpsertQueryBase
19
+ from ormlambda.components.select import TableColumn
20
+ from ormlambda.components.insert import InsertQueryBase
21
+ from ormlambda.components.where.abstract_where import AbstractWhere
22
+
23
+ from ormlambda.databases.my_sql.clauses.select import SelectQuery
24
+ from ormlambda.databases.my_sql.clauses.count import CountQuery
19
25
 
20
26
 
21
27
  class JoinType(Enum):
@@ -82,7 +88,11 @@ class AbstractSQLStatements[T: Table, TRepo](IStatements_two_generic[T, TRepo]):
82
88
  def ORDER_QUERY(self) -> Type[IQuery]: ...
83
89
  @property
84
90
  @abstractmethod
85
- def SELECT_QUERY(self) -> Type[ISelect]: ...
91
+ def SELECT_QUERY(self) -> Type[SelectQuery]: ...
92
+
93
+ @property
94
+ @abstractmethod
95
+ def COUNT(self) -> Type[CountQuery]: ...
86
96
 
87
97
  @override
88
98
  def create_table(self) -> None:
@@ -151,9 +161,16 @@ class AbstractSQLStatements[T: Table, TRepo](IStatements_two_generic[T, TRepo]):
151
161
  self._query_list["offset"].append(offset)
152
162
  return self
153
163
 
164
+ @override
165
+ def count(self) -> int:
166
+ count_select: IQuery = self.COUNT(self._model)
167
+ self._query_list["select"].append(count_select)
168
+ query = self.build()
169
+ return self.repository.read_sql(query)[0][0]
170
+
154
171
  @override
155
172
  def join(self, table_left: Table, table_right: Table, *, by: str) -> "IStatements_two_generic[T,TRepo]":
156
- where = ForeignKey.MAPPED[table_left][table_right]
173
+ where = ForeignKey.MAPPED[table_left.__table_name__][table_right.__table_name__]
157
174
  join_query = self.JOIN_QUERY[table_left, Table](table_left, table_right, JoinType(by), where=where)
158
175
  self._query_list["join"].append(join_query)
159
176
  return self
@@ -1,13 +1,15 @@
1
+ from __future__ import annotations
1
2
  from abc import abstractmethod
2
- from typing import Any, Optional, override
3
+ from typing import Any, Optional, Type, override, TYPE_CHECKING
3
4
 
4
- from ..interfaces.INonQueryCommand import INonQueryCommand
5
+ from ormlambda.common.interfaces.INonQueryCommand import INonQueryCommand
5
6
 
6
- from ..interfaces import IRepositoryBase
7
- from ...utils import Table
7
+ if TYPE_CHECKING:
8
+ from ormlambda import IRepositoryBase
9
+ from ormlambda import Table
8
10
 
9
11
 
10
- class NonQueryBase[T: Table, TRepo: IRepositoryBase](INonQueryCommand):
12
+ class NonQueryBase[T: Type[Table], TRepo: IRepositoryBase](INonQueryCommand):
11
13
  __slots__: tuple[str, ...] = ("_model", "_repository", "_values", "_query")
12
14
 
13
15
  def __init__(self, model: T, repository: TRepo) -> None:
@@ -1,7 +1,10 @@
1
1
  from abc import abstractmethod
2
+ from typing import TYPE_CHECKING
2
3
 
3
- from ...utils import Table
4
- from ..interfaces import IQuery
4
+ from ormlambda.common.interfaces import IQuery
5
+
6
+ if TYPE_CHECKING:
7
+ from ormlambda import Table
5
8
 
6
9
 
7
10
  class QueryBase[T: Table](IQuery):
@@ -1,2 +1,2 @@
1
1
  from .join_type import JoinType # noqa: F401
2
- from .condition_types import ConditionType # noqa: F401
2
+ from .condition_types import ConditionType # noqa: F401
@@ -1,5 +1,6 @@
1
1
  from enum import Enum
2
2
 
3
+
3
4
  class JoinType(Enum):
4
5
  RIGHT_INCLUSIVE = "RIGHT JOIN"
5
6
  LEFT_INCLUSIVE = "LEFT JOIN"
@@ -7,4 +8,4 @@ class JoinType(Enum):
7
8
  LEFT_EXCLUSIVE = "LEFT JOIN"
8
9
  FULL_OUTER_INCLUSIVE = "RIGHT JOIN"
9
10
  FULL_OUTER_EXCLUSIVE = "RIGHT JOIN"
10
- INNER_JOIN = "INNER JOIN"
11
+ INNER_JOIN = "INNER JOIN"
@@ -2,7 +2,8 @@ from abc import abstractmethod, ABC
2
2
 
3
3
 
4
4
  class IQuery(ABC):
5
- """ Interface to queries that retrieve any element such as select, limit, offset, where, group by, etc..."""
5
+ """Interface to queries that retrieve any element such as select, limit, offset, where, group by, etc..."""
6
+
6
7
  @property
7
8
  @abstractmethod
8
9
  def query(self) -> str: ...
@@ -1,10 +1,13 @@
1
- from typing import Any, Callable, Iterable, Optional, Literal, Type, overload
1
+ from __future__ import annotations
2
+ from typing import Any, Callable, Iterable, Optional, Literal, Type, overload, TYPE_CHECKING
2
3
  from enum import Enum
3
4
  from abc import abstractmethod, ABC
4
5
 
5
6
  from .IRepositoryBase import IRepositoryBase
6
- from ..enums import JoinType
7
- from ...utils import Table
7
+ from ormlambda.common.enums import JoinType
8
+
9
+ if TYPE_CHECKING:
10
+ from ormlambda import Table
8
11
 
9
12
  OrderType = Literal["ASC", "DESC"]
10
13
 
@@ -38,6 +41,8 @@ class IStatements[T: Table](ABC):
38
41
  @abstractmethod
39
42
  def insert(self, values: T | list[T]) -> None: ...
40
43
 
44
+ # endregion
45
+
41
46
  # region upsert
42
47
  @overload
43
48
  def upsert(self, values: T) -> None:
@@ -71,13 +76,19 @@ class IStatements[T: Table](ABC):
71
76
 
72
77
  # region limit
73
78
  @abstractmethod
74
- def limit(self, number: int) -> "IStatements[T]": ...
79
+ def limit(self, number: int) -> IStatements[T]: ...
75
80
 
76
81
  # endregion
77
82
 
78
83
  # region offset
79
84
  @abstractmethod
80
- def offset(self, number: int) -> "IStatements[T]": ...
85
+ def offset(self, number: int) -> IStatements[T]: ...
86
+
87
+ # endregion
88
+
89
+ # region count
90
+ @abstractmethod
91
+ def count(self) -> int: ...
81
92
 
82
93
  # endregion
83
94
 
@@ -97,13 +108,13 @@ class IStatements[T: Table](ABC):
97
108
 
98
109
  # region join
99
110
  @abstractmethod
100
- def join(self, table_left: Table, table_right: Table, *, by: str) -> "IStatements[T]": ...
111
+ def join(self, table_left: Table, table_right: Table, *, by: str) -> IStatements[T]: ...
101
112
 
102
113
  # endregion
103
114
 
104
115
  # region where
105
116
  @overload
106
- def where(self, lambda_: Callable[[T], bool]) -> "IStatements[T]":
117
+ def where(self, lambda_: Callable[[T], bool]) -> IStatements[T]:
107
118
  """
108
119
  This method creates where clause by passing the lambda's condition
109
120
 
@@ -115,7 +126,7 @@ class IStatements[T: Table](ABC):
115
126
  ...
116
127
 
117
128
  @overload
118
- def where(self, lambda_: Callable[[T], Iterable]) -> "IStatements[T]":
129
+ def where(self, lambda_: Callable[[T], Iterable]) -> IStatements[T]:
119
130
  """
120
131
  This method creates where clause by passing the Iterable in lambda function
121
132
  EXAMPLE
@@ -126,7 +137,7 @@ class IStatements[T: Table](ABC):
126
137
  ...
127
138
 
128
139
  @overload
129
- def where(self, lambda_: Callable[[T], bool], **kwargs) -> "IStatements[T]":
140
+ def where(self, lambda_: Callable[[T], bool], **kwargs) -> IStatements[T]:
130
141
  """
131
142
  PARAM
132
143
  -
@@ -143,17 +154,17 @@ class IStatements[T: Table](ABC):
143
154
  ...
144
155
 
145
156
  @abstractmethod
146
- def where(self, lambda_: Callable[[T], bool] = lambda: None, **kwargs) -> "IStatements[T]": ...
157
+ def where(self, lambda_: Callable[[T], bool] = lambda: None, **kwargs) -> IStatements[T]: ...
147
158
 
148
159
  # endregion
149
160
 
150
161
  # region order
151
162
  @overload
152
- def order[TValue](self, _lambda_col: Callable[[T], TValue]) -> "IStatements[T]": ...
163
+ def order[TValue](self, _lambda_col: Callable[[T], TValue]) -> IStatements[T]: ...
153
164
  @overload
154
- def order[TValue](self, _lambda_col: Callable[[T], TValue], order_type: OrderType) -> "IStatements[T]": ...
165
+ def order[TValue](self, _lambda_col: Callable[[T], TValue], order_type: OrderType) -> IStatements[T]: ...
155
166
  @abstractmethod
156
- def order[TValue](self, _lambda_col: Callable[[T], TValue], order_type: OrderType) -> "IStatements[T]": ...
167
+ def order[TValue](self, _lambda_col: Callable[[T], TValue], order_type: OrderType) -> IStatements[T]: ...
157
168
 
158
169
  # endregion
159
170
 
@@ -203,7 +214,7 @@ class IStatements[T: Table](ABC):
203
214
  @overload
204
215
  def select_one(self) -> T: ...
205
216
  @overload
206
- def select_one(self, *, by: Optional[Enum] = JoinType.INNER_JOIN, flavour: Type[tuple]) -> tuple: ...
217
+ def select_one[TFlavour](self, *, by: Optional[Enum] = JoinType.INNER_JOIN, flavour: Type[TFlavour]) -> TFlavour: ...
207
218
  @overload
208
219
  def select_one[T1](self, selector: Callable[[T], tuple[T1]], *, by: Optional[Enum] = JoinType.INNER_JOIN) -> T1: ...
209
220
  @overload
@@ -1,4 +1,4 @@
1
1
  from .IQueryCommand import IQuery # noqa: F401
2
2
  from .INonQueryCommand import INonQueryCommand # noqa: F401
3
3
  from .IRepositoryBase import IRepositoryBase # noqa: F401
4
- from .IStatements import IStatements,IStatements_two_generic # noqa: F401
4
+ from .IStatements import IStatements, IStatements_two_generic # noqa: F401
@@ -1,9 +1,12 @@
1
+ from __future__ import annotations
1
2
  from abc import abstractmethod
3
+ from typing import TYPE_CHECKING
4
+
5
+ if TYPE_CHECKING:
6
+ from ormlambda import Table, IRepositoryBase
7
+ from ormlambda.common.abstract_classes import NonQueryBase
2
8
 
3
9
  from .IDelete import IDelete
4
- from ...utils import Table
5
- from ...common.interfaces import IRepositoryBase
6
- from ...common.abstract_classes import NonQueryBase
7
10
 
8
11
 
9
12
  class DeleteQueryBase[T: Table, TRepo: IRepositoryBase](NonQueryBase[T, TRepo], IDelete[T]):
@@ -3,4 +3,4 @@ from abc import ABC, abstractmethod
3
3
 
4
4
  class IInsert[T](ABC):
5
5
  @abstractmethod
6
- def insert(self, instances: T | list[T])->None: ...
6
+ def insert(self, instances: T | list[T]) -> None: ...
@@ -1,12 +1,16 @@
1
+ from __future__ import annotations
1
2
  from abc import abstractmethod
2
- from ...utils import Table
3
+ from typing import TYPE_CHECKING
3
4
 
5
+ if TYPE_CHECKING:
6
+ from ormlambda import Table
7
+ from ormlambda import IRepositoryBase
8
+
9
+ from ormlambda.common.abstract_classes import NonQueryBase
4
10
  from .IInsert import IInsert
5
- from ...common.interfaces import IRepositoryBase
6
- from ...common.abstract_classes import NonQueryBase
7
11
 
8
12
 
9
- class InsertQueryBase[T: Table, TRepo: IRepositoryBase](NonQueryBase[T, IRepositoryBase], IInsert[T]):
13
+ class InsertQueryBase[T: Table, TRepo: IRepositoryBase](NonQueryBase[T, TRepo], IInsert[T]):
10
14
  def __init__(self, model: T, repository: TRepo) -> None:
11
15
  super().__init__(model, repository)
12
16
 
@@ -1,6 +1,6 @@
1
1
  from abc import abstractmethod
2
2
 
3
- from ...common.interfaces import IQuery
3
+ from ormlambda.common.interfaces import IQuery
4
4
  from .table_column import TableColumn
5
5
 
6
6
 
@@ -1,5 +1,9 @@
1
+ from __future__ import annotations
1
2
  from typing import Any, Iterator
2
- from ...utils import Table
3
+ from typing import TYPE_CHECKING
4
+
5
+ if TYPE_CHECKING:
6
+ from ormlambda import Table
3
7
 
4
8
 
5
9
  class TableColumn:
@@ -4,4 +4,4 @@ from typing import Any
4
4
 
5
5
  class IUpdate(ABC):
6
6
  @abstractmethod
7
- def update(self, dicc: dict[str|property, Any]) -> None: ...
7
+ def update(self, dicc: dict[str | property, Any]) -> None: ...
@@ -1,2 +1,2 @@
1
1
  from .abstract_update import UpdateQueryBase # noqa: F401
2
- from .IUpdate import IUpdate # noqa: F401
2
+ from .IUpdate import IUpdate # noqa: F401
@@ -1,11 +1,15 @@
1
+ from __future__ import annotations
1
2
  from abc import abstractmethod
2
- from typing import Any, Optional
3
+ from typing import Any, Optional, TYPE_CHECKING
4
+
5
+ from ormlambda.components.where import AbstractWhere
6
+ from ormlambda.common.abstract_classes import NonQueryBase
7
+
8
+ if TYPE_CHECKING:
9
+ from ormlambda import IRepositoryBase
10
+ from ormlambda import Table
3
11
 
4
12
  from .IUpdate import IUpdate
5
- from ..where import AbstractWhere
6
- from ...common.interfaces import IRepositoryBase
7
- from ...common.abstract_classes import NonQueryBase
8
- from ...utils import Table
9
13
 
10
14
 
11
15
  class UpdateQueryBase[T: Table, TRepo: IRepositoryBase](NonQueryBase[T, TRepo], IUpdate):
@@ -1,2 +1,2 @@
1
1
  from .abstract_upsert import UpsertQueryBase # noqa: F401
2
- from .IUpsert import IUpsert # noqa: F401
2
+ from .IUpsert import IUpsert # noqa: F401
@@ -1,9 +1,13 @@
1
+ from __future__ import annotations
1
2
  from abc import abstractmethod
2
- from .IUpsert import IUpsert
3
- from ...common.interfaces import IRepositoryBase
4
- from ...common.abstract_classes import NonQueryBase
3
+ from typing import TYPE_CHECKING
4
+
5
+ if TYPE_CHECKING:
6
+ from ormlambda import IRepositoryBase
7
+ from ormlambda import Table
5
8
 
6
- from ...utils import Table
9
+ from ormlambda.common.abstract_classes import NonQueryBase
10
+ from .IUpsert import IUpsert
7
11
 
8
12
 
9
13
  class UpsertQueryBase[T: Table, TRepo: IRepositoryBase](NonQueryBase[T, TRepo], IUpsert[T]):
@@ -1,7 +1,11 @@
1
+ from __future__ import annotations
1
2
  from abc import abstractmethod
2
- from ...utils import Table
3
+ from typing import TYPE_CHECKING
3
4
 
4
- from ...common.interfaces import IQuery
5
+ if TYPE_CHECKING:
6
+ from ormlambda import Table
7
+
8
+ from ormlambda.common.interfaces import IQuery
5
9
 
6
10
 
7
11
  class AbstractWhere(IQuery):
@@ -11,3 +11,4 @@ from .select import SelectQuery # noqa: F401
11
11
  from .update import UpdateQuery # noqa: F401
12
12
  from .upsert import UpsertQuery # noqa: F401
13
13
  from .where_condition import WhereCondition # noqa: F401
14
+ from .count import CountQuery # noqa: F401
@@ -0,0 +1,35 @@
1
+ from typing import Callable, Type, override
2
+
3
+ from ormlambda import Table, JoinType, ForeignKey
4
+ from ormlambda.databases.my_sql.clauses.joins import JoinSelector
5
+ from ormlambda.databases.my_sql.clauses.select import SelectQuery
6
+
7
+
8
+ class CountQuery[T: Type[Table]](SelectQuery[T]):
9
+ CLAUSE: str = "COUNT"
10
+
11
+ def __init__(
12
+ self,
13
+ tables: T | tuple[T] = (),
14
+ select_lambda: Callable[[T], None] | None = lambda: None,
15
+ *,
16
+ by: JoinType = JoinType.INNER_JOIN,
17
+ ) -> None:
18
+ super().__init__(tables, select_lambda, by=by)
19
+
20
+ @override
21
+ @property
22
+ def query(self) -> str:
23
+ query: str = f"{self.SELECT} {self.CLAUSE}(*) FROM {self._first_table.__table_name__}"
24
+
25
+ involved_tables = self.get_involved_tables()
26
+ if not involved_tables:
27
+ return query
28
+
29
+ sub_query: str = ""
30
+ for l_tbl, r_tbl in involved_tables:
31
+ join = JoinSelector(l_tbl, r_tbl, by=self._by, where=ForeignKey.MAPPED[l_tbl.__table_name__].referenced_tables[r_tbl.__table_name__].relationship)
32
+ sub_query += f" {join.query}"
33
+
34
+ query += sub_query
35
+ return query
@@ -1,8 +1,8 @@
1
1
  from typing import Literal, override
2
- from mysql.connector import Error, errorcode
2
+ from mysql.connector import errorcode, errors
3
3
  from mysql.connector import MySQLConnection
4
4
 
5
- from ....common.interfaces import IRepositoryBase
5
+ from ormlambda import IRepositoryBase
6
6
 
7
7
  TypeExists = Literal["fail", "replace", "append"]
8
8
 
@@ -18,12 +18,19 @@ class CreateDatabase:
18
18
 
19
19
  @override
20
20
  def execute(self, name: str, if_exists: TypeExists = "fail") -> None:
21
- with self._repository.connection.cursor() as cursor:
22
- try:
23
- cursor.execute(f"{self.CLAUSE} {name} DEFAULT CHARACTER SET 'utf8'")
24
- except Error as err:
25
- if err.errno == errorcode.ER_DB_CREATE_EXISTS and if_exists != "fail":
26
- cursor.execute(f"USE {name};")
27
- else:
28
- raise err
21
+ if self._repository.database_exists(name):
22
+ if if_exists == "replace":
23
+ self._repository.drop_database(name)
24
+ elif if_exists == "fail":
25
+ raise errors.DatabaseError(msg=f"Database '{name}' already exists", errno=errorcode.ER_DB_CREATE_EXISTS)
26
+ elif if_exists == "append":
27
+ counter: int = 0
28
+ char: str = ""
29
+ while self._repository.database_exists(name + char):
30
+ counter += 1
31
+ char = f"_{counter}"
32
+ name += char
33
+
34
+ query = f"{self.CLAUSE} {name} DEFAULT CHARACTER SET 'utf8'"
35
+ self._repository.execute(query)
29
36
  return None
@@ -1,8 +1,11 @@
1
- from typing import Any, override, Iterable
1
+ from typing import Any, override, Iterable, TYPE_CHECKING
2
2
 
3
- from ....utils import Table, Column
4
- from ....common.interfaces import IRepositoryBase
5
- from ....components.delete import DeleteQueryBase
3
+ if TYPE_CHECKING:
4
+ from ormlambda import Column
5
+
6
+ from ormlambda import Table
7
+ from ormlambda import IRepositoryBase
8
+ from ormlambda.components.delete import DeleteQueryBase
6
9
  from mysql.connector import MySQLConnection
7
10
 
8
11
 
@@ -2,7 +2,7 @@ from typing import override
2
2
  from mysql.connector import MySQLConnection
3
3
 
4
4
 
5
- from ....common.interfaces import IRepositoryBase
5
+ from ormlambda import IRepositoryBase
6
6
 
7
7
 
8
8
  class DropDatabase:
@@ -1,6 +1,6 @@
1
1
  from typing import Literal, override
2
2
 
3
- from ....common.interfaces import IRepositoryBase
3
+ from ormlambda import IRepositoryBase
4
4
 
5
5
  from mysql.connector import MySQLConnection
6
6
 
@@ -1,9 +1,10 @@
1
1
  from typing import Any, override, Iterable
2
2
  from mysql.connector import MySQLConnection
3
3
 
4
- from ....utils import Table, Column
5
- from ....components.insert import InsertQueryBase
6
- from ....common.interfaces import IRepositoryBase
4
+ from ormlambda import Table
5
+ from ormlambda import Column
6
+ from ormlambda.components.insert import InsertQueryBase
7
+ from ormlambda import IRepositoryBase
7
8
 
8
9
 
9
10
  class InsertQuery[T: Table](InsertQueryBase[T, IRepositoryBase[MySQLConnection]]):
@@ -1,13 +1,14 @@
1
- from typing import override, Callable, overload, Optional, TypeVar
1
+ from __future__ import annotations
2
+ from typing import override, Callable, overload, Optional, TYPE_CHECKING
2
3
 
3
- # from ..table import Table
4
4
 
5
- from ....common.interfaces.IQueryCommand import IQuery
6
- from ....utils.lambda_disassembler import Disassembler
7
- from ....common.enums import JoinType
5
+ from ormlambda.common.interfaces.IQueryCommand import IQuery
6
+ from ormlambda import Disassembler
7
+ from ormlambda import JoinType
8
8
 
9
- # TODOL: Try to import Table module without circular import Error
10
- Table = TypeVar("Table")
9
+ # TODOL [x]: Try to import Table module without circular import Error
10
+ if TYPE_CHECKING:
11
+ from ormlambda import Table
11
12
 
12
13
 
13
14
  class JoinSelector[TLeft, TRight](IQuery):
@@ -1,6 +1,6 @@
1
1
  from typing import override
2
2
 
3
- from ....common.interfaces.IQueryCommand import IQuery
3
+ from ormlambda.common.interfaces.IQueryCommand import IQuery
4
4
 
5
5
 
6
6
  class LimitQuery(IQuery):
@@ -1,6 +1,6 @@
1
1
  from typing import override
2
2
 
3
- from ....common.interfaces.IQueryCommand import IQuery
3
+ from ormlambda.common.interfaces.IQueryCommand import IQuery
4
4
 
5
5
 
6
6
  class OffsetQuery(IQuery):
@@ -1,8 +1,8 @@
1
1
  from typing import override, Callable
2
2
 
3
- from ....utils.lambda_disassembler.tree_instruction import TreeInstruction
4
- from ....common.interfaces.IQueryCommand import IQuery
5
- from ....common.interfaces.IStatements import OrderType
3
+ from ormlambda.utils.lambda_disassembler.tree_instruction import TreeInstruction
4
+ from ormlambda.common.interfaces.IQueryCommand import IQuery
5
+ from ormlambda.common.interfaces.IStatements import OrderType
6
6
 
7
7
  ASC = "ASC"
8
8
  DESC = "DESC"
@@ -1,10 +1,10 @@
1
1
  from typing import Callable, Optional, Type, override
2
2
  import inspect
3
3
 
4
- from ....utils.lambda_disassembler import TreeInstruction, TupleInstruction, NestedElement
5
- from ....components.select import ISelect, TableColumn
6
- from ....utils import Table, ForeignKey
7
- from ....utils.table_constructor import TableMeta
4
+ from ormlambda.utils.lambda_disassembler import TreeInstruction, TupleInstruction, NestedElement
5
+ from ormlambda.components.select import ISelect, TableColumn
6
+ from ormlambda import Table, ForeignKey
7
+ from ormlambda.utils.table_constructor import TableMeta
8
8
 
9
9
  from . import JoinSelector, JoinType
10
10
 
@@ -152,7 +152,7 @@ class SelectQuery[T: Table, *Ts](ISelect):
152
152
 
153
153
  sub_query: str = ""
154
154
  for l_tbl, r_tbl in involved_tables:
155
- join = JoinSelector(l_tbl, r_tbl, by=self._by, where=ForeignKey.MAPPED[l_tbl][r_tbl])
155
+ join = JoinSelector(l_tbl, r_tbl, by=self._by, where=ForeignKey.MAPPED[l_tbl.__table_name__].referenced_tables[r_tbl.__table_name__].relationship)
156
156
  sub_query += f" {join.query}"
157
157
 
158
158
  query += sub_query
@@ -1,9 +1,9 @@
1
1
  from typing import Type, override, Any
2
2
  from mysql.connector import MySQLConnection
3
3
 
4
- from ....components.update import UpdateQueryBase
5
- from ....utils import Table, Column
6
- from ....common.interfaces import IRepositoryBase
4
+ from ormlambda.components.update import UpdateQueryBase
5
+ from ormlambda import Table, Column
6
+ from ormlambda import IRepositoryBase
7
7
  from .where_condition import WhereCondition
8
8
 
9
9