def filter_by_meta(self, field, value): """Search the metadata. It will be always searched from the root (you don't need to add [root]). :param field: - The field to search on (e.g sparq.ticketware.user_id) :param value: - The value to search for. It will be converted to string. """ from paylogic.core.metadata import Object, Field, Value if not field.startswith('[root]'): field = '[root].' + field value = str(value) owner = self._mapper_adapter_map.keys()[0] query = (self.join((Value, Value.instance_id == owner.id)).join( (Object, Object.path == '.'.join([owner.__module__, owner.__name__])))) query = query.filter(Value.value == value) prev = None for name in field.split('.'): f = aliased(Field) cond = f.obj_id == Object.id if prev is not None: cond = and_(cond, f.parent_id == prev.id) query = query.join((f, cond)).filter(f.name == name) prev = f query = query.filter(Value.field_id == f.id) return query
def filter(self, owner, field, value): """Search the metadata. It returns a query on the owner class. :param owner: - The owner class model (e.g Order) :param field: - The field to search on (e.g sparq.ticketware.user_id) :param value: - The value to search for :param operator: - The operator to apply to the field and value. Optional; defaults to equality. """ if not field.startswith('[root]'): field = '.'.join(['[root]', field]) value_alias = aliased(Value) session = inspect(owner).session query = (session.query(owner.__class__).join( (value_alias, value_alias.instance_id == get_object_id(owner))).join( (Object, Object.path == format('.'.join( [owner.__module__, owner.__name__]))))) query = query.filter(Value.value == value) parents = field.split('.') prev = None for parent_name in parents: f = aliased(Field) cond = f.obj_id == Object.id if prev is not None: cond = and_(cond, f.parent_id == prev.id) query = query.join((f, cond)).filter(f.name == parent_name) prev = f query = query.filter(value_alias.field_id == f.id) return query
def addCTimeConstraints(self, query, classObj): if "period" in self: if "from" in self["period"]: query = query.filter(getattr(classObj, "ctime") >= self["period"]["from"]) if "to" in self["period"]: query = query.filter(getattr(classObj, "ctime") <= self["period"]["to"]) return query
def query_video(self, linename, videotime): query = self.session_power.query(Video) if linename != None: query = query.filter(Video.video_line == linename) if videotime != None: query = query.filter(Video.video_time == videotime) videos = query.all() return class_to_dict(videos)
def addCTimeConstraints(self, query, classObj): if "period" in self: if "from" in self["period"]: query = query.filter( getattr(classObj, "ctime") >= self["period"]["from"]) if "to" in self["period"]: query = query.filter( getattr(classObj, "ctime") <= self["period"]["to"]) return query
def _create_layer_query(self, interface: str) -> sqlalchemy.orm.query.Query: """ Create an SQLAlchemy query for Layer and for the role identified to by ``role_id``. """ query = models.DBSession.query(main.Layer.name).filter( main.Layer.public.is_(True)) if interface is not None: query = query.join(main.Layer.interfaces) query = query.filter(main.Interface.name == interface) query2 = get_protected_layers_query(self.request, None, what=main.LayerWMS.name) if interface is not None: query2 = query2.join(main.Layer.interfaces) query2 = query2.filter(main.Interface.name == interface) query = query.union(query2) query3 = get_protected_layers_query(self.request, None, what=main.LayerWMTS.name) if interface is not None: query3 = query3.join(main.Layer.interfaces) query3 = query3.filter(main.Interface.name == interface) query = query.union(query3) return query
def get_query(cls, info, **kwargs): query = super(SQLAlchemyObjectType, cls).get_query(info) query = query.options(sqlalchemy.orm.raiseload('*')) model = cls._meta.model for field, value in kwargs.items(): query = query.filter(getattr(model, field) == value) return query
def get_query(cls, info, **kwargs): query = super(SQLAlchemyObjectType, cls).get_query(info) query = query.options(sqlalchemy.orm.raiseload('*')) model = cls._meta.model for field, value in kwargs.items(): query = query.filter(getattr(model, field) == value) return query
def single_lastver( me, oid, where =None, **kargs4time): '''1t: last version of single object / последната версия на един обект''' if _debug: print 'single_lastver oid=%(oid)s where=%(where)s times=%(kargs4time)s' % locals() if kargs4time: me.filter_time( **kargs4time) query = me._query( and_clauses_or_None( me._where_time, where, me.filter_type() )) return query.filter( me.p_oid == oid ).order_by( *(c.desc() for c in me._order_by4single() ) ).first()
def get_all_sensor_data(self, **kwargs): # Initialize from kwargs (and default values) convert = kwargs.get('convert', False) sensor = kwargs.get('sensor', None) channel = kwargs.get('channel', None) recordset = kwargs.get('recordset', None) # Get all sensor data query = self.session.query(SensorData) if recordset is not None: query = query.filter( SensorData.id_recordset == recordset.id_recordset) if sensor is not None: # print('Should filter sensor id', sensor.id_sensor) query = query.filter(SensorData.id_sensor == sensor.id_sensor) if channel is not None: # print('Should filter channel', channel.id_channel) query = query.filter(SensorData.id_channel == channel.id_channel) # print(query) # Make sure data is ordered by timestamps # query = query.order_by(SensorData.timestamps.asc()) # print('TODO ORDERY BY TIMESTAMPS NEEDS TO BE IMPLEMENTED') # And then per channel # query = query.order_by(SensorData.channel.asc()) if not convert: return query.all() else: # Read result, data will be bytes array result = query.all() # Convert to the right format for sensor_data in result: # print('data len:', len(sensor_data.data)) sensor_data.data = DataFormat.from_bytes( sensor_data.data, sensor_data.channel.id_data_format) return result
def _filter( query, expr, isquery =None): if isquery is None: #autoguess isquery = hasattr( query, 'filter') if isquery: query = query.filter( expr) else: #clause if query is None: query = expr else: query &= expr return query
def _target_timestep(self, query, ts): """Only return those hops which reach the specified timestep""" if ts is None: query = query.filter(0 == 1) else: query = query.join(self._link_orm_class.halo_to).filter( core.halo.SimulationObjectBase.timestep_id == ts.id) return query
def get_all_channels(self, **kwargs): sensor = kwargs.get('sensor', None) # Get all channels query = self.session.query(Channel) if sensor is not None: query = query.filter(Channel.id_sensor == sensor.id_sensor) # Return all channels return query.all()
def _generate_query(self, halo_ids_only): if halo_ids_only: query = self.session.query(self._link_orm_class.halo_to_id) else: query = self.session.query(self._link_orm_class) if not self._include_startpoint: query = query.filter(self._table.c.nhops>0) query = self._filter_query_for_target(query, self._target) return query
def sa_get_relationship(session, model, name): """ Resolve the relationship on a SQLAlchemy model to either an object (in the case of one-to-one relationships) or a query to all of the objects (in the case of one-to-many relationships). :param session: The SQLAlchemy session to associate the query with. :param model: The SQLAlchemy model of the object associated with the relationship. :param name: The name of the relationship as it exists in the *model*. :return: Either the object or a SQLAlchemy query for the objects. """ mapper = sqlalchemy.inspect(model.__class__) relationship = mapper.relationships[name] foreign_model = db_models.database_tables[relationship.target.name].model query = session.query(foreign_model) if relationship.uselist: column_name = relationship.primaryjoin.right.name return query.filter(getattr(foreign_model, column_name) == model.id) column_name = relationship.primaryjoin.left.name query = query.filter(getattr(foreign_model, column_name) == getattr(model, relationship.primaryjoin.right.name)) return query.first()
def sa_get_relationship(session, model, name): """ Resolve the relationship on a SQLAlchemy model to either an object (in the case of one-to-one relationships) or a query to all of the objects (in the case of one-to-many relationships). :param session: The SQLAlchemy session to associate the query with. :param model: The SQLAlchemy model of the object associated with the relationship. :param name: The name of the relationship as it exists in the *model*. :return: Either the object or a SQLAlchemy query for the objects. """ mapper = sqlalchemy.inspect(model.__class__) relationship = mapper.relationships[name] foreign_model = db_models.database_tables[relationship.table.name].model query = session.query(foreign_model) if relationship.uselist: column_name = relationship.primaryjoin.right.name return query.filter(getattr(foreign_model, column_name) == model.id) column_name = relationship.primaryjoin.left.name query = query.filter( getattr(foreign_model, column_name) == getattr( model, relationship.primaryjoin.right.name)) return query.first()
def _supplement_halolink_query_with_filter(self, query, table=None): if table is None: table = core.halo_data.HaloLink.__table__ if self._needs_join_for_link_filter(): query = query. \ join(self.halo_old, table.c.halo_from_id == self.halo_old.id). \ join(self.halo_new, table.c.halo_to_id == self.halo_new.id). \ join(self.timestep_old, self.halo_old.timestep). \ join(self.timestep_new, self.halo_new.timestep) filter = self._generate_link_filter(self.timestep_old, self.timestep_new, table) query = query.filter(filter) return query
def fuzzy_filter_non_interactive_pipeline_runs( query: query, fuzzy_filter: str, ) -> query: fuzzy_filter = fuzzy_filter.lower().strip().split() # Quote terms to avoid operators like ! leading to syntax errors and # to avoid funny injections. fuzzy_filter = [f"''{token}'':*" for token in fuzzy_filter] fuzzy_filter = " & ".join(fuzzy_filter) # sqlalchemy is erroneously considering the query created through # func.to_tsquery invalid. fuzzy_filter = f"to_tsquery('simple', '{fuzzy_filter}')" filters = [ models.NonInteractivePipelineRun. _NonInteractivePipelineRun__text_search_vector.op( # noqa "@@")(text(fuzzy_filter)), ] query = query.filter(or_(*filters)) return query
def _query_filter(cls, model, info, query, gql_filter): sql_filter = cls.__query_filter(model, info, gql_filter) if sql_filter is not None: query = query.filter(sql_filter) return query
def _query_filter(cls, model, info, query, gql_filter): sql_filter = cls.__query_filter(model, info, gql_filter) if sql_filter is not None: query = query.filter(sql_filter) return query
def _query( me, filter =None): query = me.query if filter is not None: query = query.filter( filter) return query