readTables method

Future<List<MssqlTableSchema>> readTables({
  1. Set<String> schemas = const <String>{'dbo'},
  2. bool includeViews = true,
  3. List<String> include = const <String>['*'],
  4. List<String> exclude = const <String>[],
})

Every table, and optionally view, in schemas.

include and exclude match a table's own name or its qualified name — with * standing for any run of characters. Exclusion wins.

Implementation

Future<List<MssqlTableSchema>> readTables({
  Set<String> schemas = const <String>{'dbo'},
  bool includeViews = true,
  List<String> include = const <String>['*'],
  List<String> exclude = const <String>[],
}) async {
  if (schemas.isEmpty) {
    throw ArgumentError.value(
      schemas,
      'schemas',
      'Name at least one schema.',
    );
  }
  final objects = await _readObjects(schemas, includeViews);
  final wanted = objects.where(
    (o) => _matchesTable(o, include) && !_matchesTable(o, exclude),
  );
  final ids = <int, _ObjectRow>{for (final o in wanted) o.objectId: o};
  if (ids.isEmpty) return const <MssqlTableSchema>[];

  final columns = await _readColumns(schemas);
  final keys = await _readPrimaryKeys(schemas);
  final unique = await _readUniqueKeys(schemas);
  final foreign = await _readForeignKeys(schemas);
  final triggers = await _readTriggers(schemas);

  final tables = <MssqlTableSchema>[];
  for (final entry in ids.entries) {
    tables.add(
      MssqlTableSchema(
        schema: entry.value.schema,
        name: entry.value.name,
        isView: entry.value.isView,
        columns: columns[entry.key] ?? const <MssqlColumnSchema>[],
        primaryKey: keys[entry.key],
        foreignKeys: foreign[entry.key] ?? const <MssqlForeignKeySchema>[],
        triggers: triggers[entry.key] ?? const <MssqlTriggerSchema>[],
        uniqueKeys: unique[entry.key] ?? const <MssqlUniqueKeySchema>[],
      ),
    );
  }
  tables.sort((a, b) {
    final bySchema = a.schema.compareTo(b.schema);
    return bySchema != 0 ? bySchema : a.name.compareTo(b.name);
  });
  return tables;
}