build method

  1. @protected
  2. @override
String build({
  1. String? aggregateFunction,
  2. String? aggregateColumn,
})
inherited

Implementation

@protected
@override
String build({String? aggregateFunction, String? aggregateColumn}) {
  String sql = '';

  if (selectColumns.length > 1) {
    selectColumns.remove('*');
  }

  String withClause = buildWithClause();
  if (withClause.isNotEmpty) {
    sql = '$withClause ';
  }

  if (getTable.isNotEmpty) {
    if (aggregateFunction != null && aggregateColumn != null) {
      sql += "SELECT $aggregateFunction($aggregateColumn) FROM $getTable";
    } else {
      sql +=
          "SELECT ${selectColumns.isEmpty ? "*" : selectColumns.join(", ")} FROM $getTable";
    }

    if (joins.isNotEmpty) {
      sql += " ${joins.join(" ")}";
    }
    sql += conditions.isNotEmpty ? " WHERE ${conditions.join(" ")}" : "";

    if (unions.isNotEmpty) {
      sql += " ${unions.join(" ")}";
    }

    if (aggregateFunction == null && aggregateColumn == null) {
      if (_groupBy.isNotEmpty) {
        sql += " GROUP BY ${_groupBy.join(", ")}";
      }
      if (_having.isNotEmpty) {
        sql += " HAVING ${_having.join(" ")}";
      }
      if (_orderBy.isNotEmpty) {
        sql += " ORDER BY ${_orderBy.join(", ")}";
      }

      sql += (_limit != null) ? " LIMIT $_limit" : "";
      sql += (_offset != null) ? " OFFSET $_offset" : "";
    }
  } else if (conditions.isNotEmpty) {
    sql += conditions.join(" ");
  } else {
    sql += '';
  }

  return sql.trim();
}