compileSelect method

  1. @override
String compileSelect(
  1. Map<String, dynamic> query
)
override

Compile a select query into SQL.

Implementation

@override
String compileSelect(Map<String, dynamic> query) {
  final components = <String>[];

  // Columns
  final columns = query['columns'] as List<dynamic>?;
  final distinct = query['distinct'] as bool? ?? false;
  final select = distinct ? 'SELECT DISTINCT' : 'SELECT';

  if (columns == null || columns.isEmpty) {
    components.add('$select *');
  } else {
    components.add('$select ${columns.map((c) {
      if (c is Map && c['type'] == 'Raw') return c['sql'];
      return wrap(c.toString());
    }).join(', ')}');
  }

  // From
  if (query['table'] != null) {
    components.add(compileFrom(query['table']));
  }

  // Joins
  if (query['joins'] != null) {
    final joins = compileJoins(query['joins']);
    if (joins.isNotEmpty) components.add(joins);
  }

  // Wheres
  if (query['wheres'] != null) {
    final wheres = compileWheres(query['wheres']);
    if (wheres.isNotEmpty) {
      components.add(wheres);
    }
  }

  // Groups
  if (query['groups'] != null) {
    final groups = compileGroups(query['groups']);
    if (groups.isNotEmpty) components.add(groups);
  }

  // Havings
  if (query['havings'] != null) {
    final havings = compileHavings(query['havings']);
    if (havings.isNotEmpty) components.add(havings);
  }

  // Orders
  if (query['orders'] != null) {
    final orders = compileOrders(query['orders']);
    if (orders.isNotEmpty) components.add(orders);
  }

  // Limit
  if (query['limit'] != null) {
    components.add(compileLimit(query['limit']));
  }

  // Offset
  if (query['offset'] != null) {
    components.add(compileOffset(query['offset']));
  }

  // Lock
  if (query['lock'] != null) {
    components.add(query['lock']);
  }

  // Unions
  if (query['unions'] != null && (query['unions'] as List).isNotEmpty) {
    components.add(compileUnions(query['unions']));
  }

  return components.join(' ');
}