@@ -2,6 +2,8 @@ import 'package:clock/clock.dart';
2
2
import 'package:logging/logging.dart' ;
3
3
import 'package:meta/meta.dart' ;
4
4
import 'package:postgres/postgres.dart' ;
5
+ // ignore: implementation_imports
6
+ import 'package:postgres/src/types/type_registry.dart' show TypeRegistryExt;
5
7
import 'package:postgres_utils/src/config.dart' ;
6
8
import 'package:postgres_utils/src/tables/base_tables.dart' ;
7
9
import 'package:postgres_utils/src/tables/migration_tables.dart' ;
@@ -26,7 +28,7 @@ abstract class OnConflictAction {
26
28
class DatabaseTransactionBase <TABLES extends TablesBase > {
27
29
DatabaseTransactionBase (this ._conn, this .tables);
28
30
29
- final PostgreSQLExecutionContext _conn;
31
+ final TxSession _conn;
30
32
final TABLES tables;
31
33
static final columnNamePattern = RegExp (r'^[a-z_]+$' );
32
34
@@ -155,14 +157,13 @@ class DatabaseTransactionBase<TABLES extends TablesBase> {
155
157
_logger.finest ('Executing query: $fmtString with values: $values ' );
156
158
157
159
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;
166
167
if (expectedResultCount != null && result != expectedResultCount) {
167
168
throw StateError (
168
169
'Expected result: $expectedResultCount but got $result . '
@@ -176,17 +177,22 @@ class DatabaseTransactionBase<TABLES extends TablesBase> {
176
177
}
177
178
}
178
179
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 {
183
187
assert (_assertCorrectValues (values));
184
188
try {
185
189
// _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));
190
196
} catch (e, stackTrace) {
191
197
_logger.warning (
192
198
'Error while running statement $fmtString ' , e, stackTrace);
@@ -196,29 +202,30 @@ class DatabaseTransactionBase<TABLES extends TablesBase> {
196
202
}
197
203
198
204
class CustomBind {
199
- CustomBind (this ._bind, this .value, {this . type});
205
+ CustomBind (this ._bind, this .value, {Type ? type}) : _type = type ;
200
206
final String _bind;
201
207
final Object value;
202
- final PostgreSQLDataType ? type ;
208
+ final Type ? _type ;
203
209
204
210
String formatString (String bindName) => _bind;
205
211
}
206
212
207
213
class CustomTypeBind extends CustomBind {
208
- factory CustomTypeBind (PostgreSQLDataType type, Object value) {
214
+ factory CustomTypeBind (Type type, Object value) {
209
215
// _bindCount.to
210
216
return CustomTypeBind ._(
211
217
'' ,
212
218
value,
213
219
type,
214
220
);
215
221
}
216
- CustomTypeBind ._(String bind, Object value, PostgreSQLDataType type)
222
+ CustomTypeBind ._(String bind, Object value, Type type)
217
223
: super (bind, value, type: type);
218
224
219
225
@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 )}' ;
222
229
}
223
230
224
231
abstract class DatabaseAccessBase <TX extends DatabaseTransactionBase <TABLES >,
@@ -233,20 +240,19 @@ abstract class DatabaseAccessBase<TX extends DatabaseTransactionBase<TABLES>,
233
240
final DatabaseConfig config;
234
241
final MigrationsProvider <TX , TABLES > migrations;
235
242
236
- PostgreSQLConnection ? _conn;
243
+ Connection ? _conn;
237
244
238
- Future <PostgreSQLConnection > _connection () async {
245
+ Future <Connection > _connection () async {
239
246
if (_conn != null ) {
240
247
return _conn! ;
241
248
}
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,
246
253
username: config.username,
247
254
password: config.password,
248
- );
249
- await conn.open ();
255
+ ));
250
256
return _conn = conn;
251
257
}
252
258
@@ -273,15 +279,17 @@ abstract class DatabaseAccessBase<TX extends DatabaseTransactionBase<TABLES>,
273
279
});
274
280
275
281
Future <T > _transaction <T >(
276
- Future <T > Function (PostgreSQLExecutionContext conn) queryBlock) async {
282
+ Future <T > Function (TxSession conn) queryBlock) async {
277
283
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
+ });
285
293
}
286
294
287
295
Future <void > prepareDatabase () async {
@@ -362,7 +370,7 @@ abstract class DatabaseAccessBase<TX extends DatabaseTransactionBase<TABLES>,
362
370
}
363
371
364
372
@protected
365
- TX createDatabaseTransaction (PostgreSQLExecutionContext conn, TABLES tables);
373
+ TX createDatabaseTransaction (TxSession conn, TABLES tables);
366
374
}
367
375
368
376
//extension on SqlClientBase {
0 commit comments