tableCreationToSql method

String tableCreationToSql({
  1. bool ifNotExists = false,
  2. String? tableNameOverride,
  3. bool skipIndexes = false,
})

Implementation

String tableCreationToSql({
  bool ifNotExists = false,
  String? tableNameOverride,
  bool skipIndexes = false,
}) {
  final tableName = tableNameOverride ?? name;

  String out = '';

  // Table
  if (ifNotExists) {
    out += 'CREATE TABLE IF NOT EXISTS "$tableName" (\n';
  } else {
    out += 'CREATE TABLE "$tableName" (\n';
  }

  var definitions = <String>[];

  for (var column in columns) {
    definitions.add('    ${column.toSqlFragment()}');
  }

  // Inline Foreign Keys
  // In SQLite, we must define these inside the CREATE TABLE block.
  for (var key in foreignKeys) {
    definitions.add('    ${key.toInlineSql()}');
  }

  out += definitions.join(',\n');
  out += '\n) STRICT;\n';

  if (!skipIndexes) {
    // Indexes
    if (indexes.isNotEmpty) {
      out += '\n';
      out += '-- Indexes\n';
      for (var index in indexes) {
        out += index.toSql(
          tableName: tableName,
          ifNotExists: ifNotExists,
        );
      }
    }
  }

  out += '\n';

  return out;
}