Skip to content

Commit 344dd5d

Browse files
committed
Use async/await for utils
1 parent 23d7d08 commit 344dd5d

File tree

1 file changed

+18
-57
lines changed

1 file changed

+18
-57
lines changed

lib/src/utils.dart

Lines changed: 18 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,30 @@ part of utils;
66
class TableDropper {
77
ConnectionPool pool;
88
List<String> tables;
9-
List<String> _tables = [];
10-
9+
1110
/**
1211
* Create a [TableDropper]. Needs a [pool] and
1312
* a list of [tables].
1413
*/
1514
TableDropper(this.pool, this.tables);
1615

17-
void _dropTables(Completer c) {
18-
afterDrop() {
19-
if (_tables.length == 0) {
20-
c.complete(null);
21-
} else {
22-
_dropTables(c);
23-
}
24-
}
25-
26-
var table = _tables.removeAt(0);
27-
pool.query('drop table $table')
28-
// if it's an unknown table, ignore the error and continue
29-
.then((_) {
30-
afterDrop();
31-
})
32-
.catchError((e) {
33-
afterDrop();
34-
}, test: (e) => e is MySqlException && (e as MySqlException).errorNumber == ERROR_UNKNOWN_TABLE)
35-
.catchError((e) {
36-
c.completeError(e);
37-
});
38-
}
39-
4016
/**
4117
* Drops the tables this [TableDropper] was created with. The
4218
* returned [Future] completes when all the tables have been dropped.
4319
* If a table doesn't exist, it is ignored.
44-
*
45-
* Do not run this a second time until the future has completed.
4620
*/
47-
Future dropTables() {
48-
var c = new Completer();
49-
_tables.clear();
50-
_tables.addAll(tables);
51-
_dropTables(c);
52-
return c.future;
21+
Future dropTables() async {
22+
await Future.forEach(tables, (table) async {
23+
try {
24+
await pool.query('drop table $table');
25+
} catch (e) {
26+
if (e is MySqlException && (e as MySqlException).errorNumber == ERROR_UNKNOWN_TABLE) {
27+
// if it's an unknown table, ignore the error and continue
28+
} else {
29+
throw e;
30+
}
31+
}
32+
});
5333
}
5434
}
5535

@@ -60,40 +40,21 @@ class TableDropper {
6040
class QueryRunner {
6141
final ConnectionPool pool;
6242
final List<String> queries;
63-
final List<String> _queries = [];
64-
43+
6544
/**
6645
* Create a [QueryRunner]. Needs a [pool] and
6746
* a list of [queries].
6847
*/
6948
QueryRunner(this.pool, this.queries);
7049

71-
Future _executeQueries(Completer c) {
72-
var query = _queries.removeAt(0);
73-
pool.query(query).then((result) {
74-
if (_queries.length == 0) {
75-
c.complete(null);
76-
} else {
77-
_executeQueries(c);
78-
}
79-
})
80-
.catchError((e) {
81-
c.completeError(e);
82-
});
83-
}
84-
8550
/**
8651
* Executes the queries this [QueryRunner] was created with. The
8752
* returned [Future] completes when all the queries have been executed.
8853
* Results are ignored.
89-
*
90-
* Do not run this a second time until the future has completed.
9154
*/
92-
Future executeQueries() {
93-
var c = new Completer();
94-
_queries.clear();
95-
_queries.addAll(queries);
96-
_executeQueries(c);
97-
return c.future;
55+
Future executeQueries() async {
56+
await Future.forEach(queries, (query) async {
57+
await pool.query(query);
58+
});
9859
}
9960
}

0 commit comments

Comments
 (0)