ormlambda 1.0.1__tar.gz → 1.2.0__tar.gz

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 (75) hide show
  1. {ormlambda-1.0.1 → ormlambda-1.2.0}/PKG-INFO +13 -9
  2. {ormlambda-1.0.1 → ormlambda-1.2.0}/README.md +10 -6
  3. {ormlambda-1.0.1 → ormlambda-1.2.0}/pyproject.toml +3 -4
  4. ormlambda-1.2.0/src/ormlambda/__init__.py +10 -0
  5. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/common/__init__.py +0 -1
  6. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/common/abstract_classes/__init__.py +1 -1
  7. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/common/abstract_classes/abstract_model.py +30 -13
  8. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/common/abstract_classes/non_query_base.py +7 -5
  9. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/common/abstract_classes/query_base.py +5 -2
  10. ormlambda-1.2.0/src/ormlambda/common/enums/__init__.py +2 -0
  11. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/common/enums/join_type.py +2 -1
  12. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/common/interfaces/IQueryCommand.py +2 -1
  13. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/common/interfaces/IRepositoryBase.py +4 -0
  14. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/common/interfaces/IStatements.py +25 -14
  15. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/common/interfaces/__init__.py +1 -1
  16. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/components/delete/abstract_delete.py +6 -3
  17. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/components/insert/IInsert.py +1 -1
  18. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/components/insert/abstract_insert.py +8 -4
  19. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/components/select/ISelect.py +1 -1
  20. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/components/select/table_column.py +5 -1
  21. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/components/update/IUpdate.py +1 -1
  22. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/components/update/__init__.py +1 -1
  23. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/components/update/abstract_update.py +9 -5
  24. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/components/upsert/__init__.py +1 -1
  25. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/components/upsert/abstract_upsert.py +8 -4
  26. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/components/where/abstract_where.py +6 -2
  27. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/databases/my_sql/clauses/__init__.py +1 -0
  28. ormlambda-1.2.0/src/ormlambda/databases/my_sql/clauses/count.py +35 -0
  29. ormlambda-1.2.0/src/ormlambda/databases/my_sql/clauses/create_database.py +36 -0
  30. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/databases/my_sql/clauses/delete.py +7 -4
  31. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/databases/my_sql/clauses/drop_database.py +1 -1
  32. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/databases/my_sql/clauses/drop_table.py +1 -1
  33. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/databases/my_sql/clauses/insert.py +4 -3
  34. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/databases/my_sql/clauses/joins.py +8 -7
  35. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/databases/my_sql/clauses/limit.py +1 -1
  36. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/databases/my_sql/clauses/offset.py +1 -1
  37. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/databases/my_sql/clauses/order.py +3 -3
  38. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/databases/my_sql/clauses/select.py +5 -5
  39. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/databases/my_sql/clauses/update.py +3 -3
  40. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/databases/my_sql/clauses/upsert.py +3 -3
  41. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/databases/my_sql/clauses/where_condition.py +5 -5
  42. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/databases/my_sql/repository.py +26 -4
  43. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/databases/my_sql/statements.py +14 -5
  44. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/model_base.py +0 -2
  45. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/utils/column.py +4 -3
  46. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/utils/dtypes.py +6 -8
  47. ormlambda-1.2.0/src/ormlambda/utils/foreign_key.py +81 -0
  48. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/utils/lambda_disassembler/__init__.py +1 -1
  49. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/utils/lambda_disassembler/dis_types.py +22 -25
  50. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/utils/lambda_disassembler/tree_instruction.py +1 -1
  51. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/utils/module_tree/dynamic_module.py +6 -4
  52. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/utils/table_constructor.py +39 -22
  53. ormlambda-1.0.1/src/ormlambda/__init__.py +0 -4
  54. ormlambda-1.0.1/src/ormlambda/common/enums/__init__.py +0 -2
  55. ormlambda-1.0.1/src/ormlambda/databases/my_sql/clauses/create_database.py +0 -29
  56. ormlambda-1.0.1/src/ormlambda/utils/foreign_key.py +0 -36
  57. {ormlambda-1.0.1 → ormlambda-1.2.0}/LICENSE +0 -0
  58. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/common/enums/condition_types.py +0 -0
  59. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/common/interfaces/INonQueryCommand.py +0 -0
  60. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/components/__init__.py +0 -0
  61. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/components/delete/IDelete.py +0 -0
  62. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/components/delete/__init__.py +0 -0
  63. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/components/insert/__init__.py +0 -0
  64. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/components/select/__init__.py +0 -0
  65. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/components/upsert/IUpsert.py +0 -0
  66. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/components/where/__init__.py +0 -0
  67. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/databases/__init__.py +0 -0
  68. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/databases/my_sql/__init__.py +0 -0
  69. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/utils/__init__.py +0 -0
  70. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/utils/lambda_disassembler/disassembler.py +0 -0
  71. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/utils/lambda_disassembler/dtypes.py +0 -0
  72. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/utils/lambda_disassembler/name_of.py +0 -0
  73. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/utils/lambda_disassembler/nested_element.py +0 -0
  74. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/utils/module_tree/__init__.py +0 -0
  75. {ormlambda-1.0.1 → ormlambda-1.2.0}/src/ormlambda/utils/module_tree/dfs_traversal.py +0 -0
@@ -1,16 +1,20 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: ormlambda
3
- Version: 1.0.1
3
+ Version: 1.2.0
4
4
  Summary: ORM designed to interact with the database (currently with MySQL) using lambda functions and nested functions
5
5
  Author: p-hzamora
6
6
  Author-email: p.hzamora@icloud.com
7
7
  Requires-Python: >=3.12,<4.0
8
8
  Classifier: Programming Language :: Python :: 3
9
9
  Classifier: Programming Language :: Python :: 3.12
10
- Requires-Dist: mysql-connector (>=2.2.9,<3.0.0)
11
- Requires-Dist: ruff (>=0.4.5,<0.5.0)
10
+ Requires-Dist: fluent-validation (>=3.1.0,<4.0.0)
11
+ Requires-Dist: mysql-connector-python (>=2.2.9,<3.0.0)
12
12
  Description-Content-Type: text/markdown
13
13
 
14
+ ![PyPI version](https://img.shields.io/pypi/v/ormlambda.svg)
15
+ ![downloads](https://img.shields.io/pypi/dm/ormlambda?label=downloads)
16
+ ![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)
17
+
14
18
  # ormMySQL
15
19
  This ORM is designed to connect with a MySQL server, facilitating the management of various database queries. Built with flexibility and efficiency in mind, this ORM empowers developers to interact with the database using lambda functions, allowing for concise and expressive query construction.
16
20
 
@@ -19,7 +23,7 @@ This ORM is designed to connect with a MySQL server, facilitating the management
19
23
  ## Initialize MySQLRepository
20
24
  ```python
21
25
  from decouple import config
22
- from src.ormmysql.databases.my_sql import MySQLRepository
26
+ from ormlambda.databases.my_sql import MySQLRepository
23
27
 
24
28
  USERNAME = config("USERNAME")
25
29
  PASSWORD = config("PASSWORD")
@@ -31,7 +35,7 @@ database = MySQLRepository(user=USERNAME, password=PASSWORD, database="sakila",
31
35
 
32
36
  ## Select all columns
33
37
  ```python
34
- from src.test.models.address import AddressModel
38
+ from models.address import AddressModel
35
39
 
36
40
  result = AddressModel(database).select()
37
41
  ```
@@ -41,8 +45,8 @@ The `result` var will be of type `tuple[Address, ...]`
41
45
  Once the `AddressModel` class is created, we will not only be able to access all the information in that table, but also all the information in all the tables that have foreign keys related to it."
42
46
 
43
47
  ```python
44
- from src.ormmysql.common.enums import ConditionType
45
- from src.test.models.address import AddressModel
48
+ from ormlambda import ConditionType
49
+ from models.address import AddressModel
46
50
 
47
51
 
48
52
  result = AddressModel(database).where(lambda x: (x.City.Country, ConditionType.REGEXP, r"^[aA]")).select(
@@ -135,13 +139,13 @@ The easiest way to map your tables is:
135
139
  ```python
136
140
  from datetime import datetime
137
141
 
138
- from src.ormmysql import (
142
+ from ormlambda import (
139
143
  Column,
140
144
  Table,
141
145
  BaseModel,
142
146
  ForeignKey,
143
147
  )
144
- from src.ormmysql.common.interfaces import IStatements_two_generic, IRepositoryBase
148
+ from ormlambda.common.interfaces import IStatements_two_generic, IRepositoryBase
145
149
 
146
150
 
147
151
  class Country(Table):
@@ -1,3 +1,7 @@
1
+ ![PyPI version](https://img.shields.io/pypi/v/ormlambda.svg)
2
+ ![downloads](https://img.shields.io/pypi/dm/ormlambda?label=downloads)
3
+ ![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)
4
+
1
5
  # ormMySQL
2
6
  This ORM is designed to connect with a MySQL server, facilitating the management of various database queries. Built with flexibility and efficiency in mind, this ORM empowers developers to interact with the database using lambda functions, allowing for concise and expressive query construction.
3
7
 
@@ -6,7 +10,7 @@ This ORM is designed to connect with a MySQL server, facilitating the management
6
10
  ## Initialize MySQLRepository
7
11
  ```python
8
12
  from decouple import config
9
- from src.ormmysql.databases.my_sql import MySQLRepository
13
+ from ormlambda.databases.my_sql import MySQLRepository
10
14
 
11
15
  USERNAME = config("USERNAME")
12
16
  PASSWORD = config("PASSWORD")
@@ -18,7 +22,7 @@ database = MySQLRepository(user=USERNAME, password=PASSWORD, database="sakila",
18
22
 
19
23
  ## Select all columns
20
24
  ```python
21
- from src.test.models.address import AddressModel
25
+ from models.address import AddressModel
22
26
 
23
27
  result = AddressModel(database).select()
24
28
  ```
@@ -28,8 +32,8 @@ The `result` var will be of type `tuple[Address, ...]`
28
32
  Once the `AddressModel` class is created, we will not only be able to access all the information in that table, but also all the information in all the tables that have foreign keys related to it."
29
33
 
30
34
  ```python
31
- from src.ormmysql.common.enums import ConditionType
32
- from src.test.models.address import AddressModel
35
+ from ormlambda import ConditionType
36
+ from models.address import AddressModel
33
37
 
34
38
 
35
39
  result = AddressModel(database).where(lambda x: (x.City.Country, ConditionType.REGEXP, r"^[aA]")).select(
@@ -122,13 +126,13 @@ The easiest way to map your tables is:
122
126
  ```python
123
127
  from datetime import datetime
124
128
 
125
- from src.ormmysql import (
129
+ from ormlambda import (
126
130
  Column,
127
131
  Table,
128
132
  BaseModel,
129
133
  ForeignKey,
130
134
  )
131
- from src.ormmysql.common.interfaces import IStatements_two_generic, IRepositoryBase
135
+ from ormlambda.common.interfaces import IStatements_two_generic, IRepositoryBase
132
136
 
133
137
 
134
138
  class Country(Table):
@@ -3,19 +3,18 @@ line-length = 320
3
3
 
4
4
  [tool.poetry]
5
5
  name = "ormlambda"
6
- version = "1.0.1"
6
+ version = "1.2.0"
7
7
  description = "ORM designed to interact with the database (currently with MySQL) using lambda functions and nested functions"
8
8
  authors = ["p-hzamora <p.hzamora@icloud.com>"]
9
9
  readme = "README.md"
10
10
 
11
11
  [tool.poetry.dependencies]
12
12
  python = "^3.12"
13
- mysql-connector = "^2.2.9"
14
- ruff = "^0.4.5"
13
+ mysql-connector-python= "^2.2.9"
14
+ fluent-validation = "^3.1.0"
15
15
 
16
16
  [tool.poetry.group.test.dependencies]
17
17
  pandas = "^2.2.2"
18
- mysql-connector = "^2.2.9"
19
18
  ruff = "^0.4.5"
20
19
  python-decouple = "^3.8"
21
20
 
@@ -0,0 +1,10 @@
1
+ # enums
2
+ from .common.enums import ( # noqa: F401
3
+ JoinType,
4
+ ConditionType,
5
+ )
6
+
7
+ from .common.interfaces import IRepositoryBase # noqa: F401
8
+ from .utils import Table, Column, ForeignKey # noqa: F401
9
+ from .utils.lambda_disassembler import Disassembler, nameof # 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,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):
@@ -0,0 +1,2 @@
1
+ from .join_type import JoinType # 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: ...
@@ -65,3 +65,7 @@ class IRepositoryBase[T](ABC):
65
65
  def set_config(self, value: dict[str, Any]) -> dict[str, Any]:
66
66
  """Method to update database config"""
67
67
  ...
68
+
69
+ @property
70
+ @abstractmethod
71
+ def database(self) -> Optional[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
@@ -0,0 +1,36 @@
1
+ from typing import Literal, override
2
+ from mysql.connector import errorcode, errors
3
+ from mysql.connector import MySQLConnection
4
+
5
+ from ormlambda import IRepositoryBase
6
+
7
+ TypeExists = Literal["fail", "replace", "append"]
8
+
9
+
10
+ class CreateDatabase:
11
+ def __init__(self, repository: IRepositoryBase[MySQLConnection]) -> None:
12
+ self._repository: IRepositoryBase[MySQLConnection] = repository
13
+
14
+ @override
15
+ @property
16
+ def CLAUSE(self) -> str:
17
+ return "CREATE DATABASE"
18
+
19
+ @override
20
+ def execute(self, name: str, if_exists: TypeExists = "fail") -> None:
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)
36
+ return None