當前位置: 首頁>>代碼示例>>Python>>正文


Python sql.literal方法代碼示例

本文整理匯總了Python中sqlalchemy.sql.literal方法的典型用法代碼示例。如果您正苦於以下問題:Python sql.literal方法的具體用法?Python sql.literal怎麽用?Python sql.literal使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在sqlalchemy.sql的用法示例。


在下文中一共展示了sql.literal方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_exception_wrapping_non_dbapi_statement

# 需要導入模塊: from sqlalchemy import sql [as 別名]
# 或者: from sqlalchemy.sql import literal [as 別名]
def test_exception_wrapping_non_dbapi_statement(self):
        class MyType(TypeDecorator):
            impl = Integer

            def process_bind_param(self, value, dialect):
                raise SomeException("nope")

        def _go(conn):
            assert_raises_message(
                tsa.exc.StatementError,
                r"\(.*.SomeException\) " r"nope\n\[SQL\: u?SELECT 1 ",
                conn.execute,
                select([1]).where(column("foo") == literal("bar", MyType())),
            )

        _go(testing.db)
        with testing.db.connect() as conn:
            _go(conn) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:20,代碼來源:test_execute.py

示例2: test_dont_wrap_mixin

# 需要導入模塊: from sqlalchemy import sql [as 別名]
# 或者: from sqlalchemy.sql import literal [as 別名]
def test_dont_wrap_mixin(self):
        class MyException(Exception, tsa.exc.DontWrapMixin):
            pass

        class MyType(TypeDecorator):
            impl = Integer

            def process_bind_param(self, value, dialect):
                raise MyException("nope")

        def _go(conn):
            assert_raises_message(
                MyException,
                "nope",
                conn.execute,
                select([1]).where(column("foo") == literal("bar", MyType())),
            )

        _go(testing.db)
        conn = testing.db.connect()
        try:
            _go(conn)
        finally:
            conn.close() 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:26,代碼來源:test_execute.py

示例3: test_recursive_union_no_alias_three

# 需要導入模塊: from sqlalchemy import sql [as 別名]
# 或者: from sqlalchemy.sql import literal [as 別名]
def test_recursive_union_no_alias_three(self):
        # like test one, but let's refer to the CTE
        # in a sibling CTE.

        s1 = select([literal(0).label("x")])
        cte = s1.cte(name="cte", recursive=True)

        # can't do it here...
        # bar = select([cte]).cte('bar')
        cte = cte.union_all(select([cte.c.x + 1]).where(cte.c.x < 10))
        bar = select([cte]).cte("bar")

        s2 = select([cte, bar])
        self.assert_compile(
            s2,
            "WITH RECURSIVE cte(x) AS "
            "(SELECT :param_1 AS x UNION ALL "
            "SELECT cte.x + :x_1 AS anon_1 "
            "FROM cte WHERE cte.x < :x_2), "
            "bar AS (SELECT cte.x AS x FROM cte) "
            "SELECT cte.x, bar.x FROM cte, bar",
        ) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:24,代碼來源:test_cte.py

示例4: test_recursive_union_alias_three

# 需要導入模塊: from sqlalchemy import sql [as 別名]
# 或者: from sqlalchemy.sql import literal [as 別名]
def test_recursive_union_alias_three(self):
        # like test one, but let's refer to the CTE
        # in a sibling CTE.

        s1 = select([literal(0).label("x")])
        cte = s1.cte(name="cte", recursive=True)

        # can't do it here...
        # bar = select([cte]).cte('bar')
        cte = cte.union_all(select([cte.c.x + 1]).where(cte.c.x < 10)).alias(
            "cs1"
        )
        bar = select([cte]).cte("bar").alias("cs2")

        s2 = select([cte, bar])
        self.assert_compile(
            s2,
            "WITH RECURSIVE cte(x) AS "
            "(SELECT :param_1 AS x UNION ALL "
            "SELECT cte.x + :x_1 AS anon_1 "
            "FROM cte WHERE cte.x < :x_2), "
            "bar AS (SELECT cs1.x AS x FROM cte AS cs1) "
            "SELECT cs1.x, cs2.x FROM cte AS cs1, bar AS cs2",
        ) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:26,代碼來源:test_cte.py

示例5: test_named_alias_disable_quote

# 需要導入模塊: from sqlalchemy import sql [as 別名]
# 或者: from sqlalchemy.sql import literal [as 別名]
def test_named_alias_disable_quote(self):
        cte = select([literal(1).label("id")]).cte(
            name=quoted_name("CTE", quote=False)
        )

        s1 = select([cte.c.id]).alias(
            name=quoted_name("DontQuote", quote=False)
        )

        s = select([s1])
        self.assert_compile(
            s,
            "WITH CTE AS (SELECT :param_1 AS id) "
            "SELECT DontQuote.id FROM "
            "(SELECT CTE.id AS id FROM CTE) AS DontQuote",
        ) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:18,代碼來源:test_cte.py

示例6: test_math_op

# 需要導入模塊: from sqlalchemy import sql [as 別名]
# 或者: from sqlalchemy.sql import literal [as 別名]
def test_math_op(self, py_op, sql_op):
        for (lhs, rhs, res) in (
            (5, self.table1.c.myid, ":myid_1 %s mytable.myid"),
            (5, literal(5), ":param_1 %s :param_2"),
            (self.table1.c.myid, "b", "mytable.myid %s :myid_1"),
            (self.table1.c.myid, literal(2.7), "mytable.myid %s :param_1"),
            (
                self.table1.c.myid,
                self.table1.c.myid,
                "mytable.myid %s mytable.myid",
            ),
            (literal(5), 8, ":param_1 %s :param_2"),
            (literal(6), self.table1.c.myid, ":param_1 %s mytable.myid"),
            (literal(7), literal(5.5), ":param_1 %s :param_2"),
        ):
            self.assert_compile(py_op(lhs, rhs), res % sql_op) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:18,代碼來源:test_operators.py

示例7: topic_match_sa

# 需要導入模塊: from sqlalchemy import sql [as 別名]
# 或者: from sqlalchemy.sql import literal [as 別名]
def topic_match_sa(topic_name: str) -> Any:
    # _sa is short for Sql Alchemy, which we use mostly for
    # queries that search messages
    topic_cond = func.upper(column("subject")) == func.upper(literal(topic_name))
    return topic_cond 
開發者ID:zulip,項目名稱:zulip,代碼行數:7,代碼來源:topic.py

示例8: get_select_precolumns

# 需要導入模塊: from sqlalchemy import sql [as 別名]
# 或者: from sqlalchemy.sql import literal [as 別名]
def get_select_precolumns(self, select):
        """Called when building a ``SELECT`` statement, position is just
        before column list Firebird puts the limit and offset right
        after the ``SELECT``...
        """

        result = ""
        if select._limit:
            result += "FIRST %s " % self.process(sql.literal(select._limit))
        if select._offset:
            result += "SKIP %s " % self.process(sql.literal(select._offset))
        if select._distinct:
            result += "DISTINCT "
        return result 
開發者ID:gltn,項目名稱:stdm,代碼行數:16,代碼來源:base.py

示例9: query_expression

# 需要導入模塊: from sqlalchemy import sql [as 別名]
# 或者: from sqlalchemy.sql import literal [as 別名]
def query_expression(default_expr=_sql.null()):
    """Indicate an attribute that populates from a query-time SQL expression.

    :param default_expr: Optional SQL expression object that will be used in
        all cases if not assigned later with :func:`_orm.with_expression`.
        E.g.::

            from sqlalchemy.sql import literal

            class C(Base):
                #...
                my_expr = query_expression(literal(1))

        .. versionadded:: 1.3.18


    .. versionadded:: 1.2

    .. seealso::

        :ref:`mapper_querytime_expression`

    """
    prop = ColumnProperty(default_expr)
    prop.strategy_key = (("query_expression", True),)
    return prop 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:28,代碼來源:__init__.py

示例10: test_exception_event_ad_hoc_context

# 需要導入模塊: from sqlalchemy import sql [as 別名]
# 或者: from sqlalchemy.sql import literal [as 別名]
def test_exception_event_ad_hoc_context(self):
        """test that handle_error is called with a context in
        cases where _handle_dbapi_error() is normally called without
        any context.

        """

        engine = engines.testing_engine()

        listener = Mock(return_value=None)
        event.listen(engine, "handle_error", listener)

        nope = SomeException("nope")

        class MyType(TypeDecorator):
            impl = Integer

            def process_bind_param(self, value, dialect):
                raise nope

        with engine.connect() as conn:
            assert_raises_message(
                tsa.exc.StatementError,
                r"\(.*.SomeException\) " r"nope\n\[SQL\: u?SELECT 1 ",
                conn.execute,
                select([1]).where(column("foo") == literal("bar", MyType())),
            )

        ctx = listener.mock_calls[0][1][0]
        assert ctx.statement.startswith("SELECT 1 ")
        is_(ctx.is_disconnect, False)
        is_(ctx.original_exception, nope) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:34,代碼來源:test_execute.py

示例11: test_noorderby_parameters_insubquery

# 需要導入模塊: from sqlalchemy import sql [as 別名]
# 或者: from sqlalchemy.sql import literal [as 別名]
def test_noorderby_parameters_insubquery(self):
        """test that the ms-sql dialect does not include ORDER BY
        positional parameters in subqueries"""

        table1 = table(
            "mytable",
            column("myid", Integer),
            column("name", String),
            column("description", String),
        )

        q = select(
            [table1.c.myid, sql.literal("bar").label("c1")],
            order_by=[table1.c.name + "-"],
        ).alias("foo")
        crit = q.c.myid == table1.c.myid
        dialect = mssql.dialect()
        dialect.paramstyle = "qmark"
        dialect.positional = True
        self.assert_compile(
            select(["*"], crit),
            "SELECT * FROM (SELECT mytable.myid AS "
            "myid, ? AS c1 FROM mytable) AS foo, mytable WHERE "
            "foo.myid = mytable.myid",
            dialect=dialect,
            checkparams={"param_1": "bar"},
            # if name_1 is included, too many parameters are passed to dbapi
            checkpositional=("bar",),
        ) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:31,代碼來源:test_compiler.py

示例12: test_recursive_union_alias_one

# 需要導入模塊: from sqlalchemy import sql [as 別名]
# 或者: from sqlalchemy.sql import literal [as 別名]
def test_recursive_union_alias_one(self):
        s1 = select([literal(0).label("x")])
        cte = s1.cte(name="cte", recursive=True)
        cte = cte.union_all(select([cte.c.x + 1]).where(cte.c.x < 10)).alias(
            "cr1"
        )
        s2 = select([cte])
        self.assert_compile(
            s2,
            "WITH RECURSIVE cte(x) AS "
            "(SELECT :param_1 AS x UNION ALL "
            "SELECT cte.x + :x_1 AS anon_1 "
            "FROM cte WHERE cte.x < :x_2) "
            "SELECT cr1.x FROM cte AS cr1",
        ) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:17,代碼來源:test_cte.py

示例13: test_multi_subq_alias

# 需要導入模塊: from sqlalchemy import sql [as 別名]
# 或者: from sqlalchemy.sql import literal [as 別名]
def test_multi_subq_alias(self):
        cte = select([literal(1).label("id")]).cte(name="cte1").alias("aa")

        s1 = select([cte.c.id]).alias()
        s2 = select([cte.c.id]).alias()

        s = select([s1, s2])
        self.assert_compile(
            s,
            "WITH cte1 AS (SELECT :param_1 AS id) "
            "SELECT anon_1.id, anon_2.id FROM "
            "(SELECT aa.id AS id FROM cte1 AS aa) AS anon_1, "
            "(SELECT aa.id AS id FROM cte1 AS aa) AS anon_2",
        ) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:16,代碼來源:test_cte.py

示例14: test_named_alias_no_quote

# 需要導入模塊: from sqlalchemy import sql [as 別名]
# 或者: from sqlalchemy.sql import literal [as 別名]
def test_named_alias_no_quote(self):
        cte = select([literal(1).label("id")]).cte(name="CTE")

        s1 = select([cte.c.id]).alias(name="no_quotes")

        s = select([s1])
        self.assert_compile(
            s,
            'WITH "CTE" AS (SELECT :param_1 AS id) '
            "SELECT no_quotes.id FROM "
            '(SELECT "CTE".id AS id FROM "CTE") AS no_quotes',
        ) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:14,代碼來源:test_cte.py

示例15: test_named_alias_quote

# 需要導入模塊: from sqlalchemy import sql [as 別名]
# 或者: from sqlalchemy.sql import literal [as 別名]
def test_named_alias_quote(self):
        cte = select([literal(1).label("id")]).cte(name="CTE")

        s1 = select([cte.c.id]).alias(name="Quotes Required")

        s = select([s1])
        self.assert_compile(
            s,
            'WITH "CTE" AS (SELECT :param_1 AS id) '
            'SELECT "Quotes Required".id FROM '
            '(SELECT "CTE".id AS id FROM "CTE") AS "Quotes Required"',
        ) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:14,代碼來源:test_cte.py


注:本文中的sqlalchemy.sql.literal方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。