bindParameter function

Object? bindParameter(
  1. Object? value, {
  2. int? position,
})

Translates a user supplied parameter into the representation the PostgreSQL driver expects, inferring types for plain Dart values.

position is used to build helpful error messages.

Implementation

Object? bindParameter(Object? value, {int? position}) {
  if (value is SqlParam) {
    try {
      return postgresDriverValue(value);
    } on TypeError catch (error) {
      throw TikalParameterException(
        'Value of type ${value.value.runtimeType} is not valid for SQL type ${value.type.sqlName}.',
        position: position,
        value: value.value,
        cause: error,
      );
    }
  }

  try {
    if (value == null) {
      throw TikalParameterException(
        'Cannot bind an untyped NULL parameter. '
        'Use SqlParam.nullOf(SqlType.<type>) so PostgreSQL knows the type.',
        position: position,
      );
    }
    final type = SqlType.infer(value);
    if (type == null) {
      throw TikalParameterException(
        'Unsupported parameter type ${value.runtimeType}. '
        'Pass a bool, int, double, String, DateTime, List<int> or wrap the '
        'value in SqlParam.of(...) to choose the PostgreSQL type explicitly.',
        position: position,
        value: value,
      );
    }
    return postgresDriverValue(SqlParam(type, value));
  } on TypeError catch (error) {
    throw TikalParameterException(
      'Could not bind parameter of type ${value.runtimeType}.',
      position: position,
      value: value,
      cause: error,
    );
  }
}