本文整理汇总了Python中sqlalchemy.sql.literal_column方法的典型用法代码示例。如果您正苦于以下问题:Python sql.literal_column方法的具体用法?Python sql.literal_column怎么用?Python sql.literal_column使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.sql
的用法示例。
在下文中一共展示了sql.literal_column方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_aggregate_order_by_multi_col
# 需要导入模块: from sqlalchemy import sql [as 别名]
# 或者: from sqlalchemy.sql import literal_column [as 别名]
def test_aggregate_order_by_multi_col(self):
m = MetaData()
table = Table("table1", m, Column("a", Integer), Column("b", Integer))
expr = func.string_agg(
table.c.a,
aggregate_order_by(
literal_column("','"), table.c.a, table.c.b.desc()
),
)
stmt = select([expr])
self.assert_compile(
stmt,
"SELECT string_agg(table1.a, "
"',' ORDER BY table1.a, table1.b DESC) "
"AS string_agg_1 FROM table1",
)
示例2: test_on_conflict_as_cte
# 需要导入模块: from sqlalchemy import sql [as 别名]
# 或者: from sqlalchemy.sql import literal_column [as 别名]
def test_on_conflict_as_cte(self):
i = insert(self.table1, values=dict(name="foo"))
i = (
i.on_conflict_do_update(
constraint=self.excl_constr_anon,
set_=dict(name=i.excluded.name),
where=((self.table1.c.name != i.excluded.name)),
)
.returning(literal_column("1"))
.cte("i_upsert")
)
stmt = select([i])
self.assert_compile(
stmt,
"WITH i_upsert AS "
"(INSERT INTO mytable (name) VALUES (%(name)s) "
"ON CONFLICT (name, description) "
"WHERE description != %(description_1)s "
"DO UPDATE SET name = excluded.name "
"WHERE mytable.name != excluded.name RETURNING 1) "
"SELECT i_upsert.1 "
"FROM i_upsert",
)
示例3: test_deprecated_subquery_standalone
# 需要导入模块: from sqlalchemy import sql [as 别名]
# 或者: from sqlalchemy.sql import literal_column [as 别名]
def test_deprecated_subquery_standalone(self):
from sqlalchemy import subquery
with testing.expect_deprecated(
r"The standalone subquery\(\) function is deprecated"
):
stmt = subquery(
None,
[literal_column("1").label("a")],
order_by=literal_column("1"),
)
self.assert_compile(
select([stmt]),
"SELECT anon_1.a FROM (SELECT 1 AS a ORDER BY 1) AS anon_1",
)
示例4: test_ambiguous_column_case_sensitive
# 需要导入模块: from sqlalchemy import sql [as 别名]
# 或者: from sqlalchemy.sql import literal_column [as 别名]
def test_ambiguous_column_case_sensitive(self):
with testing.expect_deprecated(
"The create_engine.case_sensitive parameter is deprecated"
):
eng = engines.testing_engine(options=dict(case_sensitive=False))
with eng.connect() as conn:
row = conn.execute(
select(
[
literal_column("1").label("SOMECOL"),
literal_column("1").label("SOMECOL"),
]
)
).first()
assert_raises_message(
exc.InvalidRequestError,
"Ambiguous column name",
lambda: row._mapping["somecol"],
)
示例5: get_timestamp_expression
# 需要导入模块: from sqlalchemy import sql [as 别名]
# 或者: from sqlalchemy.sql import literal_column [as 别名]
def get_timestamp_expression(
self, time_grain: Optional[str]
) -> Union[TimestampExpression, Label]:
"""
Return a SQLAlchemy Core element representation of self to be used in a query.
:param time_grain: Optional time grain, e.g. P1Y
:return: A TimeExpression object wrapped in a Label if supported by db
"""
label = utils.DTTM_ALIAS
db_ = self.table.database
pdf = self.python_date_format
is_epoch = pdf in ("epoch_s", "epoch_ms")
if not self.expression and not time_grain and not is_epoch:
sqla_col = column(self.column_name, type_=DateTime)
return self.table.make_sqla_column_compatible(sqla_col, label)
if self.expression:
col = literal_column(self.expression)
else:
col = column(self.column_name)
time_expr = db_.db_engine_spec.get_timestamp_expr(
col, pdf, time_grain, self.type
)
return self.table.make_sqla_column_compatible(time_expr, label)
示例6: _limit_using_window_function
# 需要导入模块: from sqlalchemy import sql [as 别名]
# 或者: from sqlalchemy.sql import literal_column [as 别名]
def _limit_using_window_function(self, query):
""" Apply a limit using a window function
This approach enables us to limit the number of eagerly loaded related entities
"""
# Only do it when there is a limit
if self.skip or self.limit:
# First, add a row counter:
query = query.add_columns(
# for every group, count the rows with row_number().
func.row_number().over(
# Groups are partitioned by self._window_over_columns,
partition_by=self._window_over_columns,
# We have to apply the same ordering from the outside query;
# otherwise, the numbering will be undetermined
order_by=self.mongoquery.handler_sort.compile_columns()
).label('group_row_n') # give it a name that we can use later
)
# Now, make ourselves into a subquery
query = query.from_self()
# Well, it turns out that subsequent joins somehow work.
# I have no idea how, but they do.
# Otherwise, we would have had to ban using 'joins' after 'limit' in nested queries.
# And apply the LIMIT condition using row numbers
# These two statements simulate skip/limit using window functions
if self.skip:
query = query.filter(literal_column('group_row_n') > self.skip)
if self.limit:
query = query.filter(literal_column('group_row_n') <= ((self.skip or 0) + self.limit))
# Done
return query
示例7: query
# 需要导入模块: from sqlalchemy import sql [as 别名]
# 或者: from sqlalchemy.sql import literal_column [as 别名]
def query():
def query(table):
col1 = literal_column("TIMESTAMP_TRUNC(timestamp, DAY)").label("timestamp_label")
col2 = func.sum(table.c.integer)
query = (
select([
col1,
col2,
])
.where(col1 < '2017-01-01 00:00:00')
.group_by(col1)
.order_by(col2)
)
return query
return query
示例8: test_render_check_constraint_sqlexpr
# 需要导入模块: from sqlalchemy import sql [as 别名]
# 或者: from sqlalchemy.sql import literal_column [as 别名]
def test_render_check_constraint_sqlexpr(self):
c = column("c")
five = literal_column("5")
ten = literal_column("10")
eq_ignore_whitespace(
autogenerate.render._render_check_constraint(
CheckConstraint(and_(c > five, c < ten)), self.autogen_context
),
"sa.CheckConstraint(!U'c > 5 AND c < 10')",
)
示例9: get_recent_network_coverage
# 需要导入模块: from sqlalchemy import sql [as 别名]
# 或者: from sqlalchemy.sql import literal_column [as 别名]
def get_recent_network_coverage(probe_cc, test_groups):
where_clause = [
sql.text("test_day >= current_date - interval '31 day'"),
sql.text("test_day < current_date"),
sql.text("probe_cc = :probe_cc"),
]
if test_groups is not None:
tg_or = []
for tg in test_groups:
try:
tg_names = TEST_GROUPS[tg]
tg_or += [
sql.literal_column("test_name") == tg_name for tg_name in tg_names
]
except KeyError:
raise BadRequest("invalid test_group")
where_clause.append(or_(*tg_or))
s = (
select([sql.text("COUNT(DISTINCT probe_asn)"), sql.text("test_day")])
.where(and_(*where_clause))
.group_by(sql.text("test_day"))
.order_by(sql.text("test_day"))
.select_from(sql.table("ooexpl_daily_msm_count"))
)
network_map = {k: 0 for k in TEST_GROUPS.keys()}
q = current_app.db_session.execute(s, {"probe_cc": probe_cc})
for count, date in q:
network_map[date.strftime("%Y-%m-%d")] = count
network_coverage = []
for test_day in last_30days():
network_coverage.append(
{"count": network_map.get(test_day, 0), "test_day": test_day}
)
return network_coverage
示例10: visit_BYTEINT
# 需要导入模块: from sqlalchemy import sql [as 别名]
# 或者: from sqlalchemy.sql import literal_column [as 别名]
def visit_BYTEINT(self, type_, **kw):
return 'BYTEINT'
#@compiles(Select, 'teradata')
#def compile_select(element, compiler, **kw):
# """
# """
#
# if not getattr(element, '_window_visit', None):
# if element._limit is not None or element._offset is not None:
# limit, offset = element._limit, element._offset
#
# orderby=compiler.process(element._order_by_clause)
# if orderby:
# element = element._generate()
# element._window_visit=True
# #element._limit = None
# #element._offset = None cant set to none...
#
# # add a ROW NUMBER() OVER(ORDER BY) column
# element = element.column(sql.literal_column('ROW NUMBER() OVER (ORDER BY %s)' % orderby).label('rownum')).order_by(None)
#
# # wrap into a subquery
# limitselect = sql.select([c for c in element.alias().c if c.key != 'rownum'])
#
# limitselect._window_visit=True
# limitselect._is_wrapper=True
#
# if offset is not None:
# limitselect.append_whereclause(sql.column('rownum') > offset)
# if limit is not None:
# limitselect.append_whereclause(sql.column('rownum') <= (limit + offset))
# else:
# limitselect.append_whereclause(sql.column("rownum") <= limit)
#
# element = limitselect
#
# kw['iswrapper'] = getattr(element, '_is_wrapper', False)
# return compiler.visit_select(element, **kw)
示例11: test_aggregate_order_by_two
# 需要导入模块: from sqlalchemy import sql [as 别名]
# 或者: from sqlalchemy.sql import literal_column [as 别名]
def test_aggregate_order_by_two(self):
m = MetaData()
table = Table("table1", m, Column("a", Integer), Column("b", Integer))
expr = func.string_agg(
table.c.a, aggregate_order_by(literal_column("','"), table.c.a)
)
stmt = select([expr])
self.assert_compile(
stmt,
"SELECT string_agg(table1.a, ',' ORDER BY table1.a) "
"AS string_agg_1 FROM table1",
)
示例12: test_aggregate_orcer_by_no_arg
# 需要导入模块: from sqlalchemy import sql [as 别名]
# 或者: from sqlalchemy.sql import literal_column [as 别名]
def test_aggregate_orcer_by_no_arg(self):
assert_raises_message(
TypeError,
"at least one ORDER BY element is required",
aggregate_order_by,
literal_column("','"),
)
示例13: test_literal_column_label_embedded_select_samename_explcit_quote
# 需要导入模块: from sqlalchemy import sql [as 别名]
# 或者: from sqlalchemy.sql import literal_column [as 别名]
def test_literal_column_label_embedded_select_samename_explcit_quote(self):
col = sql.literal_column("NEEDS QUOTES").label(
quoted_name("NEEDS QUOTES", True)
)
with testing.expect_deprecated(
r"The SelectBase.select\(\) method is deprecated"
):
self.assert_compile(
select([col]).select(),
'SELECT anon_1."NEEDS QUOTES" FROM '
'(SELECT NEEDS QUOTES AS "NEEDS QUOTES") AS anon_1',
)
示例14: test_literal_column_label_embedded_select_diffname_explcit_quote
# 需要导入模块: from sqlalchemy import sql [as 别名]
# 或者: from sqlalchemy.sql import literal_column [as 别名]
def test_literal_column_label_embedded_select_diffname_explcit_quote(self):
col = sql.literal_column("NEEDS QUOTES").label(
quoted_name("NEEDS QUOTES_", True)
)
with testing.expect_deprecated(
r"The SelectBase.select\(\) method is deprecated"
):
self.assert_compile(
select([col]).select(),
'SELECT anon_1."NEEDS QUOTES_" FROM '
'(SELECT NEEDS QUOTES AS "NEEDS QUOTES_") AS anon_1',
)
示例15: test_literal_column_label_embedded_select_diffname
# 需要导入模块: from sqlalchemy import sql [as 别名]
# 或者: from sqlalchemy.sql import literal_column [as 别名]
def test_literal_column_label_embedded_select_diffname(self):
col = sql.literal_column("NEEDS QUOTES").label("NEEDS QUOTES_")
with testing.expect_deprecated(
r"The SelectBase.select\(\) method is deprecated"
):
self.assert_compile(
select([col]).select(),
'SELECT anon_1."NEEDS QUOTES_" FROM (SELECT NEEDS QUOTES AS '
'"NEEDS QUOTES_") AS anon_1',
)