-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase_access.dart
445 lines (397 loc) · 13.3 KB
/
database_access.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import 'package:clock/clock.dart';
import 'package:logging/logging.dart';
import 'package:meta/meta.dart';
import 'package:postgres/postgres.dart';
// ignore: implementation_imports
import 'package:postgres/src/types/type_registry.dart' show TypeRegistryExt;
import 'package:postgres_utils/src/config.dart';
import 'package:postgres_utils/src/tables/base_tables.dart';
import 'package:postgres_utils/src/tables/migration_tables.dart';
import 'package:quiver/check.dart';
import 'package:quiver/core.dart';
final _logger = Logger('database_access');
const whereIsNull = Object();
const whereIsNotNull = Object();
class OnConflictActionDoUpdate extends OnConflictAction {
OnConflictActionDoUpdate({
required this.indexColumns,
this.concatColumns = const {},
});
/// Columns which are used to identify uniqueness
final List<String> indexColumns;
/// Columns which should not be overwritten, but concatenated.
final Set<String> concatColumns;
}
abstract class OnConflictAction {
// doUpdate,
}
class DatabaseTransactionBase<TABLES extends TablesBase> {
DatabaseTransactionBase(this._conn, this.tables);
final TxSession _conn;
final TABLES tables;
static final columnNamePattern = RegExp(r'^[a-z_]+$');
void _assertColumnNames(Map<String, Object?> values) {
assert((() {
for (final key in values.keys) {
if (!columnNamePattern.hasMatch(key)) {
throw ArgumentError.value(key, 'values', 'Invalid column name.');
}
}
return true;
})());
}
Future<int> executeInsert(
String table,
Map<String, Object?> values, {
final OnConflictAction? onConflict,
}) async {
_assertColumnNames(values);
final entries = values.entries.toList();
final columnList = entries.map((e) => e.key).join(',');
final bindList = entries.map((e) => _bindForEntry(e)).join(',');
final String onConflictSql;
if (onConflict is OnConflictActionDoUpdate) {
final set = entries
.where((element) => !onConflict.indexColumns.contains(element.key))
.map((e) {
if (onConflict.concatColumns.contains(e.key)) {
return '''
${e.key} = CASE WHEN $table.${e.key} IS NULL THEN EXCLUDED.${e.key}
ELSE $table.${e.key} || EXCLUDED.${e.key}
END''';
}
return '${e.key} = EXCLUDED.${e.key}';
}).join(', ');
final where = entries
.where((element) => onConflict.indexColumns.contains(element.key))
.map((e) => '$table.${e.key} = EXCLUDED.${e.key}')
.join(' AND ');
onConflictSql = ' ON CONFLICT (${onConflict.indexColumns.join(', ')}) '
' DO UPDATE SET $set WHERE $where';
} else {
onConflictSql = '';
}
return await execute(
'INSERT INTO $table ($columnList) VALUES ($bindList) $onConflictSql',
values: values.map((key, value) =>
MapEntry(key, value is CustomBind ? value.value : value)),
expectedResultCount: 1,
useExtendedQuery: true,
);
}
String _bindForEntry(MapEntry<String, Object?> entry) {
final value = entry.value;
if (value is CustomBind) {
return value.formatString(entry.key);
}
return '@${entry.key}';
}
Future<int> executeUpdate(
String table, {
required final Map<String, Object?> set,
required final Map<String, Object> where,
bool setContainsOptional = false,
}) async {
_assertColumnNames(set);
_assertColumnNames(where);
// assert(!where.keys.any((key) => set.containsKey(key)));
if (where.values.contains(null)) {
throw ArgumentError('where values must not be null.');
}
assert(!setContainsOptional || set.values.whereType<Optional>().isEmpty);
final setArgs = setContainsOptional ? flattenOptionals(set) : set;
final setStatement =
setArgs.entries.map((e) => '${e.key} = @s${e.key}').join(',');
final whereStatement = where.entries.toList(growable: false).map((e) {
if (identical(e.value, whereIsNull)) {
where.remove(e.key);
return '${e.key} IS NULL';
}
if (identical(e.value, whereIsNotNull)) {
where.remove(e.key);
return '${e.key} IS NOT NULL';
}
return '${e.key} = @w${e.key}';
}).join(' AND ');
return await execute(
'UPDATE $table SET $setStatement WHERE $whereStatement',
values: {
...setArgs.map((key, value) => MapEntry('s$key', value)),
...where.map((key, value) => MapEntry('w$key', value)),
},
expectedResultCount: 1);
}
/// Removes entries in [values] which have a `null` value, and replaces
/// all [Optional] values with their actual value.
Map<String, Object?> flattenOptionals(Map<String, Object?> values) {
Object? unwrap(Object? value) => value is Optional ? value.orNull : value;
return Map.fromEntries(values.entries
.where((element) => element.value != null)
.map((e) => MapEntry(e.key, unwrap(e.value))));
}
bool _assertCorrectValues(Map<String, Object?>? values) {
if (values == null) {
return true;
}
for (final entry in values.entries) {
final value = entry.value;
if (value is DateTime) {
if (!value.isUtc) {
throw ArgumentError.value(
entry, 'Value for ${entry.key} is a non-UTC DateTime.');
}
}
}
return true;
}
Future<int> execute(
String fmtString, {
Map<String, Object?>? values,
int? timeoutInSeconds,
int? expectedResultCount,
bool useExtendedQuery = false,
}) async {
try {
assert(_assertCorrectValues(values));
_logger.finest('Executing query: $fmtString with values: $values');
final int result;
final sqlResult = await query(
fmtString,
values: values,
timeoutInSeconds: timeoutInSeconds,
queryMode: useExtendedQuery ? QueryMode.extended : QueryMode.simple,
);
result = sqlResult.affectedRows;
if (expectedResultCount != null && result != expectedResultCount) {
throw StateError(
'Expected result: $expectedResultCount but got $result. '
'for query: $fmtString');
}
return result;
} catch (e, stackTrace) {
_logger.warning(
'Error while running statement $fmtString', e, stackTrace);
rethrow;
}
}
Future<Result> query(
String fmtString, {
Map<String, Object?>? values,
int? timeoutInSeconds,
QueryMode? queryMode,
}) async {
assert(_assertCorrectValues(values));
queryMode ??= values == null || values.isEmpty
? QueryMode.simple
: QueryMode.extended;
try {
// _logger.finest('QUERY: $fmtString');
return _conn.execute(
Sql.named(fmtString),
parameters: values,
queryMode: queryMode,
timeout: timeoutInSeconds == null
? null
: Duration(seconds: timeoutInSeconds),
);
} catch (e, stackTrace) {
_logger.warning(
'Error while running statement $fmtString', e, stackTrace);
rethrow;
}
}
}
class CustomBind {
CustomBind(this._bind, this.value, {Type? type}) : _type = type;
final String _bind;
final Object value;
final Type? _type;
String formatString(String bindName) => _bind;
}
class CustomTypeBind extends CustomBind {
factory CustomTypeBind(Type type, Object value) {
// _bindCount.to
return CustomTypeBind._(
'',
value,
type,
);
}
CustomTypeBind._(String bind, Object value, Type type)
: super(bind, value, type: type);
@override
String formatString(String bindName) => _type == null
? '@$bindName'
: '@$bindName::${TypeRegistry().lookupTypeName(_type)}';
}
abstract class DatabaseAccessBase<TX extends DatabaseTransactionBase<TABLES>,
TABLES extends TablesBase> {
DatabaseAccessBase({
required this.config,
this.connectionSettings,
required this.tables,
required this.migrations,
});
final TABLES tables;
final DatabaseConfig config;
final ConnectionSettings? connectionSettings;
final MigrationsProvider<TX, TABLES> migrations;
Connection? _conn;
Future<Connection> _connection() async {
if (_conn != null) {
return _conn!;
}
final conn = await Connection.open(
Endpoint(
host: config.host,
port: config.port,
database: config.databaseName,
username: config.username,
password: config.password,
),
settings: connectionSettings);
return _conn = conn;
}
@visibleForTesting
Future<void> forTestCreateDatabase(String name) async {
await (await _connection()).execute('CREATE DATABASE $name');
}
@visibleForTesting
Future<void> forTestDropDatabase(String databaseName,
{bool ifExists = false}) async {
await (await _connection()).execute(
'DROP DATABASE ${ifExists ? ' IF EXISTS ' : ''} $databaseName');
}
Future<void> dispose() async {
await _conn!.close();
_conn = null;
}
Future<T> run<T>(Future<T> Function(TX db) block) async =>
_transaction((conn) async {
return await block(createDatabaseTransaction(conn, tables));
});
Future<T> _transaction<T>(
Future<T> Function(TxSession conn) queryBlock) async {
final conn = await _connection();
return conn.runTx((session) async {
final dynamic result = await queryBlock(session);
if (result is T) {
return result;
}
throw Exception(
'Error running in transaction, $result (${result.runtimeType})'
' is not ${T.runtimeType}');
});
}
Future<void> prepareDatabase() async {
_logger.finest('Initializing database.');
// await clean();
final lastMigration = await run((connection) async {
try {
await tables.migration.createTable(connection);
return await tables.migration.queryLastVersion(connection);
} catch (e, stackTrace) {
_logger.severe('Error during migration', e, stackTrace);
rethrow;
}
});
_logger.fine('Last migration: $lastMigration');
final migrationRun = clock.now().toUtc();
await run((conn) async {
final migrations = this.migrations.migrations;
for (final migration in migrations) {
if (migration.id > lastMigration) {
_logger.fine('Running migration ${migration.id} '
'(${migration.versionCode})');
await migration.up(conn);
await tables.migration.insertMigrationRun(
conn, migrationRun, migration.id, migration.versionCode);
}
}
});
// _database = config.database();
// final client = _database.sqlClient;
// _logger.finest('Running migration.');
// await client.runInTransaction((t) async {
// final result = await client.run(
// SqlSourceBuilder()
// ..write('CREATE TABLE IF NOT EXISTS ')
// ..identifier(_TABLE_MIGRATE)
// ..write(' (')
// ..identifier(_COLUMN_ID)
// ..write(' id PRIMARY KEY SERIAL, ')
// ..identifier(_TABLE_MIGRATE_APPLIED_AT)
// ..write(' timestamp without time zone')
// ..identifier(_TABLE_MIGRATE_VERSION)
// ..write('INT NOT NULL)'),
// );
// });
// final table = _database.sqlClient.table('authpass_migration');
// _database.collection(_TABLE_MIGRATE).document('1');
}
Future<void> clean() async {
_logger.warning('Clearing database.');
final tableNames = tables._allTables.expand((e) => e.tables);
final typeNames = tables._allTables.expand((e) => e.types);
await run((connection) async {
final tables = tableNames.join(', ');
final result =
await connection.execute('DROP TABLE IF EXISTS $tables CASCADE');
_logger.fine('Dropped $tables ($result)');
if (typeNames.isNotEmpty) {
await connection.execute('DROP TYPE IF EXISTS ${typeNames.join(', ')}');
}
// for (final tableName in tableNames) {
// final result =
// await connection.execute('DROP TABLE IF EXISTS $tableName');
// _logger.fine('Dropped $tableName ($result)');
// }
});
}
@protected
TX createDatabaseTransaction(TxSession conn, TABLES tables);
}
//extension on SqlClientBase {
// Future<SqlStatementResult> run(SqlSourceBuilder builder) async {
// final stmt = builder.build();
// _logger.finest('Running SQL ${stmt.value}');
// return execute(stmt.value, stmt.arguments);
// }
//}
abstract class TablesBase {
final migration = MigrationTable();
@protected
List<TableBase> get tables;
List<TableBase> get _allTables => [
migration,
...tables,
];
}
abstract class MigrationsProvider<TX extends DatabaseTransactionBase<TABLES>,
TABLES extends TablesBase> {
List<Migrations<TX, TABLES>> get migrations;
}
class Migrations<TX extends DatabaseTransactionBase<TABLES>,
TABLES extends TablesBase> {
Migrations({
required this.id,
this.versionCode = 'a',
required this.up,
});
final int id;
final String versionCode;
final Future<void> Function(TX tx) up;
}
class SimpleWhere {
SimpleWhere(this.conditions, {bool filterNullValues = false})
: assert(filterNullValues || !conditions.values.contains(null),
'where values must not be null.') {
if (filterNullValues) {
conditions.removeWhere((key, value) => value == null);
}
checkState(conditions.isNotEmpty);
}
final Map<String, Object?> conditions;
String sql() =>
conditions.entries.map((e) => '${e.key} = @${e.key}').join(' AND ');
}