/
connection_user.dart
510 lines (461 loc) · 19.8 KB
/
connection_user.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
part of 'runtime_api.dart';
const _zoneRootUserKey = #DatabaseConnectionUser;
typedef _CustomWriter<T> = Future<T> Function(
QueryExecutor e, String sql, List<dynamic> vars);
typedef _BatchRunner = FutureOr<void> Function(Batch batch);
/// Manages a [DatabaseConnection] to send queries to the database.
abstract class DatabaseConnectionUser {
/// The database connection used by this [DatabaseConnectionUser].
@protected
final DatabaseConnection connection;
/// The database class that this user is attached to.
@visibleForOverriding
GeneratedDatabase get attachedDatabase;
/// The type system to use with this database. The type system is responsible
/// for mapping Dart objects into sql expressions and vice-versa.
SqlTypeSystem get typeSystem => connection.typeSystem;
/// The executor to use when queries are executed.
QueryExecutor get executor => connection.executor;
/// Manages active streams from select statements.
@visibleForTesting
@protected
StreamQueryStore get streamQueries => connection.streamQueries;
/// Constructs a database connection user, which is responsible to store query
/// streams, wrap the underlying executor and perform type mapping.
DatabaseConnectionUser(SqlTypeSystem typeSystem, QueryExecutor executor,
{StreamQueryStore? streamQueries})
: connection = DatabaseConnection(
typeSystem, executor, streamQueries ?? StreamQueryStore());
/// Creates another [DatabaseConnectionUser] by referencing the implementation
/// from the [other] user.
DatabaseConnectionUser.delegate(DatabaseConnectionUser other,
{SqlTypeSystem? typeSystem,
QueryExecutor? executor,
StreamQueryStore? streamQueries})
: connection = DatabaseConnection(
typeSystem ?? other.connection.typeSystem,
executor ?? other.connection.executor,
streamQueries ?? other.connection.streamQueries,
);
/// Constructs a [DatabaseConnectionUser] that will use the provided
/// [DatabaseConnection].
DatabaseConnectionUser.fromConnection(this.connection);
/// Creates and auto-updating stream from the given select statement. This
/// method should not be used directly.
Stream<List<Map<String, Object?>>> createStream(QueryStreamFetcher stmt) =>
streamQueries.registerStream(stmt);
/// Creates a copy of the table with an alias so that it can be used in the
/// same query more than once.
///
/// Example which uses the same table (here: points) more than once to
/// differentiate between the start and end point of a route:
/// ```
/// var source = alias(points, 'source');
/// var destination = alias(points, 'dest');
///
/// select(routes).join([
/// innerJoin(source, routes.startPoint.equalsExp(source.id)),
/// innerJoin(destination, routes.startPoint.equalsExp(destination.id)),
/// ]);
/// ```
T alias<T extends Table, D>(TableInfo<T, D> table, String alias) {
return table.createAlias(alias).asDslTable;
}
/// A, potentially more specific, database engine based on the [Zone] context.
///
/// Inside a [transaction] block, drift will replace this [resolvedEngine]
/// with an engine specific to the transaction. All other methods on this
/// class implicitly use the [resolvedEngine] to run their SQL statements.
/// This let's users call methods on their top-level database or dao class
/// but run them in a transaction-specific executor.
@internal
DatabaseConnectionUser get resolvedEngine {
return (Zone.current[_zoneRootUserKey] as DatabaseConnectionUser?) ?? this;
}
/// Marks the [tables] as updated.
///
/// In response to calling this method, all streams listening on any of the
/// [tables] will load their data again.
///
/// Primarily, this method is meant to be used by drift-internal code. Higher-
/// level drift APIs will call this method to dispatch stream updates.
/// Of course, you can also call it yourself to manually dispatch table
/// updates. To obtain a [TableInfo], use the corresponding getter on the
/// database class.
void markTablesUpdated(Iterable<TableInfo> tables) {
notifyUpdates(
{for (final table in tables) TableUpdate(table.actualTableName)},
);
}
/// Dispatches the set of [updates] to the stream query manager.
///
/// This method is more specific than [markTablesUpdated] in the presence of
/// triggers or foreign key constraints. Drift needs to support both when
/// calculating which streams to update. For instance, consider a simple
/// database with two tables (`a` and `b`) and a trigger inserting into `b`
/// after a delete on `a`).
/// Now, an insert on `a` should not update a stream listening on table `b`,
/// but a delete should! This additional information is not available with
/// [markTablesUpdated], so [notifyUpdates] can be used to more efficiently
/// calculate stream updates in some instances.
void notifyUpdates(Set<TableUpdate> updates) {
final withRulesApplied = attachedDatabase.streamUpdateRules.apply(updates);
resolvedEngine.streamQueries.handleTableUpdates(withRulesApplied);
}
/// Listen for table updates reported through [notifyUpdates].
///
/// By default, this listens to every table update. Table updates are reported
/// as a set of individual updates that happened atomically.
/// An optional filter can be provided in the [query] parameter. When set,
/// only updates matching the query will be reported in the stream.
///
/// When called inside a transaction, the stream will close when the
/// transaction completes or is rolled back. Otherwise, the stream will
/// complete as the database is closed.
Stream<Set<TableUpdate>> tableUpdates(
[TableUpdateQuery query = const TableUpdateQuery.any()]) {
// The stream should refer to the transaction active when tableUpdates was
// called, not the one when a listener attaches.
final engine = resolvedEngine;
// We're wrapping updatesForSync in a stream controller to make it async.
return Stream.multi(
(controller) {
final source = engine.streamQueries.updatesForSync(query);
source.pipe(controller);
},
isBroadcast: true,
);
}
/// Performs the async [fn] after this executor is ready, or directly if it's
/// already ready.
///
/// Calling this method directly might circumvent the current transaction. For
/// that reason, it should only be called inside drift.
Future<T> doWhenOpened<T>(FutureOr<T> Function(QueryExecutor e) fn) {
return executor.ensureOpen(attachedDatabase).then((_) => fn(executor));
}
/// Starts an [InsertStatement] for a given table. You can use that statement
/// to write data into the [table] by using [InsertStatement.insert].
InsertStatement<T, D> into<T extends Table, D>(TableInfo<T, D> table) {
return InsertStatement<T, D>(resolvedEngine, table);
}
/// Starts an [UpdateStatement] for the given table. You can use that
/// statement to update individual rows in that table by setting a where
/// clause on that table and then use [UpdateStatement.write].
UpdateStatement<Tbl, R> update<Tbl extends Table, R>(
TableInfo<Tbl, R> table) =>
UpdateStatement(resolvedEngine, table);
/// Starts a query on the given table.
///
/// In drift, queries are commonly used as a builder by chaining calls on them
/// using the `..` syntax from Dart. For instance, to load the 10 oldest users
/// with an 'S' in their name, you could use:
/// ```dart
/// Future<List<User>> oldestUsers() {
/// return (
/// select(users)
/// ..where((u) => u.name.like('%S%'))
/// ..orderBy([(u) => OrderingTerm(
/// expression: u.id,
/// mode: OrderingMode.asc
/// )])
/// ..limit(10)
/// ).get();
/// }
/// ```
///
/// The [distinct] parameter (defaults to false) can be used to remove
/// duplicate rows from the result set.
///
/// For more information on queries, see the
/// [documentation](https://drift.simonbinder.eu/docs/getting-started/writing_queries/).
SimpleSelectStatement<T, R> select<T extends HasResultSet, R>(
ResultSetImplementation<T, R> table,
{bool distinct = false}) {
return SimpleSelectStatement<T, R>(resolvedEngine, table,
distinct: distinct);
}
/// Starts a complex statement on [table] that doesn't necessarily use all of
/// [table]'s columns.
///
/// Unlike [select], which automatically selects all columns of [table], this
/// method is suitable for more advanced queries that can use [table] without
/// using their column. As an example, assuming we have a table `comments`
/// with a `TextColumn content`, this query would report the average length of
/// a comment:
/// ```dart
/// Stream<num> watchAverageCommentLength() {
/// final avgLength = comments.content.length.avg();
/// final query = selectWithoutResults(comments)
/// ..addColumns([avgLength]);
///
/// return query.map((row) => row.read(avgLength)).watchSingle();
/// }
/// ```
///
/// While this query reads from `comments`, it doesn't use all of it's columns
/// (in fact, it uses none of them!). This makes it suitable for
/// [selectOnly] instead of [select].
///
/// The [distinct] parameter (defaults to false) can be used to remove
/// duplicate rows from the result set.
///
/// For simple queries, use [select].
///
/// See also:
/// - the documentation on [aggregate expressions](https://drift.simonbinder.eu/docs/getting-started/expressions/#aggregate)
/// - the documentation on [group by](https://drift.simonbinder.eu/docs/advanced-features/joins/#group-by)
JoinedSelectStatement<T, R> selectOnly<T extends HasResultSet, R>(
ResultSetImplementation<T, R> table, {
bool distinct = false,
}) {
return JoinedSelectStatement<T, R>(
resolvedEngine, table, [], distinct, false);
}
/// Starts a [DeleteStatement] that can be used to delete rows from a table.
///
/// See the [documentation](https://drift.simonbinder.eu/docs/getting-started/writing_queries/#updates-and-deletes)
/// for more details and example on how delete statements work.
DeleteStatement<T, D> delete<T extends Table, D>(TableInfo<T, D> table) {
return DeleteStatement<T, D>(resolvedEngine, table);
}
/// Executes a custom delete or update statement and returns the amount of
/// rows that have been changed.
/// You can use the [updates] parameter so that drift knows which tables are
/// affected by your query. All select streams that depend on a table
/// specified there will then update their data. For more accurate results,
/// you can also set the [updateKind] parameter to [UpdateKind.delete] or
/// [UpdateKind.update]. This is optional, but can improve the accuracy of
/// query updates, especially when using triggers.
Future<int> customUpdate(
String query, {
List<Variable> variables = const [],
Set<TableInfo>? updates,
UpdateKind? updateKind,
}) async {
return _customWrite(
query,
variables,
updates,
updateKind,
(executor, sql, vars) {
return executor.runUpdate(sql, vars);
},
);
}
/// Executes a custom insert statement and returns the last inserted rowid.
///
/// You can tell drift which tables your query is going to affect by using the
/// [updates] parameter. Query-streams running on any of these tables will
/// then be re-run.
Future<int> customInsert(String query,
{List<Variable> variables = const [], Set<TableInfo>? updates}) {
return _customWrite(
query,
variables,
updates,
UpdateKind.insert,
(executor, sql, vars) {
return executor.runInsert(sql, vars);
},
);
}
/// Runs a `INSERT`, `UPDATE` or `DELETE` statement returning rows.
///
/// You can use the [updates] parameter so that drift knows which tables are
/// affected by your query. All select streams that depend on a table
/// specified there will then update their data. For more accurate results,
/// you can also set the [updateKind] parameter.
/// This is optional, but can improve the accuracy of query updates,
/// especially when using triggers.
Future<List<QueryRow>> customWriteReturning(
String query, {
List<Variable> variables = const [],
Set<TableInfo>? updates,
UpdateKind? updateKind,
}) {
return _customWrite(query, variables, updates, updateKind,
(executor, sql, vars) async {
final rows = await executor.runSelect(sql, vars);
return [for (final row in rows) QueryRow(row, attachedDatabase)];
});
}
/// Common logic for [customUpdate] and [customInsert] which takes care of
/// mapping the variables, running the query and optionally informing the
/// stream-queries.
Future<T> _customWrite<T>(
String query,
List<Variable> variables,
Set<TableInfo>? updates,
UpdateKind? updateKind,
_CustomWriter<T> writer,
) async {
final engine = resolvedEngine;
final ctx = GenerationContext.fromDb(engine);
final mappedArgs = variables.map((v) => v.mapToSimpleValue(ctx)).toList();
final result =
await engine.doWhenOpened((e) => writer(e, query, mappedArgs));
if (updates != null) {
engine.notifyUpdates({
for (final table in updates)
TableUpdate(table.actualTableName, kind: updateKind),
});
}
return result;
}
/// Creates a custom select statement from the given sql [query]. To run the
/// query once, use [Selectable.get]. For an auto-updating streams, set the
/// set of tables the ready [readsFrom] and use [Selectable.watch]. If you
/// know the query will never emit more than one row, you can also use
/// `getSingle` and `SelectableUtils.watchSingle` which return the item
/// directly without wrapping it into a list.
///
/// If you use variables in your query (for instance with "?"), they will be
/// bound to the [variables] you specify on this query.
Selectable<QueryRow> customSelect(String query,
{List<Variable> variables = const [],
Set<ResultSetImplementation> readsFrom = const {}}) {
return CustomSelectStatement(query, variables, readsFrom, resolvedEngine);
}
/// Creates a custom select statement from the given sql [query]. To run the
/// query once, use [Selectable.get]. For an auto-updating streams, set the
/// set of tables the ready [readsFrom] and use [Selectable.watch]. If you
/// know the query will never emit more than one row, you can also use
/// `getSingle` and `watchSingle` which return the item directly without
/// wrapping it into a list.
///
/// If you use variables in your query (for instance with "?"), they will be
/// bound to the [variables] you specify on this query.
@Deprecated('Renamed to customSelect')
Selectable<QueryRow> customSelectQuery(String query,
{List<Variable> variables = const [],
Set<ResultSetImplementation> readsFrom = const {}}) {
return customSelect(query, variables: variables, readsFrom: readsFrom);
}
/// Executes the custom sql [statement] on the database.
Future<void> customStatement(String statement, [List<dynamic>? args]) {
final engine = resolvedEngine;
return engine.doWhenOpened((executor) {
return executor.runCustom(statement, args);
});
}
/// Executes [action] in a transaction, which means that all its queries and
/// updates will be called atomically.
///
/// Returns the value of [action].
/// When [action] throws an exception, the transaction will be reset and no
/// changes will be applied to the databases. The exception will be rethrown
/// by [transaction].
///
/// The behavior of stream queries in transactions depends on where the stream
/// was created:
///
/// - streams created outside of a [transaction] block: The stream will update
/// with the tables modified in the transaction after it completes
/// successfully. If the transaction fails, the stream will not update.
/// - streams created inside a [transaction] block: The stream will update for
/// each write in the transaction. When the transaction completes,
/// successful or not, streams created in it will close. Writes happening
/// outside of this transaction will not affect the stream.
///
/// Please note that nested transactions are not supported. Creating another
/// transaction inside a transaction returns the parent transaction.
///
/// See also:
/// - the docs on [transactions](https://drift.simonbinder.eu/docs/transactions/)
Future<T> transaction<T>(Future<T> Function() action) async {
final resolved = resolvedEngine;
if (resolved is Transaction) {
return action();
}
return await resolved.doWhenOpened((executor) {
final transactionExecutor = executor.beginTransaction();
final transaction = Transaction(this, transactionExecutor);
return _runConnectionZoned(transaction, () async {
var success = false;
try {
final result = await action();
success = true;
return result;
} catch (e, s) {
try {
await transactionExecutor.rollback();
} catch (rollBackException) {
throw CouldNotRollBackException(e, s, rollBackException);
}
// pass the exception on to the one who called transaction()
rethrow;
} finally {
if (success) {
// complete() will also take care of committing the transaction
await transaction.complete();
}
await transaction.disposeChildStreams();
}
});
});
}
/// Runs statements inside a batch.
///
/// A batch can only run a subset of statements, and those statements must be
/// called on the [Batch] instance. The statements aren't executed with a call
/// to [Batch]. Instead, all generated queries are queued up and are then run
/// and executed atomically in a transaction.
/// If [batch] is called outside of a [transaction] call, it will implicitly
/// start a transaction. Otherwise, the batch will re-use the transaction,
/// and will have an effect when the transaction completes.
/// Typically, running bulk updates (so a lot of similar statements) over a
/// [Batch] is much faster than running them via the [GeneratedDatabase]
/// directly.
///
/// An example that inserts users in a batch:
/// ```dart
/// await batch((b) {
/// b.insertAll(
/// todos,
/// [
/// TodosCompanion.insert(content: 'Use batches'),
/// TodosCompanion.insert(content: 'Have fun'),
/// ],
/// );
/// });
/// ```
Future<void> batch(_BatchRunner runInBatch) {
final engine = resolvedEngine;
final batch = Batch._(engine, engine is! Transaction);
final result = runInBatch(batch);
if (result is Future) {
return result.then((_) => batch._commit());
} else {
return batch._commit();
}
}
/// Runs [calculation] in a forked [Zone] that has its [resolvedEngine] set
/// to the [user].
@protected
Future<T> _runConnectionZoned<T>(
DatabaseConnectionUser user, Future<T> Function() calculation) {
return runZoned(calculation, zoneValues: {_zoneRootUserKey: user});
}
/// Will be used by generated code to resolve inline Dart components in sql.
@protected
GenerationContext $write(Component component, {bool? hasMultipleTables}) {
final context = GenerationContext.fromDb(this);
if (hasMultipleTables != null) {
context.hasMultipleTables = hasMultipleTables;
}
component.writeInto(context);
return context;
}
/// Writes column names and `VALUES` for an insert statement.
///
/// Used by generated code.
@protected
GenerationContext $writeInsertable(TableInfo table, Insertable insertable) {
final context = GenerationContext.fromDb(this);
table.validateIntegrity(insertable, isInserting: true);
InsertStatement(this, table)
.writeInsertable(context, insertable.toColumns(true));
return context;
}
}