Skip to content

Commit 1d74c21

Browse files
authored
Merge pull request isoos#10 from lslv1243/master
add support for interval
2 parents 9f66256 + dd904fc commit 1d74c21

File tree

7 files changed

+49
-1
lines changed

7 files changed

+49
-1
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+
## 2.4.1
4+
5+
- Support for type `interval`, [#10](https://github.com/isoos/postgresql-dart/pull/10).
6+
37
## 2.4.0
48

59
- Support for type `numeric` / `decimal` ([#7](https://github.com/isoos/postgresql-dart/pull/7), [#9](https://github.com/isoos/postgresql-dart/pull/9)).

lib/src/binary_codec.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,18 @@ class PostgresBinaryEncoder extends Converter<dynamic, Uint8List?> {
148148
'Invalid type for parameter value. Expected: DateTime Got: ${value.runtimeType}');
149149
}
150150

151+
case PostgreSQLDataType.interval:
152+
{
153+
if (value is Duration) {
154+
final bd = ByteData(16);
155+
bd.setInt64(0, value.inMicroseconds);
156+
// ignoring the second 8 bytes
157+
return bd.buffer.asUint8List();
158+
}
159+
throw FormatException(
160+
'Invalid type for parameter value. Expected: Duration Got: ${value.runtimeType}');
161+
}
162+
151163
case PostgreSQLDataType.numeric:
152164
{
153165
if (value is double || value is int) {
@@ -422,6 +434,12 @@ class PostgresBinaryDecoder extends Converter<Uint8List, dynamic> {
422434
return DateTime.utc(2000)
423435
.add(Duration(microseconds: buffer.getInt64(0)));
424436

437+
case PostgreSQLDataType.interval:
438+
{
439+
if (buffer.getInt64(8) != 0) throw UnimplementedError();
440+
return Duration(microseconds: buffer.getInt64(0));
441+
}
442+
425443
case PostgreSQLDataType.numeric:
426444
return _decodeNumeric(value);
427445

@@ -541,6 +559,7 @@ class PostgresBinaryDecoder extends Converter<Uint8List, dynamic> {
541559
1082: PostgreSQLDataType.date,
542560
1114: PostgreSQLDataType.timestampWithoutTimezone,
543561
1184: PostgreSQLDataType.timestampWithTimezone,
562+
1186: PostgreSQLDataType.interval,
544563
1700: PostgreSQLDataType.numeric,
545564
2950: PostgreSQLDataType.uuid,
546565
3802: PostgreSQLDataType.jsonb,

lib/src/query.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ class PostgreSQLFormatIdentifier {
321321
'date': PostgreSQLDataType.date,
322322
'timestamp': PostgreSQLDataType.timestampWithoutTimezone,
323323
'timestamptz': PostgreSQLDataType.timestampWithTimezone,
324+
'interval': PostgreSQLDataType.interval,
324325
'numeric': PostgreSQLDataType.numeric,
325326
'jsonb': PostgreSQLDataType.jsonb,
326327
'bytea': PostgreSQLDataType.byteArray,

lib/src/substituter.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class PostgreSQLFormat {
3737
return 'timestamp';
3838
case PostgreSQLDataType.timestampWithTimezone:
3939
return 'timestamptz';
40+
case PostgreSQLDataType.interval:
41+
return 'interval';
4042
case PostgreSQLDataType.numeric:
4143
return 'numeric';
4244
case PostgreSQLDataType.date:

lib/src/types.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ enum PostgreSQLDataType {
4242
/// Must be a [DateTime] (microsecond date and time precision)
4343
timestampWithTimezone,
4444

45+
/// Must be a [Duration]
46+
interval,
47+
4548
/// Must be a [List<int>]
4649
numeric,
4750

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 statement reuse and binary protocol.
3-
version: 2.4.0
3+
version: 2.4.1
44
homepage: https://github.com/isoos/postgresql-dart
55

66
environment:

test/encoding_test.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,25 @@ void main() {
183183
}
184184
});
185185

186+
test('interval', () async {
187+
await expectInverse(Duration(minutes: 15), PostgreSQLDataType.interval);
188+
await expectInverse(
189+
Duration(days: 1, minutes: 15), PostgreSQLDataType.interval);
190+
await expectInverse(
191+
-Duration(days: 1, seconds: 5), PostgreSQLDataType.interval);
192+
await expectInverse(Duration(days: 365 * 100000, microseconds: 1),
193+
PostgreSQLDataType.interval);
194+
await expectInverse(-Duration(days: 365 * 100000, microseconds: 1),
195+
PostgreSQLDataType.interval);
196+
try {
197+
await conn.query('INSERT INTO t (v) VALUES (@v:interval)',
198+
substitutionValues: {'v': 'not-interval'});
199+
fail('unreachable');
200+
} on FormatException catch (e) {
201+
expect(e.toString(), contains('Expected: Duration'));
202+
}
203+
});
204+
186205
test('numeric', () async {
187206
final binaries = {
188207
'-123400000.20000': [

0 commit comments

Comments
 (0)