init method

void init()

Implementation

void init() {
  dbVersion = dbVersion ?? 1;
  final manyToManyTables = <SqfEntityTableBase>[];
  //print('ModelBase init() begin');
  for (final table in databaseTables!) {
    if (table.dbModel == null) {
      if (instanceName == null) {
        table.dbModel = toCamelCase(table.tableName!);
      } else {
        table.dbModel = toCamelCase(instanceName!);
      }
    }
    //print('CHECK AND CONFIGURE MANY_TO_MANY RELATIONS');
    // CHECK AND CONFIGURE MANY_TO_MANY RELATIONS
    for (final field in table.fields!.whereType<SqfEntityFieldRelationshipBase>()) {
      // if (field.relationType == RelationType.ONE_TO_MANY_VICEVERSA) {
      //   field.relationType = RelationType.ONE_TO_MANY;
      //   databaseTables!
      //       .singleWhere((t) => t.tableName == field.table!.tableName!)
      //     ..fields!.add(SqfEntityFieldRelationshipBase(
      //         table, field.deleteRule,
      //         relationType: RelationType.ONE_TO_MANY));
      //   field.table!.init();
      // } else
      if (field.relationType == RelationType.MANY_TO_MANY) {
        print('found RelationShip ManyToMany');
        final many2ManyTableName = field.manyToManyTableName ?? '${table.tableName}${toCamelCase(field.table!.tableName!)}';
        table.relationType = table.relationType != RelationType.ONE_TO_ONE ? null : table.relationType;
        field.table!.relationType = field.table!.relationType != RelationType.ONE_TO_ONE ? null : field.table!.relationType;
        field.manyToManyTableName = many2ManyTableName;

        final many2manyTableExist = databaseTables!.where((t) => t.tableName!.toLowerCase() == many2ManyTableName.toLowerCase()).toList();
        SqfEntityTableBase many2manyTable;
        if (many2manyTableExist.isNotEmpty) {
          many2manyTable = many2manyTableExist[0];
        } else {
          many2manyTable = SqfEntityTableBase()
            ..tableName = many2ManyTableName
            ..modelName = toModelName(many2ManyTableName, many2ManyTableName)
            ..primaryKeyName = ''
            ..primaryKeyType = null
            ..relationType = RelationType.MANY_TO_MANY
            ..useSoftDeleting = table.useSoftDeleting
            ..fields = [SqfEntityFieldRelationshipBase(table, field.deleteRule, fieldName: table.primaryKeyNames[0] == field.table!.primaryKeyNames[0] ? '${table.tableName}${toCamelCase(table.primaryKeyNames[0])}' : table.primaryKeyNames[0], isPrimaryKeyField: true, relationType: RelationType.ONE_TO_MANY), SqfEntityFieldRelationshipBase(field.table, field.deleteRule, fieldName: table.primaryKeyNames[0] == field.table!.primaryKeyNames[0] ? '${field.table!.tableName}${toCamelCase(field.table!.primaryKeyNames[0])}' : field.table!.primaryKeyNames[0], isPrimaryKeyField: true, relationType: RelationType.ONE_TO_MANY)]
            ..init();
          manyToManyTables.add(many2manyTable);
        }
        if (databaseTables!.length > 1) {
          if (field.table!.fields!.where((f) => f is SqfEntityFieldRelationshipBase && f.relationType == RelationType.MANY_TO_MANY && f.table == table).isEmpty) {
            databaseTables!.singleWhere((t) => t.tableName == field.table!.tableName!)..fields!.add(SqfEntityFieldRelationshipBase(table, field.deleteRule, manyToManyTableName: many2ManyTableName, manyToManyTable: many2manyTable, relationType: RelationType.MANY_TO_MANY));
            field.table!.init();
          }
        }
        field.manyToManyTable = many2manyTable;
        final rFieldList = field.table!.fields!.whereType<SqfEntityFieldRelationshipBase>().toList();
        if (rFieldList.isNotEmpty) {
          for (final f in rFieldList) {
            if (f.relationType != null && f.relationType == RelationType.MANY_TO_MANY && f.table != null && f.table == table) {
              f.manyToManyTable = many2manyTable;
            }
          }
        }
      } else if (field.relationType == RelationType.ONE_TO_ONE && (field.table != null && field.table != table)) {
        table.relationType = RelationType.ONE_TO_ONE;
      }
    }
  }
  //print('before final table in manyToManyTables lenght=${manyToManyTables.length}');
  for (final table in manyToManyTables) {
    databaseTables!.add(table);
  }
  for (final table in databaseTables!) {
    table.collections = _getCollections(table, this);
    // databaseTables!.add(table);
  }
}