Skip to content

Commit

Permalink
Fix binding bigint values in postgres backend
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 committed May 3, 2024
1 parent 0b332cb commit 6ba049e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
4 changes: 4 additions & 0 deletions extras/drift_postgres/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.2.3

- Fix binding `BigInt` variables.

## 1.2.2

- Fix parameter binding when NULL is provided.
Expand Down
22 changes: 21 additions & 1 deletion extras/drift_postgres/lib/src/pg_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,14 @@ class _BoundArguments {
return switch (value) {
TypedValue() => value,
null => TypedValue(Type.unspecified, null),
int() || BigInt() => TypedValue(Type.bigInteger, value),
int() => TypedValue(Type.bigInteger, value),
String() => TypedValue(Type.text, value),
bool() => TypedValue(Type.boolean, value),
double() => TypedValue(Type.double, value),
List<int>() => TypedValue(Type.byteArray, value),
// Drift's BigInts are also just 64bit, we just support them to
// represent large numbers on the web.
BigInt() => TypedValue(Type.bigInteger, value.rangeCheckedToInt()),
_ => throw ArgumentError.value(value, 'value', 'Unsupported type'),
};
},
Expand Down Expand Up @@ -215,3 +218,20 @@ class _PgVersionDelegate extends DynamicVersionDelegate {
);
}
}

extension on BigInt {
static final _bigIntMinValue64 = BigInt.from(-9223372036854775808);
static final _bigIntMaxValue64 = BigInt.from(9223372036854775807);

int rangeCheckedToInt() {
if (this < _bigIntMinValue64 || this > _bigIntMaxValue64) {
throw ArgumentError.value(
this,
'this',
'Should be in signed 64bit range ($_bigIntMinValue64..=$_bigIntMaxValue64)',
);
}

return toInt();
}
}
2 changes: 1 addition & 1 deletion extras/drift_postgres/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: drift_postgres
description: Postgres implementation and APIs for the drift database package.
version: 1.2.2
version: 1.2.3
repository: https://github.com/simolus3/drift
homepage: https://drift.simonbinder.eu/docs/platforms/postgres/
issue_tracker: https://github.com/simolus3/drift/issues
Expand Down
10 changes: 10 additions & 0 deletions extras/drift_postgres/test/types_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,14 @@ void main() {
.isBiggerOrEqualValue(moonLanding)),
isTrue);
});

test('bigint', () async {
expect(await eval(Variable<BigInt>(BigInt.two)), BigInt.two);

await expectLater(
eval(Variable<BigInt>(BigInt.parse('9223372036854775808'))),
throwsArgumentError,
reason: 'Out of range',
);
});
}

0 comments on commit 6ba049e

Please sign in to comment.