BoostryJP / ibet-Wallet-API

A powerful API suite for seamlessly building ibet blockchain wallet systems 🛠
https://ibet.jp/ibet-for-fin
Apache License 2.0
9 stars 0 forks source link

Change to SQLAlchemy 2.0 Style #1401

Closed purplesmoke05 closed 1 year ago

purplesmoke05 commented 1 year ago

Close: #1391

purplesmoke05 commented 1 year ago

1. single model & execute().all()

statement = select(User).where(name="test")
result = session.execute(statement).all()

print(result)
# [(User(name="test"), )]
# returned tupled value of select clause

2. single model & execute().scalars().all()

statement = select(User).where(name="test")
result = session.execute(statement).scalars().all()
# same as "result = session.scalars(statement).all()"

print(result)
# [User(name="test")]
# returned a value of first entity in select clause

3. multiple model & execute().all()

statement = select(User, Address).join(Address, User.name == Address.name)
result = session.execute(statement).all()

print(result)
# [(User(name="test"), Address)]
# returned tupled value of select clause

4. multiple model & execute().scalars().all()

statement = select(User, Address).join(Address, User.name == Address.name)
result = session.execute(statement).scalars().all()
# same as "result = session.scalars(statement).all()"

print(result)
# [User(name="test")]
# returned a value of first entity in select clause
# Address is missing

5. execute(.limit(1)).first()

statement = select(User).where(name="test")
result = session.execute(statement.limit(1)).first()

print(result)
# (User(name="test"), )

6. execute(.limit(1)).scalars().first()

statement = select(User).where(name="test")
result = session.execute(statement.limit(1)).scalars().first()
# same as "result = session.scalars(statement.limit(1)).first()"

print(result)
# User(name="test")
purplesmoke05 commented 1 year ago

7. Count

stmt = select(User).where(name="test")

7-1. Count execute().all()

result = session.execute(select(func.count()).select_from(stmt)).all()
print(result)
# [(3,), ]

7-2. Count execute().first()

result = session.execute(select(func.count()).select_from(stmt)).first()
print(result)
# (3,)

7-3. Count execute().scalars().all()

statement = select(User).where(name="test")
result = session.execute(select(func.count()).select_from(stmt)).scalars().all()
print(result)
# [3]

7-4. Count execute().scalars().first()

result = session.execute(select(func.count()).select_from(stmt)).scalars().first()
print(result)
# 3

7-5. Count execute().scalar()

result = session.execute(select(func.count()).select_from(stmt)).scalar()
print(result)
# 3