applyToBuffer method
Implementation
@override
void applyToBuffer(PgByteDataWriter buffer) {
buffer.writeUint8(ClientMessageId.bind);
final portalName = buffer.encodeString(_portalName);
final statementName = buffer.encodeString(_statementName);
final binaryCount = _parameters.where((p) => p?.isBinary ?? false).length;
final isAllBinary = binaryCount == _parameters.length;
final isAllText = binaryCount == 0;
final inputParameterElementCount =
isAllBinary || isAllText ? 1 : _parameters.length;
var length = 14;
length += statementName.bytesLength;
length += portalName.bytesLength;
length += inputParameterElementCount * 2;
length +=
_parameters.fold<int>(0, (len, v) => len + 4 + (v?.bytes?.length ?? 0));
buffer.writeUint32(length);
// Name of portal.
buffer.writeEncodedString(portalName);
// Name of prepared statement.
buffer.writeEncodedString(statementName);
// OK, if we have no specified types at all, we can use 0. If we have all specified types, we can use 1. If we have a mix, we have to individually
// call out each type.
if (isAllBinary) {
buffer.writeUint16(1);
// Apply following format code for all parameters by indicating 1
buffer.writeUint16(ClientMessageFormat.binary);
} else if (isAllText) {
buffer.writeUint16(1);
// Apply following format code for all parameters by indicating 1
buffer.writeUint16(ClientMessageFormat.text);
} else {
// Well, we have some text and some binary, so we have to be explicit about each one
buffer.writeUint16(_parameters.length);
for (final p in _parameters) {
buffer.writeUint16(p != null && p.isBinary
? ClientMessageFormat.binary
: ClientMessageFormat.text);
}
}
// This must be the number of $n's in the query.
buffer.writeUint16(_parameters.length);
for (final p in _parameters) {
final bytes = p?.bytes;
if (bytes == null) {
buffer.writeInt32(-1);
} else {
buffer.writeInt32(bytes.length);
buffer.write(bytes);
}
}
// Result columns - we always want binary for all of them, so specify 1:1.
buffer.writeUint16(1);
buffer.writeUint16(1);
}