Skip to content

Migration to null-safety. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.3.0-null-safety.0

- Migrate to null safety. (Thanks to [j4qfrost](https://github.com/j4qfrost), [#153](https://github.com/stablekernel/postgresql-dart/pull/153)).

## 2.2.0

- Supporting Unix socket connections. (Thanks to [grillbiff](https://github.com/grillbiff),
Expand Down
38 changes: 20 additions & 18 deletions lib/src/binary_codec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ final _hex = <String>[
'f',
];

class PostgresBinaryEncoder extends Converter<dynamic, Uint8List> {
class PostgresBinaryEncoder extends Converter<dynamic, Uint8List?> {
final PostgreSQLDataType _dataType;

const PostgresBinaryEncoder(this._dataType);

@override
Uint8List convert(dynamic value) {
Uint8List? convert(dynamic value) {
if (value == null) {
return null;
}
Expand Down Expand Up @@ -169,7 +169,7 @@ class PostgresBinaryEncoder extends Converter<dynamic, Uint8List> {
'Invalid type for parameter value. Expected: String Got: ${value.runtimeType}');
}

final hexBytes = (value as String)
final hexBytes = value
.toLowerCase()
.codeUnits
.where((c) => c != _dashUnit)
Expand Down Expand Up @@ -199,9 +199,9 @@ class PostgresBinaryEncoder extends Converter<dynamic, Uint8List> {
}
return outBuffer;
}
default:
throw PostgreSQLException('Unsupported datatype');
}

throw PostgreSQLException('Unsupported datatype');
}
}

Expand All @@ -211,13 +211,13 @@ class PostgresBinaryDecoder extends Converter<Uint8List, dynamic> {
final int typeCode;

@override
dynamic convert(Uint8List value) {
final dataType = typeMap[typeCode];

dynamic convert(Uint8List? value) {
if (value == null) {
return null;
}

final dataType = typeMap[typeCode];

final buffer =
ByteData.view(value.buffer, value.offsetInBytes, value.lengthInBytes);

Expand Down Expand Up @@ -277,16 +277,18 @@ class PostgresBinaryDecoder extends Converter<Uint8List, dynamic> {

return buf.toString();
}
}

// We'll try and decode this as a utf8 string and return that
// for many internal types, this is valid. If it fails,
// we just return the bytes and let the caller figure out what to
// do with it.
try {
return utf8.decode(value);
} catch (_) {
return value;
default:
{
// We'll try and decode this as a utf8 string and return that
// for many internal types, this is valid. If it fails,
// we just return the bytes and let the caller figure out what to
// do with it.
try {
return utf8.decode(value);
} catch (_) {
return value;
}
}
}
}

Expand Down
20 changes: 10 additions & 10 deletions lib/src/client_messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ void _applyStringToBuffer(UTF8BackedString string, ByteDataWriter buffer) {
}

class StartupMessage extends ClientMessage {
final UTF8BackedString _username;
final UTF8BackedString? _username;
final UTF8BackedString _databaseName;
final UTF8BackedString _timeZone;

StartupMessage(String databaseName, String timeZone, {String username})
StartupMessage(String databaseName, String timeZone, {String? username})
: _databaseName = UTF8BackedString(databaseName),
_timeZone = UTF8BackedString(timeZone),
_username = username == null ? null : UTF8BackedString(username);
Expand All @@ -58,15 +58,15 @@ class StartupMessage extends ClientMessage {

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

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

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

buffer.write(UTF8ByteConstants.database);
Expand All @@ -83,7 +83,7 @@ class StartupMessage extends ClientMessage {
}

class AuthMD5Message extends ClientMessage {
UTF8BackedString _hashedAuthString;
UTF8BackedString? _hashedAuthString;

AuthMD5Message(String username, String password, List<int> saltBytes) {
final passwordHash = md5.convert('$password$username'.codeUnits).toString();
Expand All @@ -96,9 +96,9 @@ class AuthMD5Message extends ClientMessage {
@override
void applyToBuffer(ByteDataWriter buffer) {
buffer.writeUint8(ClientMessage.PasswordIdentifier);
final length = 5 + _hashedAuthString.utf8Length;
final length = 5 + _hashedAuthString!.utf8Length;
buffer.writeUint32(length);
_applyStringToBuffer(_hashedAuthString, buffer);
_applyStringToBuffer(_hashedAuthString!, buffer);
}
}

Expand Down Expand Up @@ -157,14 +157,14 @@ class BindMessage extends ClientMessage {
final List<ParameterValue> _parameters;
final UTF8BackedString _statementName;
final int _typeSpecCount;
int _cachedLength;
int _cachedLength = -1;

BindMessage(this._parameters, {String statementName = ''})
: _typeSpecCount = _parameters.where((p) => p.isBinary).length,
_statementName = UTF8BackedString(statementName);

int get length {
if (_cachedLength == null) {
if (_cachedLength == -1) {
var inputParameterElementCount = _parameters.length;
if (_typeSpecCount == _parameters.length || _typeSpecCount == 0) {
inputParameterElementCount = 1;
Expand Down Expand Up @@ -221,7 +221,7 @@ class BindMessage extends ClientMessage {
buffer.writeInt32(-1);
} else {
buffer.writeInt32(p.length);
buffer.write(p.bytes);
buffer.write(p.bytes!);
}
});

Expand Down
Loading