Skip to content

Commit

Permalink
Use v3 as delegate behind v2 interface. (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos authored Oct 10, 2023
1 parent d022423 commit 7c75c92
Show file tree
Hide file tree
Showing 2 changed files with 218 additions and 0 deletions.
207 changes: 207 additions & 0 deletions lib/src/v2_v3_delegate.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
import 'dart:collection';
import 'dart:convert';

import 'dart:io';

import 'package:postgres/postgres_v3_experimental.dart';
import 'package:postgres/src/client_messages.dart';
import 'package:postgres/src/connection.dart';
import 'package:postgres/src/execution_context.dart';
import 'package:postgres/src/replication.dart';
import 'package:postgres/src/server_messages.dart';

class V3BackedPostgreSQLConnection implements PostgreSQLConnection {
final PgEndpoint _endpoint;
final PgSessionSettings _sessionSettings;
PgConnection? _connection;

V3BackedPostgreSQLConnection(this._endpoint, this._sessionSettings);

@override
void addMessage(ClientMessage message) {
throw UnimplementedError();
}

@override
bool get allowClearTextPassword => throw UnimplementedError();

@override
void cancelTransaction({String? reason}) {
throw UnimplementedError();
}

@override
Future close() async {
await _connection?.close();
_connection = null;
}

@override
String get databaseName => throw UnimplementedError();

@override
Encoding get encoding => throw UnimplementedError();

@override
String get host => throw UnimplementedError();

@override
bool get isClosed => throw UnimplementedError();

@override
bool get isUnixSocket => throw UnimplementedError();

@override
Future<List<Map<String, Map<String, dynamic>>>> mappedResultsQuery(
String fmtString,
{Map<String, dynamic>? substitutionValues = const {},
bool? allowReuse,
int? timeoutInSeconds}) {
throw UnimplementedError();
}

@override
Stream<ServerMessage> get messages => throw UnimplementedError();

@override
Stream<Notification> get notifications => throw UnimplementedError();

@override
Future open() async {
_connection = await PgConnection.open(
_endpoint,
sessionSettings: _sessionSettings,
);
}

@override
String? get password => throw UnimplementedError();

@override
int get port => throw UnimplementedError();

@override
int get processID => throw UnimplementedError();

@override
Future<int> execute(
String fmtString, {
Map<String, dynamic>? substitutionValues = const {},
int? timeoutInSeconds,
}) async {
final rs = await _connection!.execute(
PgSql.map(fmtString),
parameters: substitutionValues,
ignoreRows: true,
);
return rs.affectedRows;
}

@override
Future<PostgreSQLResult> query(
String fmtString, {
Map<String, dynamic>? substitutionValues,
bool? allowReuse,
int? timeoutInSeconds,
bool? useSimpleQueryProtocol,
}) async {
final rs = await _connection!.execute(
PgSql.map(fmtString),
parameters: substitutionValues,
queryMode: (useSimpleQueryProtocol ?? false) ? QueryMode.simple : null,
);
return _PostgreSQLResult(rs, rs.map((e) => _PostgreSQLResultRow(e, e)));
}

@override
int get queryTimeoutInSeconds => throw UnimplementedError();

@override
int get queueSize => throw UnimplementedError();

@override
ReplicationMode get replicationMode => throw UnimplementedError();

@override
Map<String, String> get settings => throw UnimplementedError();

@override
Socket? get socket => throw UnimplementedError();

@override
String get timeZone => throw UnimplementedError();

@override
int get timeoutInSeconds => throw UnimplementedError();

@override
Future transaction(
Future Function(PostgreSQLExecutionContext connection) queryBlock,
{int? commitTimeoutInSeconds}) {
throw UnimplementedError();
}

@override
bool get useSSL => throw UnimplementedError();

@override
String? get username => throw UnimplementedError();
}

class _PostgreSQLResult extends UnmodifiableListView<PostgreSQLResultRow>
implements PostgreSQLResult {
final PgResult _result;
_PostgreSQLResult(this._result, super.source);

@override
int get affectedRowCount => _result.affectedRows;

@override
late final columnDescriptions = _result.schema.columns
.map((e) => _ColumnDescription(
e.type.oid ?? 0,
e.tableName ?? '',
e.columnName ?? '',
))
.toList();
}

class _ColumnDescription implements ColumnDescription {
@override
final int typeId;
@override
final String tableName;
@override
final String columnName;

_ColumnDescription(
this.typeId,
this.tableName,
this.columnName,
);
}

class _PostgreSQLResultRow extends UnmodifiableListView
implements PostgreSQLResultRow {
final PgResultRow _row;
_PostgreSQLResultRow(this._row, super.source);

@override
late final columnDescriptions = _row.schema.columns
.map((e) => _ColumnDescription(
e.type.oid ?? 0,
e.tableName ?? '',
e.columnName ?? '',
))
.toList();

@override
Map<String, dynamic> toColumnMap() {
throw UnimplementedError();
}

@override
Map<String, Map<String, dynamic>> toTableColumnMap() {
throw UnimplementedError();
}
}
11 changes: 11 additions & 0 deletions test/docker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:postgres/messages.dart';
import 'package:postgres/postgres_v3_experimental.dart';
import 'package:postgres/src/connection.dart';
import 'package:postgres/src/replication.dart';
import 'package:postgres/src/v2_v3_delegate.dart';
import 'package:stream_channel/stream_channel.dart';
import 'package:test/test.dart';

Expand Down Expand Up @@ -78,6 +79,16 @@ class PostgresServer {
ReplicationMode replicationMode = ReplicationMode.none,
}) async {
final e = await endpoint();

if (Platform.environment['V3'] == '1') {
return V3BackedPostgreSQLConnection(
e,
PgSessionSettings(
onBadSslCertificate: (_) => true,
),
);
}

return PostgreSQLConnection(
e.host,
e.port,
Expand Down

0 comments on commit 7c75c92

Please sign in to comment.