def _process_from(from_, query, query_context=None): if not getattr(query, '_safe', None): return tenant_id_col = from_.c.get('tenant_id') if tenant_id_col is not None: if query.session.tenant is None: raise UnboundTenantError( "Tried to do a tenant-bound query in a tenantless context.") # logic copied from orm.Query.filter, in order to be able to modify # the existing query in place criterion = expression._literal_as_text( tenant_id_col == query.session.tenant.id) criterion = query._adapt_clause(criterion, True, True) if query_context is None: if query._criterion is not None: query._criterion = query._criterion & criterion else: query._criterion = criterion else: if query_context.whereclause is not None: query_context.whereclause = ( query_context.whereclause & criterion) else: query_context.whereclause = criterion
def _process_from(from_, query, query_context=None): if not getattr(query, '_safe', None): return tenant_id_col = from_.c.get('tenant_id') if tenant_id_col is not None: if query.session.tenant is None: raise UnboundTenantError( "Tried to do a tenant-bound query in a tenantless context.") # logic copied from orm.Query.filter, in order to be able to modify # the existing query in place criterion = expression._literal_as_text( tenant_id_col == query.session.tenant.id) criterion = query._adapt_clause(criterion, True, True) if query_context is None: if query._criterion is not None: query._criterion = query._criterion & criterion else: query._criterion = criterion else: if query_context.whereclause is not None: query_context.whereclause = (query_context.whereclause & criterion) else: query_context.whereclause = criterion
def _get_log_useful_cols(sample_size): """ Columns in returned DataFrame:: "ControllerName", "Direction", "Location", "SubLocation", "Lane", "Source", "Description", "Reporter", "Responder", "Category", "detection", "duration" """ db_wrapper = AutoBMSWrapper() session = db_wrapper.session query = session.query( Log.ControllerName, Log.Direction, Log.Location, Log.SubLocation, Log.Lane, Log.Source, Log.Description, Log.Reporter, Log.Responder, Log.Category, func.TIME(Log.IncidentDetection).label('detection'), func.TIMESTAMPDIFF(_literal_as_text("SECOND"),Log.IncidentDetection, Log.IncidentClear).label("duration"))\ .filter(Log.Category.notin_(["Fire Incident", "Accident", "Debris"]))\ .order_by(func.RAND())\ .limit(sample_size).all() # We are done with the database connection now, lets not leave 10^6 threads open on the server # session.close() df = DataFrame.from_dict(query) df.columns = [ "ControllerName", "Direction", "Location", "SubLocation", "Lane", "Source", "Description", "Reporter", "Responder", "Category", "detection", "duration" ] return df
def add_match(self, value): def replace(node): if isinstance(node, MatchClause): return MatchClause(' '.join([node.value, value])) return node if self._criterion is not None: self._criterion = replacement_traverse(self._criterion, {}, replace) else: criterion = _literal_as_text(MatchClause(value)) self._criterion = self._adapt_clause(criterion, True, True)
def match(self, *args, **kwargs): clause_already_exists = [False] def replace(node): if isinstance(node, MatchClause): clause_already_exists[0] = True node.extend(args, kwargs) return node self._criterion = replacement_traverse(self._criterion, {}, replace) if not clause_already_exists[0]: criterion = _literal_as_text(MatchClause(args, kwargs)) self._criterion = self._adapt_clause(criterion, True, True)
def __init__(self, stmt, analyze=False, verbose=False, costs=True, buffers=False, timing=True, format='text'): self.statement = _literal_as_text(stmt) self.analyze = analyze self.verbose = verbose self.costs = costs self.buffers = buffers self.timing = timing self.format = format
def __init__( self, stmt, analyze=False, verbose=False, costs=True, buffers=False, timing=True, format='text' ): self.statement = _literal_as_text(stmt) self.analyze = analyze self.verbose = verbose self.costs = costs self.buffers = buffers self.timing = timing self.format = format
def __init__(self, *elements, **kw): """ :param \*elements: A sequence of two tuples of the form ``(column, operator)`` where column must be a column name or Column object and operator must be a string containing the operator to use. :param name: Optional, the in-database name of this constraint. :param deferrable: Optional bool. If set, emit DEFERRABLE or NOT DEFERRABLE when issuing DDL for this constraint. :param initially: Optional string. If set, emit INITIALLY <value> when issuing DDL for this constraint. :param using: Optional string. If set, emit USING <index_method> when issuing DDL for this constraint. Defaults to 'gist'. :param where: Optional string. If set, emit WHERE <predicate> when issuing DDL for this constraint. """ ColumnCollectionConstraint.__init__( self, name=kw.get('name'), deferrable=kw.get('deferrable'), initially=kw.get('initially'), *[col for col, op in elements] ) self.operators = {} for col_or_string, op in elements: name = getattr(col_or_string, 'name', col_or_string) self.operators[name] = op self.using = kw.get('using', 'gist') where = kw.get('where') if where: self.where = expression._literal_as_text(where)
def __init__(self, *elements, **kw): """ :param \*elements: A sequence of two tuples of the form ``(column, operator)`` where column must be a column name or Column object and operator must be a string containing the operator to use. :param name: Optional, the in-database name of this constraint. :param deferrable: Optional bool. If set, emit DEFERRABLE or NOT DEFERRABLE when issuing DDL for this constraint. :param initially: Optional string. If set, emit INITIALLY <value> when issuing DDL for this constraint. :param using: Optional string. If set, emit USING <index_method> when issuing DDL for this constraint. Defaults to 'gist'. :param where: Optional string. If set, emit WHERE <predicate> when issuing DDL for this constraint. """ ColumnCollectionConstraint.__init__( self, *[col for col, op in elements], name=kw.get('name'), deferrable=kw.get('deferrable'), initially=kw.get('initially') ) self.operators = {} for col_or_string, op in elements: name = getattr(col_or_string, 'name', col_or_string) self.operators[name] = op self.using = kw.get('using', 'gist') where = kw.get('where') if where: self.where = expression._literal_as_text(where)
def distinct(self, *criterion): """ X{Backport} depuis SQLAlchemy 0.7. Ajoute un ``DISTINCT`` à la requête et retourne la nouvelle ``Query`` ainsi créée. :param \*expr: expressions optionnels se rapportant à des colonnes et qui généreront une expression DISTINCT ON dans PostgreSQL. """ # pylint: disable-msg=W0212 # W0212: Access to a protected member of a client class if not criterion: self._distinct = True else: criterion = [ self._adapt_clause( expression._literal_as_text(o), True, True) for o in criterion ] if isinstance(self._distinct, list): self._distinct += criterion else: self._distinct = criterion
def __init__(self, stmt): self.statement = _literal_as_text(stmt)
def __init__(self, stmt, analyze=False): self.statement = _literal_as_text(stmt) self.analyze = analyze # helps with INSERT statements self.inline = getattr(stmt, 'inline', None)
def __init__(self, statement, analyze=False, buffers=False): self.statement = _literal_as_text(statement) self.analyze = analyze self.buffers = buffers
def __init__(self, stmt, analyze=False): self.statement = _literal_as_text(stmt) self.analyze = analyze
def __init__(self, stmt, analyze=False): self.statement = _literal_as_text(stmt) self.analyze = analyze self.inline = getattr(stmt, 'inline', None)
from sqlalchemy.sql import expression __author__ = "wangshubin" a = [1, 2, 3] b = [4, 5, 6] c = a + b # print(c) class A(object): a = 1 b = 2 arr = (A.a == "a", A.b == 1, "c" >= "a") for cri in list(arr): e = expression._literal_as_text(cri) print e