mirror of
https://github.com/mariadb-corporation/dev-example-blog-samples.git
synced 2025-08-06 09:58:00 +00:00
Added part 2 content
This commit is contained in:
8
mariadb_python_sqlalchemy/part_2/onboarding/base.py
Normal file
8
mariadb_python_sqlalchemy/part_2/onboarding/base.py
Normal file
@ -0,0 +1,8 @@
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
engine = create_engine("mariadb+mariadbconnector://app_user:Password123!@127.0.0.1:3306/planning")
|
||||
Session = sessionmaker(bind=engine)
|
||||
|
||||
Base = declarative_base()
|
@ -0,0 +1,18 @@
|
||||
from sqlalchemy import Column, String, Integer, ForeignKey
|
||||
from sqlalchemy.orm import relationship, backref
|
||||
|
||||
from base import Base
|
||||
|
||||
class ContactDetails(Base):
|
||||
__tablename__ = 'contact_details'
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
phone_number = Column(String(length=12))
|
||||
address = Column(String(length=100))
|
||||
employee_id = Column(Integer, ForeignKey('employees.id'))
|
||||
employee = relationship("Employee", backref=backref("contact_details", uselist=False))
|
||||
|
||||
def __init__(self, phone_number, address, employee):
|
||||
self.phone_number = phone_number
|
||||
self.address = address
|
||||
self.employee = employee
|
14
mariadb_python_sqlalchemy/part_2/onboarding/department.py
Normal file
14
mariadb_python_sqlalchemy/part_2/onboarding/department.py
Normal file
@ -0,0 +1,14 @@
|
||||
from sqlalchemy import Column, String, Integer, ForeignKey
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from base import Base
|
||||
|
||||
class Department(Base):
|
||||
__tablename__ = 'departments'
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
name = Column(String(length=50))
|
||||
employees = relationship("Employee")
|
||||
|
||||
def __init__(self, name):
|
||||
self.name = name
|
21
mariadb_python_sqlalchemy/part_2/onboarding/employee.py
Normal file
21
mariadb_python_sqlalchemy/part_2/onboarding/employee.py
Normal file
@ -0,0 +1,21 @@
|
||||
from sqlalchemy import Column, String, Integer, Boolean, ForeignKey
|
||||
from sqlalchemy.orm import relationship, backref
|
||||
|
||||
from base import Base
|
||||
|
||||
class Employee(Base):
|
||||
__tablename__ = 'employees'
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
department_id = Column(Integer, ForeignKey('departments.id'))
|
||||
firstname = Column(String(length=100))
|
||||
lastname = Column(String(length=100))
|
||||
active = Column(Boolean, default=True)
|
||||
department = relationship("Department", back_populates="employees")
|
||||
|
||||
#contact_details = relationship("ContactDetails", backref=backref("contact_details", uselist=False))
|
||||
|
||||
def __init__(self, firstname, lastname, department):
|
||||
self.firstname = firstname
|
||||
self.lastname = lastname
|
||||
self.department = department
|
59
mariadb_python_sqlalchemy/part_2/onboarding/load.py
Normal file
59
mariadb_python_sqlalchemy/part_2/onboarding/load.py
Normal file
@ -0,0 +1,59 @@
|
||||
from datetime import date
|
||||
|
||||
from base import Session, engine, Base
|
||||
from employee import Employee
|
||||
from project import Project
|
||||
from department import Department
|
||||
from contact_details import ContactDetails
|
||||
|
||||
# Generate database schema
|
||||
Base.metadata.create_all(engine)
|
||||
|
||||
# Create a new session
|
||||
session = Session()
|
||||
|
||||
# Create projects
|
||||
project_1 = Project("Project 1", "Project 1 description", date(2021, 5, 31))
|
||||
project_2 = Project("Project 2", "Project 2 description", date(2021, 4, 30))
|
||||
project_3 = Project("Project 3", "Project 3 description", date(2021, 6, 15))
|
||||
|
||||
# Create departments
|
||||
dept_marketing = Department("Marketing")
|
||||
dept_engineering = Department("Engineering")
|
||||
|
||||
# Create employees
|
||||
emp_john = Employee("John", "Locke", dept_marketing)
|
||||
emp_kate = Employee("Kate", "Austin", dept_engineering)
|
||||
emp_jack = Employee("Jack", "Shepherd", dept_marketing)
|
||||
emp_ben = Employee("Ben", "Linus", dept_marketing)
|
||||
emp_sun = Employee("Sun", "Kwan", dept_engineering)
|
||||
|
||||
# Add employees to projects
|
||||
project_1.employees = [emp_john,emp_kate]
|
||||
project_2.employees = [emp_jack,emp_ben,emp_sun]
|
||||
project_3.employees = [emp_john,emp_kate,emp_jack,emp_ben,emp_sun]
|
||||
|
||||
# Create contact details
|
||||
cd_john = ContactDetails("417 315 2531", "123 S Main ST", emp_john)
|
||||
cd_kate = ContactDetails("417 315 2533", "124 S Main ST", emp_kate)
|
||||
cd_jack = ContactDetails("417 315 2534", "125 S Main ST", emp_jack)
|
||||
cd_ben = ContactDetails("417 315 2535", "126 S Main ST", emp_ben)
|
||||
cd_sun = ContactDetails("417 315 2536", "127 S Main ST", emp_sun)
|
||||
|
||||
|
||||
# Persist data
|
||||
session.add(project_1)
|
||||
session.add(project_2)
|
||||
session.add(project_3)
|
||||
|
||||
session.add(dept_marketing)
|
||||
session.add(dept_engineering)
|
||||
|
||||
session.add(cd_john)
|
||||
session.add(cd_kate)
|
||||
session.add(cd_jack)
|
||||
session.add(cd_ben)
|
||||
session.add(cd_sun)
|
||||
|
||||
session.commit()
|
||||
session.close()
|
24
mariadb_python_sqlalchemy/part_2/onboarding/project.py
Normal file
24
mariadb_python_sqlalchemy/part_2/onboarding/project.py
Normal file
@ -0,0 +1,24 @@
|
||||
from sqlalchemy import Column, String, Integer, Date, Table, ForeignKey
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from base import Base
|
||||
|
||||
projects_employees_association = Table(
|
||||
'projects_employees', Base.metadata,
|
||||
Column('project_id', Integer, ForeignKey('projects.id')),
|
||||
Column('employee_id', Integer, ForeignKey('employees.id'))
|
||||
)
|
||||
|
||||
class Project(Base):
|
||||
__tablename__ = 'projects'
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
name = Column(String(length=100))
|
||||
description = Column(String(length=500))
|
||||
due_date = Column(Date)
|
||||
employees = relationship("Employee", secondary=projects_employees_association)
|
||||
|
||||
def __init__(self, name, description, due_date):
|
||||
self.name = name
|
||||
self.description = description
|
||||
self.due_date = due_date
|
36
mariadb_python_sqlalchemy/part_2/onboarding/query.py
Normal file
36
mariadb_python_sqlalchemy/part_2/onboarding/query.py
Normal file
@ -0,0 +1,36 @@
|
||||
from base import Session
|
||||
from project import Project
|
||||
from employee import Employee
|
||||
from department import Department
|
||||
|
||||
session = Session()
|
||||
|
||||
# Get all projects
|
||||
projects = session.query(Project).all()
|
||||
|
||||
print('### Projects ###')
|
||||
for project in projects:
|
||||
print(project.name)
|
||||
for employee in project.employees:
|
||||
print(f' - {employee.firstname} {employee.lastname} ({employee.department.name})')
|
||||
|
||||
|
||||
# Get all departments
|
||||
departments = session.query(Department).all()
|
||||
|
||||
print('### Departments ###')
|
||||
for department in departments:
|
||||
print(department.name)
|
||||
for employee in department.employees:
|
||||
print(f' - {employee.firstname} {employee.lastname}')
|
||||
|
||||
# John Lock projects
|
||||
john_lock_projects = session.query(Project) \
|
||||
.join(Employee, Project.employees) \
|
||||
.filter(Employee.firstname == 'John') \
|
||||
.all()
|
||||
|
||||
print('### John Locke projects ###')
|
||||
|
||||
for project in john_lock_projects:
|
||||
print(f'- {project.name}')
|
Reference in New Issue
Block a user