1+ import 'dart:convert' ;
12import 'dart:isolate' ;
23
4+ import 'package:collection/collection.dart' ;
35import 'package:meta/meta.dart' ;
46
57import '../drift.dart' ;
68
7- /// Generates `CREATE` statements for the given database [schema ] for all
8- /// [dialects] and sends generated statements through the send [port] .
9+ /// Generates `CREATE` statements for the given [database ] for all [dialects]
10+ /// and sends generated statements through the send [port] .
911///
1012/// Each statement will be encoded as a `[index, name, sql]` list, where `index`
1113/// is the index in [dialects] , `name` is the [DatabaseSchemaEntity.entityName]
@@ -21,14 +23,24 @@ import '../drift.dart';
2123/// of that.
2224@internal
2325void sendCreateStatements (
26+ List <String > args,
2427 SendPort port,
25- DatabaseSchema schema ,
26- List <DriftDialect > dialects,
28+ GeneratedDatabase Function ( DriftConnection ) database ,
29+ List <DriftDialectFactory > dialects,
2730) {
28- final statements = < List <Object > > [] ;
31+ final statements = < DriftDialect , List <_CollectedStatement > > {} ;
2932
30- for (final (index, dialect) in dialects.indexed) {
31- for (final entity in schema) {
33+ for (final dialectFactory in dialects) {
34+ final opened = database (
35+ DriftConnection (
36+ dialect: dialectFactory,
37+ openConnection: () => Future .error (UnsupportedError ('Stub connection' )),
38+ ),
39+ );
40+ final dialect = opened.dialect;
41+ final statementsForDialect = < _CollectedStatement > [];
42+
43+ for (final entity in opened.schema) {
3244 StatementInfo compiled;
3345
3446 switch (entity) {
@@ -40,7 +52,51 @@ void sendCreateStatements(
4052 compiled = dialect.compile (definition);
4153 }
4254
43- statements.add ([index, entity.entityName, compiled.sql]);
55+ statementsForDialect.add (
56+ _CollectedStatement (entity.entityName, compiled.sql),
57+ );
58+ }
59+
60+ statements[dialect] = statementsForDialect;
61+ }
62+
63+ List <_CollectedStatement > statementsForDialect (String dialectName) {
64+ final dialect = KnownSqlDialect .values.byName (dialectName);
65+ final entry = statements.entries.firstWhereOrNull (
66+ (e) => e.key.known == dialect,
67+ );
68+
69+ if (entry == null ) {
70+ throw ArgumentError (
71+ 'Dialect ${dialect .name } is not registered on this database' ,
72+ );
4473 }
74+
75+ return entry.value;
4576 }
77+
78+ if (args case ['v2' , final options]) {
79+ final parsedOptions = json.decode (options);
80+ final dialectNames = (parsedOptions['dialects' ] as List ).cast <String >();
81+ final encodedStatements = < List > [];
82+
83+ for (final name in dialectNames) {
84+ encodedStatements.addAll (
85+ statementsForDialect (name).map ((e) => [e.element, name, e.stmt]),
86+ );
87+ }
88+
89+ port.send (encodedStatements);
90+ } else {
91+ port.send ([
92+ for (final stmt in statementsForDialect (args.single)) stmt.stmt,
93+ ]);
94+ }
95+ }
96+
97+ final class _CollectedStatement {
98+ final String element;
99+ final String stmt;
100+
101+ _CollectedStatement (this .element, this .stmt);
46102}
0 commit comments