writeInto method

  1. @override
void writeInto(
  1. GenerationContext context
)
override

Writes this component into the context by writing to its GenerationContext.buffer or by introducing bound variables. When writing into the buffer, no whitespace around the this component should be introduced. When a component consists of multiple composed component, it's responsible for introducing whitespace between its child components.

Implementation

@override
void writeInto(GenerationContext context) {
  if (!context.supportsVariables ||
      // Workaround for https://github.com/simolus3/drift/issues/2441
      // Binding nulls on postgres is currently untyped which causes issues.
      (value == null && context.dialect == SqlDialect.postgres)) {
    // Write as constant instead.
    Constant<T>(value).writeInto(context);
    return;
  }

  var explicitStart = context.explicitVariableIndex;

  var mark = '?';
  var suffix = '';
  if (context.dialect == SqlDialect.postgres) {
    explicitStart = 1;
    mark = r'$';
  }

  if (explicitStart != null) {
    context.buffer
      ..write(mark)
      ..write(explicitStart + context.amountOfVariables)
      ..write(suffix);
    context.introduceVariable(
      this,
      mapToSimpleValue(context),
    );
  } else {
    context.buffer.write(mark);
    context.introduceVariable(this, mapToSimpleValue(context));
  }
}