本文整理汇总了Python中sqlalchemy.engine.Connection方法的典型用法代码示例。如果您正苦于以下问题:Python engine.Connection方法的具体用法?Python engine.Connection怎么用?Python engine.Connection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.engine
的用法示例。
在下文中一共展示了engine.Connection方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: bind
# 需要导入模块: from sqlalchemy import engine [as 别名]
# 或者: from sqlalchemy.engine import Connection [as 别名]
def bind(self):
"""Return the current "bind".
In online mode, this is an instance of
:class:`sqlalchemy.engine.Connection`, and is suitable
for ad-hoc execution of any kind of usage described
in :ref:`sqlexpression_toplevel` as well as
for usage with the :meth:`sqlalchemy.schema.Table.create`
and :meth:`sqlalchemy.schema.MetaData.create_all` methods
of :class:`~sqlalchemy.schema.Table`,
:class:`~sqlalchemy.schema.MetaData`.
Note that when "standard output" mode is enabled,
this bind will be a "mock" connection handler that cannot
return results and is only appropriate for a very limited
subset of commands.
"""
return self.connection
示例2: auction_model_with_a_bid
# 需要导入模块: from sqlalchemy import engine [as 别名]
# 或者: from sqlalchemy.engine import Connection [as 别名]
def auction_model_with_a_bid(
connection: Connection, winning_bid_amount: Decimal, bidder_id: int, ends_at: datetime
) -> RowProxy:
connection.execute(
auctions.insert().values(
{
"id": 1,
"title": "Cool socks",
"starting_price": winning_bid_amount / 2,
"current_price": winning_bid_amount,
"ends_at": ends_at,
"ended": False,
}
)
)
connection.execute(bids.insert().values({"amount": winning_bid_amount, "auction_id": 1, "bidder_id": bidder_id}))
return connection.execute(auctions.select(whereclause=auctions.c.id == 1)).first()
示例3: test_removes_withdrawn_bids
# 需要导入模块: from sqlalchemy import engine [as 别名]
# 或者: from sqlalchemy.engine import Connection [as 别名]
def test_removes_withdrawn_bids(
connection: Connection, bid_model: RowProxy, auction_model_with_a_bid: dict, ends_at: datetime, event_bus_mock: Mock
) -> None:
auction = Auction(
id=auction_model_with_a_bid.id,
title=auction_model_with_a_bid.title,
starting_price=get_dollars(auction_model_with_a_bid.starting_price),
ends_at=ends_at,
bids=[Bid(bid_model.id, bid_model.bidder_id, get_dollars(bid_model.amount))],
ended=False,
)
auction.withdraw_bids([bid_model.id])
SqlAlchemyAuctionsRepo(connection, event_bus_mock).save(auction)
assert connection.execute(select([func.count()]).select_from(bids)).scalar() == 0
示例4: test_AuctionsRepo_UponSavingAuction_ClearsPendingEvents
# 需要导入模块: from sqlalchemy import engine [as 别名]
# 或者: from sqlalchemy.engine import Connection [as 别名]
def test_AuctionsRepo_UponSavingAuction_ClearsPendingEvents(
connection: Connection, another_bidder_id: int, auction_model_with_a_bid: RowProxy, event_bus_mock: Mock
) -> None:
repo = SqlAlchemyAuctionsRepo(connection, event_bus_mock)
auction = repo.get(auction_model_with_a_bid.id)
auction.place_bid(another_bidder_id, auction.current_price + get_dollars("1.00"))
repo.save(auction)
assert len(auction.domain_events) == 0
# @pytest.mark.usefixtures("transaction")
# def test_AuctionsRepo_UponSavingAuction_PostsPendingEventsViaEventBus(
# connection: Connection, another_bidder_id: int, auction_model_with_a_bid: RowProxy, event_bus_stub: EventBusStub
# ) -> None:
# repo = SqlAlchemyAuctionsRepo(connection, event_bus_stub)
# auction = repo.get(auction_model_with_a_bid.id)
# auction.place_bid(another_bidder_id, auction.current_price + get_dollars("1.00"))
#
# repo.save(auction)
#
# event_bus_stub.events
示例5: test_create_drop_constructor_bound
# 需要导入模块: from sqlalchemy import engine [as 别名]
# 或者: from sqlalchemy.engine import Connection [as 别名]
def test_create_drop_constructor_bound(self):
for bind in (testing.db, testing.db.connect()):
try:
for args in (([bind], {}), ([], {"bind": bind})):
metadata = MetaData(*args[0], **args[1])
table = Table(
"test_table", metadata, Column("foo", Integer)
)
assert metadata.bind is table.bind is bind
metadata.create_all()
is_true(inspect(bind).has_table(table.name))
metadata.drop_all()
table.create()
table.drop()
is_false(inspect(bind).has_table(table.name))
finally:
if isinstance(bind, engine.Connection):
bind.close()
示例6: db_connection
# 需要导入模块: from sqlalchemy import engine [as 别名]
# 或者: from sqlalchemy.engine import Connection [as 别名]
def db_connection(module_scoped_container_getter) -> Connection:
retries = 60
while True:
try:
connection = get_connection()
try:
yield connection
finally:
if not connection.closed:
connection.close()
break
except Exception as e:
print(str(e))
if retries > 0:
retries -= 1
time.sleep(1)
continue
raise
示例7: create_sa_connection
# 需要导入模块: from sqlalchemy import engine [as 别名]
# 或者: from sqlalchemy.engine import Connection [as 别名]
def create_sa_connection(con, **kwargs):
import sqlalchemy as sa
from sqlalchemy.engine import Connection, Engine
# process con
engine = None
if isinstance(con, Connection):
# connection create by user
close = False
dispose = False
elif isinstance(con, Engine):
con = con.connect()
close = True
dispose = False
else:
engine = sa.create_engine(con, **kwargs)
con = engine.connect()
close = True
dispose = True
try:
yield con
finally:
if close:
con.close()
if dispose:
engine.dispose()
示例8: start_new_payment
# 需要导入模块: from sqlalchemy import engine [as 别名]
# 或者: from sqlalchemy.engine import Connection [as 别名]
def start_new_payment(payment_uuid: UUID, customer_id: int, amount: Money, description: str, conn: Connection) -> None:
conn.execute(
payments.insert(
{
"uuid": str(payment_uuid),
"customer_id": customer_id,
"amount": int(amount.amount) * 100,
"currency": amount.currency.iso_code,
"description": description,
"status": PaymentStatus.NEW.value,
}
)
)
示例9: get_pending_payments
# 需要导入模块: from sqlalchemy import engine [as 别名]
# 或者: from sqlalchemy.engine import Connection [as 别名]
def get_pending_payments(customer_id: int, conn: Connection) -> List[PaymentDto]:
rows = conn.execute(
payments.select((payments.c.customer_id == customer_id) & (payments.c.status == PaymentStatus.NEW.value))
).fetchall()
return [PaymentDto.from_row(row) for row in rows]
示例10: get_payment
# 需要导入模块: from sqlalchemy import engine [as 别名]
# 或者: from sqlalchemy.engine import Connection [as 别名]
def get_payment(payment_uuid: UUID, customer_id: int, conn: Connection) -> PaymentDto:
row = conn.execute(
payments.select((payments.c.customer_id == customer_id) & (payments.c.uuid == str(payment_uuid)))
).first()
return PaymentDto.from_row(row)
示例11: get_payment_charge_id
# 需要导入模块: from sqlalchemy import engine [as 别名]
# 或者: from sqlalchemy.engine import Connection [as 别名]
def get_payment_charge_id(payment_uuid: UUID, customer_id: int, conn: Connection) -> Optional[str]:
row = conn.execute(
payments.select((payments.c.customer_id == customer_id) & (payments.c.uuid == str(payment_uuid)))
).first()
return str(row.charge_id) if row.charge_id else None
示例12: update_payment
# 需要导入模块: from sqlalchemy import engine [as 别名]
# 或者: from sqlalchemy.engine import Connection [as 别名]
def update_payment(payment_uuid: UUID, customer_id: int, values: dict, conn: Connection) -> None:
conn.execute(
payments.update()
.where((payments.c.uuid == str(payment_uuid)) & (payments.c.customer_id == customer_id))
.values(**values)
)
示例13: facade
# 需要导入模块: from sqlalchemy import engine [as 别名]
# 或者: from sqlalchemy.engine import Connection [as 别名]
def facade(connection: Connection, event_bus: Mock) -> PaymentsFacade:
return PaymentsFacade(PaymentsConfig("", ""), connection, event_bus)
示例14: inserted_payment
# 需要导入模块: from sqlalchemy import engine [as 别名]
# 或者: from sqlalchemy.engine import Connection [as 别名]
def inserted_payment(request: SubRequest, connection: Connection) -> dict:
status = getattr(request, "param", None) or PaymentStatus.NEW.value
charge_id = None if status not in (PaymentStatus.CHARGED.value, PaymentStatus.CAPTURED.value) else "token"
data: Dict[str, Any] = {
"uuid": str(uuid.uuid4()),
"customer_id": 1,
"amount": 100,
"currency": "USD",
"status": status,
"description": "irrelevant",
"charge_id": charge_id,
}
connection.execute(payments.insert(data))
return data
示例15: get_payment
# 需要导入模块: from sqlalchemy import engine [as 别名]
# 或者: from sqlalchemy.engine import Connection [as 别名]
def get_payment(connection: Connection, payment_uuid: str) -> RowProxy:
row = connection.execute(payments.select(payments.c.uuid == payment_uuid)).first()
return row