Skip to content

[Enhancement] Add support for additonal types which are created via extensions e.g Geospatial objects. #139

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

Closed
wants to merge 3 commits into from
Closed
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
32 changes: 16 additions & 16 deletions lib/src/binary_codec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -290,20 +290,20 @@ class PostgresBinaryDecoder extends Converter<Uint8List, dynamic> {
}
}

static final Map<int, PostgreSQLDataType> typeMap = {
16: PostgreSQLDataType.boolean,
17: PostgreSQLDataType.byteArray,
19: PostgreSQLDataType.name,
20: PostgreSQLDataType.bigInteger,
21: PostgreSQLDataType.smallInteger,
23: PostgreSQLDataType.integer,
25: PostgreSQLDataType.text,
700: PostgreSQLDataType.real,
701: PostgreSQLDataType.double,
1082: PostgreSQLDataType.date,
1114: PostgreSQLDataType.timestampWithoutTimezone,
1184: PostgreSQLDataType.timestampWithTimezone,
2950: PostgreSQLDataType.uuid,
3802: PostgreSQLDataType.json,
};
// static final Map<int, PostgreSQLDataType> typeMap = {
// 16: PostgreSQLDataType.boolean,
// 17: PostgreSQLDataType.byteArray,
// 19: PostgreSQLDataType.name,
// 20: PostgreSQLDataType.bigInteger,
// 21: PostgreSQLDataType.smallInteger,
// 23: PostgreSQLDataType.integer,
// 25: PostgreSQLDataType.text,
// 700: PostgreSQLDataType.real,
// 701: PostgreSQLDataType.double,
// 1082: PostgreSQLDataType.date,
// 1114: PostgreSQLDataType.timestampWithoutTimezone,
// 1184: PostgreSQLDataType.timestampWithTimezone,
// 2950: PostgreSQLDataType.uuid,
// 3802: PostgreSQLDataType.json,
// };
}
49 changes: 49 additions & 0 deletions lib/src/connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'dart:io';
import 'dart:typed_data';

import 'package:buffer/buffer.dart';
import 'package:postgres/postgres.dart';

import 'client_messages.dart';
import 'execution_context.dart';
Expand Down Expand Up @@ -167,6 +168,8 @@ class PostgreSQLConnection extends Object

await connectionComplete.future
.timeout(Duration(seconds: timeoutInSeconds));

await _scanPostgresTypes();
} on TimeoutException catch (e, st) {
final err = PostgreSQLException(
'Failed to connect to database $host:$port/$databaseName failed to connect.');
Expand All @@ -179,6 +182,52 @@ class PostgreSQLConnection extends Object
}
}

Future createExtension(String extensionName) async {
try {
await _query('CREATE EXTENSION IF NOT EXISTS @name',
substitutionValues: {'name': extensionName});
// After creating an extension e.g CREATE EXTENSION postgis or create extension hstore -> the data type is added to pg_types which contains oid and typename from postgres.
// i.e values stored in the PostgresBinaryDecoder.typeMap. To have this driver able to understand any postgres type, scanPostgresTypes should scan database on connecting/after creating and extension.
//
await scanPostgresTypes();
} catch (e, st) {
await _close(e, st);

rethrow;
}
}

Future scanPostgresTypes() => _scanPostgresTypes();

Future _scanPostgresTypes() async {
// fetch oids from database (dynamic values)
final dataTypes = await _connection.query(
'''
select oid::int,typname from pg_type where typname in ('text','int2','int4','int8','float4','float8','bool','date','bytea', 'timestamp','timestamptz','jsonb','name','uuid');
''',
allowReuse: false,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After adding this, the allow_reuse_test passes.

);

final mapped = dataTypes.map((row) {
final oid = row[0] as int;
final typname = row[1] as String;
return MapEntry<String, int>(typname, oid);
});
final _extraDataTypes = <String, int>{};

_extraDataTypes.addEntries(mapped);

typeMap = _extraDataTypes.map((key, value) {
// add boolean since it's not called bool on pg_types
if (key == 'bool') {
return MapEntry<int, PostgreSQLDataType>(
value, PostgreSQLFormatIdentifier.typeStringToCodeMap['boolean']);
}
return MapEntry<int, PostgreSQLDataType>(
value, PostgreSQLFormatIdentifier.typeStringToCodeMap[key]);
});
}

/// Closes a connection.
///
/// After the returned [Future] completes, this connection can no longer be used to execute queries. Any queries in progress or queued are cancelled.
Expand Down
3 changes: 1 addition & 2 deletions lib/src/query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ class Query<T> {
return true;
}

final actualType = PostgresBinaryDecoder
.typeMap[actualParameterTypeCodeIterator.current];
final actualType = typeMap[actualParameterTypeCodeIterator.current];
return actualType == specifiedType;
}).any((v) => v == false);

Expand Down
17 changes: 17 additions & 0 deletions lib/src/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,20 @@ enum PostgreSQLDataType {
/// When returned from database, format will be xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
uuid
}

Map<int, PostgreSQLDataType> typeMap = {
16: PostgreSQLDataType.boolean,
17: PostgreSQLDataType.byteArray,
19: PostgreSQLDataType.name,
20: PostgreSQLDataType.bigInteger,
21: PostgreSQLDataType.smallInteger,
23: PostgreSQLDataType.integer,
25: PostgreSQLDataType.text,
700: PostgreSQLDataType.real,
701: PostgreSQLDataType.double,
1082: PostgreSQLDataType.date,
1114: PostgreSQLDataType.timestampWithoutTimezone,
1184: PostgreSQLDataType.timestampWithTimezone,
2950: PostgreSQLDataType.uuid,
3802: PostgreSQLDataType.json,
};
2 changes: 1 addition & 1 deletion test/encoding_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ Future expectInverse(dynamic value, PostgreSQLDataType dataType) async {
dataType = PostgreSQLDataType.bigInteger;
}
int code;
PostgresBinaryDecoder.typeMap.forEach((key, type) {
typeMap.forEach((key, type) {
if (type == dataType) {
code = key;
}
Expand Down