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 = "SHOW COLUMNS FROM `$table`";
    var results = await connection.query(sql);

    var scheme = results.toList();

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

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

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

    notifyTableFieldTypes(table, fieldsTypes);

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

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

    await releaseIntoPool(connection);

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

    _log.info('$tableScheme');

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