TableSchema constructor

TableSchema(
  1. String name, {
  2. String? namespace,
  3. required List<Column<Object?>> columns,
  4. List<String> primaryKey = const [],
  5. List<List<String>> uniqueKeys = const [],
  6. List<ForeignKey> foreignKeys = const [],
  7. List<IndexSchema> indexes = const [],
  8. List<CheckSchema> checks = const [],
})

Copies schema collections without opening or altering a database.

Throws SCHEMA.IDENTIFIER for empty table/schema names or names containing dots or NUL, including foreign-key targets, before the metadata can be bound to queries or change tracking.

Implementation

TableSchema(
  this.name, {
  this.namespace,
  required List<Column<Object?>> columns,
  List<String> primaryKey = const [],
  List<List<String>> uniqueKeys = const [],
  List<ForeignKey> foreignKeys = const [],
  List<IndexSchema> indexes = const [],
  List<CheckSchema> checks = const [],
}) : columns = List.unmodifiable(columns),
     clientDefaults = List.unmodifiable(
       columns.where((c) => c.clientDefault != null),
     ),
     primaryKey = List.unmodifiable(primaryKey),
     uniqueKeys = List.unmodifiable(
       uniqueKeys.map(List<String>.unmodifiable),
     ),
     foreignKeys = List.unmodifiable([
       for (final key in foreignKeys)
         ForeignKey(
           List.unmodifiable(key.columns),
           key.target,
           List.unmodifiable(key.targetColumns),
           onDelete: key.onDelete,
           targetNamespace: key.targetNamespace,
         ),
     ]),
     checks = List.unmodifiable(checks),
     indexes = List.unmodifiable([
       for (final index in indexes)
         IndexSchema(
           index.name,
           List.unmodifiable(index.columns),
           unique: index.unique,
         ),
     ]) {
  void checkIdentifier(String name) {
    if (name.isEmpty || name.contains('.') || name.contains('\u0000')) {
      throw const OrmException(
        'SCHEMA.IDENTIFIER',
        'Table and schema names must be non-empty identifiers, without dots or NUL.',
      );
    }
  }

  checkIdentifier(name);
  if (namespace != null) checkIdentifier(namespace!);
  for (final key in this.foreignKeys) {
    checkIdentifier(key.target);
    if (key.targetNamespace != null) checkIdentifier(key.targetNamespace!);
  }
}