compileWheres method

  1. @override
String compileWheres(
  1. List<Map<String, dynamic>> wheres
)
override

Compile the "where" portion of the query.

Implementation

@override
String compileWheres(List<Map<String, dynamic>> wheres) {
  if (wheres.isEmpty) return '';

  final sql = wheres
      .map((where) {
        final boolean = where['boolean'];
        final type = where['type'];
        String segment = '';

        if (type == 'Basic') {
          segment = '${wrap(where['column'])} ${where['operator']} ?';
        } else if (type == 'Null') {
          segment = '${wrap(where['column'])} IS NULL';
        } else if (type == 'NotNull') {
          segment = '${wrap(where['column'])} IS NOT NULL';
        } else if (type == 'In') {
          final values = (where['values'] as List).map((_) => '?').join(', ');
          segment = '${wrap(where['column'])} IN ($values)';
        } else if (type == 'NotIn') {
          final values = (where['values'] as List).map((_) => '?').join(', ');
          segment = '${wrap(where['column'])} NOT IN ($values)';
        } else if (type == 'Between') {
          segment = '${wrap(where['column'])} BETWEEN ? AND ?';
        } else if (type == 'NotBetween') {
          segment = '${wrap(where['column'])} NOT BETWEEN ? AND ?';
        } else if (type == 'Column') {
          segment =
              '${wrap(where['first'])} ${where['operator']} ${wrap(where['second'])}';
        } else if (type == 'BetweenColumns') {
          segment =
              '${wrap(where['column'])} BETWEEN ${wrap(where['start'])} AND ${wrap(where['end'])}';
        } else if (type == 'NotBetweenColumns') {
          segment =
              '${wrap(where['column'])} NOT BETWEEN ${wrap(where['start'])} AND ${wrap(where['end'])}';
        } else if (type == 'Date') {
          segment = 'DATE(${wrap(where['column'])}) ${where['operator']} ?';
        } else if (type == 'Year') {
          segment =
              'strftime(\'%Y\', ${wrap(where['column'])}) ${where['operator']} ?';
        } else if (type == 'Month') {
          segment =
              'strftime(\'%m\', ${wrap(where['column'])}) ${where['operator']} ?';
        } else if (type == 'Day') {
          segment =
              'strftime(\'%d\', ${wrap(where['column'])}) ${where['operator']} ?';
        } else if (type == 'Raw') {
          segment = where['sql'];
        } else if (type == 'Nested') {
          final nestedSql = compileWheres(where['query'].wheres);
          if (nestedSql.isNotEmpty) {
            final cleanedSql = nestedSql
                .replaceFirst('WHERE ', '')
                .replaceFirst(RegExp(r'^(AND|OR) '), '');
            segment = '($cleanedSql)';
          }
        } else if (type == 'Exists') {
          final subquery = (where['query'] as dynamic).toSql();
          segment = 'EXISTS ($subquery)';
        } else if (type == 'NotExists') {
          final subquery = (where['query'] as dynamic).toSql();
          segment = 'NOT EXISTS ($subquery)';
        } else if (type == 'InSub') {
          final subquery = (where['query'] as dynamic).toSql();
          segment = '${wrap(where['column'])} IN ($subquery)';
        } else if (type == 'NotInSub') {
          final subquery = (where['query'] as dynamic).toSql();
          segment = '${wrap(where['column'])} NOT IN ($subquery)';
        }

        if (segment.isEmpty) return '';
        return '$boolean $segment';
      })
      .where((s) => s.isNotEmpty)
      .join(' ');

  if (sql.isEmpty) return '';

  return 'WHERE ' + sql.replaceFirst(RegExp(r'^(AND|OR) '), '');
}