Compile the "union" queries attached to the main query.
971979
function _union() {
    var onlyUnions = this.onlyUnions();
    var unions = this.grouped.union;
    if (!unions) {
      return '';
    }
    var sql = '';
    for (var i = 0, l = unions.length; i < l; i++) {
      var union = unions[i];
      if (i > 0) {
        sql += ' ';
      }
      if (i > 0 || !onlyUnions) {
        sql += union.clause + ' ';
      }
      var statement = this.formatter.rawOrFn(union.value);
      if (statement) {
        if (union.wrap) {
          sql += '(';
        }
        sql += statement;
        if (union.wrap) {
          sql += ')';
        }
      }
    }
    return sql;
  }
1187708
protected function compileJoins(Builder $query, $joins)
    {
        return collect($joins)->map(function ($join) use ($query) {
            $table = $this->wrapTable($join->table);

            return trim("{$join->type} join {$table} {$this->compileWheres($join)}");
        })->implode(' ');
    }
899791
function() {
    var queries = new nodes.QueryList
      , skip = ['comment', 'newline', 'space'];

    do {
      this.skip(skip);
      queries.push(this.query());
      this.skip(skip);
    } while (this.accept(','));
    return queries;
  }
393944
protected function compileJoins(Builder $query, $joins)
    {
        return collect($joins)->map(function ($join) use ($query) {
            $table = $this->wrapTable($join->table);

            $nestedJoins = is_null($join->joins) ? '' : ' '.$this->compileJoins($query, $join->joins);

            $tableAndNestedJoins = is_null($join->joins) ? $table : '('.$table.$nestedJoins.')';

            return trim("{$join->type} join {$tableAndNestedJoins} {$this->compileWheres($join)}");
        })->implode(' ');
    }
447699
def __build_query(self, routine, parameters):
        """"""

        parameter_names = []
        replacements = {}
        for i, value in enumerate(parameters):
            name = 'arg' + str(i)

            parameter_names.append(name)
            replacements[name] = value

        parameter_pieces = []
        for i, name in enumerate(parameter_names):
            if i > 0:
                parameter_pieces.append(', ')

            # Embed encoded blobs directly.
            if issubclass(replacements[name].__class__, _BlobArgumentWrapper) is True:
                parameter_pieces.append(replacements[name].encoded)
                del replacements[name]
            else:
                parameter_pieces.append(':' + name)

        query = "CALL " + routine + "(" + ''.join(parameter_pieces) + ")"

        return (query, replacements)
529087
def _merge_wheres_to_has(self, has_query, relation):
        
        relation_query = relation.get_base_query()

        has_query.merge_wheres(relation_query.wheres, relation_query.get_bindings())

        self._query.merge_bindings(has_query.get_query())
135096
final protected function build_query(){
        $select = empty($this->_select) ? $this->default_select() : implode(', ', $this->_select);
        $calc = $this->_calc_rows ? ' SQL_CALC_FOUND_ROWS' : '';
        $distinct = $this->_distinct ? sprintf('DISTINCT(%s)', $this->escape_column_name($this->_distinct)) : '';
        $from = empty($this->_from) ? $this->table : $this->_from;
        // Build query
        $sql = <<<SQL
          SELECT{$calc} {$distinct} {$select} FROM {$from}
SQL;
        // Add join
        $join = [];
        if( !empty($this->_join) ){
            $join = $this->_join;
        }else{
            $default_join = $this->default_join();
            if( !empty($default_join) ){
                $join = $default_join;
            }
        }
        foreach($join as list($table, $on, $method)){
            $method = 'INNER' === strtoupper($method) ? 'INNER' : 'LEFT';
            $sql .= <<<SQL
            {$method} JOIN {$table}
            ON {$on}
SQL;
        }
        // Add where
        if( !empty($this->_wheres) ){
            $where_clause = $this->build_where($this->_wheres);
            $sql .= <<<SQL
            WHERE {$where_clause}
SQL;
        }
        // Add group by
        if( !empty($this->_group_by) ){
            $group_by = implode(', ', $this->_group_by);
            $sql .= <<<SQL
            GROUP BY {$group_by}
SQL;
        }
        // Add order by
        if( !empty($this->_order_by) ){
            $order_by = implode(', ', $this->_order_by);
            $sql .= <<<SQL
            ORDER BY {$order_by}
SQL;
        }
        // Add limit
        if( !empty($this->_limit) ){
            list($per_page, $offset) = $this->_limit;
            $limit = <<<SQL
            LIMIT %d, %d
SQL;
            $sql .= sprintf($limit, $offset, $per_page);
        }
        return $sql;
    }
1308301
function run(query, params) {
        let stmt = sql.prepare(query);
        params = normalizeParams(params);
        stmt.run(params);
        stmt.free();
    }
1363625
protected static function unionQueriesToOne($queries)
    {
        if (count($queries) === 0) {
            throw new Exception(Module::t('app', 'Nothing to union'));
        }

        $query = array_reduce(
            $queries,
            function ($query, $item) {
                /**
                 * @var $query Query
                 */
                if ($query === null) {
                    $query = $item;
                } else {
                    $query->union($item);
                }
                return $query;
            }
        );

        return $query;
    }
1127144
def queries_start_with(queries, prefixes):
    """"""
    for query in sqlparse.split(queries):
        if query and query_starts_with(query, prefixes) is True:
            return True
    return False
1014997
def _encode_query(query):
    """"""
    if query == '':
        return query
    query_args = []
    for query_kv in query.split('&'):
        k, v = query_kv.split('=')
        query_args.append(k + "=" + quote(v.encode('utf-8')))
    return '&'.join(query_args)
141357
function mergeQueries(query, options) {
  if (options == null) {
    return query;
  }

  Object.keys(options).forEach(key => {
    let label = key;
    let value = options[key];

    validateProp(key, value);

    switch (key) {
    case 'orderByKey':
      label = orderedBy;
      value = orderByKey;
      break;

    case 'orderByPriority':
      label = orderedBy;
      value = orderByPriority;
      break;

    case 'orderByValue':
      label = orderedBy;
      value = orderByValue;
      break;

    case 'orderByChild':
      label = orderedBy;
      break;

    default:
      break;
    }

    query[label] = value;
  });

  return query;
}
84123
def _canonicalize_query(self, query):

        

        def transform_query(q):

            if isinstance(q, dict):
                nq = {}
                for key,value in q.items():
                    nq[key] = transform_query(value)
                return nq
            elif isinstance(q, (list,QuerySet,tuple)):
                return [transform_query(x) for x in q]
            elif isinstance(q,Document):
                collection = self.get_collection_for_obj(q)
                ref = "%s:%s" % (collection,q.pk)
                return ref
            else:
                return q

        return transform_query(query)
889306
protected function compileJoinPart(Query $query, array &$bindings): string
    {
        $joinsSQL = [];

        foreach ($query->join as $join) {
            $joinsSQL[] = $this->compileOneJoin($join, $bindings);
        }

        return $this->implodeSQL($joinsSQL);
    }
1019022
def merge(self, query):
        
        self.columns += query.columns
        self.joins += query.joins
        self.wheres += query.wheres
        self.groups += query.groups
        self.havings += query.havings
        self.orders += query.orders
        self.distinct_ = query.distinct_

        if self.columns:
            self.columns = Collection(self.columns).unique().all()

        if query.limit_:
            self.limit_ = query.limit_

        if query.offset_:
            self.offset_ = None

        self.unions += query.unions

        if query.union_limit:
            self.union_limit = query.union_limit

        if query.union_offset:
            self.union_offset = query.union_offset

        self.union_orders += query.union_orders

        self.merge_bindings(query)
777392
def compile_query(query: dict) -> PyQuery:
    

    def _pair_to_dict(pair):
        if isinstance(pair, dict):
            return pair
        return {pair[0]: pair[1]}

    def _reduce(pair):
        try:
            return {'and': (_pair_to_dict(pair[0]), _pair_to_dict(pair[1]))}
        except (KeyError, IndexError):
            return _pair_to_dict(pair)

    def _get_first_pair(d: dict):
        return list(query.items())[0]

    if len(query) < 2:
        return compile_single_item(*_get_first_pair(query))
    try:
        reduced = reduce(lambda x, y: _reduce((x, y)), query.items())
    except KeyError:
        print(query)
        reduced = query
        raise
    return compile_single_item(*_get_first_pair(reduced))
648033
def build_db_query(fields_names, field_values):
    
    if isinstance(field_values, string_types):
        field_values = [field_values]

    if len(fields_names) != len(field_values):
        raise ValueError('Error: unable to build a primary key query due '
                         'to mismatch in number of fields {0} vs {1}'
                         .format(len(fields_names), len(field_values)))

    query = dict()
    for k, v in zip(fields_names, field_values):
        query[k] = v
    return query