Ejemplo n.º 1
0
 def order(self, *args):
     # q.order(Eployee.name, -Employee.age)
     if not args:
         return self
     orders = []
     o = self.__orders
     if o:
         orders.append(o)
     for arg in args:
         if isinstance(arg, model.Property):
             orders.append(datastore_query.PropertyOrder(arg._name, ASC))
         elif isinstance(arg, datastore_query.Order):
             orders.append(arg)
         else:
             assert False, arg
     if not orders:
         orders = None
     elif len(orders) == 1:
         orders = orders[0]
     else:
         orders = datastore_query.CompositeOrder(orders)
     return self.__class__(kind=self.kind,
                           ancestor=self.ancestor,
                           filters=self.filters,
                           orders=orders)
Ejemplo n.º 2
0
def orderings_to_orders(orderings):
    orders = [ordering_to_order(o) for o in orderings]
    if not orders:
        return None
    if len(orders) == 1:
        return orders[0]
    return datastore_query.CompositeOrder(orders)
Ejemplo n.º 3
0
def parse_gql(query_string):
    """Parse a GQL query string.

  Args:
    query_string: Full GQL query, e.g. 'SELECT * FROM Kind WHERE prop = 1'.

  Returns:
    A tuple (query, options, bindings) where query is a Query instance,
    options a datastore_query.QueryOptions instance, and bindings a dict
    mapping integers and strings to Binding instances.
  """
    gql_qry = gql.GQL(query_string)
    ancestor = None
    flt = gql_qry.filters()
    bindings = {}
    filters = []
    for ((name, op), values) in flt.iteritems():
        op = op.lower()
        if op == 'is' and name == gql.GQL._GQL__ANCESTOR:
            assert len(values) == 1
            [(func, args)] = values
            ancestor = _args_to_val(func, args, bindings)
            continue
        assert op in _OPS.values()
        for (func, args) in values:
            val = _args_to_val(func, args, bindings)
            filters.append(FilterNode(name, op, val))
    if filters:
        filters.sort()  # For predictable tests.
        filters = ConjunctionNode(filters)
    else:
        filters = None
    orderings = gql_qry.orderings()
    orders = []
    for (name, direction) in orderings:
        orders.append(datastore_query.PropertyOrder(name, direction))
    if not orders:
        orders = None
    elif len(orders) == 1:
        orders = orders[0]
    else:
        orders = datastore_query.CompositeOrder(orders)
    qry = Query(kind=gql_qry._entity,
                ancestor=ancestor,
                filters=filters,
                orders=orders)
    offset = gql_qry.offset()
    if offset < 0:
        offset = None
    limit = gql_qry.limit()
    if limit < 0:
        limit = None
    options = QueryOptions(offset=offset, limit=limit)
    return qry, options, bindings