applyToBuffer method

  1. @override
void applyToBuffer(
  1. ByteDataWriter buffer
)
override

Implementation

@override
void applyToBuffer(ByteDataWriter buffer) {
  List<int> encodingString = [];
  if (encoding is Utf8Codec) {
    encodingString = [...utf8.encode('UTF8')];
  } else if (encoding is AsciiCodec) {
    encodingString = [...utf8.encode('ascii')];
  } else if (encoding is Latin1Codec) {
    encodingString = [...utf8.encode('ISO-8859-1')];
  } else if (encoding is Windows1252Codec) {
    encodingString = [...utf8.encode('WIN1252')];
  } else {
    //throw PostgreSQLException('encoding not supported');
    encodingString = [...utf8.encode('UTF8')]; //UTF8ByteConstants.utf8;
  }
  encodingString.add(0);

  var fixedLength = 43; //48
  //var variableLength = _databaseName.byteLength + _timeZone.byteLength + 2;
  var variableLength = _databaseName.byteLength + 1;
  variableLength = variableLength + encodingString.length;

  if (_timeZone != null) {
    variableLength += _timeZone!.byteLength + 1;
  }

  if (_username != null) {
    fixedLength += 5;
    variableLength += _username!.byteLength + 1;
  }

  if (_replication.string != ReplicationMode.none.value) {
    fixedLength += UTF8ByteConstants.replication.length;
    variableLength += _replication.byteLength + 1;
  }

  buffer.writeInt32(fixedLength + variableLength);
  buffer.writeInt32(ClientMessage.ProtocolVersion);

  if (_username != null) {
    buffer.write(UTF8ByteConstants.user);
    _username!.applyToBuffer(buffer);
  }

  if (_replication.string != ReplicationMode.none.value) {
    buffer.write(UTF8ByteConstants.replication);
    _replication.applyToBuffer(buffer);
  }

  buffer.write(UTF8ByteConstants.database);
  _databaseName.applyToBuffer(buffer);

  buffer.write(UTF8ByteConstants.clientEncoding);
  buffer.write(encodingString);

  if (_timeZone != null) {
    buffer.write(UTF8ByteConstants.timeZone);
    _timeZone!.applyToBuffer(buffer);
  }

  buffer.writeInt8(0);
}