process method

  1. @override
Query process()
override

Implementation

@override
Query process() {
  _assertNoNullableParameters();

  final indices = <String, int>{};
  final fixedParameters = <String>{};
  //map parameters to index (1-based) or 0 (if its a list)
  int currentIndex = 1;
  for (final parameter in _parameters) {
    if (parameter.type.isDartCoreList) {
      indices[':${parameter.name}'] = 0;
    } else {
      fixedParameters.add(parameter.name);
      indices[':${parameter.name}'] = currentIndex++;
    }
  }

  //get List of query variables
  final variables = findVariables(_query);
  _assertAllParametersAreUsed(variables);

  final newQuery = StringBuffer();
  final listParameters = <ListParameter>[];
  // iterate over all found variables, replace them with their assigned
  // numbered variable (?1,?2,...) or a placeholder if the variable is a list.
  // the list variables have to be handled in the writer, so write down their
  // positions and names.
  int currentLast = 0;
  for (final varToken in variables) {
    newQuery.write(_query
        .substring(currentLast, varToken.startPosition)
        .replaceAll('\n', ' '));
    final varIndexInMethod = indices[varToken.name];
    if (varIndexInMethod == null) {
      throw _processorError.unknownQueryVariable(varToken.name);
    } else if (varIndexInMethod > 0) {
      //normal variable/parameter
      if (varToken.isListVar)
        throw _processorError
            .queryMethodParameterIsNormalButVariableIsList(varToken.name);
      newQuery.write('?');
      newQuery.write(varIndexInMethod);
    } else {
      //list variable/parameter
      if (!varToken.isListVar)
        throw _processorError
            .queryMethodParameterIsListButVariableIsNot(varToken.name);
      listParameters
          .add(ListParameter(newQuery.length, varToken.name.substring(1)));
      newQuery.write(varlistPlaceholder);
    }
    currentLast = varToken.endPosition;
  }
  newQuery.write(_query.substring(currentLast).replaceAll('\n', ' '));

  return Query(
    newQuery.toString(),
    listParameters,
  );
}