Initial commit

This commit is contained in:
2022-04-28 04:34:45 +03:00
commit 4c0cdb2f71
30 changed files with 1218 additions and 0 deletions

70
python/sqlalchemy/core.py Normal file
View File

@@ -0,0 +1,70 @@
from sqlalchemy import MetaData
from sqlalchemy import Table, Column, Integer, String
from sqlalchemy import create_engine
from sqlalchemy import ForeignKey
# Model declarations
metadata_obj = MetaData()
user_table = Table(
"user_account",
metadata_obj,
Column('id', Integer, primary_key=True),
Column('name', String(30)),
Column('fullname', String)
)
address_table = Table(
"address",
metadata_obj,
Column('id', Integer, primary_key=True),
Column('user_id', ForeignKey('user_account.id'), nullable=False),
Column('email_address', String, nullable=False)
)
# Connection
engine = create_engine("sqlite+pysqlite:///:memory:", future=True)
# Model migration
metadata_obj.create_all(engine)
# INSERT
from sqlalchemy import insert, select
# values method
stmt = insert(user_table).values(name='spongebob', fullname="Spongebob Squarepants")
with engine.begin() as conn:
conn.execute(stmt)
# autogenerate values
with engine.connect() as conn:
conn.execute(
insert(user_table),
[
{"name": "sandy", "fullname": "Sandy Cheeks"},
{"name": "patrick", "fullname": "Patrick Star"}
]
)
conn.commit()
# insert..select
select_stmt = select(user_table.c.id, user_table.c.name + "@aol.com")
insert_stmt = insert(address_table).from_select(
["user_id", "email_address"], select_stmt
)
with engine.begin() as conn:
res = conn.execute(insert_stmt)
# SELECT
from sqlalchemy import select
stmt = select(user_table).where(user_table.c.name == 'spongebob')
with engine.connect() as conn:
for row in conn.execute(stmt):
print(row)

54
python/sqlalchemy/orm.py Normal file
View File

@@ -0,0 +1,54 @@
from sqlalchemy import Column, Integer, String
from sqlalchemy import create_engine
from sqlalchemy import ForeignKey
from sqlalchemy.orm import Session
from sqlalchemy.orm import registry
from sqlalchemy.orm import relationship
mapper_registry = registry()
Base = mapper_registry.generate_base()
# from sqlalchemy.orm import declarative_base
# Base = declarative_base()
class User(Base):
__tablename__ = 'user_account'
id = Column(Integer, primary_key=True)
name = Column(String(30))
fullname = Column(String)
addresses = relationship("Address", back_populates="user")
def __repr__(self):
return f"User(id={self.id!r}, name={self.name!r}, fullname={self.fullname!r})"
class Address(Base):
__tablename__ = 'address'
id = Column(Integer, primary_key=True)
email_address = Column(String, nullable=False)
user_id = Column(Integer, ForeignKey('user_account.id'))
user = relationship("User", back_populates="addresses")
def __repr__(self):
return f"Address(id={self.id!r}, email_address={self.email_address!r})"
engine = create_engine("sqlite+pysqlite:///:memory:", future=True)
Base.metadata.create_all(engine)
# SELECT
from sqlalchemy import select
stmt = select(User).where(User.name == 'spongebob')
with Session(engine) as conn:
for row in conn.execute(stmt):
print(row)