Skip to content

[Enhancement] Nullsafety #153

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

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
c62f501
initial migration
j4qfrost Feb 23, 2021
ec523df
update travis
j4qfrost Feb 23, 2021
1ebb99f
update sdk requirement
j4qfrost Feb 23, 2021
850f475
change error type from dynamic to Object
j4qfrost Feb 23, 2021
ee5eb44
update tests and set longer timeout
j4qfrost Feb 23, 2021
57d6e64
nullable column description
j4qfrost Feb 23, 2021
603fa18
allow null key
j4qfrost Feb 23, 2021
3e37c29
change to empty stacktrace
j4qfrost Feb 23, 2021
16700fd
revert stacktrace
j4qfrost Feb 23, 2021
c17db31
query null result
j4qfrost Feb 23, 2021
e0fa64f
nullable execute
j4qfrost Feb 23, 2021
be96021
nullable future
j4qfrost Feb 24, 2021
2affed0
oops forgot
j4qfrost Feb 24, 2021
daaedfe
nullable tablename
j4qfrost Feb 24, 2021
430ceb1
overwritten reuse bool error
j4qfrost Feb 24, 2021
e8026d8
encoding empty string
j4qfrost Feb 24, 2021
17bac0c
encoding empty string
j4qfrost Feb 24, 2021
0a981d7
more encoding decoding stuff
j4qfrost Feb 24, 2021
62381eb
linting
j4qfrost Feb 24, 2021
39e4461
consistency
j4qfrost Feb 25, 2021
9d0851a
own worst enemy
j4qfrost Feb 25, 2021
e741fa0
change params to nullable and revert port
j4qfrost Feb 28, 2021
473f4df
revert port on tests
j4qfrost Feb 28, 2021
d4b155e
revert and change incrementally
j4qfrost Feb 28, 2021
3fe22bd
nullable sub values
j4qfrost Feb 28, 2021
45b2873
ReadyForQueryMessage transaction
j4qfrost Feb 28, 2021
72db712
forgot to save
j4qfrost Feb 28, 2021
3a45f6c
nullable default substitute
j4qfrost Feb 28, 2021
45fd4bc
changelog bump version
j4qfrost Feb 28, 2021
fd31392
oops forgot
j4qfrost Feb 28, 2021
2232839
nullable param dataTypeStringForDataType
j4qfrost Mar 7, 2021
515ad44
change not caught before inheritance is weird
j4qfrost Mar 7, 2021
15026ca
change not caught before inheritance is weird
j4qfrost Mar 7, 2021
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: dart
sudo: required
dart:
- stable
- beta
addons:
postgresql: "9.6"
services:
Expand Down
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

- Migrate to null safety

## 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