TetherColumnInfo.fromRow constructor

TetherColumnInfo.fromRow(
  1. List row,
  2. String nameConverter(
    1. String
    )
)

Creates a TetherColumnInfo instance from a raw SQL query result row.

The row is a list of values corresponding to column attributes fetched from the database's information schema. The exact indices depend on the SELECT query used. nameConverter transforms the original DB column name to a Dart-friendly one.

Example SELECT statement columns and their typical row indices:

  • column_name (String) -> row[0]
  • data_type (String) -> row[1]
  • is_nullable (String: 'YES'/'NO') -> row[2]
  • column_default (String or null) -> row[3]
  • description (String or null, from pg_description) -> row[4]
  • is_primary_key (bool, from joining with constraints) -> row[5]
  • is_unique (bool, from joining with constraints) -> row[6]
  • is_identity (String: 'YES'/'NO') -> row[7] (Adjust indexOfIsIdentity if different)

Implementation

factory TetherColumnInfo.fromRow(
  List<dynamic> row,
  String Function(String) nameConverter,
) {
  final originalDbName = row[0] as String;

  // --- Determine the correct index for 'is_identity' ---
  // This index MUST match the position of 'is_identity' in the SELECT list
  // of your database schema introspection query.
  // For this example, let's assume it's at index 7.
  const int indexOfIsIdentity = 7; // <<< ADJUST THIS INDEX

  bool isIdentityValue = false;
  if (row.length > indexOfIsIdentity && row[indexOfIsIdentity] is String) {
    // PostgreSQL's information_schema.columns.is_identity is 'YES' or 'NO'
    isIdentityValue =
        (row[indexOfIsIdentity] as String).toUpperCase() == 'YES';
  } else if (row.length > indexOfIsIdentity &&
      row[indexOfIsIdentity] is bool) {
    // In case your query somehow pre-converts it to a boolean
    isIdentityValue = row[indexOfIsIdentity] as bool;
  }
  // Add more checks if other representations are possible

  return TetherColumnInfo(
    name: nameConverter(originalDbName),
    originalName: originalDbName,
    localName: _makeSafeDartIdentifier(originalDbName),
    type: row[1] as String, // Assuming type is at index 1
    isNullable:
        (row[2] as String? ?? 'NO').toUpperCase() ==
        'YES', // Assuming is_nullable is at index 2
    defaultValue: row[3] as String?, // Assuming column_default is at index 3
    comment:
        row[4] as String?, // Assuming comment is at index 4 (if you fetch it)
    isPrimaryKey:
        row[5] as bool? ??
        false, // Assuming is_primary_key is at index 5 (you'd need to join to get this)
    isUnique:
        row[6] as bool? ??
        false, // Assuming is_unique is at index 6 (you'd need to join to get this)
    isIdentity: isIdentityValue,
  );
}