DVRecordTable constructor

DVRecordTable({
  1. required String table,
  2. required String key,
  3. required List<String> columns,
  4. Set<String> sensitive = const <String>{},
  5. Set<String> unique = const <String>{},
  6. DVHistory? history,
  7. bool versioned = true,
  8. bool softDelete = false,
  9. DVCapture? capture,
  10. DVRecordScope? scope,
  11. Map<String, String>? types,
  12. DVDatabaseAdapter? database,
})

Implementation

DVRecordTable({
  required this.table,
  required this.key,
  required List<String> columns,
  Set<String> sensitive = const <String>{},
  Set<String> unique = const <String>{},
  DVHistory? history,
  this.versioned = true,
  this.softDelete = false,
  this.capture,
  this.scope,
  Map<String, String>? types,
  DVDatabaseAdapter? database,
})  : historyPolicy = history,
      columns = List<String>.unmodifiable(columns),
      types = types == null ? null : Map<String, String>.unmodifiable(types),
      sensitive = Set<String>.unmodifiable(sensitive),
      unique = Set<String>.unmodifiable(unique),
      _database = database {
  // Names are interpolated into SQL, so they are checked rather than
  // trusted; a value never is -- values are always bound parameters.
  // The table may be schema-qualified, because schemaPerTenant resolves a
  // model's table to `tenant.orders`; nothing else may be.
  if (!_tableName.hasMatch(table)) {
    throw ArgumentError.value(table, 'table', 'not a plain SQL identifier');
  }
  for (final String name in <String>[key, ...columns]) {
    if (!_identifier.hasMatch(name)) {
      throw ArgumentError.value(name, 'name', 'not a plain SQL identifier');
    }
  }
  if (!columns.contains(key)) {
    throw ArgumentError.value(key, 'key', 'is not one of the columns');
  }
  final DVRecordScope? scoped = scope;
  if (scoped != null && !columns.contains(scoped.column)) {
    throw ArgumentError.value(
        scoped.column, 'scope', 'is not one of the columns');
  }
  for (final String name in <String>{...sensitive, ...unique}) {
    if (!columns.contains(name)) {
      throw ArgumentError.value(name, 'field', 'is not one of the columns');
    }
  }
  final Map<String, String>? declared = this.types;
  if (declared != null) {
    for (final String column in columns) {
      final String? type = declared[column];
      if (type == null) {
        throw ArgumentError.value(column, 'types', 'declares no type for it');
      }
      if (!dvIsSqlType(type)) {
        throw ArgumentError.value(type, 'types', 'is not a SQL type');
      }
    }
    for (final String name in declared.keys) {
      if (!columns.contains(name)) {
        throw ArgumentError.value(name, 'types', 'is not one of the columns');
      }
    }
  }
  capture?.track(this);
}