From adb99eccd1c407bc38dd83115e886d8b05eb8d47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Istv=C3=A1n=20So=C3=B3s?= Date: Sat, 23 Dec 2023 01:12:09 +0100 Subject: [PATCH] Support char[acter] type. (#274) --- CHANGELOG.md | 4 ++++ lib/src/types.dart | 3 +++ lib/src/types/binary_codec.dart | 2 ++ lib/src/types/text_codec.dart | 1 + lib/src/types/type_registry.dart | 5 +++++ pubspec.yaml | 3 ++- test/encoding_test.dart | 10 ++++++++++ 7 files changed, 27 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ebde1872..85acb931 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 3.0.5 + +- Support for type `char`/`character`/`bpchar`. + ## 3.0.4 - Fix: SSL connection problem handler. diff --git a/lib/src/types.dart b/lib/src/types.dart index c9f23190..f23d391a 100644 --- a/lib/src/types.dart +++ b/lib/src/types.dart @@ -147,6 +147,9 @@ abstract class Type { /// Used to represent value without any type representation. static const unspecified = UnspecifiedType(); + /// Must be a [String] + static const char = GenericType(TypeOid.char); + /// Must be a [String]. static const text = GenericType(TypeOid.text); diff --git a/lib/src/types/binary_codec.dart b/lib/src/types/binary_codec.dart index a0f3fb98..2dacff52 100644 --- a/lib/src/types/binary_codec.dart +++ b/lib/src/types/binary_codec.dart @@ -92,6 +92,7 @@ class PostgresBinaryEncoder { throw FormatException( 'Invalid type for parameter value. Expected: int Got: ${input.runtimeType}'); } + case TypeOid.char: case TypeOid.name: case TypeOid.text: case TypeOid.varChar: @@ -518,6 +519,7 @@ class PostgresBinaryDecoder { ByteData.view(input.buffer, input.offsetInBytes, input.lengthInBytes); switch (typeOid) { + case TypeOid.char: case TypeOid.name: case TypeOid.text: case TypeOid.varChar: diff --git a/lib/src/types/text_codec.dart b/lib/src/types/text_codec.dart index 4bdc3c59..cea0a470 100644 --- a/lib/src/types/text_codec.dart +++ b/lib/src/types/text_codec.dart @@ -228,6 +228,7 @@ class PostgresTextDecoder { Object? convert(DecodeInput di) { // ignore: unnecessary_cast switch (_typeOid) { + case TypeOid.char: case TypeOid.name: case TypeOid.text: case TypeOid.varChar: diff --git a/lib/src/types/type_registry.dart b/lib/src/types/type_registry.dart index fc409289..f888923f 100644 --- a/lib/src/types/type_registry.dart +++ b/lib/src/types/type_registry.dart @@ -20,6 +20,7 @@ class TypeOid { static const boolean = 16; static const booleanArray = 1000; static const byteArray = 17; + static const char = 1042; static const date = 1082; static const double = 701; static const doubleArray = 1022; @@ -47,6 +48,7 @@ class TypeOid { final _builtInTypes = { Type.unspecified, + Type.char, Type.name, Type.text, Type.varChar, @@ -81,6 +83,9 @@ final _builtInTypeNames = { 'bigint': Type.bigInteger, 'boolean': Type.boolean, 'bytea': Type.byteArray, + 'bpchar': Type.char, + 'char': Type.char, + 'character': Type.char, 'date': Type.date, 'double precision': Type.double, 'float4': Type.real, diff --git a/pubspec.yaml b/pubspec.yaml index dcd536ca..f13354d8 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,9 +1,10 @@ name: postgres description: PostgreSQL database driver. Supports statement reuse and binary protocol and connection pooling. -version: 3.0.4 +version: 3.0.5 homepage: https://github.com/isoos/postgresql-dart topics: - sql + - db - database - postgres diff --git a/test/encoding_test.dart b/test/encoding_test.dart index 800318fd..8c7c21a4 100644 --- a/test/encoding_test.dart +++ b/test/encoding_test.dart @@ -305,6 +305,16 @@ void main() { } }); + test('char', () async { + await expectReversible( + 'char', + [null, 'a'], + negative: 0, + expectedDartType: 'String', + ); + }); + + test('varchar', () async { await expectReversible( 'varchar',