sqlgen.statement_generator package

Submodules

sqlgen.statement_generator.base module

class sqlgen.statement_generator.base.StatementGenerator

Bases: Generic

cls: type[T]
create(**kwargs: Any) T

generate an instance of the cls model with specified properties

Parameters:

kwargs – the arguments provided by the consumer of the repository to filter, update …

Returns:

an instance of the cls model with specified properties

get_by(*args, options: list[ExecutableOption] = None, load_all: bool = False, property_name: str = None, **kwargs) Select

Generate a SQL Select query with the associated filters/options

Parameters:
  • args – filter arguments for sqlalchemy filter function

  • options – optional options for the query (mostly ofr joinedloads)

  • kwargs – arguments to filter the model to return

  • load_all – load all relationship on the model when doing the query

  • property_name – get a property instead of the object

Returns:

a SQL Select statement with specified options, filter, …

get_by_id(object_id, *args, **kwargs)

generate a SQL select filtering on object_id

Parameters:
  • object_id – the id of the object to filter the query for

  • args – optional additional filters

  • kwargs – additional parameters for get_by

Returns:

a SQL Select statement with specified options, filter, …

sqlgen.statement_generator.constrained module

class sqlgen.statement_generator.constrained.ConstrainedStatementGenerator(**bound_object_ids)

Bases: StatementGenerator, Generic

cls: type[T]
constraints: list[Constraint]
create(safe_constraints: list[Constraint] = None, **kwargs: Any) T

generate an instance of the cls model with specified properties plus the foreign_key=bound_object_id if relationship is direct

Parameters:
  • kwargs – the args of the created model

  • safe_constraints – a list of constraint considered to be safe

Raises:
  • BoundObjectLinkNotSafe – if the foreign_key value is not bound to the bound_model (e.g host is bound to another project)

  • ForeignKeyNotSpecified – if no foreign_key parameters is present to create the model and bound_model relation is not direct (and safe=True is not set)

Returns:

an instance of the cls model with specified properties

get_by(*args, options: list[ExecutableOption] = None, load_all: bool = False, property_name: str = None, **kwargs) Select

Generate the select query and filter it with the bound object id

Parameters:
  • args – filter arguments for sqlalchemy filter function

  • options – optional options for the query (mostly ofr joinedloads)

  • kwargs – arguments to filter the model to return

  • load_all – load all relationship on the model when doing the query

  • property_name – get a property instead of the object

Returns:

a sql statement with specified options, filter, …

validate_constraint(constraint: Constraint, safe_constraints: list[Constraint], **kwargs)

Validate a constraint and return a dict of properties to add to the created model Not Really expected to be called from outside

Parameters:
  • constraint – the constraint to validate

  • safe_constraints – a list of constraint already validated

  • kwargs – the properties of the created model

Raises:
  • ConstraintNotSafe – if a constraint has not been validated yet and there is at least one Constraint that will return the foreign_key needed to validate this constraint (e.g. ProjectBoundWebserverBoundRequestRepository)

  • BoundObjectLinkNotSafe – if a constraint has not been validated, depends on a chain of join and the value of the foreign_key is provided (e.g. ProjectBoundRequestRepository)

  • ForeignKeyNotSpecified – if a constraint has not been validated, depends on a chain of join and the value of the foreign_key is NOT provided.

Returns:

a dict of property_name:property_value to add to the created object

sqlgen.statement_generator.constrained.build_parent_constraint(constraint: Constraint, model: type[DeclarativeBase])

construct a constraint from an existing constraint and filter the joins to only includes those from model onward e.g. Request constraint and Webserver model. this will remove the Request.webserver join e.g. Request constraint and Host model. this will remove the Request.webserver and Webserver.host joins

Parameters:
  • constraint – the constraint to build the new constraint from

  • model – the model to filter the joins for

Returns:

a Constraint model for given model (expected to be used as a ConstrainedStatementGenerator constraint for the given model)

sqlgen.statement_generator.exc module

sqlgen.statement_generator.factories module

sqlgen.statement_generator.factories.make_constrained_statement_generator_class_for(cls: type[T], models_to_join: list[type[DeclarativeBase]]) type[ConstrainedStatementGenerator]

Generate a ConstrainedStatementGenerator class for the given model cls with filters for bounding to the models_to_join

bound model == query will be filtered to only match objects that have a relation with the instance of the model specified at class init (this function generate a class but does not instantiate it)

Parameters:
  • cls – the model to use for the return of ConstrainedStatementGenerator

  • models_to_join – the models to bound for the sql queries

Returns:

a child class of ConstrainedStatementGenerator with cls and constraints set

sqlgen.statement_generator.factories.make_object_bound_statement_generator_class_for(cls: type[S], model_to_join: type[D]) type[ObjectBoundStatementGenerator]

Generate a ObjectBoundStatementGenerator class for the given model cls with filters for bounding to model_to_join

bound model == query will be filtered to only match objects that have a relation with the instance of the model specified at class init (this function generate a class but does not instantiate it)

Parameters:
  • cls – the model to use for the return of ObjectBoundStatementGenerator

  • model_to_join – the model to bound for the requests

Returns:

a child class of ObjectBoundStatementGenerator with cls set and joins to model_to_join

sqlgen.statement_generator.factories.make_statement_generator_class_for(cls: type[T]) type[StatementGenerator]

Generate a StatementGenerator class for the given model

Parameters:

cls – the model to use for the StatementGenerator class

Returns:

a child class of StatementGenerator with cls set

sqlgen.statement_generator.object_bound module

class sqlgen.statement_generator.object_bound.ObjectBoundStatementGenerator(bound_object_id: P)

Bases: StatementGenerator, Generic

Internal Class

Statement Generator filtering on a bound_object_id (specified at object creation) using joins and primary_key (specified by the class) to filter the result

cls: type[M]
create(safe: bool = False, **kwargs: Any) M

generate an instance of the cls model with specified properties plus the foreign_key=bound_object_id if relationship is direct

Parameters:
  • kwargs – the args of the created model

  • safe – a special parameter for forcing the generation even if the bound_model relation is not respected

Raises:
  • BoundObjectLinkNotSafe – if the foreign_key value is not bound to the bound_model (e.g host is bound to another project)

  • ForeignKeyNotSpecified – if no foreign_key parameters is present to create the model and bound_model relation is not direct (and safe=True is not set)

Returns:

an instance of the cls model with specified properties

get_by(*args, options: list[ExecutableOption] = None, load_all: bool = False, property_name: str = None, **kwargs) Select

Generate the select query and filter it with the bound object id

Parameters:
  • args – filter arguments for sqlalchemy filter function

  • options – optional options for the query (mostly ofr joinedloads)

  • kwargs – arguments to filter the model to return

  • load_all – load all relationship on the model when doing the query

  • property_name – get a property instead of the object

Returns:

a sql statement with specified options, filter, …

joins: list[InstrumentedAttribute | Column]

Module contents