Skip to content

Commit 0971b55

Browse files
authored
Cleanup of PostgreSQLDataType (using PgDataType instead). (#165)
1 parent e2bdea5 commit 0971b55

11 files changed

+210
-217
lines changed

lib/postgres.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
library postgres;
22

3+
import 'package:postgres/src/v3/types.dart';
4+
35
export 'src/connection.dart';
46
export 'src/execution_context.dart';
57
export 'src/replication.dart' show ReplicationMode;
68
export 'src/substituter.dart';
79
export 'src/types.dart';
810
export 'src/v3/types.dart' show PgPoint;
11+
12+
typedef PostgreSQLDataType = PgDataType<Object>;

lib/src/binary_codec.dart

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import 'package:buffer/buffer.dart';
55
import 'package:postgres/src/v3/types.dart';
66

77
import 'buffer.dart';
8-
import 'types.dart';
98

109
final _bool0 = Uint8List(1)..[0] = 0;
1110
final _bool1 = Uint8List(1)..[0] = 1;
@@ -515,56 +514,56 @@ class PostgresBinaryDecoder<T> {
515514
ByteData.view(input.buffer, input.offsetInBytes, input.lengthInBytes);
516515

517516
switch (type) {
518-
case PostgreSQLDataType.name:
519-
case PostgreSQLDataType.text:
520-
case PostgreSQLDataType.varChar:
517+
case PgDataType.name:
518+
case PgDataType.text:
519+
case PgDataType.varChar:
521520
return encoding.decode(input) as T;
522-
case PostgreSQLDataType.boolean:
521+
case PgDataType.boolean:
523522
return (buffer.getInt8(0) != 0) as T;
524-
case PostgreSQLDataType.smallInteger:
523+
case PgDataType.smallInteger:
525524
return buffer.getInt16(0) as T;
526-
case PostgreSQLDataType.serial:
527-
case PostgreSQLDataType.integer:
525+
case PgDataType.serial:
526+
case PgDataType.integer:
528527
return buffer.getInt32(0) as T;
529-
case PostgreSQLDataType.bigSerial:
530-
case PostgreSQLDataType.bigInteger:
528+
case PgDataType.bigSerial:
529+
case PgDataType.bigInteger:
531530
return buffer.getInt64(0) as T;
532-
case PostgreSQLDataType.real:
531+
case PgDataType.real:
533532
return buffer.getFloat32(0) as T;
534-
case PostgreSQLDataType.double:
533+
case PgDataType.double:
535534
return buffer.getFloat64(0) as T;
536-
case PostgreSQLDataType.timestampWithoutTimezone:
537-
case PostgreSQLDataType.timestampWithTimezone:
535+
case PgDataType.timestampWithoutTimezone:
536+
case PgDataType.timestampWithTimezone:
538537
return DateTime.utc(2000)
539538
.add(Duration(microseconds: buffer.getInt64(0))) as T;
540539

541-
case PostgreSQLDataType.interval:
540+
case PgDataType.interval:
542541
{
543542
if (buffer.getInt64(8) != 0) throw UnimplementedError();
544543
return Duration(microseconds: buffer.getInt64(0)) as T;
545544
}
546545

547-
case PostgreSQLDataType.numeric:
546+
case PgDataType.numeric:
548547
return _decodeNumeric(input) as T;
549548

550-
case PostgreSQLDataType.date:
549+
case PgDataType.date:
551550
return DateTime.utc(2000).add(Duration(days: buffer.getInt32(0))) as T;
552551

553-
case PostgreSQLDataType.jsonb:
552+
case PgDataType.jsonb:
554553
{
555554
// Removes version which is first character and currently always '1'
556555
final bytes = input.buffer
557556
.asUint8List(input.offsetInBytes + 1, input.lengthInBytes - 1);
558557
return _jsonFusedEncoding(encoding).decode(bytes) as T;
559558
}
560559

561-
case PostgreSQLDataType.json:
560+
case PgDataType.json:
562561
return _jsonFusedEncoding(encoding).decode(input) as T;
563562

564-
case PostgreSQLDataType.byteArray:
563+
case PgDataType.byteArray:
565564
return input as T;
566565

567-
case PostgreSQLDataType.uuid:
566+
case PgDataType.uuid:
568567
{
569568
final buf = StringBuffer();
570569
for (var i = 0; i < buffer.lengthInBytes; i++) {
@@ -590,39 +589,39 @@ class PostgresBinaryDecoder<T> {
590589
case PgDataType.voidType:
591590
return null;
592591

593-
case PostgreSQLDataType.point:
592+
case PgDataType.point:
594593
return PgPoint(buffer.getFloat64(0), buffer.getFloat64(8)) as T;
595594

596-
case PostgreSQLDataType.booleanArray:
595+
case PgDataType.booleanArray:
597596
return readListBytes<bool>(
598597
input, (reader, _) => reader.readUint8() != 0) as T;
599598

600-
case PostgreSQLDataType.integerArray:
599+
case PgDataType.integerArray:
601600
return readListBytes<int>(input, (reader, _) => reader.readInt32())
602601
as T;
603-
case PostgreSQLDataType.bigIntegerArray:
602+
case PgDataType.bigIntegerArray:
604603
return readListBytes<int>(input, (reader, _) => reader.readInt64())
605604
as T;
606605

607-
case PostgreSQLDataType.varCharArray:
608-
case PostgreSQLDataType.textArray:
606+
case PgDataType.varCharArray:
607+
case PgDataType.textArray:
609608
return readListBytes<String>(input, (reader, length) {
610609
return encoding.decode(length > 0 ? reader.read(length) : []);
611610
}) as T;
612611

613-
case PostgreSQLDataType.doubleArray:
612+
case PgDataType.doubleArray:
614613
return readListBytes<double>(input, (reader, _) => reader.readFloat64())
615614
as T;
616615

617-
case PostgreSQLDataType.jsonbArray:
616+
case PgDataType.jsonbArray:
618617
return readListBytes<dynamic>(input, (reader, length) {
619618
reader.read(1);
620619
final bytes = reader.read(length - 1);
621620
return _jsonFusedEncoding(encoding).decode(bytes);
622621
}) as T;
623622

624-
case PostgreSQLDataType.unknownType:
625-
case PostgreSQLDataType.unspecified:
623+
case PgDataType.unknownType:
624+
case PgDataType.unspecified:
626625
{
627626
// We'll try and decode this as a utf8 string and return that
628627
// for many internal types, this is valid. If it fails,
@@ -660,7 +659,7 @@ class PostgresBinaryDecoder<T> {
660659
}
661660

662661
/// See: https://github.com/postgres/postgres/blob/master/src/include/catalog/pg_type.dat
663-
static final Map<int, PostgreSQLDataType> typeMap = PgDataType.byTypeOid;
662+
static final Map<int, PgDataType> typeMap = PgDataType.byTypeOid;
664663

665664
/// Decode numeric / decimal to String without loosing precision.
666665
/// See encoding: https://github.com/postgres/postgres/blob/0e39a608ed5545cc6b9d538ac937c3c1ee8cdc36/src/backend/utils/adt/numeric.c#L305

lib/src/execution_context.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import 'dart:async';
33
import 'connection.dart';
44
import 'query.dart';
55
import 'substituter.dart';
6-
import 'types.dart';
6+
7+
import 'v3/types.dart';
78

89
abstract class PostgreSQLExecutionContext {
910
/// Returns this context queue size
@@ -22,7 +23,7 @@ abstract class PostgreSQLExecutionContext {
2223
/// connection.query("SELECT * FROM table WHERE id = @idParam:int4", {"idParam" : 2});
2324
///
2425
/// Available types are listed in [PostgreSQLFormatIdentifier.typeStringToCodeMap]. Some types have multiple options. It is preferable to use the [PostgreSQLFormat.id]
25-
/// function to add parameters to a query string. This method inserts a parameter name and the appropriate ':type' string for a [PostgreSQLDataType].
26+
/// function to add parameters to a query string. This method inserts a parameter name and the appropriate ':type' string for a [PgDataType].
2627
///
2728
/// If successful, the returned [Future] completes with a [List] of rows. Each is row is represented by a [List] of column values for that row that were returned by the query.
2829
///

lib/src/query.dart

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import 'connection.dart';
1212
import 'execution_context.dart';
1313
import 'substituter.dart';
1414
import 'text_codec.dart';
15-
import 'types.dart';
1615

1716
class Query<T> {
1817
Query(
@@ -38,7 +37,7 @@ class Query<T> {
3837
final PostgreSQLExecutionContext transaction;
3938
final PostgreSQLConnection connection;
4039

41-
late List<PostgreSQLDataType?> _specifiedParameterTypeCodes;
40+
late List<PgDataType?> _specifiedParameterTypeCodes;
4241
final rows = <List<dynamic>>[];
4342

4443
CachedQuery? cache;
@@ -298,8 +297,7 @@ class FieldDescription implements ColumnDescription {
298297
final formatCode = reader.readUint16();
299298

300299
final converter = PostgresBinaryDecoder(
301-
PostgreSQLDataType.byTypeOid[typeOid] ??
302-
PostgreSQLDataType.unknownType);
300+
PgDataType.byTypeOid[typeOid] ?? PgDataType.unknownType);
303301
return FieldDescription._(
304302
converter, fieldName, tableID, columnID, typeOid,
305303
dataTypeSize, typeModifier, formatCode,
@@ -331,12 +329,12 @@ class PostgreSQLFormatToken {
331329
}
332330

333331
class PostgreSQLFormatIdentifier {
334-
static Map<String, PostgreSQLDataType> typeStringToCodeMap =
335-
PostgreSQLDataType.bySubstitutionName;
332+
static Map<String, PgDataType> typeStringToCodeMap =
333+
PgDataType.bySubstitutionName;
336334

337335
factory PostgreSQLFormatIdentifier(String t) {
338336
String name;
339-
PostgreSQLDataType? type;
337+
PgDataType? type;
340338
String? typeCast;
341339

342340
final components = t.split('::');
@@ -369,6 +367,6 @@ class PostgreSQLFormatIdentifier {
369367
PostgreSQLFormatIdentifier._(this.name, this.type, this.typeCast);
370368

371369
final String name;
372-
final PostgreSQLDataType? type;
370+
final PgDataType? type;
373371
final String? typeCast;
374372
}

lib/src/substituter.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1+
import 'package:postgres/src/v3/types.dart';
2+
13
import 'query.dart';
24
import 'text_codec.dart';
3-
import 'types.dart';
45

56
class PostgreSQLFormat {
67
static final int _atSignCodeUnit = '@'.codeUnitAt(0);
78

8-
static String id(String name, {PostgreSQLDataType? type}) {
9+
static String id(String name, {PgDataType? type}) {
910
if (type != null) {
1011
return '@$name:${dataTypeStringForDataType(type)}';
1112
}
1213

1314
return '@$name';
1415
}
1516

16-
static String? dataTypeStringForDataType(PostgreSQLDataType? dt) {
17+
static String? dataTypeStringForDataType(PgDataType? dt) {
1718
return dt?.nameForSubstitution;
1819
}
1920

lib/src/types.dart

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
5. add identifying key to PostgreSQLFormat.dataTypeStringForDataType
1010
*/
1111

12-
import 'package:postgres/src/v3/types.dart';
13-
14-
typedef PostgreSQLDataType = PgDataType<Object>;
15-
1612
/// LSN is a PostgreSQL Log Sequence Number.
1713
///
1814
/// For more details, see: https://www.postgresql.org/docs/current/datatype-pg-lsn.html

test/decode_test.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'dart:typed_data';
33

44
import 'package:postgres/postgres.dart';
55
import 'package:postgres/src/binary_codec.dart';
6+
import 'package:postgres/src/v3/types.dart';
67
import 'package:test/test.dart';
78

89
import 'docker.dart';
@@ -249,7 +250,7 @@ void main() {
249250
'0.0': [0, 0, 0, 0, 0, 0, 0, 1], // .0 or 0.0
250251
};
251252

252-
final decoder = PostgresBinaryDecoder(PostgreSQLDataType.numeric);
253+
final decoder = PostgresBinaryDecoder(PgDataType.numeric);
253254
binaries.forEach((key, value) {
254255
final uint8List = Uint8List.fromList(value);
255256
final res = decoder.convert(uint8List, utf8);

0 commit comments

Comments
 (0)