writeTableInfoClass method

void writeTableInfoClass()

Implementation

void writeTableInfoClass() {
  buffer = scope.leaf();

  if (!scope.generationOptions.writeDataClasses) {
    // Write a small table header without data class
    buffer.write('class ${table.entityInfoName} extends Table with '
        'TableInfo');
    if (table.isVirtualTable) {
      buffer.write(', VirtualTableInfo');
    }
  } else {
    // Regular generation, write full table class
    final dataClass = table.dartTypeCode(scope.generationOptions);
    final tableDslName = table.fromClass?.name ?? 'Table';

    // class UsersTable extends Users implements TableInfo<Users, User> {
    final typeArgs = '<${table.entityInfoName}, $dataClass>';
    buffer.write('class ${table.entityInfoName} extends $tableDslName with '
        'TableInfo$typeArgs ');

    if (table.isVirtualTable) {
      buffer.write(', VirtualTableInfo$typeArgs ');
    }
  }

  buffer
    ..writeln('{')
    // write a GeneratedDatabase reference that is set in the constructor
    ..writeln('@override final GeneratedDatabase attachedDatabase;')
    ..writeln('final ${scope.nullableType('String')} _alias;')
    ..writeln(
        '${table.entityInfoName}(this.attachedDatabase, [this._alias]);');

  // Generate the columns
  for (final column in table.columns) {
    _writeColumnVerificationMeta(column);
    // Only add an @override to a column getter if we're overriding the column
    // from a Dart DSL class.
    writeColumnGetter(column, scope.generationOptions, !table.isFromSql);
  }

  // Generate $columns, $tableName, asDslTable getters
  writeGetColumnsOverride();
  buffer
    ..write('@override\nString get aliasedName => '
        '_alias ?? \'${table.sqlName}\';\n')
    ..write(
        '@override\n String get actualTableName => \'${table.sqlName}\';\n');

  _writeValidityCheckMethod();
  _writePrimaryKeyOverride();
  _writeUniqueKeyOverride();

  writeMappingMethod(scope);
  // _writeReverseMappingMethod();

  _writeAliasGenerator();

  _writeConvertersAsStaticFields();
  _overrideFieldsIfNeeded();

  // close class
  buffer.write('}');
}