Generate queries to execute.

        Sets instance variables so that Mongo query strings, etc. can now
        be extracted from the object.

        :param constraint_spec: Constraint specification
        :type constraint_spec: ConstraintSpec
511577
def _build(self, constraint_spec):
        
        self._sections = []

        # For each condition in the spec

        for sval in constraint_spec:
            rpt_fld = self._base_report_fields.copy()
            #print("@@ CONDS = {}".format(sval.filters))
            #print("@@ MAIN = {}".format(sval.constraints))

            # Constraints

            # If the constraint is an external call to Python code
            if self._is_python(sval.constraints):
                query, proj = self._process_python(sval.constraints)
                rpt_fld.update(proj.to_mongo())

            # All other constraints, e.g. 'foo > 12'
            else:
                query = MongoQuery()
                if sval.constraints is not None:
                    groups = self._process_constraint_expressions(sval.constraints)
                    projection = Projection()
                    for cg in groups.values():
                        for c in cg:
                            projection.add(c.field, c.op, c.value)
                            query.add_clause(MongoClause(c))
                        if self._add_exists:
                            for c in cg.existence_constraints:
                                query.add_clause(MongoClause(c, exists_main=True))
                    rpt_fld.update(projection.to_mongo())

            # Filters

            cond_query = MongoQuery()
            if sval.filters is not None:
                cond_groups = self._process_constraint_expressions(sval.filters, rev=False)
                for cg in cond_groups.values():
                    for c in cg:
                        cond_query.add_clause(MongoClause(c, rev=False))

            # Done. Add a new 'SectionPart' for the filter and constraint

            result = self.SectionParts(cond_query, query, sval.sampler, rpt_fld)
            self._sections.append(result)
119192
def execute(self):
        
        if self.query_set_obj is None:
            return []

        if self.is_mongoengine:
            result = self.query_set_obj.aggregate(*self.get_query())
        else:
            result = self.query_set_obj.aggregate(self.get_query())

        if IS_PYMONGO_3:
            result = list(result)
        else:
            result = result.get('result')
        if result:
            return result
        return []
857244
def refine(query, description):
    

    for attribute, arguments in description.items():
        if hasattr(query, attribute):
            attribute = getattr(query, attribute)
        else:
            raise ValueError("Unknown query method: " + attribute)

        # query descriptions are often automatically generated, and
        # may include empty calls, which we skip
        if utils.isempty(arguments):
            continue

        if callable(attribute):
            method = attribute
            if isinstance(arguments, dict):
                query = method(**arguments)
            elif isinstance(arguments, list):
                query = method(*arguments)
            else:
                query = method(arguments)
        else:
            setattr(attribute, arguments)

    return query
146865
def make_query(self, **kw):
        
        query = kw.pop("query", {})

        query.update(self.get_request_query())
        query.update(self.get_custom_query())
        query.update(self.get_keyword_query(**kw))

        sort_on, sort_order = self.get_sort_spec()
        if sort_on and "sort_on" not in query:
            query.update({"sort_on": sort_on})
        if sort_order and "sort_order" not in query:
            query.update({"sort_order": sort_order})

        logger.info("make_query:: query={} | catalog={}".format(
            query, self.catalog))

        return query
383884
def compile(pattern, *args, **kwargs):
    
    id, p = repr(pattern) + repr(args), pattern
    if id in _cache and not kwargs:
        return _cache[id]
    if isinstance(pattern, basestring):
        p = Pattern.fromstring(pattern, *args, **kwargs)
    if isinstance(pattern, regexp):
        p = Pattern([Constraint(words=[pattern], taxonomy=kwargs.get("taxonomy", TAXONOMY))], *args, **kwargs)
    if len(_cache) > _CACHE_SIZE:
        _cache.clear()
    if isinstance(p, Pattern) and not kwargs:
        _cache[id] = p
    if isinstance(p, Pattern):
        return p
    else:
        raise TypeError("can't compile '%s' object" % pattern.__class__.__name__)
1050810
def execute(self, query, args=None):
        
        self._ensure_conn()
        cur = self._conn.cursor()
        yield cur.execute(query, args)
        raise Return(cur)
269386
def get_constraints(self, cursor, table_name):
    
    constraints = {}
    # Loop over the key table, collecting things as constraints. The column
    # array must return column names in the same order in which they were
    # created.
    # The subquery containing generate_series can be replaced with
    # "WITH ORDINALITY" when support for PostgreSQL 9.3 is dropped.
    cursor.execute("""
        SELECT
            c.conname,
            array(
                SELECT attname
                FROM (
                    SELECT unnest(c.conkey) AS colid,
                           generate_series(1, array_length(c.conkey, 1)) AS arridx
                ) AS cols
                JOIN pg_attribute AS ca ON cols.colid = ca.attnum
                WHERE ca.attrelid = c.conrelid
                ORDER BY cols.arridx
            ),
            c.contype,
            (SELECT fkc.relname || '.' || fka.attname
            FROM pg_attribute AS fka
            JOIN pg_class AS fkc ON fka.attrelid = fkc.oid
            WHERE fka.attrelid = c.confrelid AND fka.attnum = c.confkey[1]),
            cl.reloptions
        FROM pg_constraint AS c
        JOIN pg_class AS cl ON c.conrelid = cl.oid
        JOIN pg_namespace AS ns ON cl.relnamespace = ns.oid
        WHERE ns.nspname = %s AND cl.relname = %s
    """, [self.connection.schema_name, table_name])
    for constraint, columns, kind, used_cols, options in cursor.fetchall():
        constraints[constraint] = {
            "columns": columns,
            "primary_key": kind == "p",
            "unique": kind in ["p", "u"],
            "foreign_key": tuple(used_cols.split(".", 1)) if kind == "f" else None,
            "check": kind == "c",
            "index": False,
            "definition": None,
            "options": options,
        }
    # Now get indexes
    # The row_number() function for ordering the index fields can be
    # replaced by WITH ORDINALITY in the unnest() functions when support
    # for PostgreSQL 9.3 is dropped.
    cursor.execute("""
        SELECT
            indexname, array_agg(attname ORDER BY rnum), indisunique, indisprimary,
            array_agg(ordering ORDER BY rnum), amname, exprdef, s2.attoptions
        FROM (
            SELECT
                row_number() OVER () as rnum, c2.relname as indexname,
                idx.*, attr.attname, am.amname,
                CASE
                    WHEN idx.indexprs IS NOT NULL THEN
                        pg_get_indexdef(idx.indexrelid)
                END AS exprdef,
                CASE am.amname
                    WHEN 'btree' THEN
                        CASE (option & 1)
                            WHEN 1 THEN 'DESC' ELSE 'ASC'
                        END
                END as ordering,
                c2.reloptions as attoptions
            FROM (
                SELECT
                    *, unnest(i.indkey) as key, unnest(i.indoption) as option
                FROM pg_index i
            ) idx
            LEFT JOIN pg_class c ON idx.indrelid = c.oid
            LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
            LEFT JOIN pg_class c2 ON idx.indexrelid = c2.oid
            LEFT JOIN pg_am am ON c2.relam = am.oid
            LEFT JOIN pg_attribute attr ON attr.attrelid = c.oid AND attr.attnum = idx.key
            WHERE c.relname = %s and n.nspname = %s
        ) s2
        GROUP BY indexname, indisunique, indisprimary, amname, exprdef, attoptions;
    """, [table_name, self.connection.schema_name])
    for index, columns, unique, primary, orders, type_, definition, options in cursor.fetchall():
        if index not in constraints:
            constraints[index] = {
                "columns": columns if columns != [None] else [],
                "orders": orders if orders != [None] else [],
                "primary_key": primary,
                "unique": unique,
                "foreign_key": None,
                "check": False,
                "index": True,
                "type": Index.suffix if type_ == 'btree' else type_,
                "definition": definition,
                "options": options,
            }
    return constraints
860364
def store_constraint(constr)
      # Update the constraint to track the index in the model
      constr.model = self

      # Set constraint properties
      Cbc.Cbc_setRowName(@ptr, constr.index, constr.name) unless constr.name.nil?
      store_constraint_bounds constr.index, constr.sense, constr.rhs
    end
560622
def query(self, model, **kwargs):
        ''''''
        sm = self.model(model)
        query_class = sm.manager.query_class or Query
        return query_class(sm._meta, self, **kwargs)
967206
def _execute_query(self):
        ''''''
        pipe = self.pipe
        if not self.card:
            if self.meta.ordering:
                self.ismember = getattr(self.backend.client, 'zrank')
                self.card = getattr(pipe, 'zcard')
                self._check_member = self.zism
            else:
                self.ismember = getattr(self.backend.client, 'sismember')
                self.card = getattr(pipe, 'scard')
                self._check_member = self.sism
        else:
            self.ismember = None
        self.card(self.query_key)
        result = yield pipe.execute()
        yield result[-1]
999187
def query_string(cls,
                     query,
                     default_field=None,
                     default_operator=None,
                     analyzer=None,
                     allow_leading_wildcard=None,
                     lowercase_expanded_terms=None,
                     enable_position_increments=None,
                     fuzzy_prefix_length=None,
                     fuzzy_min_sim=None,
                     phrase_slop=None,
                     boost=None,
                     analyze_wildcard=None,
                     auto_generate_phrase_queries=None,
                     minimum_should_match=None):
        '''
        
        '''
        instance = cls(query_string={'query': query})
        if default_field is not None:
            instance['query_string']['default_field'] = default_field
        if default_operator is not None:
            instance['query_string']['default_operator'] = default_operator
        if analyzer is not None:
            instance['query_string']['analyzer'] = analyzer
        if allow_leading_wildcard is not None:
            instance['query_string']['allow_leading_wildcard'] = allow_leading_wildcard
        if lowercase_expanded_terms is not None:
            instance['query_string']['lowercase_expanded_terms'] = lowercase_expanded_terms
        if enable_position_increments is not None:
            instance['query_string']['enable_position_increments'] = enable_position_increments
        if fuzzy_prefix_length is not None:
            instance['query_string']['fuzzy_prefix_length'] = fuzzy_prefix_length
        if fuzzy_min_sim is not None:
            instance['query_string']['fuzzy_min_sim'] = fuzzy_min_sim
        if phrase_slop is not None:
            instance['query_string']['phrase_slop'] = phrase_slop
        if boost is not None:
            instance['query_string']['boost'] = boost
        if analyze_wildcard is not None:
            instance['query_string']['analyze_wildcard'] = analyze_wildcard
        if auto_generate_phrase_queries is not None:
            instance['query_string']['auto_generate_phrase_queries'] = auto_generate_phrase_queries
        if minimum_should_match is not None:
            instance['query_string']['minimum_should_match'] = minimum_should_match
        return instance
1173894
def __execute_text(self, query, **replacements):
        

        # We've had persistent and long-term issues where we can't access tables 
        # from another client because they're locked, as well as getting the 
        # "gone away" error quite frequenctly. Per the documentation, apparently 
        # it is common to -not- reuse the same session from request to request,
        # and we believe this to be the problem. Hopefully it's fixed, now.
        #
        # REF: http://docs.sqlalchemy.org/en/rel_0_9/orm/session.html#when-do-i-construct-a-session-when-do-i-commit-it-and-when-do-i-close-it
        session = self.__get_session()
        
        try:
            records = session.execute(query, replacements)
        except:
            _LOGGER.exception("Query failure:\n%s", query)

            raise

        # Verifying that we have a realized list of results before we commit 
        # the transaction.

        try:
            records = list(records)
        finally:
            session.commit()

        for record in records:
            yield dict(record)
589624
def set_conditions(self, variables, constraints):
        
        self._vars, self._constraints = variables, []
        # build constraint objects
        for func, variables, values in constraints:
            c = Constraint(func, variables, values, self._compute_search_spaces(variables))
            self._constraints.append(c)
        # sort into most constraining first
        self._constraints.sort()
1211577
def _gql(query_string, query_class=Query):
  
  from .google_imports import gql  # Late import, to avoid name conflict.
  gql_qry = gql.GQL(query_string)
  kind = gql_qry.kind()
  if kind is None:
    # The query must be lacking a "FROM <kind>" class.  Let Expando
    # stand in for the model class (it won't actually be used to
    # construct the results).
    modelclass = model.Expando
  else:
    modelclass = model.Model._lookup_model(
        kind,
        tasklets.get_context()._conn.adapter.default_model)
    # Adjust kind to the kind of the model class.
    kind = modelclass._get_kind()
  ancestor = None
  flt = gql_qry.filters()
  filters = list(modelclass._default_filters())
  for name_op in sorted(flt):
    name, op = name_op
    values = flt[name_op]
    op = op.lower()
    if op == 'is' and name == gql.GQL._GQL__ANCESTOR:
      if len(values) != 1:
        raise ValueError('"is" requires exactly one value')
      [(func, args)] = values
      ancestor = _args_to_val(func, args)
      continue
    if op not in _OPS:
      raise NotImplementedError('Operation %r is not supported.' % op)
    for (func, args) in values:
      val = _args_to_val(func, args)
      prop = _get_prop_from_modelclass(modelclass, name)
      if prop._name != name:
        raise RuntimeError('Whoa! _get_prop_from_modelclass(%s, %r) '
                           'returned a property whose name is %r?!' %
                           (modelclass.__name__, name, prop._name))
      if isinstance(val, ParameterizedThing):
        node = ParameterNode(prop, op, val)
      elif op == 'in':
        node = prop._IN(val)
      else:
        node = prop._comparison(op, val)
      filters.append(node)
  if filters:
    filters = ConjunctionNode(*filters)
  else:
    filters = None
  orders = _orderings_to_orders(gql_qry.orderings(), modelclass)
  offset = gql_qry.offset()
  limit = gql_qry.limit()
  if limit < 0:
    limit = None
  keys_only = gql_qry._keys_only
  if not keys_only:
    keys_only = None
  options = QueryOptions(offset=offset, limit=limit, keys_only=keys_only)
  projection = gql_qry.projection()
  if gql_qry.is_distinct():
    group_by = projection
  else:
    group_by = None
  qry = query_class(kind=kind,
                    ancestor=ancestor,
                    filters=filters,
                    orders=orders,
                    default_options=options,
                    projection=projection,
                    group_by=group_by)
  return qry
78971
def __query_spec(self):
        
        operators = self.__modifiers.copy()
        if self.__ordering:
            operators["$orderby"] = self.__ordering
        if self.__explain:
            operators["$explain"] = True
        if self.__hint:
            operators["$hint"] = self.__hint
        if self.__comment:
            operators["$comment"] = self.__comment
        if self.__max_scan:
            operators["$maxScan"] = self.__max_scan
        if self.__max_time_ms is not None:
            operators["$maxTimeMS"] = self.__max_time_ms
        if self.__max:
            operators["$max"] = self.__max
        if self.__min:
            operators["$min"] = self.__min
        if self.__return_key:
            operators["$returnKey"] = self.__return_key
        if self.__show_record_id:
            # This is upgraded to showRecordId for MongoDB 3.2+ "find" command.
            operators["$showDiskLoc"] = self.__show_record_id
        if self.__snapshot:
            operators["$snapshot"] = self.__snapshot

        if operators:
            # Make a shallow copy so we can cleanly rewind or clone.
            spec = self.__spec.copy()

            # White-listed commands must be wrapped in $query.
            if "$query" not in spec:
                # $query has to come first
                spec = SON([("$query", spec)])

            if not isinstance(spec, SON):
                # Ensure the spec is SON. As order is important this will
                # ensure its set before merging in any extra operators.
                spec = SON(spec)

            spec.update(operators)
            return spec
        # Have to wrap with $query if "query" is the first key.
        # We can't just use $query anytime "query" is a key as
        # that breaks commands like count and find_and_modify.
        # Checking spec.keys()[0] covers the case that the spec
        # was passed as an instance of SON or OrderedDict.
        elif ("query" in self.__spec and
              (len(self.__spec) == 1 or
               next(iter(self.__spec)) == "query")):
            return SON({"$query": self.__spec})

        return self.__spec
1041924
function GENERATE_PARAMS(spec) {
  return _.map(spec, function(paramSpec, paramName) {
    return paramName + ': ' + paramSpec.type;
  }).join(', ');
}
945936
def parse(cls, query_str):
		
		parsed_query = parse_qs(query_str, keep_blank_values=True, strict_parsing=True)
		result = cls()
		for parameter_name in parsed_query.keys():
			for parameter_value in parsed_query[parameter_name]:
				result.add_parameter(
					parameter_name,
					parameter_value if len(parameter_value) > 0 else None
				)
		return result