write method

void write()

Implementation

void write() {
  final parentClass = table.customParentClass ?? 'DataClass';
  _buffer.write('class ${table.dartTypeName} extends $parentClass ');

  if (isInsertable) {
    // The data class is only an insertable if we can actually insert rows
    // into the target entity.
    _buffer.writeln('implements Insertable<${table.dartTypeName}> {');
  } else {
    _buffer.writeln('{');
  }

  // write view columns
  final view = table;
  if (view is MoorView && view.viewQuery != null) {
    columns.addAll(view.viewQuery!.columns.map((e) => e.value));
  } else {
    columns.addAll(table.columns);
  }

  // write individual fields
  for (final column in columns) {
    if (column.documentationComment != null) {
      _buffer.write('${column.documentationComment}\n');
    }
    final modifier = scope.options.fieldModifier;
    _buffer.write('$modifier ${column.dartTypeCode(scope.generationOptions)} '
        '${column.dartGetterName}; \n');
  }

  // write constructor with named optional fields
  _buffer
    ..write(table.dartTypeName)
    ..write('({')
    ..write(columns.map((column) {
      final nullableDartType = column.typeConverter != null &&
              scope.options.nullAwareTypeConverters
          ? column.typeConverter!.hasNullableDartType
          : column.nullable;

      if (nullableDartType) {
        return 'this.${column.dartGetterName}';
      } else {
        return '${scope.required} this.${column.dartGetterName}';
      }
    }).join(', '))
    ..write('});');

  // Also write parsing factory
  _writeMappingConstructor();

  if (isInsertable) {
    _writeToColumnsOverride();
    if (scope.options.dataClassToCompanions) {
      _writeToCompanion();
    }
  }

  // And a serializer and deserializer method
  _writeFromJson();
  _writeToJson();

  // And a convenience method to copy data from this class.
  _writeCopyWith();

  _writeToString();
  _writeHashCode();

  overrideEquals(
      columns.map((c) => c.dartGetterName), table.dartTypeName, _buffer);

  // finish class declaration
  _buffer.write('}');
}