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

Simpler API surface, better names, small fixes #358

Merged
merged 7 commits into from
Sep 4, 2024
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
10 changes: 6 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

## 3.4.0

- Allowing custom type codecs to be registered when creating the `TypeRegistry`.
- `TypeCodec` interface is used for encoding/decoding value by OIDs.
Gets a reference to `TypeCodecContext` which contains `encoding` and runtime parameters.
- `TypeEncoderFn` value converter for generic Dart -> Postgres object encoders (where type is not specified as parameter).
**Allowing custom type codecs**:

- `Codec` interface is used for encoding/decoding value by type OIDs or Dart values.
- `Codec.encode` and `Codec.decode` gets a reference to `CodecContext` which provides
access to `encoding`, observed runtime parameters and the `TypeRegistry`.
- `EncoderFn` value converter for generic Dart object -> Postgres-encoded bytes (for values where type is not specified).
- `RelationTracker` tracks information about relations (currently limited to `RelationMessage` caching).
- `RuntimeParameters` to access server-provided parameter status values.

Expand Down
10 changes: 8 additions & 2 deletions lib/postgres.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@ export 'src/exceptions.dart';
export 'src/pool/pool_api.dart';
export 'src/replication.dart';
export 'src/types.dart';
export 'src/types/codec.dart'
show
Codec,
CodecContext,
EncodedValue,
EncoderFn,
EncodingFormat,
RuntimeParameters;
export 'src/types/geo_types.dart';
export 'src/types/range_types.dart';
export 'src/types/text_search.dart'
show TsVector, TsWord, TsWordPos, TsWeight, TsQuery;
export 'src/types/type_codec.dart'
show RuntimeParameters, TypeCodec, TypeCodecContext, TypeEncoderFn;
export 'src/types/type_registry.dart' show TypeRegistry;

/// A description of a SQL query as interpreted by this package.
Expand Down
10 changes: 5 additions & 5 deletions lib/src/buffer.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:convert';

import 'package:buffer/buffer.dart';
import 'package:postgres/src/types/type_codec.dart';
import 'package:postgres/src/types/codec.dart';

/// This class doesn't add much over using `List<int>` instead, however,
/// it creates a nice explicit type difference from both `String` and `List<int>`,
Expand Down Expand Up @@ -43,19 +43,19 @@ class PgByteDataWriter extends ByteDataWriter {
const _emptyString = '';

class PgByteDataReader extends ByteDataReader {
final TypeCodecContext typeCodecContext;
final CodecContext codecContext;

PgByteDataReader({
required this.typeCodecContext,
required this.codecContext,
});

Encoding get encoding => typeCodecContext.encoding;
Encoding get encoding => codecContext.encoding;

String readNullTerminatedString() {
final bytes = readUntilTerminatingByte(0);
if (bytes.isEmpty) {
return _emptyString;
}
return typeCodecContext.encoding.decode(bytes);
return codecContext.encoding.decode(bytes);
}
}
10 changes: 5 additions & 5 deletions lib/src/message_window.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'dart:typed_data';

import 'package:buffer/buffer.dart';
import 'package:charcode/ascii.dart';
import 'package:postgres/src/types/type_codec.dart';
import 'package:postgres/src/types/codec.dart';

import 'buffer.dart';
import 'messages/server_messages.dart';
Expand Down Expand Up @@ -35,11 +35,11 @@ Map<int, _ServerMessageFn> _messageTypeMap = {
};

class MessageFramer {
final TypeCodecContext _typeCodecContext;
late final _reader = PgByteDataReader(typeCodecContext: _typeCodecContext);
final CodecContext _codecContext;
late final _reader = PgByteDataReader(codecContext: _codecContext);
final messageQueue = Queue<ServerMessage>();

MessageFramer(this._typeCodecContext);
MessageFramer(this._codecContext);

int? _type;
int _expectedLength = 0;
Expand Down Expand Up @@ -119,7 +119,7 @@ ServerMessage _parseCopyDataMessage(PgByteDataReader reader, int length) {
return XLogDataMessage.parse(
reader.read(length - 1),
reader.encoding,
typeCodecContext: reader.typeCodecContext,
codecContext: reader.codecContext,
);
} else {
final bb = BytesBuffer();
Expand Down
2 changes: 1 addition & 1 deletion lib/src/messages/client_messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import '../buffer.dart';
import '../replication.dart';
import '../time_converters.dart';
import '../types.dart';
import '../types/type_codec.dart';
import '../types/codec.dart';
import 'shared_messages.dart';

abstract class ClientMessageFormat {
Expand Down
8 changes: 4 additions & 4 deletions lib/src/messages/server_messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:convert';
import 'dart:typed_data';

import 'package:meta/meta.dart';
import 'package:postgres/src/types/type_codec.dart';
import 'package:postgres/src/types/codec.dart';

import '../buffer.dart';
import '../time_converters.dart';
Expand Down Expand Up @@ -374,11 +374,11 @@ class XLogDataMessage implements ReplicationMessage, ServerMessage {
static XLogDataMessage parse(
Uint8List bytes,
Encoding encoding, {
TypeCodecContext? typeCodecContext,
CodecContext? codecContext,
}) {
final reader = PgByteDataReader(
typeCodecContext: typeCodecContext ??
TypeCodecContext.withDefaults(encoding: encoding))
codecContext:
codecContext ?? CodecContext.withDefaults(encoding: encoding))
..add(bytes);
final walStart = LSN(reader.readUint64());
final walEnd = LSN(reader.readUint64());
Expand Down
7 changes: 5 additions & 2 deletions lib/src/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -430,11 +430,14 @@ class TypedValue<T extends Object> {
}

@override
int get hashCode => Object.hash(type, value);
int get hashCode => Object.hash(type, value, isSqlNull);

@override
bool operator ==(Object other) {
return other is TypedValue && other.type == type && other.value == value;
return other is TypedValue &&
other.type == type &&
other.value == value &&
other.isSqlNull == isSqlNull;
}

@override
Expand Down
14 changes: 7 additions & 7 deletions lib/src/types/binary_codec.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import 'dart:convert';
import 'dart:convert' hide Codec;
import 'dart:convert' as convert;
import 'dart:typed_data';

import 'package:buffer/buffer.dart';

import '../buffer.dart';
import '../types.dart';
import 'codec.dart';
import 'geo_types.dart';
import 'range_types.dart';
import 'type_codec.dart';
import 'type_registry.dart';

final _bool0 = Uint8List(1)..[0] = 0;
Expand Down Expand Up @@ -39,7 +40,7 @@ final _trailingZerosRegExp = RegExp(r'0+$');
// that doesn't allocate intermediate strings.
final _jsonUtf8Codec = json.fuse(utf8);

Codec<Object?, List<int>> _jsonFusedEncoding(Encoding encoding) {
convert.Codec<Object?, List<int>> _jsonFusedEncoding(Encoding encoding) {
if (encoding == utf8) {
return _jsonUtf8Codec;
} else {
Expand Down Expand Up @@ -754,8 +755,7 @@ class PostgresBinaryEncoder {
}

class PostgresBinaryDecoder {
static Object? convert(
TypeCodecContext context, int typeOid, Uint8List input) {
static Object? convert(CodecContext context, int typeOid, Uint8List input) {
late final buffer =
ByteData.view(input.buffer, input.offsetInBytes, input.lengthInBytes);

Expand Down Expand Up @@ -1012,7 +1012,7 @@ class PostgresBinaryDecoder {
return result;
}

static (T?, T?, Bounds)? _decodeRange<T>(TypeCodecContext context,
static (T?, T?, Bounds)? _decodeRange<T>(CodecContext context,
ByteData buffer, Uint8List dinput, int elementTypeOid) {
final flag = buffer.getInt8(0);
final bounds = Bounds.fromFlag(flag);
Expand Down Expand Up @@ -1040,7 +1040,7 @@ class PostgresBinaryDecoder {
}

static T _decodeRangeElement<T>(
TypeCodecContext context, int elementTypeOid, Uint8List bytes) {
CodecContext context, int elementTypeOid, Uint8List bytes) {
return convert(context, elementTypeOid, bytes) as T;
}

Expand Down
Loading
Loading