generateFullCreateTableSQLs method

Future<String> generateFullCreateTableSQLs({
  1. String? title,
  2. bool withDate = true,
  3. bool ifNotExists = true,
  4. bool sortColumns = true,
})

Generate a full text with all the SQLs to create the tables.

Implementation

Future<String> generateFullCreateTableSQLs(
    {String? title,
    bool withDate = true,
    bool ifNotExists = true,
    bool sortColumns = true}) async {
  return generateCreateTableSQLs(
          ifNotExists: ifNotExists, sortColumns: sortColumns)
      .resolveMapped((allSQLs) {
    if (allSQLs.isEmpty) return '';

    var dialect = allSQLs.first.dialect;

    var fullSQL = StringBuffer();

    if (title != null && title.isNotEmpty) {
      var parts = title.split(RegExp(r'\r?\n'));
      for (var l in parts) {
        fullSQL.write('-- $l\n');
      }
    }

    fullSQL.write('-- SQLAdapter: $runtimeType\n');
    fullSQL.write('-- Dialect: ${dialect.name}\n');
    fullSQL.write('-- Generator: BonesAPI/${BonesAPI.VERSION}\n');
    if (withDate) fullSQL.write('-- Date: ${DateTime.now()}\n');
    fullSQL.write('\n');

    for (var s in allSQLs) {
      if (s is CreateTableSQL && s.entityRepository != null) {
        var sqlEntityRepository = s.entityRepository!;
        fullSQL.write(
            '-- Entity: ${sqlEntityRepository.type} @ ${sqlEntityRepository.name}\n\n');
      }

      var sql = s.buildSQL(multiline: s is! AlterTableSQL);
      fullSQL.write('$sql\n\n');
    }

    return fullSQL.toString();
  });
}