package io.vertx.pgclient.impl.codec;
import io.vertx.sqlclient.impl.ParamDesc;
import io.vertx.pgclient.impl.util.Util;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
class PgParamDesc extends ParamDesc {
private final DataType[] paramDataTypes;
PgParamDesc(DataType[] paramDataTypes) {
this.paramDataTypes = paramDataTypes;
}
DataType[] paramDataTypes() {
return paramDataTypes;
}
@Override
public String prepare(List<Object> values) {
if (values.size() != paramDataTypes.length) {
return buildReport(values);
}
for (int i = 0;i < paramDataTypes.length;i++) {
DataType paramDataType = paramDataTypes[i];
Object value = values.get(i);
Object val = DataTypeCodec.prepare(paramDataType, value);
if (val != value) {
if (val == DataTypeCodec.REFUSED_SENTINEL) {
return buildReport(values);
} else {
values.set(i, val);
}
}
}
return null;
}
private String buildReport(List<Object> values) {
return Util.buildInvalidArgsError(values.stream(), Stream.of(paramDataTypes).map(type -> type.decodingType));
}
@Override
public String toString() {
return "PgParamDesc{" +
"paramDataTypes=" + Arrays.toString(paramDataTypes) +
'}';
}
}