createTable static method
Future<void>
createTable(
{ - required Database database,
- required Map<String, dynamic> migrationScript,
})
Implementation
static Future<void> createTable({
required Database database,
required Map<String, dynamic> migrationScript,
}) async {
/// Initialize query
debugPrint('Initialize Table');
var migrationObject = MapToTable.fromJson(migrationScript);
var definitions = migrationObject.definition;
var definitionLength = definitions?.length ?? 0;
var query = '';
/// DDL Building
for (var i = 0; i < definitionLength; i++) {
var definition = definitions?[i];
if (definition?.fields != null) query += '${definition?.fields}';
if (definition?.type != null) query += ' ${definition?.type}';
if (definition?.attribute != null) query += ' ${definition?.attribute}';
if ((definitionLength - 1) != i) query += ", ";
}
var ddl = '''create table ${migrationObject.tableName} ($query)''';
/// Execute ddl
await database.execute(ddl).then((_) {
debugPrint('DDL Executed DDL : $ddl');
}).onError((error, stackTrace) {
debugPrint('Error when execute DDL : ${stackTrace.toString()}');
}).whenComplete(() => close(database));
}