columnToList method
void
columnToList({
- required Map columns,
- dynamic tableName,
- Map tables = const {},
- Map foreigns = const {},
- required void callback(
- List<
String> importForeignCallback, - List<
String> constructorCallback, - List<
String> constructorAssignCallback, - List<
String> variablesCallback, - List<
String> toMapCallback, - List<
String> fromMapCallback, - List<
String> fromMapWithJoinCallback, - List<
String> propsCallback, - List<
String> paramCopyWithCallback, - List<
String> valueCopyWithCallback,
- List<
Implementation
void columnToList({
required Map columns,
dynamic tableName,
Map tables = const {},
Map foreigns = const {},
required void Function(
List<String> importForeignCallback,
List<String> constructorCallback,
List<String> constructorAssignCallback,
List<String> variablesCallback,
List<String> toMapCallback,
List<String> fromMapCallback,
List<String> fromMapWithJoinCallback,
List<String> propsCallback,
List<String> paramCopyWithCallback,
List<String> valueCopyWithCallback,
) callback,
}) {
List<String> importForeign = [];
List<String> constructor = [];
List<String> constructorAssign = [];
List<String> variables = [];
List<String> toMap = [];
List<String> fromMap = [];
List<String> fromMapWithJoin = [];
List<String> props = [];
List<String> paramCopyWith = [];
List<String> valueCopyWith = [];
columns.forEach((columnName, column) {
String type = 'String';
bool isTypeBool = false;
bool isRequired = false;
bool isPrimary = false;
if (column is String) {
type = getType(column);
isTypeBool = type == 'bool';
constructor.add('this.${columnName.toString().camelCase},');
variables.add('final $type? ${columnName.toString().camelCase};');
} else if (column is Map) {
isPrimary =
column['constraint']?.toString().toUpperCase() == 'PRIMARY KEY';
isRequired = isPrimary || column['nullable']?.toString() == 'false';
type = getType(column['type']?.toString() ?? '');
isTypeBool = type == 'bool';
variables.add(
'final $type${isRequired ? '' : '?'} ${columnName.toString().camelCase};');
if (isPrimary) {
constructor.add('$type? ${columnName.toString().camelCase},');
constructorAssign.add(
'${columnName.toString().camelCase} = ${columnName.toString().camelCase} ?? ${getDefaultType(type)}');
} else {
constructor.add(
'${isRequired ? 'required' : ''} this.${columnName.toString().camelCase},');
}
}
if (isPrimary) {
toMap.add(
"if (${columnName.toString().camelCase} != ${getDefaultType(type)}) '${columnName.toString().snakeCase}': ${columnName.toString().camelCase},");
} else {
toMap.add(
"${isRequired ? '' : 'if (${columnName.toString().camelCase} != null)'} '${columnName.toString().snakeCase}': ${isTypeBool ? '${columnName.toString().camelCase} == true ? 1 : 0' : columnName.toString().camelCase},");
}
fromMap.add(getFromMap(
columnName: columnName, type: type, isRequired: isRequired));
props.add(columnName.toString().camelCase);
paramCopyWith.add('$type? ${columnName.toString().camelCase},');
valueCopyWith.add(
'${columnName.toString().camelCase}: ${columnName.toString().camelCase} ?? this.${columnName.toString().camelCase},');
if (foreigns.isNotEmpty) {
fromMapWithJoin.add(
"${columnName.toString().camelCase}: map['${isTypeBool ? '${tableName.toString().snakeCase}}.${columnName.toString().snakeCase}} == 1' : '${tableName.toString().snakeCase}.${columnName.toString().snakeCase}'}']${isRequired ? ' ?? ${getDefaultType(type)}' : ''},");
}
});
foreigns.forEach((foreignName, foreign) {
final String tableNameForeign = foreign['to_table'];
final String toTable = '${tableNameForeign}_table';
importForeign.add("import '${toTable.snakeCase}.dart';");
constructor.add('this.${toTable.camelCase},');
variables.add('final ${toTable.pascalCase}? ${toTable.camelCase};');
props.add(toTable.camelCase);
paramCopyWith.add('${toTable.pascalCase}? ${toTable.camelCase},');
valueCopyWith.add(
'${toTable.camelCase}: ${toTable.camelCase} ?? this.${toTable.camelCase},');
List<String> fromMapWithJoinForign = [];
final Map references = tables[tableNameForeign];
final Map columnsForeign = references['column'];
columnsForeign.forEach((columnName, columnForeign) {
if (columnForeign is String) {
final type = getType(columnForeign);
final isTypeBool = type == 'bool';
fromMapWithJoinForign.add(
"${columnName.toString().camelCase}: map['${isTypeBool ? '${tableNameForeign.toString().snakeCase}}.${columnName.toString().snakeCase}} == 1' : '${tableNameForeign.toString().snakeCase}.${columnName.toString().snakeCase}'}'],");
} else if (columnForeign is Map) {
final isRequired =
columnForeign['constraint']?.toString().toUpperCase() ==
'PRIMARY KEY' ||
columnForeign['nullable']?.toString() == 'false';
final type = getType(columnForeign['type']?.toString() ?? '');
final isTypeBool = type == 'bool';
fromMapWithJoinForign.add(
"${columnName.toString().camelCase}: map['${isTypeBool ? '${tableNameForeign.toString().snakeCase}}.${columnName.toString().snakeCase}} == 1' : '${tableNameForeign.toString().snakeCase}.${columnName.toString().snakeCase}'}']${isRequired ? ' ?? ${getDefaultType(type)}' : ''},");
}
});
toMap.add(
"if (withJoin) '${toTable.snakeCase}': ${toTable.camelCase}?.toMap(withJoin: withJoin),");
fromMap.add(
"${toTable.camelCase}: !withJoin || map['${toTable.snakeCase}'] == null ? null : ${toTable.pascalCase}.fromMap(map['${toTable.snakeCase}'], withJoin: withJoin),");
fromMapWithJoin.add(
'''${toTable.camelCase}: map['${tableName.toString().snakeCase}.${foreignName.toString().snakeCase}'] == null ? null : ${toTable.pascalCase}(
${fromMapWithJoinForign.join('\n')}
),''');
});
callback.call(importForeign, constructor, constructorAssign, variables,
toMap, fromMap, fromMapWithJoin, props, paramCopyWith, valueCopyWith);
}