parseWhereClause function

WhereClauseGroup parseWhereClause(
  1. String whereClause
)

Converts a SelectStatement to a SQL SELECT statement.

Implementation

WhereClauseGroup parseWhereClause(String whereClause) {
  if (whereClause.isEmpty) {
    return const WhereClauseGroup([]);
  }

  final elements = <WhereClauseElement>[];
  final tokens = _tokenizeWhereClause(whereClause);

  for (var i = 0; i < tokens.length; i++) {
    switch (tokens[i].toUpperCase()) {
      case '(':
        elements.add(GroupingOperator.open);
      case ')':
        elements.add(GroupingOperator.close);
      case 'AND':
        elements.add(LogicalOperator.and);
      case 'OR':
        elements.add(LogicalOperator.or);
      default:
        // Find the next logical operator, parenthesis, or end of clause
        final nextSpecialIndex = tokens.indexWhere(
          (t) => ['AND', 'OR', '(', ')'].contains(t.toUpperCase()),
          i + 1,
        );
        final conditionEndIndex =
            nextSpecialIndex == -1 ? tokens.length : nextSpecialIndex;

        // Parse the condition
        elements.add(_parseCondition(tokens.sublist(i, conditionEndIndex)));

        // Move the index to the end of this condition
        i = conditionEndIndex - 1;
    }
  }

  return WhereClauseGroup(elements);
}