def _build_ds_query(self, discovery_query, id_only=True): query_exp = discovery_query["query"] or {} index = query_exp.get("index", "resources_index") ds_name = DATASTORE_MAP.get(index, None) ds_profile = PROFILE_MAP.get(ds_name, None) # TODO: Enable service defined indexes in addition to standard indexes if ds_name is None: raise BadRequest("Unknown index: %s" % index) limit = discovery_query.get("limit", 0) skip = discovery_query.get("skip", 0) order = discovery_query.get("order", None) qb = DatastoreQueryBuilder(limit=limit, skip=skip, id_only=id_only, profile=ds_profile) where = None for qm in self._qmatchers: where = qm(discovery_query, qb) if where: break if where is None: raise BadRequest("Query had no matcher") if index == "data_products_index": filter_types = ["DataProduct", "DataProcess", "Deployment", "InstrumentDevice", "InstrumentModel", "InstrumentAgentInstance", "InstrumentAgent", "PlatformDevice", "PlatformModel", "PlatformAgentInstance", "PlatformAgent", "PlatformSite", "Observatory", "UserRole", "Org", "Attachment", "ExternalDatasetAgent", "ExternalDatasetAgentInstance", "Asset", "EventDuration"] where = qb.and_(where, qb.in_(DQ.ATT_TYPE, *filter_types), qb.neq(DQ.RA_LCSTATE, "DELETED")) order_by = None if order: order_list = [] if type(order) is dict: for col, colsort in order.iteritems(): order_list.append((col, colsort)) elif type(order) in (list, tuple): for column in order: if type(column) in (list, tuple): col, colsort = column else: col, colsort = column, "asc" order_list.append((col, colsort)) order_by = qb.order_by(order_list) qb.build_query(where=where, order_by=order_by) return qb.get_query(), ds_name
def _build_ds_query(self, discovery_query, id_only=True): query_exp = discovery_query["query"] or {} index = query_exp.get("index", "resources_index") ds_name = DATASTORE_MAP.get(index, None) ds_profile = PROFILE_MAP.get(ds_name, None) # TODO: Enable service defined indexes in addition to standard indexes if ds_name is None: raise BadRequest("Unknown index: %s" % index) limit = discovery_query.get("limit", 0) skip = discovery_query.get("skip", 0) order = discovery_query.get("order", None) qb = DatastoreQueryBuilder(limit=limit, skip=skip, id_only=id_only, profile=ds_profile) where = None for qm in self._qmatchers: where = qm(discovery_query, qb) if where: break if where is None: raise BadRequest("Query had no matcher") order_by = None if order: order_list = [] if type(order) is dict: for col, colsort in order.iteritems(): order_list.append((col, colsort)) elif type(order) in (list, tuple): for column in order: if type(column) in (list, tuple): col, colsort = column else: col, colsort = column, "asc" order_list.append((col, colsort)) order_by = qb.order_by(order_list) qb.build_query(where=where, order_by=order_by) return qb.get_query(), ds_name
def test_wkt(self): """ unit test to verify the DatastoreQuery to PostgresQuery to SQL translation for PostGIS WKT """ wkt = 'POINT(-72.0 40.0)' buf = 0.1 # PostgresQueryBuilder - WKT (no buffer) qb = DatastoreQueryBuilder() qb.build_query(where=qb.overlaps_geom(qb.RA_GEOM_LOC,wkt,0.0)) pqb = PostgresQueryBuilder(qb.get_query(), 'test') self.assertEquals(pqb.get_query(),"SELECT id,doc FROM test WHERE ST_Intersects(geom_loc,ST_GeomFromEWKT('SRID=4326;POINT(-72.0 40.0)'))") qb = DatastoreQueryBuilder() qb.build_query(where=qb.contains_geom(qb.RA_GEOM_LOC,wkt,0.0)) pqb = PostgresQueryBuilder(qb.get_query(), 'test') self.assertEquals(pqb.get_query(),"SELECT id,doc FROM test WHERE ST_Contains(geom_loc,ST_GeomFromEWKT('SRID=4326;POINT(-72.0 40.0)'))") qb = DatastoreQueryBuilder() qb.build_query(where=qb.within_geom(qb.RA_GEOM_LOC,wkt,0.0)) pqb = PostgresQueryBuilder(qb.get_query(), 'test') self.assertEquals(pqb.get_query(),"SELECT id,doc FROM test WHERE ST_Within(geom_loc,ST_GeomFromEWKT('SRID=4326;POINT(-72.0 40.0)'))") # PostgresQueryBuilder - WKT (with buffer) qb = DatastoreQueryBuilder() qb.build_query(where=qb.overlaps_geom(qb.RA_GEOM_LOC,wkt,buf)) pqb = PostgresQueryBuilder(qb.get_query(), 'test') self.assertEquals(pqb.get_query(),"SELECT id,doc FROM test WHERE ST_Intersects(geom_loc,ST_Buffer(ST_GeomFromEWKT('SRID=4326;POINT(-72.0 40.0)'), 0.100000))") qb = DatastoreQueryBuilder() qb.build_query(where=qb.contains_geom(qb.RA_GEOM_LOC,wkt,buf)) pqb = PostgresQueryBuilder(qb.get_query(), 'test') self.assertEquals(pqb.get_query(),"SELECT id,doc FROM test WHERE ST_Contains(geom_loc,ST_Buffer(ST_GeomFromEWKT('SRID=4326;POINT(-72.0 40.0)'), 0.100000))") qb = DatastoreQueryBuilder() qb.build_query(where=qb.within_geom(qb.RA_GEOM_LOC,wkt,buf)) pqb = PostgresQueryBuilder(qb.get_query(), 'test') self.assertEquals(pqb.get_query(),"SELECT id,doc FROM test WHERE ST_Within(geom_loc,ST_Buffer(ST_GeomFromEWKT('SRID=4326;POINT(-72.0 40.0)'), 0.100000))") qb = DatastoreQueryBuilder() qb.build_query(where=qb.equals_geom(qb.RA_GEOM_LOC,wkt,buf)) pqb = PostgresQueryBuilder(qb.get_query(), 'test') self.assertEquals(pqb.get_query(),"SELECT id,doc FROM test WHERE ST_Equals(geom_loc,ST_Buffer(ST_GeomFromEWKT('SRID=4326;POINT(-72.0 40.0)'), 0.100000))")