readingCode method

String readingCode(
  1. ResultColumn column,
  2. GenerationOptions generationOptions,
  3. MoorOptions moorOptions
)

Returns Dart code that, given a variable of type QueryRow named row in the same scope, reads the column from that row and brings it into a suitable type.

Implementation

String readingCode(ResultColumn column, GenerationOptions generationOptions,
    MoorOptions moorOptions) {
  var rawDartType = dartTypeNames[column.type];
  if (column.nullable && generationOptions.nnbd) {
    rawDartType = '$rawDartType?';
  }

  String? specialName;
  if (options.newSqlCodeGeneration) {
    specialName = _transformer.newNameFor(column.sqlParserColumn!);
  }

  final dartLiteral = asDartLiteral(specialName ?? column.name);
  var code = 'row.read<$rawDartType>($dartLiteral)';

  if (column.typeConverter != null) {
    final nullableDartType = moorOptions.nullAwareTypeConverters
        ? column.typeConverter!.hasNullableDartType
        : column.nullable;
    final needsAssert = !nullableDartType && generationOptions.nnbd;

    final converter = column.typeConverter;
    code = '${_converter(converter!)}.mapToDart($code)';
    if (needsAssert) code += '!';
  }
  return code;
}