write method

void write()

Implementation

void write() {
  // Write referenced tables and views
  for (final table in db.tables) {
    TableWriter(table, scope.child()).writeInto();
  }
  for (final view in db.views) {
    ViewWriter(view, scope.child(), this).write();
  }

  // Write the database class
  final dbScope = scope.child();

  final className = dbClassName;
  final firstLeaf = dbScope.leaf();
  final isAbstract = !scope.generationOptions.isGeneratingForSchema;
  if (isAbstract) {
    firstLeaf.write('abstract ');
  }

  firstLeaf.write('class $className extends GeneratedDatabase {\n'
      '$className(QueryExecutor e) : '
      'super(SqlTypeSystem.defaultInstance, e); \n');

  if (dbScope.options.generateConnectConstructor) {
    firstLeaf.write(
        '$className.connect(DatabaseConnection c): super.connect(c); \n');
  }

  final entityGetters = <MoorSchemaEntity, String>{};

  for (final entity in db.entities) {
    final getterName = entity.dbGetterName;
    if (getterName != null) {
      entityGetters[entity] = getterName;
    }

    if (entity is MoorTable) {
      final tableClassName = entity.entityInfoName;

      writeMemoizedGetter(
        buffer: dbScope.leaf(),
        getterName: entity.dbGetterName,
        returnType: tableClassName,
        code: '$tableClassName(this)',
        options: scope.generationOptions,
      );
    } else if (entity is MoorTrigger) {
      writeMemoizedGetter(
        buffer: dbScope.leaf(),
        getterName: entity.dbGetterName,
        returnType: 'Trigger',
        code: 'Trigger(${asDartLiteral(entity.createSql(scope.options))}, '
            '${asDartLiteral(entity.displayName)})',
        options: scope.generationOptions,
      );
    } else if (entity is MoorIndex) {
      writeMemoizedGetter(
        buffer: dbScope.leaf(),
        getterName: entity.dbGetterName,
        returnType: 'Index',
        code: 'Index(${asDartLiteral(entity.displayName)}, '
            '${asDartLiteral(entity.createSql(scope.options))})',
        options: scope.generationOptions,
      );
    } else if (entity is MoorView) {
      writeMemoizedGetter(
        buffer: dbScope.leaf(),
        getterName: entity.dbGetterName,
        returnType: entity.entityInfoName,
        code: '${entity.entityInfoName}(this)',
        options: scope.generationOptions,
      );
    }
  }

  // Write fields to access an dao. We use a lazy getter for that.
  for (final dao in db.daos) {
    final typeName = dao.codeString(scope.generationOptions);
    final getterName = ReCase(typeName).camelCase;
    final databaseImplName = db.fromClass!.name;

    writeMemoizedGetter(
      buffer: dbScope.leaf(),
      getterName: getterName,
      returnType: typeName,
      code: '$typeName(this as $databaseImplName)',
      options: scope.generationOptions,
    );
  }

  // Write implementation for query methods
  db.queries?.forEach((query) => QueryWriter(dbScope.child()).write(query));

  // Write List of tables
  final schemaScope = dbScope.leaf();
  schemaScope
    ..write('@override\nIterable<TableInfo> get allTables => ')
    ..write('allSchemaEntities.whereType<TableInfo>();\n')
    ..write('@override\nList<DatabaseSchemaEntity> get allSchemaEntities ')
    ..write('=> [');

  schemaScope
    ..write(db.entities.map((e) {
      if (e is SpecialQuery) {
        final sql = e.formattedSql(scope.options);
        return 'OnCreateQuery(${asDartLiteral(sql)})';
      }

      return entityGetters[e];
    }).join(', '))
    // close list literal and allSchemaEntities getter
    ..write('];\n');

  final updateRules = FindStreamUpdateRules(db).identifyRules();
  if (updateRules.rules.isNotEmpty) {
    schemaScope
      ..write('@override\nStreamQueryUpdateRules get streamUpdateRules => ')
      ..write('const StreamQueryUpdateRules([');

    for (final rule in updateRules.rules) {
      rule.writeConstructor(schemaScope);
      schemaScope.write(', ');
    }

    schemaScope.write('],);\n');
  }

  if (scope.generationOptions.isGeneratingForSchema) {
    final version = scope.generationOptions.forSchema;

    schemaScope
      ..writeln('@override')
      ..writeln('int get schemaVersion => $version;');
  }

  // close the class
  schemaScope.write('}\n');
}