help_en: 'convert the conceptual model into a physical model for SQL'
help_fr: 'convertit le modèle conceptuel en un modèle physique pour SQL'
help_zh: '将概念模型转换为 SQL 的物理模型'
stem_suffix: '_ddl'
extension: 'sql'
to_defer: false
highlight: 'sql'
transform_attribute:
  - order: 100
    comment: 'Ensure there are only letters, digits and underscores in the label.'
    search: '(?u)\W+'
    replace: '_'
transform_relation_name:
  - order: 100
    comment: 'Ensure there are only letters, digits and underscores in the label.'
    search: '(?u)\W+'
    replace: '_'
transform_datatype:
  - order: 100
    comment: 'Fill in empty data types with the default one.'
    search: '^\s*$'
    replace: 'VARCHAR(42)'
  - order: 200
    search: 'BOOLEAN_PLACEHOLDER'
    replace: 'BOOLEAN'
  - order: 300
    search: 'UNSIGNED_INT_PLACEHOLDER'
    replace: 'UNSIGNED INT'
transform_optionality:
  - order: 100
    comment: 'Replace BANG with NOT NULL.'
    search: '^!$'
    replace: ' NOT NULL'
  - order: 200
    comment: 'Replace QMARK with NULL.'
    search: '^\?$'
    replace: ' NULL'
label_role_separator: '_'
compose_primary_key: '{label}{filler}{datatype}{optionality}\nPRIMARY KEY ({label})'
compose_normal_attribute: '{label}{filler}{datatype}{optionality}'
compose_foreign_key: '{label}{filler}{datatype}{optionality}\nFOREIGN KEY ({label}) REFERENCES {outer_source} ({non_disambiguated_label})'
compose_primary_foreign_key: '{label}{filler}{datatype}{optionality}\nPRIMARY KEY ({label})\nFOREIGN KEY ({label}) REFERENCES {outer_source} ({non_disambiguated_label})'
add_unicity_constraints:
  - order: 100
    search: '$'
    replace: '\nCONSTRAINT {this_relation_name}_u{unicities} UNIQUE ({label})'
column_separator: '\n  '
compose_relation: 'CREATE TABLE {this_relation_name} (\n  {columns}\n);\n'
transform_relation:
  - order: 100
    comment: 'Move the primary keys to the end of the table.'
    search: '(?sm)^(PRIMARY KEY [^\n]+)\n(.*)^\)'
    replace: '\2  \1\n)'
    iterated: true
  - order: 200
    comment: 'Concatenate them.'
    search: '(?m)^(  PRIMARY KEY )\((.+)\)\n\1\((.+)\)'
    replace: '\1(\2, \3)'
    iterated: true
  - order: 300
    comment: 'Move the resulting clause to the beginning of the table.'
    search: '(?m)^(CREATE TABLE .+\n)((?:.+\n)*)(  PRIMARY KEY .+\n)'
    replace: '\1\3\2'
  - order: 1100
    comment: 'Move the foreign keys to the end of the table.'
    search: '(?sm)^(FOREIGN KEY [^\n]+)\n(.*)^\)'
    replace: '\2  \1\n)'
    iterated: true
  - order: 1200
    comment: 'Concatenate them when the reference tables are the same and the foreign column are distinct.'
    search: '(?sm)^(  FOREIGN KEY )\(([^\n]+)\) (REFERENCES [^\n]+ )\(([^\n]+)\)\n(.*?)^\1\(([^\n]+)\) \3\((?!\4)([^\n]+)\)\n'
    replace: '\1(\2, \6) \3(\4, \7)\n\5'
    iterated: true
  - order: 2100
    comment: 'Move the unique constraints to the end of the table.'
    search: '(?sm)^(CONSTRAINT [^\n]+ UNIQUE [^\n]+)\n(.*)^\)'
    replace: '\2  \1\n)'
    iterated: true
  - order: 2200
    comment: 'Explode the composite constraints, e.g. _u123.'
    search: '(?m)^(  CONSTRAINT .+)_u(\d)(\d+)( UNIQUE .+\n)'
    replace: '\1_u\2\4\1_u\3\4'
    iterated: true
  - order: 2300
    comment: 'Concatenate them.'
    search: '(?sm)^(  CONSTRAINT [^\n]+ UNIQUE )\(([^\n]+)\)\n(.*?)^\1\(([^\n]+)\)\n'
    replace: '\1(\2, \4)\n\3'
    iterated: true
  - order: 2400
    comment: 'Suppress their number, let the system name them.'
    search: '(?m)^  CONSTRAINT [^\n]+ (UNIQUE .+)'
    replace: '  \1'
compose_relational_schema: '-- Generated by Mocodo {version}\n\n{relations}\n'
transform_relational_schema:
  - order: 1000
    comment: 'Move all foreign keys constraints to the end of the document.'
    search: '(?m)(^CREATE TABLE ([^\n]+) \(\n(?:  .+\n)*)  (FOREIGN KEY .+)\n((?:.*\n)*)'
    replace: '\1\4ALTER TABLE \2 ADD \3;\n'
    iterated: true
  - order: 1100
    comment: 'Group the foreign key constraints by table'
    search: '(?m)^((ALTER TABLE .+?) ADD .+\n)(?!\2)'
    replace: '\1\n'
  - order: 10000
    comment: 'Add a newline after the last "^);" followed by a non-empty line.'
    search: '(?ms)(.+^\);\n)(?!\n)'
    replace: '\1\n'
  - order: 10100
    comment: 'Add a trailing comma to every line starting with two spaces and followed by two spaces.'
    search: '(\n  \S.+)(?=\n  \S)'
    replace: '\1,'
