getTableSchemeImpl method

  1. @override
Future<TableScheme?> getTableSchemeImpl(
  1. String table,
  2. TableRelationshipReference? relationship, {
  3. Object? contextID,
})
override

Implementation that returns a TableScheme for table.

  • contextID should be Expando compatible. It informs that other calls to getTableSchemeImpl are in the same context and could have shared internal caches for the same contextID instance.

Implementation

@override
Future<TableScheme?> getTableSchemeImpl(
  String table,
  TableRelationshipReference? relationship, {
  Object? contextID,
}) async {
  var connection = await catchFromPool();

  try {
    //_log.info('getTableSchemeImpl> $table ; relationship: $relationship');

    var sql =
        "SELECT column_name, data_type, column_default, is_updatable FROM information_schema.columns WHERE table_name = '$table'";

    var scheme = await connection.mappedResultsQuery(sql);

    if (scheme.isEmpty) {
      await releaseIntoPool(connection);
      return null;
    }

    var idFieldName = await _findIDField(connection, table, scheme);

    var fieldsTypes = Map<String, Type>.fromEntries(
      scheme.map((e) {
        var k = e['column_name'] as String;
        var v = _toFieldType(e['data_type'] as String);
        return MapEntry(k, v);
      }),
    );

    notifyTableFieldTypes(table, fieldsTypes);

    var fieldsReferencedTables = await _findFieldsReferencedTables(
      connection,
      table,
      contextID: contextID,
    );

    var relationshipTables = await _findRelationshipTables(
      connection,
      table,
      idFieldName,
      contextID: contextID,
    );

    var constraints = await _findConstraints(connection, table, fieldsTypes);

    await releaseIntoPool(connection);

    var tableScheme = TableScheme(
      table,
      relationship: relationship != null,
      idFieldName: idFieldName,
      fieldsTypes: fieldsTypes,
      constraints: constraints,
      fieldsReferencedTables: fieldsReferencedTables,
      relationshipTables: relationshipTables,
    );

    _log.info('$tableScheme');

    return tableScheme;
  } catch (_) {
    await disposePoolElement(connection);
    rethrow;
  }
}