write method

  1. @override
Class write()
override

Implementation

@override
Class write() {
  const databaseFieldName = 'database';
  const changeListenerFieldName = 'changeListener';

  final daoName = dao.name;
  final classBuilder = ClassBuilder()
    ..name = '_\$$daoName'
    ..extend = refer(daoName)
    ..fields
        .addAll(_createFields(databaseFieldName, changeListenerFieldName));

  final databaseParameter = Parameter((builder) => builder
    ..name = databaseFieldName
    ..toThis = true);

  final changeListenerParameter = Parameter((builder) => builder
    ..name = changeListenerFieldName
    ..toThis = true);

  final constructorBuilder = ConstructorBuilder()
    ..requiredParameters.addAll([databaseParameter, changeListenerParameter]);

  final queryMethods = dao.queryMethods;
  if (queryMethods.isNotEmpty) {
    classBuilder
      ..fields.add(Field((builder) => builder
        ..modifier = FieldModifier.final$
        ..name = '_queryAdapter'
        ..type = refer('QueryAdapter')));

    final queriesRequireChangeListener =
        dao.streamEntities.isNotEmpty || dao.streamViews.isNotEmpty;

    constructorBuilder
      ..initializers.add(Code(
          "_queryAdapter = QueryAdapter(database${queriesRequireChangeListener ? ', changeListener' : ''})"));
  }

  final insertionMethods = dao.insertionMethods;
  if (insertionMethods.isNotEmpty) {
    final entities = insertionMethods.map((method) => method.entity).toSet();

    for (final entity in entities) {
      final entityClassName = entity.classElement.displayName;
      final fieldName = '_${entityClassName.decapitalize()}InsertionAdapter';
      final type = refer('InsertionAdapter<$entityClassName>');

      final field = Field((builder) => builder
        ..name = fieldName
        ..type = type
        ..modifier = FieldModifier.final$);

      classBuilder.fields.add(field);

      final valueMapper =
          '(${entity.classElement.displayName} item) => ${entity.valueMapping}';

      final requiresChangeListener =
          dbHasViewStreams || streamEntities.contains(entity);

      constructorBuilder
        ..initializers.add(Code(
            "$fieldName = InsertionAdapter(database, '${entity.name}', $valueMapper${requiresChangeListener ? ', changeListener' : ''})"));
    }
  }

  final updateMethods = dao.updateMethods;
  if (updateMethods.isNotEmpty) {
    final entities = updateMethods.map((method) => method.entity).toSet();

    for (final entity in entities) {
      final entityClassName = entity.classElement.displayName;
      final fieldName = '_${entityClassName.decapitalize()}UpdateAdapter';
      final type = refer('UpdateAdapter<$entityClassName>');

      final field = Field((builder) => builder
        ..name = fieldName
        ..type = type
        ..modifier = FieldModifier.final$);

      classBuilder.fields.add(field);

      final valueMapper =
          '(${entity.classElement.displayName} item) => ${entity.valueMapping}';

      final requiresChangeListener =
          dbHasViewStreams || streamEntities.contains(entity);

      constructorBuilder
        ..initializers.add(Code(
            "$fieldName = UpdateAdapter(database, '${entity.name}', ${entity.primaryKey.fields.map((field) => '\'${field.columnName}\'').toList()}, $valueMapper${requiresChangeListener ? ', changeListener' : ''})"));
    }
  }

  final deleteMethods = dao.deletionMethods;
  if (deleteMethods.isNotEmpty) {
    final entities = deleteMethods.map((method) => method.entity).toSet();

    for (final entity in entities) {
      final entityClassName = entity.classElement.displayName;
      final fieldName = '_${entityClassName.decapitalize()}DeletionAdapter';
      final type = refer('DeletionAdapter<$entityClassName>');

      final field = Field((builder) => builder
        ..name = fieldName
        ..type = type
        ..modifier = FieldModifier.final$);

      classBuilder.fields.add(field);

      final valueMapper =
          '(${entity.classElement.displayName} item) => ${entity.valueMapping}';

      final requiresChangeListener =
          dbHasViewStreams || streamEntities.contains(entity);

      constructorBuilder
        ..initializers.add(Code(
            "$fieldName = DeletionAdapter(database, '${entity.name}', ${entity.primaryKey.fields.map((field) => '\'${field.columnName}\'').toList()}, $valueMapper${requiresChangeListener ? ', changeListener' : ''})"));
    }
  }

  classBuilder
    ..constructors.add(constructorBuilder.build())
    ..methods.addAll(_generateQueryMethods(queryMethods))
    ..methods.addAll(_generateInsertionMethods(insertionMethods))
    ..methods.addAll(_generateUpdateMethods(updateMethods))
    ..methods.addAll(_generateDeletionMethods(deleteMethods))
    ..methods.addAll(_generateTransactionMethods(dao.transactionMethods));

  return classBuilder.build();
}