@@ -6,50 +6,30 @@ part of utils;
6
6
class TableDropper {
7
7
ConnectionPool pool;
8
8
List <String > tables;
9
- List <String > _tables = [];
10
-
9
+
11
10
/**
12
11
* Create a [TableDropper] . Needs a [pool] and
13
12
* a list of [tables] .
14
13
*/
15
14
TableDropper (this .pool, this .tables);
16
15
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
-
40
16
/**
41
17
* Drops the tables this [TableDropper] was created with. The
42
18
* returned [Future] completes when all the tables have been dropped.
43
19
* If a table doesn't exist, it is ignored.
44
- *
45
- * Do not run this a second time until the future has completed.
46
20
*/
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
+ });
53
33
}
54
34
}
55
35
@@ -60,40 +40,21 @@ class TableDropper {
60
40
class QueryRunner {
61
41
final ConnectionPool pool;
62
42
final List <String > queries;
63
- final List <String > _queries = [];
64
-
43
+
65
44
/**
66
45
* Create a [QueryRunner] . Needs a [pool] and
67
46
* a list of [queries] .
68
47
*/
69
48
QueryRunner (this .pool, this .queries);
70
49
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
-
85
50
/**
86
51
* Executes the queries this [QueryRunner] was created with. The
87
52
* returned [Future] completes when all the queries have been executed.
88
53
* Results are ignored.
89
- *
90
- * Do not run this a second time until the future has completed.
91
54
*/
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
+ });
98
59
}
99
60
}
0 commit comments