SQLAlchemy Generator

class besser.generators.sql_alchemy.sql_alchemy_generator.SQLAlchemyGenerator(model: DomainModel, output_dir: str = None)[source]

Bases: GeneratorInterface

SQLAlchemyGenerator is a class that implements the GeneratorInterface and is responsible for generating SQLAlchemy code based on B-UML models.

Parameters:
  • model (DomainModel) – An instance of the DomainModel class representing the B-UML model.

  • output_dir (str, optional) – The output directory where the generated code will be saved. Defaults to None.

RESERVED_NAMES = {'Base', 'Boolean_', 'Column_', 'DateTime_', 'Date_', 'Enum', 'Float_', 'ForeignKey_', 'Integer_', 'List_', 'Mapped_', 'Optional_', 'String_', 'Table_', 'Text_', 'Time_', 'enum'}
TYPES = {'bool': 'Boolean_', 'date': 'Date_', 'datetime': 'DateTime_', 'float': 'Float_', 'int': 'Integer_', 'str': 'String_(100)', 'time': 'Time_'}
VALID_DBMS = {'mariadb', 'mssql', 'mysql', 'oracle', 'postgresql', 'sqlite'}
_abc_impl = <_abc._abc_data object>
generate(dbms: str = 'sqlite')[source]

Generates SQLAlchemy code based on the provided B-UML model and saves it to the specified output directory. If the output directory was not specified, the code generated will be stored in the <current directory>/output folder.

Parameters:
  • dbms (str, optional) – The database management system to be used. Values allowed:

  • "sqlite"

  • "postgresql"

  • "mysql"

  • "mssql"

  • "mariadb"

  • "sqlite". (or "oracle". Defaults to)

Returns:

None, but stores the generated code as a file named sql_alchemy.py

get_concrete_table_inheritance()[source]

Determines if the model uses concrete table inheritance.

Returns:

A list of class parents that use concrete table inheritance.

Return type:

list

get_ids()[source]

Returns a dictionary with the class names as keys and the id attributes as values.

separate_classes()[source]

Separates regular classes from association classes in the model.

Returns:

A tuple containing two lists (regular_classes, association_classes)

Return type:

tuple

validate_model()[source]

Validates that the model doesn’t use reserved names for classes, enumerations, or attributes.

Raises:

ValueError – If any reserved names are found in the model.