當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。