def get(self): from ndb import model, query from mol.db.layers import Layer, LayerIndex limit = self.request.get_range('limit', min_value=1, max_value=100, default=10) offset = self.request.get_range('offset', min_value=0, default=0) args = {} for arg in self.request.arguments(): if arg in POLYGON_PROPS: args['polygon.%s' % arg] = self.request.get(arg, None) qry = LayerIndex.query() if len(args) > 0: gql = 'SELECT * FROM LayerIndex WHERE' for k,v in args.iteritems(): gql = "%s \"%s\" = '%s' AND " % (gql, k, v) gql = gql[:-5] # Removes trailing AND logging.info(gql) qry = query.parse_gql(gql)[0] logging.info('QUERY='+str(qry)) polygons = [] for x in qry.fetch(limit, offset=offset): #[simplejson.loads(x.json) for x in qry.fetch(limit, offset=offset)] p = simplejson.loads(x.json) p['layer_index_key'] = str(x.key.id()) p['layer_key'] = str(x.key.parent().id()) polygons.append(p) self.response.headers["Content-Type"] = "application/json" self.response.out.write(simplejson.dumps(polygons))
def testGqlMinimal(self): qry, options, bindings = query.parse_gql('SELECT * FROM Kind') self.assertEqual(qry.kind, 'Kind') self.assertEqual(qry.ancestor, None) self.assertEqual(qry.filters, None) self.assertEqual(qry.orders, None) self.assertEqual(bindings, {})
def testResolveBindings(self): qry, options, bindings = query.parse_gql( 'SELECT * FROM Foo WHERE name = :1') bindings[1].value = 'joe' self.assertEqual(list(qry), [self.joe]) bindings[1].value = 'jill' self.assertEqual(list(qry), [self.jill])
def get(self): from ndb import model, query from mol.db.layers import Layer, LayerIndex limit = self.request.get_range('limit', min_value=1, max_value=100, default=10) offset = self.request.get_range('offset', min_value=0, default=0) args = {} for arg in self.request.arguments(): if arg in INDEXED_PROPS: if arg in POLYGON_PROPS: args['polygon.%s' % arg] = self.request.get(arg, None) else: args[arg] = self.request.get(arg, None) qry = LayerIndex.query() if len(args) > 0: gql = 'SELECT * FROM LayerIndex WHERE' for k,v in args.iteritems(): gql = "%s \"%s\" = '%s' AND " % (gql, k, v) gql = gql[:-5] # Removes trailing AND logging.info(gql) qry = query.parse_gql(gql)[0] # No keyword search yet #for keyword in keywords: # qry = qry.filter(RecordIndex.corpus == keyword) logging.info('QUERY='+str(qry)) layers = model.get_multi(set([x.parent() for x in qry.fetch(limit, offset=offset, keys_only=True)])) layers_json = [simplejson.loads(x.json) for x in layers] self.response.headers["Content-Type"] = "application/json" self.response.out.write(simplejson.dumps(layers_json))
def testGqlAncestor(self): qry, options, bindings = query.parse_gql( 'SELECT * FROM Kind WHERE ANCESTOR IS :1') self.assertEqual(qry.kind, 'Kind') self.assertEqual(qry.ancestor, query.Binding(None, 1)) self.assertEqual(qry.filters, None) self.assertEqual(qry.orders, None) self.assertEqual(bindings, {1: query.Binding(None, 1)})
def testGqlAncestor(self): key = model.Key('Foo', 42) qry, options, bindings = query.parse_gql( "SELECT * FROM Kind WHERE ANCESTOR IS KEY('%s')" % key.urlsafe()) self.assertEqual(qry.kind, 'Kind') self.assertEqual(qry.ancestor, key) self.assertEqual(qry.filters, None) self.assertEqual(qry.orders, None) self.assertEqual(bindings, {})
def search(cls, varname, within, pivot, limit, offset): prop = 'within_%s' % within gql = "SELECT * FROM CellIndex WHERE n = '%s'" % varname gql = "%s AND %s = %d" % (gql, prop, int(pivot)) logging.info(gql) qry = query.parse_gql(gql)[0] logging.info('QUERY='+str(qry)) results = qry.fetch(limit, offset=offset, keys_only=True) cell_keys = [key.parent().id() for key in results] return set(cell_keys)
def testGqlFilter(self): qry, options, bindings = query.parse_gql( "SELECT * FROM Kind WHERE prop1 = 1 AND prop2 = 'a'") self.assertEqual(qry.kind, 'Kind') self.assertEqual(qry.ancestor, None) self.assertEqual(qry.filters, query.ConjunctionNode( [query.FilterNode('prop1', '=', 1), query.FilterNode('prop2', '=', 'a')])) self.assertEqual(qry.orders, None) self.assertEqual(bindings, {})
def search(cls, args={}, keywords=[]): qry = RecordIndex.query() if len(args) > 0: gql = "SELECT * FROM RecordIndex WHERE" for k, v in args.iteritems(): gql = "%s %s='%s' AND " % (gql, k, v) gql = gql[:-5] # Removes trailing AND qry = query.parse_gql(gql)[0] for keyword in keywords: qry = qry.filter(RecordIndex.corpus == keyword) logging.info("QUERY=" + str(qry)) return model.get_multi([x.parent() for x in qry.fetch(keys_only=True)])
def search(cls, args={}, keywords=[]): qry = RecordIndex.query() if len(args) > 0: gql = 'SELECT * FROM RecordIndex WHERE' for k, v in args.iteritems(): gql = "%s %s='%s' AND " % (gql, k, v) gql = gql[:-5] # Removes trailing AND qry = query.parse_gql(gql)[0] for keyword in keywords: qry = qry.filter(RecordIndex.corpus == keyword) logging.info('QUERY=' + str(qry)) return model.get_multi([x.parent() for x in qry.fetch(keys_only=True)])
def testGqlBindings(self): qry, options, bindings = query.parse_gql( 'SELECT * FROM Kind WHERE prop1 = :1 AND prop2 = :foo') self.assertEqual(qry.kind, 'Kind') self.assertEqual(qry.ancestor, None) self.assertEqual(qry.filters, query.ConjunctionNode( [query.FilterNode('prop1', '=', query.Binding(None, 1)), query.FilterNode('prop2', '=', query.Binding(None, 'foo'))])) self.assertEqual(qry.orders, None) self.assertEqual(bindings, {1: query.Binding(None, 1), 'foo': query.Binding(None, 'foo')})
def search(cls, params): """Returns (records, cursor). Arguments args - Dictionary with Darwin Core concept keys keywords - list of keywords to search on """ ctx = tasklets.get_context() ctx.set_memcache_policy(False) qry = RecordIndex.query() # Add darwin core name filters args = params['args'] if len(args) > 0: gql = 'SELECT * FROM RecordIndex WHERE' for k,v in args.iteritems(): gql = "%s %s = '%s' AND " % (gql, k, v) gql = gql[:-5] # Removes trailing AND logging.info(gql) qry = query.parse_gql(gql)[0] # Add full text keyword filters keywords = params['keywords'] for keyword in keywords: qry = qry.filter(RecordIndex.corpus == keyword) logging.info('QUERY='+str(qry)) # Setup query paging limit = params['limit'] cursor = params['cursor'] if cursor: logging.info('Cursor') index_keys, next_cursor, more = qry.fetch_page(limit, start_cursor=cursor, keys_only=True) record_keys = [x.parent() for x in index_keys] else: logging.info('No cursor') index_keys, next_cursor, more = qry.fetch_page(limit, keys_only=True) record_keys = [x.parent() for x in index_keys] # Return results return (model.get_multi(record_keys), next_cursor, more)
def testGqlLimit(self): qry, options, bindings = query.parse_gql( 'SELECT * FROM Kind LIMIT 2') self.assertEqual(options.limit, 2)
def testGqlOffset(self): qry, options, bindings = query.parse_gql( 'SELECT * FROM Kind OFFSET 2') self.assertEqual(options.offset, 2)
def testGqlOrder(self): qry, options, bindings = query.parse_gql( 'SELECT * FROM Kind ORDER BY prop1') self.assertEqual(query._orders_to_orderings(qry.orders), [('prop1', query._ASC)])
def testGqlOrder(self): qry, options, bindings = query.parse_gql( 'SELECT * FROM Kind ORDER BY prop1') self.assertEqual(query.orders_to_orderings(qry.orders), [('prop1', query.ASC)])