当前位置: 首页>>代码示例>>Python>>正文


Python expression.FunctionElement方法代码示例

本文整理汇总了Python中sqlalchemy.sql.expression.FunctionElement方法的典型用法代码示例。如果您正苦于以下问题:Python expression.FunctionElement方法的具体用法?Python expression.FunctionElement怎么用?Python expression.FunctionElement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sqlalchemy.sql.expression的用法示例。


在下文中一共展示了expression.FunctionElement方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_conn_execute

# 需要导入模块: from sqlalchemy.sql import expression [as 别名]
# 或者: from sqlalchemy.sql.expression import FunctionElement [as 别名]
def test_conn_execute(self, connection):
        from sqlalchemy.sql.expression import FunctionElement
        from sqlalchemy.ext.compiler import compiles

        class myfunc(FunctionElement):
            type = Date()

        @compiles(myfunc)
        def compile_(elem, compiler, **kw):
            return compiler.process(func.current_date())

        x = connection.execute(func.current_date()).scalar()
        y = connection.execute(func.current_date().select()).scalar()
        z = connection.scalar(func.current_date())
        q = connection.scalar(myfunc())

        assert (x == y == z == q) is True 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:19,代码来源:test_functions.py

示例2: test_functions

# 需要导入模块: from sqlalchemy.sql import expression [as 别名]
# 或者: from sqlalchemy.sql.expression import FunctionElement [as 别名]
def test_functions(self):
        from sqlalchemy.dialects import postgresql

        class MyUtcFunction(FunctionElement):
            pass

        @compiles(MyUtcFunction)
        def visit_myfunc(element, compiler, **kw):
            return "utcnow()"

        @compiles(MyUtcFunction, "postgresql")
        def visit_myfunc_pg(element, compiler, **kw):
            return "timezone('utc', current_timestamp)"

        self.assert_compile(
            MyUtcFunction(), "utcnow()", use_default_dialect=True
        )
        self.assert_compile(
            MyUtcFunction(),
            "timezone('utc', current_timestamp)",
            dialect=postgresql.dialect(),
        ) 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:24,代码来源:test_compiler.py

示例3: test_function_subclasses_one

# 需要导入模块: from sqlalchemy.sql import expression [as 别名]
# 或者: from sqlalchemy.sql.expression import FunctionElement [as 别名]
def test_function_subclasses_one(self):
        class Base(FunctionElement):
            name = "base"

        class Sub1(Base):
            name = "sub1"

        class Sub2(Base):
            name = "sub2"

        @compiles(Base)
        def visit_base(element, compiler, **kw):
            return element.name

        @compiles(Sub1)
        def visit_sub1(element, compiler, **kw):
            return "FOO" + element.name

        self.assert_compile(
            select([Sub1(), Sub2()]),
            "SELECT FOOsub1 AS sub1_1, sub2 AS sub2_1",
            use_default_dialect=True,
        ) 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:25,代码来源:test_compiler.py

示例4: test_use_labels_function_element

# 需要导入模块: from sqlalchemy.sql import expression [as 别名]
# 或者: from sqlalchemy.sql.expression import FunctionElement [as 别名]
def test_use_labels_function_element(self):
        from sqlalchemy.ext.compiler import compiles

        class max_(FunctionElement):
            name = "max"

        @compiles(max_)
        def visit_max(element, compiler, **kw):
            return "max(%s)" % compiler.process(element.clauses, **kw)

        self.assert_compile(
            select([max_(5, 6)], use_labels=True),
            "SELECT max(:max_2, :max_3) AS max_1",
        ) 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:16,代码来源:test_functions.py

示例5: test_incorrect_none_type

# 需要导入模块: from sqlalchemy.sql import expression [as 别名]
# 或者: from sqlalchemy.sql.expression import FunctionElement [as 别名]
def test_incorrect_none_type(self):
        from sqlalchemy.sql.expression import FunctionElement

        class MissingType(FunctionElement):
            name = "mt"
            type = None

        assert_raises_message(
            TypeError,
            "Object None associated with '.type' attribute is "
            "not a TypeEngine class or object",
            lambda: column("x", MissingType()) == 5,
        ) 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:15,代码来源:test_functions.py

示例6: test_function_element_column_labels

# 需要导入模块: from sqlalchemy.sql import expression [as 别名]
# 或者: from sqlalchemy.sql.expression import FunctionElement [as 别名]
def test_function_element_column_labels(self):
        users = self.tables.users
        sess = Session()

        class max_(expression.FunctionElement):
            name = "max"

        @compiles(max_)
        def visit_max(element, compiler, **kw):
            return "max(%s)" % compiler.process(element.clauses, **kw)

        q = sess.query(max_(users.c.id))
        eq_(q.all(), [(10,)]) 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:15,代码来源:test_query.py

示例7: test_function_calls_base

# 需要导入模块: from sqlalchemy.sql import expression [as 别名]
# 或者: from sqlalchemy.sql.expression import FunctionElement [as 别名]
def test_function_calls_base(self):
        from sqlalchemy.dialects import mssql

        class greatest(FunctionElement):
            type = Numeric()
            name = "greatest"

        @compiles(greatest)
        def default_greatest(element, compiler, **kw):
            return compiler.visit_function(element)

        @compiles(greatest, "mssql")
        def case_greatest(element, compiler, **kw):
            arg1, arg2 = list(element.clauses)
            return "CASE WHEN %s > %s THEN %s ELSE %s END" % (
                compiler.process(arg1),
                compiler.process(arg2),
                compiler.process(arg1),
                compiler.process(arg2),
            )

        self.assert_compile(
            greatest("a", "b"),
            "greatest(:greatest_1, :greatest_2)",
            use_default_dialect=True,
        )
        self.assert_compile(
            greatest("a", "b"),
            "CASE WHEN :greatest_1 > :greatest_2 "
            "THEN :greatest_1 ELSE :greatest_2 END",
            dialect=mssql.dialect(),
        ) 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:34,代码来源:test_compiler.py

示例8: test_function_subclasses_two

# 需要导入模块: from sqlalchemy.sql import expression [as 别名]
# 或者: from sqlalchemy.sql.expression import FunctionElement [as 别名]
def test_function_subclasses_two(self):
        class Base(FunctionElement):
            name = "base"

        class Sub1(Base):
            name = "sub1"

        @compiles(Base)
        def visit_base(element, compiler, **kw):
            return element.name

        class Sub2(Base):
            name = "sub2"

        class SubSub1(Sub1):
            name = "subsub1"

        self.assert_compile(
            select([Sub1(), Sub2(), SubSub1()]),
            "SELECT sub1 AS sub1_1, sub2 AS sub2_1, subsub1 AS subsub1_1",
            use_default_dialect=True,
        )

        @compiles(Sub1)
        def visit_sub1(element, compiler, **kw):
            return "FOO" + element.name

        self.assert_compile(
            select([Sub1(), Sub2(), SubSub1()]),
            "SELECT FOOsub1 AS sub1_1, sub2 AS sub2_1, "
            "FOOsubsub1 AS subsub1_1",
            use_default_dialect=True,
        ) 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:35,代码来源:test_compiler.py


注:本文中的sqlalchemy.sql.expression.FunctionElement方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。