python-general-be-lib 0.5.3__py3-none-any.whl → 0.5.5__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.
@@ -5,8 +5,9 @@ from typing import Optional, Any, Type, Iterable, Sequence
5
5
 
6
6
  from pydantic import BaseModel
7
7
  from sqlalchemy import MetaData, Engine, Column, Table, inspect, Connection, select, RowMapping, CursorResult, not_
8
+ from sqlalchemy.exc import NoSuchTableError
8
9
 
9
- from ...exception.crud_exceptions import HasNoAttributeException
10
+ from ...exception.crud_exceptions import HasNoAttributeException, InternalServerException
10
11
 
11
12
 
12
13
  class CrudMetadata:
@@ -158,7 +159,10 @@ class CrudMetadata:
158
159
  def get_table(self, name: str) -> Optional[Table]:
159
160
  table = self._get_table(name=name)
160
161
  if table is None:
161
- table = Table(name, self.metadata, schema=self.schema, autoload_with=self.engine)
162
+ try:
163
+ table = Table(name, self.metadata, schema=self.schema, autoload_with=self.engine)
164
+ except NoSuchTableError:
165
+ raise InternalServerException(f"No such table {name}")
162
166
  return table
163
167
 
164
168
  def has_table(self, name: str):
@@ -217,12 +217,13 @@ class CrudRepository[Entity: Base | Type[Base]]:
217
217
  else:
218
218
  pass
219
219
 
220
- def find(self, session: Session = None, where: dict[str, Any] = None, columns: list[str] = None, exclude_columns: list[str] = None, limit: int = 0, parsing_model: Type[BaseModel] = None, **kwhere) -> list[Entity]:
220
+ def find(self, session: Session = None, where: dict[str, Any] = None, columns: list[str] = None, exclude_columns: list[str] = None, limit: int = 0, parsing_model: Type[BaseModel] = None, order_by: str = None,
221
+ asc: bool = None, **kwhere) -> list[Entity]:
221
222
  """Cerca i record che corrispondono ai criteri specificati.
222
223
 
223
224
  I filtri possono essere passati come dizionario ``where`` o come
224
225
  keyword arguments. Supporta caricamento parziale delle colonne,
225
- limit e modelli di parsing alternativi.
226
+ limit, ordinamento e modelli di parsing alternativi.
226
227
 
227
228
  Args:
228
229
  session (Session, optional): Sessione esterna. Se ``None``, ne viene
@@ -234,6 +235,10 @@ class CrudRepository[Entity: Base | Type[Base]]:
234
235
  limit (int): Numero massimo di risultati. 0 = nessun limite. Default: 0.
235
236
  parsing_model (Type[BaseModel], optional): Modello Pydantic alternativo
236
237
  per la serializzazione del risultato.
238
+ order_by (str, optional): Nome della colonna per l'ordinamento dei risultati.
239
+ Se ``None``, non viene applicato nessun ordinamento.
240
+ asc (bool, optional): Direzione dell'ordinamento. ``True`` per ASC,
241
+ ``False`` per DESC. Se ``None``, l'ordinamento di default è DESC.
237
242
  **kwhere: Filtri aggiuntivi come keyword arguments.
238
243
 
239
244
  Returns:
@@ -243,17 +248,21 @@ class CrudRepository[Entity: Base | Type[Base]]:
243
248
  Example:
244
249
  >>> repo.find(active=True, limit=10)
245
250
  >>> repo.find(where={"role": ["admin", "editor"]})
251
+ >>> repo.find(active=True, order_by="name", asc=True)
246
252
  """
253
+
247
254
  keep_open = False if session is None else True
248
255
  session = self.open_session if session is None else session
249
- entities = self._find(session=session, where=where, columns=columns, exclude_columns=exclude_columns, limit=limit, **kwhere)
256
+ entities = self._find(session=session, where=where, columns=columns, exclude_columns=exclude_columns, limit=limit, order_by=order_by, asc=asc, **kwhere)
250
257
  return self._return(session=session, entities=entities, keep_open=keep_open, commit=False, parsing_model=parsing_model)
251
258
 
252
- def _find(self, session: Session, where: dict[str, Any] = None, columns: list[str] = None, exclude_columns: list[str] = None, limit: int = 0, **kwhere):
259
+ def _find(self, session: Session, where: dict[str, Any] = None, columns: list[str] = None, exclude_columns: list[str] = None, limit: int = 0, order_by: str = None,
260
+ asc: bool = None, **kwhere):
253
261
  if where is None:
254
262
  where = kwhere if kwhere else {}
255
263
  stmt = self._select(entity=self.entity, columns=columns, exclude_columns=exclude_columns)
256
264
  stmt = self.stmt_manager.prepare_statement(stmt=stmt, **where)
265
+ stmt = self.stmt_manager.order_by_condition(stmt=stmt, order_by=order_by, asc=asc)
257
266
  stmt = self.stmt_manager.limit_offset_condition(stmt=stmt, limit=limit)
258
267
  return self.execute(session, stmt)
259
268
 
@@ -500,7 +509,8 @@ class CrudRepository[Entity: Base | Type[Base]]:
500
509
  """
501
510
  pk = self.entity.primary_key()[0]
502
511
  stmt = select(count(pk))
503
- stmt = stmt.where(where)
512
+ if where is not None:
513
+ stmt = stmt.where(where)
504
514
  result = session.execute(stmt).all()[0]
505
515
  return result[0]
506
516
 
@@ -74,10 +74,11 @@ class StatementManager:
74
74
  Raises:
75
75
  HasNoAttributeException: Se la colonna ``order_by`` non esiste sull'entità.
76
76
  """
77
- try:
78
- stmt = stmt.order_by(self._base_handler.columns[order_by].asc()) if asc else stmt.order_by(self._base_handler.columns[order_by].desc())
79
- except KeyError:
80
- raise HasNoAttributeException(order_by)
77
+ if order_by:
78
+ try:
79
+ stmt = stmt.order_by(self._base_handler.columns[order_by].asc()) if asc else stmt.order_by(self._base_handler.columns[order_by].desc())
80
+ except KeyError:
81
+ raise HasNoAttributeException(order_by)
81
82
  return stmt
82
83
 
83
84
  def limit_offset_condition(self, stmt: Select, limit: int, page: int = None):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-general-be-lib
3
- Version: 0.5.3
3
+ Version: 0.5.5
4
4
  Summary: General purpose backend library — SQLAlchemy CRUD/Geometry repositories, FastAPI exceptions, Pydantic base models, logger utilities.
5
5
  Author-email: Andrea Di Placido <a.diplacido@arpes.it>, "Arpes S.r.l." <it.admin@arpes.it>
6
6
  License: MIT
@@ -10,10 +10,10 @@ general/interface/base/__init__.py,sha256=SDlTi28M8rzJ9yiKefo5l7V3K109LnnBHW-SuZ
10
10
  general/interface/base/base_model.py,sha256=SVrYl1kSAj37yCScfO3Qe6ELyLxou65njpvbYmjTG00,2215
11
11
  general/interface/base/declarative_base.py,sha256=5hw2VpJHHebjixuI3d4wpkyAentGBGN54QXcniaoJ-A,5236
12
12
  general/interface/metadata/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
13
- general/interface/metadata/crud_metadata.py,sha256=z1fTh77K7fkhDgNus5Xrv4EYerbdlPVl4F1NLbSxqcM,8478
13
+ general/interface/metadata/crud_metadata.py,sha256=qZLYakASWkA9120HA4n4-9EOb8ZbkIpNip96jwdmjNU,8676
14
14
  general/interface/metadata/geom_metadata.py,sha256=CZPzlqos5XuDWn_FeKLxxYqOuGx1Bg9C0xH5-ZR0vXw,5326
15
15
  general/interface/repository/__init__.py,sha256=EcRukwf61Ua-9-JfM_ingVv1wXNRV8HBrrWwjj8m23w,136
16
- general/interface/repository/crud_repository.py,sha256=j70VKjcVmkFVTZTYA9KQBKSnZpk6P9FkLQDfFeDg8HM,28582
16
+ general/interface/repository/crud_repository.py,sha256=HFWFDaHqcCbI1Ks4gTRDrbG4ih43id4lDIrxWuvx01E,29244
17
17
  general/interface/repository/geometry_repository.py,sha256=bJVPhsr-fGhVTywdHwx9Qt_inWPHk5wqq9EFWFLfuGU,11181
18
18
  general/interface/repository/many_to_many_repository.py,sha256=iEhWyD8nPZSAPL8Z2wXrK-aRBnoh3evaSN-voaf0EP0,8662
19
19
  general/interface/repository/view_repository.py,sha256=Sm591FUP0iBYlI-ULNWszD5fOvTGwXX0iYoeDbUqo34,5397
@@ -23,9 +23,9 @@ general/interface/repository/handler/ilike_handler.py,sha256=_HHTvj6Tm1L-WvUy1S9
23
23
  general/interface/repository/handler/interval_handler.py,sha256=XxMQr6UuWnoCtbOOO7l80_Yja_aPKYtHhDfhFCRMaKg,4135
24
24
  general/interface/repository/handler/json_handler.py,sha256=7bS59seB-It5KHsWXTbHGsEvbQZ6lYyvX-d29BcRLVE,2956
25
25
  general/interface/repository/handler/json_ilike_handler.py,sha256=yDuKER3yqtnHpVWndF8PVEaqDThcq1EhZzLaqxbQ0bU,4491
26
- general/interface/repository/handler/statement_manager.py,sha256=2_AjORmDnKtRaHxZc8RLKe68soNVv2YBRCB5jihgvcY,4392
27
- python_general_be_lib-0.5.3.dist-info/licenses/LICENSE,sha256=iUaO1XZyB9P3Tmog0OILuTisP6vXGe3QKz-4yRTxOFk,1069
28
- python_general_be_lib-0.5.3.dist-info/METADATA,sha256=ppE7Y1Ij9o6lkL9QY0Kn8NLBPfvaFRHFMmmtEnK8tKU,1384
29
- python_general_be_lib-0.5.3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
30
- python_general_be_lib-0.5.3.dist-info/top_level.txt,sha256=tTZePW8_CNUqSgKFd2SEH72ZbnhS0OYjRsgcv0ikSFY,8
31
- python_general_be_lib-0.5.3.dist-info/RECORD,,
26
+ general/interface/repository/handler/statement_manager.py,sha256=sGDMU4fPkF0_9-SbDmAw8Vpel1Z7eImBs8fHO-5Lr_0,4429
27
+ python_general_be_lib-0.5.5.dist-info/licenses/LICENSE,sha256=iUaO1XZyB9P3Tmog0OILuTisP6vXGe3QKz-4yRTxOFk,1069
28
+ python_general_be_lib-0.5.5.dist-info/METADATA,sha256=tlgM7YpM6ii5vsqSp59qp3Bx3VqTkNxSBb0iqgBnoPI,1384
29
+ python_general_be_lib-0.5.5.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
30
+ python_general_be_lib-0.5.5.dist-info/top_level.txt,sha256=tTZePW8_CNUqSgKFd2SEH72ZbnhS0OYjRsgcv0ikSFY,8
31
+ python_general_be_lib-0.5.5.dist-info/RECORD,,