Python sqlalchemy.sql.case() Examples

The following are 9 code examples of sqlalchemy.sql.case(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sqlalchemy.sql , or try the search function .
Example #1
Source File: scheduled_transaction.py    From biweeklybudget with GNU Affero General Public License v3.0 6 votes vote down vote up
def recurrence_str(cls):
        return case(
            [
                (
                    cls.date.isnot(None),
                    func.date_format(cls.date, '%Y-%m-%d')
                ),
                (
                    cls.day_of_month.isnot(None),
                    cls.day_of_month
                ),
                (
                    cls.num_per_period.isnot(None),
                    func.concat(cls.num_per_period, ' per period')
                )
            ],
            else_=''
        ) 
Example #2
Source File: verbs.py    From siuba with MIT License 6 votes vote down vote up
def _validate_join_arg_on(on, sql_on = None):
    # handle sql on case
    if sql_on is not None:
        if on is not None:
            raise ValueError("Cannot specify both on and sql_on")

        return sql_on

    # handle general cases
    if on is None:
        raise NotImplementedError("on arg currently cannot be None (default) for SQL")
    elif isinstance(on, str):
        on = {on: on}
    elif isinstance(on, (list, tuple)):
        on = dict(zip(on, on))

    if not isinstance(on, Mapping):
        raise TypeError("on must be a Mapping (e.g. dict)")

    return on 
Example #3
Source File: scheduled_transaction.py    From biweeklybudget with GNU Affero General Public License v3.0 5 votes vote down vote up
def schedule_type(cls):
        return case(
            [
                (cls.date.isnot(None), 'date'),
                (cls.day_of_month.isnot(None), 'monthly'),
                (cls.num_per_period.isnot(None), 'per period')
            ],
            else_=''
        ) 
Example #4
Source File: db.py    From sticker-finder with MIT License 5 votes vote down vote up
def case_greatest(element, compiler, **kw):
    arg1, arg2 = list(element.clauses)
    return compiler.process(case([(arg1 > arg2, arg1)], else_=arg2), **kw) 
Example #5
Source File: support.py    From choochoo with GNU General Public License v2.0 5 votes vote down vote up
def case_greatest(element, compiler, **kw):
    arg1, arg2 = list(element.clauses)
    return compiler.process(case([(arg1 > arg2, arg1)], else_=arg2), **kw) 
Example #6
Source File: api.py    From koschei with GNU General Public License v2.0 5 votes vote down vote up
def sql_if(cond, then, else_=None):
    return case([(cond, then)], else_=else_) 
Example #7
Source File: vector.py    From siuba with MIT License 5 votes vote down vote up
def _sql_rank_over(rank_func, col, partition):
    # partitioning ensures aggregates that use total length are correct
    # e.g. percent rank, cume_dist and friends
    over_clause = RankOver(
            rank_func(),
            order_by = col,
            partition_by = col.isnot(None) if partition else None
            )

    return sql.case({col.isnot(None): over_clause}) 
Example #8
Source File: verbs.py    From siuba with MIT License 5 votes vote down vote up
def _case_when(__data, cases):
    # TODO: will need listener to enter case statements, to handle when they use windows
    if isinstance(cases, Call):
        cases = cases(__data)

    whens = []
    case_items = list(cases.items())
    n_items = len(case_items)

    else_val = None
    for ii, (expr, val) in enumerate(case_items):
        # handle where val is a column expr
        if callable(val):
            val = val(__data)

        # handle when expressions
        if ii+1 == n_items and expr is True:
            else_val = val
        elif callable(expr):
            whens.append((expr(__data), val))
        else:
            whens.append((expr, val))

    return sql.case(whens, else_ = else_val)
        

# Join ------------------------------------------------------------------------ 
Example #9
Source File: verbs.py    From siuba with MIT License 5 votes vote down vote up
def _if_else(cond, true_vals, false_vals):
    whens = [(cond, true_vals)]
    return sql.case(whens, else_ = false_vals)