writeColumnGetter method

void writeColumnGetter(
  1. MoorColumn column,
  2. GenerationOptions options,
  3. bool isOverride
)

Implementation

void writeColumnGetter(
    MoorColumn column, GenerationOptions options, bool isOverride) {
  final isNullable = column.nullable;
  final additionalParams = <String, String>{};
  final expressionBuffer = StringBuffer();
  final constraints = defaultConstraints(column);

  for (final feature in column.features) {
    if (feature is LimitingTextLength) {
      final buffer = StringBuffer('GeneratedColumn.checkTextLength(');

      if (feature.minLength != null) {
        buffer.write('minTextLength: ${feature.minLength},');
      }
      if (feature.maxLength != null) {
        buffer.write('maxTextLength: ${feature.maxLength}');
      }
      buffer.write(')');

      additionalParams['additionalChecks'] = buffer.toString();
    }

    if (feature is DartCheckExpression) {
      additionalParams['check'] = '() => ${feature.dartExpression}';
    }
  }

  additionalParams['type'] = 'const ${column.sqlType().runtimeType}()';

  if (tableOrView is MoorTable) {
    additionalParams['requiredDuringInsert'] = (tableOrView as MoorTable)
        .isColumnRequiredForInsert(column)
        .toString();
  }

  if (column.customConstraints != null) {
    additionalParams['\$customConstraints'] =
        asDartLiteral(column.customConstraints!);
  } else if (constraints.isNotEmpty) {
    // Use the default constraints supported by drift
    additionalParams['defaultConstraints'] = asDartLiteral(constraints);
  }

  if (column.defaultArgument != null) {
    additionalParams['defaultValue'] = column.defaultArgument!;
  }

  if (column.clientDefaultCode != null) {
    additionalParams['clientDefault'] = column.clientDefaultCode!;
  }

  if (column.generatedAs != null) {
    final generateAs = column.generatedAs!;
    final code = generateAs.dartExpression;

    if (code != null) {
      additionalParams['generatedAs'] =
          'GeneratedAs($code, ${generateAs.stored})';
    }
  }

  final innerType = column.innerColumnType(options);
  var type = 'GeneratedColumn<$innerType>';
  expressionBuffer
    ..write(type)
    ..write(
        '(${asDartLiteral(column.name.name)}, aliasedName, $isNullable, ');

  var first = true;
  additionalParams.forEach((name, value) {
    if (!first) {
      expressionBuffer.write(', ');
    } else {
      first = false;
    }

    expressionBuffer
      ..write(name)
      ..write(': ')
      ..write(value);
  });

  expressionBuffer.write(')');

  final converter = column.typeConverter;
  if (converter != null) {
    // Generate a GeneratedColumnWithTypeConverter instance, as it has
    // additional methods to check for equality against a mapped value.
    final mappedType = converter.mappedType.codeString(options);
    type = 'GeneratedColumnWithTypeConverter<$mappedType, $innerType>';
    expressionBuffer
      ..write('.withConverter<')
      ..write(mappedType)
      ..write('>(')
      ..write(converter.tableAndField)
      ..write(')');
  }

  writeMemoizedGetter(
    buffer: buffer,
    getterName: column.dartGetterName,
    returnType: type,
    code: expressionBuffer.toString(),
    options: options,
    hasOverride: isOverride,
  );
}