Skip to content
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

add support for interval #10

Merged
merged 6 commits into from
Aug 21, 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.
Jump to
Jump to file
Failed to load files.
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.4.1

- Support for type `interval`, [#10](https://github.com/isoos/postgresql-dart/pull/10).

## 2.4.0

- Support for type `numeric` / `decimal` ([#7](https://github.com/isoos/postgresql-dart/pull/7), [#9](https://github.com/isoos/postgresql-dart/pull/9)).
Expand Down
19 changes: 19 additions & 0 deletions lib/src/binary_codec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,18 @@ class PostgresBinaryEncoder extends Converter<dynamic, Uint8List?> {
'Invalid type for parameter value. Expected: DateTime Got: ${value.runtimeType}');
}

case PostgreSQLDataType.interval:
{
if (value is Duration) {
final bd = ByteData(16);
bd.setInt64(0, value.inMicroseconds);
// ignoring the second 8 bytes
return bd.buffer.asUint8List();
}
throw FormatException(
'Invalid type for parameter value. Expected: Duration Got: ${value.runtimeType}');
}

case PostgreSQLDataType.numeric:
{
if (value is double || value is int) {
Expand Down Expand Up @@ -422,6 +434,12 @@ class PostgresBinaryDecoder extends Converter<Uint8List, dynamic> {
return DateTime.utc(2000)
.add(Duration(microseconds: buffer.getInt64(0)));

case PostgreSQLDataType.interval:
{
if (buffer.getInt64(8) != 0) throw UnimplementedError();
return Duration(microseconds: buffer.getInt64(0));
}

case PostgreSQLDataType.numeric:
return _decodeNumeric(value);

Expand Down Expand Up @@ -541,6 +559,7 @@ class PostgresBinaryDecoder extends Converter<Uint8List, dynamic> {
1082: PostgreSQLDataType.date,
1114: PostgreSQLDataType.timestampWithoutTimezone,
1184: PostgreSQLDataType.timestampWithTimezone,
1186: PostgreSQLDataType.interval,
1700: PostgreSQLDataType.numeric,
2950: PostgreSQLDataType.uuid,
3802: PostgreSQLDataType.jsonb,
Expand Down
1 change: 1 addition & 0 deletions lib/src/query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ class PostgreSQLFormatIdentifier {
'date': PostgreSQLDataType.date,
'timestamp': PostgreSQLDataType.timestampWithoutTimezone,
'timestamptz': PostgreSQLDataType.timestampWithTimezone,
'interval': PostgreSQLDataType.interval,
'numeric': PostgreSQLDataType.numeric,
'jsonb': PostgreSQLDataType.jsonb,
'bytea': PostgreSQLDataType.byteArray,
Expand Down
2 changes: 2 additions & 0 deletions lib/src/substituter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class PostgreSQLFormat {
return 'timestamp';
case PostgreSQLDataType.timestampWithTimezone:
return 'timestamptz';
case PostgreSQLDataType.interval:
return 'interval';
case PostgreSQLDataType.numeric:
return 'numeric';
case PostgreSQLDataType.date:
Expand Down
3 changes: 3 additions & 0 deletions lib/src/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ enum PostgreSQLDataType {
/// Must be a [DateTime] (microsecond date and time precision)
timestampWithTimezone,

/// Must be a [Duration]
interval,

/// Must be a [List<int>]
numeric,

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: postgres
description: PostgreSQL database driver. Supports statement reuse and binary protocol.
version: 2.4.0
version: 2.4.1
homepage: https://github.com/isoos/postgresql-dart

environment:
Expand Down
19 changes: 19 additions & 0 deletions test/encoding_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,25 @@ void main() {
}
});

test('interval', () async {
await expectInverse(Duration(minutes: 15), PostgreSQLDataType.interval);
await expectInverse(
Duration(days: 1, minutes: 15), PostgreSQLDataType.interval);
await expectInverse(
-Duration(days: 1, seconds: 5), PostgreSQLDataType.interval);
await expectInverse(Duration(days: 365 * 100000, microseconds: 1),
PostgreSQLDataType.interval);
await expectInverse(-Duration(days: 365 * 100000, microseconds: 1),
PostgreSQLDataType.interval);
try {
await conn.query('INSERT INTO t (v) VALUES (@v:interval)',
substitutionValues: {'v': 'not-interval'});
fail('unreachable');
} on FormatException catch (e) {
expect(e.toString(), contains('Expected: Duration'));
}
});

test('numeric', () async {
final binaries = {
'-123400000.20000': [
Expand Down