encodeValue method

String encodeValue(
  1. dynamic value,
  2. String? type, {
  3. String? connectionName,
})

Implementation

String encodeValue(value, String? type, {String? connectionName}) {
  if (type == null)
    return encodeValueDefault(value, connectionName: connectionName);
  if (value == null)
    return 'null';

  switch (type) {
    case 'text': case 'string':
      return encodeString(value.toString());

    case 'integer': case 'smallint':
    case 'bigint': case 'serial':
    case 'bigserial': case 'int':
      if (value is int || value is BigInt)
        return encodeNumber(value);
      break;

    case 'real': case 'double':
    case 'num': case 'number':
    case 'numeric': case 'decimal': //Work only for smaller precision
      if (value is num || value is BigInt)
        return encodeNumber(value);
      break;

    case 'boolean': case 'bool':
      if (value is bool)
        return value.toString();
      break;

    case 'timestamp': case 'timestamptz': case 'datetime':
      if (value is DateTime)
        return encodeDateTime(value, isDateOnly: false);
      break;

    case 'date':
      if (value is DateTime)
        return encodeDateTime(value, isDateOnly: true);
      break;

    case 'json': case 'jsonb':
      return encodeJson(value);

    case 'array':
      if (value is Iterable)
        return encodeArray(value);
      break;

    case 'bytea':
      if (value is Iterable<int>)
        return encodeBytea(value);
      break;

    default:
      if (type.endsWith('_array'))
        return encodeArray(value, pgType: type.substring(0, type.length - 6));

      final t = type.toLowerCase(); //backward compatible
      if (t != type)
        return encodeValue(value, t, connectionName: connectionName);

      throw _error('Unknown type name: $type.', connectionName);
  }

  throw _error('Invalid runtime type and type modifier: '
      '${value.runtimeType} to $type.', connectionName);
}