def _where_clause(self, table, map, values): cl = ClauseList(operator=operators.and_) for key, val in values.iteritems(): if key not in map.keys(): continue if val is None: continue col = map[key] if IRelation.providedBy(col): if isinstance(val, dict): # read related object included in the query try: o = self.pickup(col.cls, **val) assert len(o) == 1 # sanity check val = o[0]._id except IndexError: raise DbError('A related object could not be ' + \ 'located in the database. %s: %s' % \ (col.cls, str(val))) elif isinstance(val, col.cls): # related object was included, use it's id val = val._id elif val == DB_NULL: val = None elif val: raise DbError("Invalid value for key '%s': %s" % \ (str(col.name), str(val))) col = col.name cl.append(table.c[col] == val) return cl
def append_order_by(self, *clauses): if len(clauses) == 1 and clauses[0] is None: self._order_by_clause = ClauseList() else: if getattr(self, '_order_by_clause', None) is not None: clauses = list(self._order_by_clause) + list(clauses) self._order_by_clause = ClauseList(*clauses)
def get(self): w = ClauseList() for k in self.keyargs.keys(): w.append(self.table.c[k] == self.keyargs[k]) try: q = select([self.table.c[self.attr_name]], w) res = self.db_storage.db.execute(q).fetchall() assert len(res) <= 1 # sanity check return res[0][self.attr_name] except IndexError: raise DbError('An error occurred while getting related object ' + \ 'data "%s": %s' % (self.attr_name, self.keyargs))
def __init__(self, type_=None, group=True, args=(), **kwargs): self.packagenames = [] self.name = self.__class__.__name__ self._bind = kwargs.get('bind', None) if group: self.clause_expr = ClauseList(operator=operators.comma_op, group_contents=True, *args).self_group() else: self.clause_expr = ClauseList(operator=operators.comma_op, group_contents=True, *args) self.type = sqltypes.to_instance( type_ or getattr(self, '__return_type__', None))
def test_notin(self): left = column('left') assert left.comparator.operate(operators.notin_op, [1, 2, 3]).compare( BinaryExpression( left, Grouping(ClauseList(literal(1), literal(2), literal(3))), operators.notin_op)) self._loop_test(operators.notin_op, [1, 2, 3])
def test_unusual_column_elements_clauselist(self): """Test that raw ClauseList is expanded into .c.""" from sqlalchemy.sql.expression import ClauseList s = select([table1.c.col1, ClauseList(table1.c.col2, table1.c.col3)]) eq_( list(s.c), [s.c.col1, s.c.col2, s.c.col3] )
def __init__(self, table, whereclause, bind=None, returning=None, order_by=None, limit=None, offset=None, **kwargs): Delete.__init__(self, table, whereclause, bind, returning, **kwargs) self._order_by_clause = ClauseList(*util.to_list(order_by) or []) self._limit = limit self._offset = offset
def __init__(self, columns, against, bool=False): self.columns = ClauseList(*columns) self.against = bindparam('search', against) self.bool = bool
def __init__(self, column, seperator, order_by=None): self.column = ClauseList(*to_list(column)) self.seperator = literal(seperator) self.order_by = None if order_by is not None: self.order_by = ClauseList(*to_list(order_by))
def __init__(self, expr, order_by=None): self.expr = ClauseElement(expr) self.order_by = None if order_by is not None: self.order_by = ClauseList(*to_list(order_by))