A Bind packet is sent by the database client to connect a previously defined prepared statement to a cursor (called a portal in Postgres, for some reason), and provide values for the prepared statement's parameters.
String packetType: "Bind"
String destinationPortal: the name of the portal/cursor to which the statement should be bound. Can be an empty string to indicate the default portal.
String preparedStatementName: the name of the prepared statement. This can be an empty string to indicate the default prepared statement. This cannot be null.
List<byte[]> parameters: the raw values of the parameters for the prepared statement. This list can be modified, though the number of values should normally not be changed.
List<int> parameterFormat: the format of all the parameters, which is either zero (text) or one (binary). This list can be modified but that would be unusual.
List<int> resultColumnFormats: the format for the result set columns, either zero (text) or one (binary). If not specified, the text format will be used.
object getParameter(int idx): returns the value of the parameter. The index is zero-based.
object getJavaParameter(int idx): returns the Java value of the parameter. The index is zero-based. This is exactly the same as getParameter, except for parameters of type decimal or numeric, for which a Java object of type BigDecimal will be returned.
void setParameter(int idx, object value): sets the value of the parameter.
void setJavaParameter(int idx, object value): sets the value of the parameter to a Java object. This is useful only for parameters of type decimal or numeric. See examples below.
To change the value of a parameter:
let paramValue = context.packet.getParameter(2);
if (paramValue === "Canada") {
context.packet.setParameter(3, 883);
}
For parameters of type numeric or decimal, if you don't want to run the risk of precision loss:
let paramValue = context.packet.getJavaParameter(3);
const BigDecimal = Java.type("java.math.BigDecimal");
const rate = new BigDecimal("0.1015");
paramValue = paramValue.multiply(rate);
context.packet.setJavaParameter(3, paramValue);
For parameters of type numeric or decimal, setting an exact value:
const BigDecimal = Java.type("java.math.BigDecimal");
let bd = new BigDecimal("987654321987654321987654321987654321.123456789123456789123456789");
context.packet.setJavaParameter(3, bd);