本文整理匯總了Python中sqlalchemy.bindparam方法的典型用法代碼示例。如果您正苦於以下問題:Python sqlalchemy.bindparam方法的具體用法?Python sqlalchemy.bindparam怎麽用?Python sqlalchemy.bindparam使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sqlalchemy
的用法示例。
在下文中一共展示了sqlalchemy.bindparam方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: cleanup_expired
# 需要導入模塊: import sqlalchemy [as 別名]
# 或者: from sqlalchemy import bindparam [as 別名]
def cleanup_expired(self, max_age_in_seconds):
cutoff = timestamp() - max_age_in_seconds * 1000
with self.db.begin() as conn:
to_delete = conn.execute(
sa.select([clusters.c.id]).where(clusters.c.stop_time < cutoff)
).fetchall()
if to_delete:
to_delete = [i for i, in to_delete]
conn.execute(
clusters.delete().where(clusters.c.id == sa.bindparam("id")),
[{"id": i} for i in to_delete],
)
for i in to_delete:
cluster = self.id_to_cluster.pop(i)
self.name_to_cluster.pop(cluster.name, None)
user_clusters = self.username_to_clusters[cluster.username]
user_clusters.pop(cluster.name)
if not user_clusters:
self.username_to_clusters.pop(cluster.username)
return len(to_delete)
示例2: get_pending_or_processing_ops
# 需要導入模塊: import sqlalchemy [as 別名]
# 或者: from sqlalchemy import bindparam [as 別名]
def get_pending_or_processing_ops(context, object_uuid, operation=None):
# NOTE (sai): For performance reasons, we expect this method to use baked
# query (http://docs.sqlalchemy.org/en/latest/orm/extensions/baked.html)
baked_query = bakery(lambda s: s.query(
models.OpenDaylightJournal))
baked_query += lambda q: q.filter(
or_(models.OpenDaylightJournal.state == odl_const.PENDING,
models.OpenDaylightJournal.state == odl_const.PROCESSING),
models.OpenDaylightJournal.object_uuid == bindparam('uuid'))
if operation:
if isinstance(operation, (list, tuple)):
baked_query += lambda q: q.filter(
models.OpenDaylightJournal.operation.in_(bindparam('op',
expanding=True)))
else:
baked_query += lambda q: q.filter(
models.OpenDaylightJournal.operation == bindparam('op'))
return baked_query(context.session).params(
uuid=object_uuid, op=operation).all()
示例3: __init__
# 需要導入模塊: import sqlalchemy [as 別名]
# 或者: from sqlalchemy import bindparam [as 別名]
def __init__(self, api, db):
super().__init__(api, db)
ins_user = sa.dialects.postgresql.insert(db.user)
ins_user_groups = sa.dialects.postgresql.insert(db.user_groups)
self.sql = {
("insert", "user"):
ins_user.on_conflict_do_update(
index_elements=[db.user.c.user_id],
set_={
"user_name": ins_user.excluded.user_name,
"user_registration": ins_user.excluded.user_registration,
"user_editcount": ins_user.excluded.user_editcount,
}),
("update", "user"):
db.user.update() \
.where(db.user.c.user_name == sa.bindparam("b_olduser")),
("insert", "user_groups"):
ins_user_groups.on_conflict_do_nothing(),
("delete", "user_groups"):
db.user_groups.delete().where(
db.user_groups.c.ug_user == sa.bindparam("b_ug_user")),
}
示例4: __init__
# 需要導入模塊: import sqlalchemy [as 別名]
# 或者: from sqlalchemy import bindparam [as 別名]
def __init__(self, api, db):
super().__init__(api, db)
self.sql = {
("delete", "user"):
db.user.delete() \
.where(db.user.c.user_id == sa.bindparam("b_oldid")),
("update", "logging"):
db.logging.update() \
.where(db.logging.c.log_user == sa.bindparam("b_oldid")),
("update", "ipb"):
db.ipblocks.update() \
.where(db.ipblocks.c.ipb_by == sa.bindparam("b_oldid")),
("update", "archive"):
db.archive.update() \
.where(db.archive.c.ar_user == sa.bindparam("b_oldid")),
("update", "revision"):
db.revision.update() \
.where(db.revision.c.rev_user == sa.bindparam("b_oldid")),
}
示例5: __init__
# 需要導入模塊: import sqlalchemy [as 別名]
# 或者: from sqlalchemy import bindparam [as 別名]
def __init__(self, api, db):
super().__init__(api, db)
ins_iw = sa.dialects.postgresql.insert(db.interwiki)
self.sql = {
("insert", "interwiki"):
db.interwiki.insert(),
("delete", "interwiki"):
db.interwiki.delete().where(db.interwiki.c.iw_prefix == sa.bindparam("b_iw_prefix")),
# iw_api is not visible in the logs so it is not updated
("update", "interwiki"):
ins_iw.on_conflict_do_update(
index_elements=[db.interwiki.c.iw_prefix],
set_={
"iw_url": ins_iw.excluded.iw_url,
"iw_local": ins_iw.excluded.iw_local,
"iw_trans": ins_iw.excluded.iw_trans,
}),
}
示例6: __init__
# 需要導入模塊: import sqlalchemy [as 別名]
# 或者: from sqlalchemy import bindparam [as 別名]
def __init__(self, api, db):
super().__init__(api, db)
ins_pt = sa.dialects.postgresql.insert(db.protected_titles)
self.sql = {
("insert", "protected_titles"):
ins_pt.on_conflict_do_update(
index_elements=[
db.protected_titles.c.pt_namespace,
db.protected_titles.c.pt_title,
],
set_={
"pt_level": ins_pt.excluded.pt_level,
"pt_expiry": ins_pt.excluded.pt_expiry,
}),
("delete", "protected_titles"):
db.protected_titles.delete().where(
(db.protected_titles.c.pt_namespace == sa.bindparam("b_pt_namespace")) &
(db.protected_titles.c.pt_title == sa.bindparam("b_pt_title"))),
}
示例7: _get_alarm_definition
# 需要導入模塊: import sqlalchemy [as 別名]
# 或者: from sqlalchemy import bindparam [as 別名]
def _get_alarm_definition(self, conn, tenant_id, _id):
ad = self.ad_s
query = (self.base_query
.select_from(self.base_query_from)
.where(ad.c.tenant_id == bindparam('b_tenant_id'))
.where(ad.c.id == bindparam('b_id'))
.where(ad.c.deleted_at == null()))
row = conn.execute(query,
b_tenant_id=tenant_id,
b_id=_id).fetchone()
if row is not None:
return dict(row)
else:
raise exceptions.DoesNotExistException
示例8: params
# 需要導入模塊: import sqlalchemy [as 別名]
# 或者: from sqlalchemy import bindparam [as 別名]
def params(self, *optionaldict, **kwargs):
"""Return a copy with :func:`_expression.bindparam` elements
replaced.
Returns a copy of this ClauseElement with
:func:`_expression.bindparam`
elements replaced with values taken from the given dictionary::
>>> clause = column('x') + bindparam('foo')
>>> print(clause.compile().params)
{'foo':None}
>>> print(clause.params({'foo':7}).compile().params)
{'foo':7}
"""
return self._replace_params(False, optionaldict, kwargs)
示例9: _replace_params
# 需要導入模塊: import sqlalchemy [as 別名]
# 或者: from sqlalchemy import bindparam [as 別名]
def _replace_params(self, unique, optionaldict, kwargs):
if len(optionaldict) == 1:
kwargs.update(optionaldict[0])
elif len(optionaldict) > 1:
raise exc.ArgumentError(
"params() takes zero or one positional dictionary argument"
)
def visit_bindparam(bind):
if bind.key in kwargs:
bind.value = kwargs[bind.key]
bind.required = False
if unique:
bind._convert_to_unique()
return cloned_traverse(self, {}, {"bindparam": visit_bindparam})
示例10: test_multi_update_rowcount
# 需要導入模塊: import sqlalchemy [as 別名]
# 或者: from sqlalchemy import bindparam [as 別名]
def test_multi_update_rowcount(self, connection):
employees_table = self.tables.employees
stmt = (
employees_table.update()
.where(employees_table.c.name == bindparam("emp_name"))
.values(department="C")
)
r = connection.execute(
stmt,
[
{"emp_name": "Bob"},
{"emp_name": "Cynthia"},
{"emp_name": "nonexistent"},
],
)
eq_(r.rowcount, 2)
示例11: test_multi_delete_rowcount
# 需要導入模塊: import sqlalchemy [as 別名]
# 或者: from sqlalchemy import bindparam [as 別名]
def test_multi_delete_rowcount(self, connection):
employees_table = self.tables.employees
stmt = employees_table.delete().where(
employees_table.c.name == bindparam("emp_name")
)
r = connection.execute(
stmt,
[
{"emp_name": "Bob"},
{"emp_name": "Cynthia"},
{"emp_name": "nonexistent"},
],
)
eq_(r.rowcount, 2)
示例12: test_exception_format_hide_parameters_nondbapi_round_trip
# 需要導入模塊: import sqlalchemy [as 別名]
# 或者: from sqlalchemy import bindparam [as 別名]
def test_exception_format_hide_parameters_nondbapi_round_trip(self):
foo = Table("foo", MetaData(), Column("data", String))
with self.no_param_engine.connect() as conn:
assert_raises_message(
tsa.exc.StatementError,
r"\(sqlalchemy.exc.InvalidRequestError\) A value is required "
r"for bind parameter 'the_data_2'\n"
r"\[SQL: SELECT foo.data \nFROM foo \nWHERE "
r"foo.data = \? OR foo.data = \?\]\n"
r"\[SQL parameters hidden due to hide_parameters=True\]",
conn.execute,
select([foo]).where(
or_(
foo.c.data == bindparam("the_data_1"),
foo.c.data == bindparam("the_data_2"),
)
),
{"the_data_1": "some data"},
)
示例13: test_no_clobs_for_string_params
# 需要導入模塊: import sqlalchemy [as 別名]
# 或者: from sqlalchemy import bindparam [as 別名]
def test_no_clobs_for_string_params(self):
"""test that simple string params get a DBAPI type of
VARCHAR, not CLOB. This is to prevent setinputsizes
from setting up cx_oracle.CLOBs on
string-based bind params [ticket:793]."""
class FakeDBAPI(object):
def __getattr__(self, attr):
return attr
dialect = oracle.OracleDialect()
dbapi = FakeDBAPI()
b = bindparam("foo", "hello world!")
eq_(b.type.dialect_impl(dialect).get_dbapi_type(dbapi), "STRING")
b = bindparam("foo", "hello world!")
eq_(b.type.dialect_impl(dialect).get_dbapi_type(dbapi), "STRING")
示例14: test_bindparam_quote
# 需要導入模塊: import sqlalchemy [as 別名]
# 或者: from sqlalchemy import bindparam [as 別名]
def test_bindparam_quote(self):
"""test that bound parameters take on quoting for reserved words,
column names quote flag enabled."""
# note: this is only in cx_oracle at the moment. not sure
# what other hypothetical oracle dialects might need
self.assert_compile(bindparam("option"), ':"option"')
self.assert_compile(bindparam("plain"), ":plain")
t = Table("s", MetaData(), Column("plain", Integer, quote=True))
self.assert_compile(
t.insert().values(plain=5),
'INSERT INTO s ("plain") VALUES (:"plain")',
)
self.assert_compile(
t.update().values(plain=5), 'UPDATE s SET "plain"=:"plain"'
)
示例15: test_update_executemany
# 需要導入模塊: import sqlalchemy [as 別名]
# 或者: from sqlalchemy import bindparam [as 別名]
def test_update_executemany(self):
with testing.db.connect() as conn:
timestamp = datetime.datetime(2015, 4, 17, 18, 5, 2)
conn.execute(
self.tables.t.insert(),
[
{"x": 5, "data": timestamp},
{"x": 6, "data": timestamp},
{"x": 7, "data": timestamp},
],
)
conn.execute(
self.tables.t.update()
.values(data=func.utc_timestamp())
.where(self.tables.t.c.x == bindparam("xval")),
[{"xval": 5}, {"xval": 6}, {"xval": 7}],
)