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

Decoder for numeric / decimal SQL Data type #7

Merged
merged 3 commits into from
Jul 13, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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

## Unreleased

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

## 2.3.2

- Expose `ColumnDescription.typeId`.
Expand Down
29 changes: 29 additions & 0 deletions lib/src/binary_codec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,9 @@ class PostgresBinaryDecoder extends Converter<Uint8List, dynamic> {
return DateTime.utc(2000)
.add(Duration(microseconds: buffer.getInt64(0)));

case PostgreSQLDataType.numeric:
return _decodeNumeric(value);

case PostgreSQLDataType.date:
return DateTime.utc(2000).add(Duration(days: buffer.getInt32(0)));

Expand Down Expand Up @@ -425,6 +428,7 @@ class PostgresBinaryDecoder extends Converter<Uint8List, dynamic> {
return decoded;
}

/// See: https://github.com/postgres/postgres/blob/master/src/include/catalog/pg_type.dat
static final Map<int, PostgreSQLDataType> typeMap = {
16: PostgreSQLDataType.boolean,
17: PostgreSQLDataType.byteArray,
Expand All @@ -444,8 +448,33 @@ class PostgresBinaryDecoder extends Converter<Uint8List, dynamic> {
1082: PostgreSQLDataType.date,
1114: PostgreSQLDataType.timestampWithoutTimezone,
1184: PostgreSQLDataType.timestampWithTimezone,
1700: PostgreSQLDataType.numeric,
2950: PostgreSQLDataType.uuid,
3802: PostgreSQLDataType.jsonb,
3807: PostgreSQLDataType.jsonbArray,
};

/// Decode numeric / decimal to String without loosing precision.
/// See encoding: https://github.com/postgres/postgres/blob/0e39a608ed5545cc6b9d538ac937c3c1ee8cdc36/src/backend/utils/adt/numeric.c#L305
/// See implementation: https://github.com/charmander/pg-numeric/blob/0c310eeb11dc680dffb7747821e61d542831108b/index.js#L13
static String _decodeNumeric(Uint8List value) {
final reader = ByteDataReader()..add(value);
final nDigits = reader.readInt16(); // non-zero digits, data buffer length = 2 * nDigits
var weight = reader.readInt16(); // weight of first digit
final signByte = reader.readInt16(); // NUMERIC_POS, NEG, NAN, PINF, or NINF
final dScale = reader.readInt16(); // display scale
if (signByte == 0xc000) return 'NaN';
final sign = signByte == 0x4000 ? '-' : '';
var intPart = '';
var fractPart = '';
for (var i = 0; i < nDigits; i++) {
if (weight >= 0) {
intPart += reader.readInt16().toString().padLeft(4, '0');
} else {
fractPart += reader.readInt16().toString().padLeft(4, '0');
}
weight--;
}
return '$sign${intPart.replaceAll(RegExp(r'^0+'), '')}.${fractPart.padRight(dScale, '0').substring(0, dScale)}';
}
}
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,
'numeric': PostgreSQLDataType.numeric,
'jsonb': PostgreSQLDataType.jsonb,
'bytea': PostgreSQLDataType.byteArray,
'name': PostgreSQLDataType.name,
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.numeric:
return 'numeric';
case PostgreSQLDataType.date:
return 'date';
case PostgreSQLDataType.jsonb:
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 [List<int>]
numeric,

/// Must be a [DateTime] (contains year, month and day only)
date,

Expand Down
19 changes: 19 additions & 0 deletions test/decode_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import 'dart:typed_data';

import 'package:postgres/postgres.dart';
import 'package:postgres/src/binary_codec.dart';
import 'package:test/test.dart';

void main() {
Expand Down Expand Up @@ -167,4 +170,20 @@ void main() {
[null]
]);
});

test('Decode Numeric to String', () {
// -123400000.2
final binary1 = [0, 4, 0, 2, 64, 0, 0, 5, 0, 1, 9, 36, 0, 0, 7, 208];

// -123400001.01234
final binary2 = [0, 5, 0, 2, 64, 0, 0, 5, 0, 1, 9, 36, 0, 1, 0, 0, 7, 208];

final decoder = PostgresBinaryDecoder(1700);
final uint8List1 = Uint8List.fromList(binary1);
final uint8List2 = Uint8List.fromList(binary2);
final res1 = decoder.convert(uint8List1);
final res2 = decoder.convert(uint8List2);
expect(res1, '-123400000.20000');
expect(res2, '-123400001.00002');
});
}