decodeArray method

dynamic decodeArray(
  1. String value,
  2. int pgType, {
  3. String? connectionName,
})

Decodes an array value, value. Each item of it is pgType.

Implementation

decodeArray(String value, int pgType, {String? connectionName}) {
  final len = value.length - 2;
  assert(value.codeUnitAt(0) == $lbrace && value.codeUnitAt(len + 1) == $rbrace);
  if (len <= 0) return [];
  value = value.substring(1, len + 1);

  if (const {_TEXT, _CHAR, _VARCHAR, _NAME}.contains(pgType)) {
    final result = [];
    for (int i = 0; i < len; ++i) {
      if (value.codeUnitAt(i) == $quot) {
        final buf = <int>[];
        for (;;) {
          final cc = value.codeUnitAt(++i);
          if (cc == $quot) {
            result.add(new String.fromCharCodes(buf));
            ++i;
            assert(i >= len || value.codeUnitAt(i) == $comma);
            break;
          }
          if (cc == $backslash) buf.add(value.codeUnitAt(++i));
          else buf.add(cc);
        }
      } else { //not quoted
        for (int j = i;; ++j) {
          if (j >= len || value.codeUnitAt(j) == $comma) {
            final v = value.substring(i, j);
            result.add(v == 'NULL' ? null: v);
            i = j;
            break;
          }
        }
      }
    }
    return result;
  }

  if (const {_JSON, _JSONB}.contains(pgType))
    return jsonDecode('[$value]');

  final result = [];
  for (final v in value.split(','))
    result.add(v == 'NULL' ? null:
        decodeValue(v, pgType, connectionName: connectionName));
  return result;
}