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) {
    // 'ascii' não é um nome de encoding do PostgreSQL: a conexão era recusada.
    encodingString = [...utf8.encode('SQL_ASCII')];
  } else if (encoding is Latin1Codec) {
    encodingString = [...utf8.encode('ISO-8859-1')];
  } else if (encoding.name == 'windows-1252') {
    // Ex.: Windows1252Codec do pacote enough_convert.
    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;
  }

  for (final p in _extraParameters) {
    variableLength += p.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);
  }

  for (final p in _extraParameters) {
    p.applyToBuffer(buffer);
  }

  buffer.writeInt8(0);
}