Skip to content

Commit

Permalink
Support char[acter] type. (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos authored Dec 23, 2023
1 parent 0bdc0d6 commit adb99ec
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 3.0.5

- Support for type `char`/`character`/`bpchar`.

## 3.0.4

- Fix: SSL connection problem handler.
Expand Down
3 changes: 3 additions & 0 deletions lib/src/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ abstract class Type<T extends Object> {
/// Used to represent value without any type representation.
static const unspecified = UnspecifiedType();

/// Must be a [String]
static const char = GenericType<String>(TypeOid.char);

/// Must be a [String].
static const text = GenericType<String>(TypeOid.text);

Expand Down
2 changes: 2 additions & 0 deletions lib/src/types/binary_codec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions lib/src/types/text_codec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions lib/src/types/type_registry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -47,6 +48,7 @@ class TypeOid {

final _builtInTypes = <Type>{
Type.unspecified,
Type.char,
Type.name,
Type.text,
Type.varChar,
Expand Down Expand Up @@ -81,6 +83,9 @@ final _builtInTypeNames = <String, Type>{
'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,
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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

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

test('char', () async {
await expectReversible(
'char',
[null, 'a'],
negative: 0,
expectedDartType: 'String',
);
});


test('varchar', () async {
await expectReversible(
'varchar',
Expand Down

0 comments on commit adb99ec

Please sign in to comment.