@@ -2,6 +2,8 @@ import 'package:clock/clock.dart';
22import 'package:logging/logging.dart' ;
33import 'package:meta/meta.dart' ;
44import 'package:postgres/postgres.dart' ;
5+ // ignore: implementation_imports
6+ import 'package:postgres/src/types/type_registry.dart' show TypeRegistryExt;
57import 'package:postgres_utils/src/config.dart' ;
68import 'package:postgres_utils/src/tables/base_tables.dart' ;
79import 'package:postgres_utils/src/tables/migration_tables.dart' ;
@@ -26,7 +28,7 @@ abstract class OnConflictAction {
2628class DatabaseTransactionBase <TABLES extends TablesBase > {
2729 DatabaseTransactionBase (this ._conn, this .tables);
2830
29- final PostgreSQLExecutionContext _conn;
31+ final TxSession _conn;
3032 final TABLES tables;
3133 static final columnNamePattern = RegExp (r'^[a-z_]+$' );
3234
@@ -155,14 +157,13 @@ class DatabaseTransactionBase<TABLES extends TablesBase> {
155157 _logger.finest ('Executing query: $fmtString with values: $values ' );
156158
157159 final int result;
158- if (useExtendedQuery) {
159- final sqlResult = await _conn.query (fmtString,
160- substitutionValues: values, timeoutInSeconds: timeoutInSeconds);
161- result = sqlResult.affectedRowCount;
162- } else {
163- result = await _conn.execute (fmtString,
164- substitutionValues: values, timeoutInSeconds: timeoutInSeconds);
165- }
160+ final sqlResult = await query (
161+ fmtString,
162+ values: values,
163+ timeoutInSeconds: timeoutInSeconds,
164+ queryMode: useExtendedQuery ? QueryMode .extended : null ,
165+ );
166+ result = sqlResult.affectedRows;
166167 if (expectedResultCount != null && result != expectedResultCount) {
167168 throw StateError (
168169 'Expected result: $expectedResultCount but got $result . '
@@ -176,17 +177,22 @@ class DatabaseTransactionBase<TABLES extends TablesBase> {
176177 }
177178 }
178179
179- Future <PostgreSQLResult > query (String fmtString,
180- {Map <String , Object ?>? values,
181- bool allowReuse = true ,
182- int ? timeoutInSeconds}) async {
180+ Future <Result > query (
181+ String fmtString, {
182+ Map <String , Object ?>? values,
183+ bool allowReuse = true ,
184+ int ? timeoutInSeconds,
185+ QueryMode ? queryMode,
186+ }) async {
183187 assert (_assertCorrectValues (values));
184188 try {
185189 // _logger.finest('QUERY: $fmtString');
186- return _conn.query (fmtString,
187- substitutionValues: values,
188- allowReuse: allowReuse,
189- timeoutInSeconds: timeoutInSeconds);
190+ return _conn.execute (Sql .named (fmtString),
191+ parameters: values,
192+ queryMode: queryMode,
193+ timeout: timeoutInSeconds == null
194+ ? null
195+ : Duration (seconds: timeoutInSeconds));
190196 } catch (e, stackTrace) {
191197 _logger.warning (
192198 'Error while running statement $fmtString ' , e, stackTrace);
@@ -196,29 +202,30 @@ class DatabaseTransactionBase<TABLES extends TablesBase> {
196202}
197203
198204class CustomBind {
199- CustomBind (this ._bind, this .value, {this . type});
205+ CustomBind (this ._bind, this .value, {Type ? type}) : _type = type ;
200206 final String _bind;
201207 final Object value;
202- final PostgreSQLDataType ? type ;
208+ final Type ? _type ;
203209
204210 String formatString (String bindName) => _bind;
205211}
206212
207213class CustomTypeBind extends CustomBind {
208- factory CustomTypeBind (PostgreSQLDataType type, Object value) {
214+ factory CustomTypeBind (Type type, Object value) {
209215 // _bindCount.to
210216 return CustomTypeBind ._(
211217 '' ,
212218 value,
213219 type,
214220 );
215221 }
216- CustomTypeBind ._(String bind, Object value, PostgreSQLDataType type)
222+ CustomTypeBind ._(String bind, Object value, Type type)
217223 : super (bind, value, type: type);
218224
219225 @override
220- String formatString (String bindName) =>
221- '${PostgreSQLFormat .id (bindName )}::jsonb' ;
226+ String formatString (String bindName) => _type == null
227+ ? '@$bindName '
228+ : '@$bindName ::${TypeRegistry ().lookupTypeName (_type )}' ;
222229}
223230
224231abstract class DatabaseAccessBase <TX extends DatabaseTransactionBase <TABLES >,
@@ -233,20 +240,19 @@ abstract class DatabaseAccessBase<TX extends DatabaseTransactionBase<TABLES>,
233240 final DatabaseConfig config;
234241 final MigrationsProvider <TX , TABLES > migrations;
235242
236- PostgreSQLConnection ? _conn;
243+ Connection ? _conn;
237244
238- Future <PostgreSQLConnection > _connection () async {
245+ Future <Connection > _connection () async {
239246 if (_conn != null ) {
240247 return _conn! ;
241248 }
242- final conn = PostgreSQLConnection (
243- config.host,
244- config.port,
245- config.databaseName,
249+ final conn = await Connection . open ( Endpoint (
250+ host : config.host,
251+ port : config.port,
252+ database : config.databaseName,
246253 username: config.username,
247254 password: config.password,
248- );
249- await conn.open ();
255+ ));
250256 return _conn = conn;
251257 }
252258
@@ -273,15 +279,17 @@ abstract class DatabaseAccessBase<TX extends DatabaseTransactionBase<TABLES>,
273279 });
274280
275281 Future <T > _transaction <T >(
276- Future <T > Function (PostgreSQLExecutionContext conn) queryBlock) async {
282+ Future <T > Function (TxSession conn) queryBlock) async {
277283 final conn = await _connection ();
278- final dynamic result = await conn.transaction (queryBlock);
279- if (result is T ) {
280- return result;
281- }
282- throw Exception (
283- 'Error running in transaction, $result (${result .runtimeType })'
284- ' is not ${T .runtimeType }' );
284+ return conn.runTx ((session) async {
285+ final dynamic result = await queryBlock (session);
286+ if (result is T ) {
287+ return result;
288+ }
289+ throw Exception (
290+ 'Error running in transaction, $result (${result .runtimeType })'
291+ ' is not ${T .runtimeType }' );
292+ });
285293 }
286294
287295 Future <void > prepareDatabase () async {
@@ -362,7 +370,7 @@ abstract class DatabaseAccessBase<TX extends DatabaseTransactionBase<TABLES>,
362370 }
363371
364372 @protected
365- TX createDatabaseTransaction (PostgreSQLExecutionContext conn, TABLES tables);
373+ TX createDatabaseTransaction (TxSession conn, TABLES tables);
366374}
367375
368376//extension on SqlClientBase {
0 commit comments