toSqfEntityTable function

SqfEntityTableBase? toSqfEntityTable(
  1. DartObject obj,
  2. String dbModelName, {
  3. List<SqfEntityFieldType>? defaultColumns,
})

convert dartObject to SqfEntityTableBase

Implementation

SqfEntityTableBase? toSqfEntityTable(DartObject obj, String dbModelName,
    {List<SqfEntityFieldType>? defaultColumns}) {
  final String? _tableName = getStringValue(obj, 'tableName');

  if (_tableName == null) {
    print('__________toSqfEntityTable() returned null');
    return null;
  }
  final table = SqfEntityTables.tableList
      .where((t) => t.tableName == _tableName && (t.dbModel == dbModelName));
  if (table.isNotEmpty) {
    // print('--------TABLE FOUND: ${table.toList().first.tableName}');
    table.toList().first..dbModel = dbModelName;

    return table.toList().first;
  }
  print('------TABLE RECOGNIZING: $_tableName');
  final newTable = SqfEntityTableBase()
    ..tableName = _tableName
    ..useSoftDeleting = ifExistTableProperty(obj, 'useSoftDeleting')
        ? getBoolValue(obj, 'useSoftDeleting')
        : false
    ..fields = toFields(
        getListValue(obj, 'fields')!,
        dbModelName //keepFieldNamesAsOriginal
        ,
        _tableName,
        defaultColumns: defaultColumns)
    ..primaryKeyType = getTypeValue(obj, 'primaryKeyType') as PrimaryKeyType?
    ..objectType = getTypeValue(obj, 'objectType') as ObjectType?
    ..defaultJsonUrl = getStringValue(obj, 'defaultJsonUrl')
    ..modelName = getStringValue(obj, 'modelName')
    ..dbModel = dbModelName
    ..formListTitleField = getStringValue(obj, 'formListTitleField')
    ..formListSubTitleField = getStringValue(obj, 'formListSubTitleField')
    ..customCode = getStringValue(obj, 'customCode')
    ..primaryKeyName = getStringValue(obj, 'primaryKeyName')
    ..abstractModelName = getStringValue(obj, 'abstractModelName');

  newTable.primaryKeyName = getStringValue(obj, 'primaryKeyName') == null &&
          newTable.objectType == ObjectType.table
      ? newTable.primaryKeyNames.isEmpty
          ? '${_tableName}Id'
          : ''
      : getStringValue(obj, 'primaryKeyName');

  if (defaultColumns != null && defaultColumns.isNotEmpty) {
    print('adding default columns on ${newTable.tableName}');
    for (SqfEntityFieldType defaultField in defaultColumns) {
      final SqfEntityFieldBase newField = SqfEntityFieldBase(
          defaultField.fieldName, defaultField.dbType,
          defaultValue: defaultField.defaultValue,
          minValue: defaultField.minValue,
          maxValue: defaultField.maxValue,
          sequencedBy: defaultField.sequencedBy,
          isPrimaryKeyField: defaultField.isPrimaryKeyField,
          primaryKeyIndex: defaultField.primaryKeyIndex,
          isNotNull: defaultField.isNotNull,
          isUnique: defaultField.isUnique,
          isIndex: defaultField.isIndex,
          isIndexGroup: defaultField.isIndexGroup,
          checkCondition: defaultField.checkCondition,
          collate: defaultField.collate,
          formLabelText: defaultField.formLabelText);
      if (!newTable.primaryKeyNames
              .any((element) => element == defaultField.fieldName) &&
          !newTable.fields!
              .any((element) => element.fieldName == defaultField.fieldName)) {
        print(
            'added: ${defaultField.fieldName}, defaultField table=${defaultField.table != null ? defaultField.table!.tableName : 'null'}');
        newTable.fields!.add(newField);
      } else {
        print(
            'The field [${defaultField.fieldName}] has not been added due to conflicts with other columns with the same name in the table.');
      }
    }
  }
  newTable.init();
  SqfEntityTables.add(newTable);

  print(
      'SqfEntityModelBuilder: $dbModelName/$_tableName added to SqfEntityTables');
  return newTable;
}