Python sqlalchemy.sql.elements.ClauseElement() Examples

The following are 18 code examples of sqlalchemy.sql.elements.ClauseElement(). 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.elements , or try the search function .
Example #1
Source File: test_compare.py    From sqlalchemy with MIT License 6 votes vote down vote up
def test_cache_key_no_method(self):
        class Foobar1(ClauseElement):
            pass

        class Foobar2(ColumnElement):
            pass

        # the None for cache key will prevent objects
        # which contain these elements from being cached.
        f1 = Foobar1()
        eq_(f1._generate_cache_key(), None)

        f2 = Foobar2()
        eq_(f2._generate_cache_key(), None)

        s1 = select([column("q"), Foobar2()])

        eq_(s1._generate_cache_key(), None) 
Example #2
Source File: test_compare.py    From sqlalchemy with MIT License 6 votes vote down vote up
def test_all_present(self):
        need = set(
            cls
            for cls in class_hierarchy(ClauseElement)
            if issubclass(cls, (ColumnElement, Selectable, LambdaElement))
            and (
                "__init__" in cls.__dict__
                or issubclass(cls, AliasedReturnsRows)
            )
            and not issubclass(cls, (Annotated))
            and "orm" not in cls.__module__
            and "compiler" not in cls.__module__
            and "crud" not in cls.__module__
            and "dialects" not in cls.__module__  # TODO: dialects?
        ).difference({ColumnElement, UnaryExpression})

        for fixture in self.fixtures + self.dont_compare_values_fixtures:
            case_a = fixture()
            for elem in case_a:
                for mro in type(elem).__mro__:
                    need.discard(mro)

        is_false(bool(need), "%d Remaining classes: %r" % (len(need), need)) 
Example #3
Source File: vector.py    From siuba with MIT License 6 votes vote down vote up
def _between_sql(x, left, right, default = None) -> ClauseElement:
    """
    Example:
        >>> print(between(sql.column('a'), 1, 2))
        a BETWEEN :a_1 AND :a_2

        >>> print(between(sql.column('a'), 1, 2, default = False))
        coalesce(a BETWEEN :a_1 AND :a_2, :coalesce_1)

    """
    
    if default is not False:
        # TODO: warn
        pass

    if default is None:
        return x.between(left, right)

    return sql.functions.coalesce(x.between(left, right), default)

# coalesce -------------------------------------------------------------------- 
Example #4
Source File: vector.py    From siuba with MIT License 5 votes vote down vote up
def _desc_sql(x) -> ClauseElement:
    """
    Example:
        >>> print(desc(sql.column('a')))
        a DESC
    """
    return x.desc()


# ranking functions -----------------------------------------------------------
# note: here we don't use the decorator syntax, but maybe we should for
#       consistency
# TODO: remove repetition in rank definitions 
Example #5
Source File: vector.py    From siuba with MIT License 5 votes vote down vote up
def _nth_sql(x, n, order_by = None, default = None) -> ClauseElement:
    if default is not None:
        raise NotImplementedError("default argument not implemented")

    if n < 0 and order_by is not None:
        # e.g. -1 in python is 0, -2 is 1
        n = abs(n + 1)
        order_by = order_by.desc()


    return RankOver(sql.func.nth_value(x, n + 1), order_by = order_by) 
Example #6
Source File: vector.py    From siuba with MIT License 5 votes vote down vote up
def _na_if_sql(x, y) -> ClauseElement:
    """
    Example:
        >>> print(na_if(sql.column('x'), 2))
        nullif(x, :nullif_1)
    """
    return sql.func.nullif(x, y)


# nth, first, last ------------------------------------------------------------
# note: first and last wrap around nth, so are not dispatchers.
#       this may need to change this in the future, since this means they won't
#       show their own name, when you print, e.g. first(_.x) 
Example #7
Source File: vector.py    From siuba with MIT License 5 votes vote down vote up
def _n_distinct_sql(x) -> ClauseElement:
    """
    Example:
        >>> print(n_distinct(sql.column('a')) )
        count(distinct(a))
    """
    return sql.func.count(sql.func.distinct(x))


# na_if ----------------------------------------------------------------------- 
Example #8
Source File: vector.py    From siuba with MIT License 5 votes vote down vote up
def _n_sql_agg(x) -> ClauseElement:
    """
    Example:
        >>> from siuba.sql.translate import SqlColumnAgg
        >>> print(n(SqlColumnAgg('x')))
        count(*)
    """

    return sql.func.count() 
Example #9
Source File: vector.py    From siuba with MIT License 5 votes vote down vote up
def _n_sql(x) -> ClauseElement:
    """
    Example:
        >>> print(n(sql.column('a')))
        count(*) OVER ()
    """
    return AggOver(sql.func.count()) 
Example #10
Source File: vector.py    From siuba with MIT License 5 votes vote down vote up
def _lead_sql(x, n = 1, default = None) -> ClauseElement:
    """
    Example:
        >>> print(lead(sql.column('a'), 2, 99))
        lead(a, :lead_1, :lead_2) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
    """
    f = win_cumul("lead")
    return f(x, n, default) 
Example #11
Source File: vector.py    From siuba with MIT License 5 votes vote down vote up
def _coalesce_sql(x, *args) -> ClauseElement:
    """
    Example:
        >>> print(coalesce(sql.column('a'), sql.column('b')))
        coalesce(a, b)

        >>> coalesce(1, sql.column('a'))
        Traceback (most recent call last):
            ...
        TypeError: ...
    """
    return sql.functions.coalesce(x, *args)


# lead and lag ---------------------------------------------------------------- 
Example #12
Source File: test_narrow.py    From zulip with Apache License 2.0 5 votes vote down vote up
def get_sqlalchemy_sql(query: ClauseElement) -> str:
    dialect = get_sqlalchemy_connection().dialect
    comp = query.compile(dialect=dialect)
    return str(comp) 
Example #13
Source File: string.py    From siuba with MIT License 5 votes vote down vote up
def _str_c_sql(x, *args, sep = "", collapse = None) -> ClauseElement:
    """
    Example:
    """

    if collapse is not None:
        raise NotImplementedError("For SQL, collapse argument of str_c not supported")

    if sep != "":
        raise NotImplementedError('For SQL, sep argument of str_c must be ""')

    return sql.func.concat(x, *args) 
Example #14
Source File: model.py    From dvhb-hybrid with MIT License 5 votes vote down vote up
def copy_object(self):
        cls = type(self)
        obj = cls(
            (n, v)
            for n, v in dict.items(self)
            if not isinstance(v, ClauseElement))
        return obj 
Example #15
Source File: test_compare.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_copy_internals_no_method(self):
        class Foobar1(ClauseElement):
            pass

        class Foobar2(ColumnElement):
            pass

        f1 = Foobar1()
        f2 = Foobar2()

        f1._copy_internals()
        f2._copy_internals() 
Example #16
Source File: test_compare.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_get_children_no_method(self):
        class Foobar1(ClauseElement):
            pass

        class Foobar2(ColumnElement):
            pass

        f1 = Foobar1()
        eq_(f1.get_children(), [])

        f2 = Foobar2()
        eq_(f2.get_children(), []) 
Example #17
Source File: test_compare.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_cache_key_unknown_traverse(self):
        class Foobar1(ClauseElement):
            _traverse_internals = [
                ("key", InternalTraversal.dp_anon_name),
                ("type_", InternalTraversal.dp_unknown_structure),
            ]

            def __init__(self, key, type_):
                self.key = key
                self.type_ = type_

        f1 = Foobar1("foo", String())
        eq_(f1._generate_cache_key(), None) 
Example #18
Source File: test_narrow.py    From zulip with Apache License 2.0 5 votes vote down vote up
def get_sqlalchemy_query_params(query: ClauseElement) -> Dict[str, object]:
    dialect = get_sqlalchemy_connection().dialect
    comp = query.compile(dialect=dialect)
    return comp.params