generateAddUniqueConstraintSQL method

SQLBuilder generateAddUniqueConstraintSQL(
  1. String table,
  2. String name,
  3. List<String> columns, {
  4. String? comment,
})

Adds the unique constraint name over columns to the existing table: ALTER TABLE ... ADD CONSTRAINT ... UNIQUE (...) (as in the CREATE TABLE), or, if the dialect can't add a constraint to a table (SQLDialect.alterTableAddConstraint: SQLite), a CREATE UNIQUE INDEX (the same uniqueness).

Implementation

SQLBuilder generateAddUniqueConstraintSQL(
  String table,
  String name,
  List<String> columns, {
  String? comment,
}) {
  var q = dialect.elementQuote;

  if (!dialect.alterTableAddConstraint) {
    return CreateIndexSQL.columns(
      dialect,
      table,
      columns,
      name,
      q: q,
      unique: true,
    );
  }

  var columnsSQL = columns.map((c) => '$q$c$q').join(', ');

  return AlterTableSQL(dialect, table, [
    SQLEntry(
      'ADD',
      ' ADD CONSTRAINT $q$name$q UNIQUE ($columnsSQL)',
      comment: comment,
    ),
  ], q: q);
}