Skip to content

Commit f79e5f8

Browse files
committed
Accept null values as part of the binary List encodings.
1 parent 6491e35 commit f79e5f8

File tree

4 files changed

+59
-43
lines changed

4 files changed

+59
-43
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 3.5.6
4+
5+
- Accept `null` values as part of the binary List encodings.
6+
37
## 3.5.5
48

59
- Better exception message when SSL is not supported.

lib/src/types/binary_codec.dart

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ class PostgresBinaryEncoder {
248248
);
249249
}
250250
throw FormatException(
251-
'Invalid type for parameter value. Expected: List<String> Got: ${input.runtimeType}');
251+
'Invalid type for parameter value. Expected: List<String?> Got: ${input.runtimeType}');
252252
}
253253

254254
case TypeOid.point:
@@ -358,7 +358,7 @@ class PostgresBinaryEncoder {
358358
);
359359
}
360360
throw FormatException(
361-
'Invalid type for parameter value. Expected: List<bool> Got: ${input.runtimeType}');
361+
'Invalid type for parameter value. Expected: List<bool?> Got: ${input.runtimeType}');
362362
}
363363

364364
case TypeOid.smallIntegerArray:
@@ -373,7 +373,7 @@ class PostgresBinaryEncoder {
373373
);
374374
}
375375
throw FormatException(
376-
'Invalid type for parameter value. Expected: List<int> Got: ${input.runtimeType}');
376+
'Invalid type for parameter value. Expected: List<int?> Got: ${input.runtimeType}');
377377
}
378378

379379
case TypeOid.integerArray:
@@ -388,7 +388,7 @@ class PostgresBinaryEncoder {
388388
);
389389
}
390390
throw FormatException(
391-
'Invalid type for parameter value. Expected: List<int> Got: ${input.runtimeType}');
391+
'Invalid type for parameter value. Expected: List<int?> Got: ${input.runtimeType}');
392392
}
393393

394394
case TypeOid.bigIntegerArray:
@@ -403,7 +403,7 @@ class PostgresBinaryEncoder {
403403
);
404404
}
405405
throw FormatException(
406-
'Invalid type for parameter value. Expected: List<int> Got: ${input.runtimeType}');
406+
'Invalid type for parameter value. Expected: List<int?> Got: ${input.runtimeType}');
407407
}
408408

409409
case TypeOid.dateArray:
@@ -419,7 +419,7 @@ class PostgresBinaryEncoder {
419419
);
420420
}
421421
throw FormatException(
422-
'Invalid type for parameter value. Expected: List<DateTime> Got: ${input.runtimeType}');
422+
'Invalid type for parameter value. Expected: List<DateTime?> Got: ${input.runtimeType}');
423423
}
424424

425425
case TypeOid.timeArray:
@@ -434,7 +434,7 @@ class PostgresBinaryEncoder {
434434
);
435435
}
436436
throw FormatException(
437-
'Invalid type for parameter value. Expected: List<Time> Got: ${input.runtimeType}');
437+
'Invalid type for parameter value. Expected: List<Time?> Got: ${input.runtimeType}');
438438
}
439439

440440
case TypeOid.timestampArray:
@@ -450,7 +450,7 @@ class PostgresBinaryEncoder {
450450
);
451451
}
452452
throw FormatException(
453-
'Invalid type for parameter value. Expected: List<DateTime> Got: ${input.runtimeType}');
453+
'Invalid type for parameter value. Expected: List<DateTime?> Got: ${input.runtimeType}');
454454
}
455455

456456
case TypeOid.timestampTzArray:
@@ -466,14 +466,15 @@ class PostgresBinaryEncoder {
466466
);
467467
}
468468
throw FormatException(
469-
'Invalid type for parameter value. Expected: List<DateTime> Got: ${input.runtimeType}');
469+
'Invalid type for parameter value. Expected: List<DateTime?> Got: ${input.runtimeType}');
470470
}
471471

472472
case TypeOid.varCharArray:
473473
{
474474
if (input is List) {
475-
final bytesArray =
476-
_castOrThrowList<String>(input).map((v) => encoding.encode(v));
475+
final bytesArray = _castOrThrowList<String>(input)
476+
.map((v) => v == null ? null : encoding.encode(v))
477+
.toList();
477478
return _writeListBytes<List<int>>(
478479
bytesArray,
479480
1043,
@@ -483,14 +484,15 @@ class PostgresBinaryEncoder {
483484
);
484485
}
485486
throw FormatException(
486-
'Invalid type for parameter value. Expected: List<String> Got: ${input.runtimeType}');
487+
'Invalid type for parameter value. Expected: List<String?> Got: ${input.runtimeType}');
487488
}
488489

489490
case TypeOid.textArray:
490491
{
491492
if (input is List) {
492-
final bytesArray =
493-
_castOrThrowList<String>(input).map((v) => encoding.encode(v));
493+
final bytesArray = _castOrThrowList<String>(input)
494+
.map((v) => v == null ? null : encoding.encode(v))
495+
.toList();
494496
return _writeListBytes<List<int>>(
495497
bytesArray,
496498
25,
@@ -500,7 +502,7 @@ class PostgresBinaryEncoder {
500502
);
501503
}
502504
throw FormatException(
503-
'Invalid type for parameter value. Expected: List<String> Got: ${input.runtimeType}');
505+
'Invalid type for parameter value. Expected: List<String?> Got: ${input.runtimeType}');
504506
}
505507

506508
case TypeOid.doubleArray:
@@ -515,13 +517,14 @@ class PostgresBinaryEncoder {
515517
);
516518
}
517519
throw FormatException(
518-
'Invalid type for parameter value. Expected: List<double> Got: ${input.runtimeType}');
520+
'Invalid type for parameter value. Expected: List<double?> Got: ${input.runtimeType}');
519521
}
520522

521523
case TypeOid.jsonbArray:
522524
{
523525
if (input is List) {
524-
final objectsArray = input.map(_jsonFusedEncoding(encoding).encode);
526+
final objectsArray =
527+
input.map(_jsonFusedEncoding(encoding).encode).toList();
525528
return _writeListBytes<List<int>>(
526529
objectsArray,
527530
3802,
@@ -581,19 +584,19 @@ class PostgresBinaryEncoder {
581584
throw ArgumentError('Cannot encode `$input` into oid($_typeOid).');
582585
}
583586

584-
List<V> _castOrThrowList<V>(List input) {
585-
if (input is List<V>) {
587+
List<V?> _castOrThrowList<V>(List input) {
588+
if (input is List<V?>) {
586589
return input;
587590
}
588-
if (input.any((e) => e is! V)) {
591+
if (input.any((e) => e is! V?)) {
589592
throw FormatException(
590-
'Invalid type for parameter value. Expected: List<${V.runtimeType}> Got: ${input.runtimeType}');
593+
'Invalid type for parameter value. Expected: List<${V.runtimeType}?> Got: ${input.runtimeType}');
591594
}
592-
return input.cast<V>();
595+
return input.cast<V?>();
593596
}
594597

595598
Uint8List _writeListBytes<V>(
596-
Iterable<V> value,
599+
List<V?> value,
597600
int type,
598601
int Function(V item) lengthEncoder,
599602
void Function(PgByteDataWriter writer, V item) valueEncoder,
@@ -608,6 +611,10 @@ class PostgresBinaryEncoder {
608611
writer.writeInt32(1); // index
609612

610613
for (final i in value) {
614+
if (i == null) {
615+
writer.writeInt32(-1);
616+
continue;
617+
}
611618
final len = lengthEncoder(i);
612619
writer.writeInt32(len);
613620
valueEncoder(writer, i);

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: postgres
22
description: PostgreSQL database driver. Supports binary protocol, connection pooling and statement reuse.
3-
version: 3.5.5
3+
version: 3.5.6
44
homepage: https://github.com/isoos/postgresql-dart
55
topics:
66
- sql

test/encoding_test.dart

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,13 @@ void main() {
175175
null,
176176
<DateTime>[],
177177
[
178+
null,
178179
DateTime.utc(1920, 10, 1),
179180
DateTime.utc(2120, 10, 5),
180181
DateTime.utc(2016, 10, 1),
181182
],
182183
],
183-
expectedDartType: 'List<DateTime>',
184+
expectedDartType: 'List<DateTime?>',
184185
);
185186
});
186187

@@ -307,11 +308,12 @@ void main() {
307308
null,
308309
<String>[],
309310
[
311+
null,
310312
'00000000-0000-0000-0000-000000000000',
311313
'12345678-abcd-efab-cdef-012345678901',
312314
],
313315
],
314-
expectedDartType: 'List<String>',
316+
expectedDartType: 'List<String?>',
315317
);
316318
});
317319

@@ -403,10 +405,10 @@ void main() {
403405
[
404406
null,
405407
<bool>[],
406-
[false, true],
408+
[null, false, true],
407409
[true],
408410
],
409-
expectedDartType: 'List<bool>',
411+
expectedDartType: 'List<bool?>',
410412
);
411413
});
412414

@@ -416,10 +418,10 @@ void main() {
416418
[
417419
null,
418420
<int>[],
419-
[-1, 0, 200],
421+
[null, -1, 0, 200],
420422
[-123],
421423
],
422-
expectedDartType: 'List<int>',
424+
expectedDartType: 'List<int?>',
423425
);
424426
});
425427

@@ -429,10 +431,10 @@ void main() {
429431
[
430432
null,
431433
<int>[],
432-
[-1, 0, 200],
434+
[null, -1, 0, 200],
433435
[-123],
434436
],
435-
expectedDartType: 'List<int>',
437+
expectedDartType: 'List<int?>',
436438
);
437439
});
438440

@@ -442,10 +444,10 @@ void main() {
442444
[
443445
null,
444446
<int>[],
445-
[-1, 0, 200],
447+
[null, -1, 0, 200],
446448
[-123],
447449
],
448-
expectedDartType: 'List<int>',
450+
expectedDartType: 'List<int?>',
449451
);
450452
});
451453

@@ -456,12 +458,13 @@ void main() {
456458
null,
457459
<DateTime>[],
458460
[
461+
null,
459462
DateTime.utc(1970),
460463
DateTime.fromMicrosecondsSinceEpoch(12345678, isUtc: true),
461464
DateTime.timestamp()
462465
],
463466
],
464-
expectedDartType: 'List<DateTime>',
467+
expectedDartType: 'List<DateTime?>',
465468
);
466469
});
467470

@@ -472,12 +475,13 @@ void main() {
472475
null,
473476
<DateTime>[],
474477
[
478+
null,
475479
DateTime.utc(1970),
476480
DateTime.fromMicrosecondsSinceEpoch(12345678, isUtc: true),
477481
DateTime.timestamp()
478482
],
479483
],
480-
expectedDartType: 'List<DateTime>',
484+
expectedDartType: 'List<DateTime?>',
481485
);
482486
});
483487

@@ -487,10 +491,10 @@ void main() {
487491
[
488492
null,
489493
<double>[],
490-
[-123.0, 0.0, 1.0],
494+
[null, -123.0, 0.0, 1.0],
491495
[0.001, 45.678],
492496
],
493-
expectedDartType: 'List<double>',
497+
expectedDartType: 'List<double?>',
494498
);
495499
});
496500

@@ -500,10 +504,10 @@ void main() {
500504
[
501505
null,
502506
<String>[],
503-
['', 'foo', 'foo\n', 'foo\nbar;s'],
507+
[null, '', 'foo', 'foo\n', 'foo\nbar;s'],
504508
],
505509
negative: 0,
506-
expectedDartType: 'List<String>',
510+
expectedDartType: 'List<String?>',
507511
);
508512
});
509513

@@ -513,10 +517,10 @@ void main() {
513517
[
514518
null,
515519
<String>[],
516-
['', 'foo', 'foo\n', 'foo\nbar;s'],
520+
[null, '', 'foo', 'foo\n', 'foo\nbar;s'],
517521
],
518522
negative: 0,
519-
expectedDartType: 'List<String>',
523+
expectedDartType: 'List<String?>',
520524
);
521525
});
522526

@@ -603,13 +607,14 @@ void main() {
603607
null,
604608
<Time>[],
605609
[
610+
null,
606611
Time(16, 0, 44, 0, 888),
607612
Time.fromMicroseconds(57644000888),
608613
Time(0, 0, 0, 0, 86400000000),
609614
Time(0),
610615
],
611616
],
612-
expectedDartType: 'List<Time>',
617+
expectedDartType: 'List<Time?>',
613618
);
614619
});
615620

0 commit comments

Comments
 (0)