Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
ORM in Class
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SOFT Core
SOFT 161 and 162
ORM in Class
Commits
fcffd468
Commit
fcffd468
authored
4 years ago
by
Brady James Garvin
Browse files
Options
Downloads
Patches
Plain Diff
Initial commit.
parents
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
.gitignore
+5
-0
5 additions, 0 deletions
.gitignore
diner.py
+60
-0
60 additions, 0 deletions
diner.py
diner_installer.py
+40
-0
40 additions, 0 deletions
diner_installer.py
with
105 additions
and
0 deletions
.gitignore
0 → 100644
+
5
−
0
View file @
fcffd468
*~
*.pyc
*.pyo
.idea
This diff is collapsed.
Click to expand it.
diner.py
0 → 100644
+
60
−
0
View file @
fcffd468
from
sqlalchemy
import
create_engine
,
Column
,
Integer
,
String
,
DateTime
,
ForeignKey
from
sqlalchemy.ext.declarative
import
declarative_base
from
sqlalchemy.orm
import
sessionmaker
,
relationship
# In Python, unlike many languages, it is possible to create new classes at
# runtime. Here we call declarative_base, which creates and returns a base
# class that we can use for objects that we want SQLAlchemy to persist in a
# database. We then store that class in variable named Persisted; effectively,
# we make a new class called Persisted.
Persisted
=
declarative_base
()
# pylint: disable=invalid-name
class
Menu
(
Persisted
):
__tablename__
=
'
menus
'
menu_id
=
Column
(
Integer
,
primary_key
=
True
)
# primary keys default to auto_increment
name
=
Column
(
String
(
256
),
nullable
=
False
)
# nullable is the default
class
Item
(
Persisted
):
__tablename__
=
'
items
'
item_id
=
Column
(
Integer
,
primary_key
=
True
)
# a foreign-key constraint restricts the allowable integers; cascade ties an item's lifetime to its menu
menu_id
=
Column
(
Integer
,
ForeignKey
(
'
menus.menu_id
'
,
ondelete
=
'
CASCADE
'
),
nullable
=
False
)
name
=
Column
(
String
(
256
),
nullable
=
False
)
price
=
Column
(
Integer
,
nullable
=
False
)
class
Order
(
Persisted
):
__tablename__
=
'
orders
'
order_id
=
Column
(
Integer
,
primary_key
=
True
)
timestamp
=
Column
(
DateTime
)
class
OrderItem
(
Persisted
):
__tablename__
=
'
order_items
'
order_id
=
Column
(
Integer
,
ForeignKey
(
'
orders.order_id
'
,
ondelete
=
'
CASCADE
'
),
primary_key
=
True
)
item_id
=
Column
(
Integer
,
ForeignKey
(
'
items.item_id
'
,
ondelete
=
'
CASCADE
'
),
primary_key
=
True
)
amount
=
Column
(
Integer
)
class
DinerDatabase
(
object
):
@staticmethod
def
construct_mysql_url
(
authority
,
port
,
database
,
username
,
password
):
return
f
'
mysql+mysqlconnector://
{
username
}
:
{
password
}
@
{
authority
}
:
{
port
}
/
{
database
}
'
@staticmethod
def
construct_in_memory_url
():
return
'
sqlite:///
'
def
__init__
(
self
,
url
):
self
.
engine
=
create_engine
(
url
)
# an engine is like an endpoint, something to connect to
self
.
Session
=
sessionmaker
()
# create a class for connections to that endpoint / pylint: disable=invalid-name
self
.
Session
.
configure
(
bind
=
self
.
engine
)
# associate the class with the endpoint
def
ensure_tables_exist
(
self
):
Persisted
.
metadata
.
create_all
(
self
.
engine
)
# create tables for all subclasses of Persisted
def
create_session
(
self
):
return
self
.
Session
()
# create a new session, which is like a connection to the database
This diff is collapsed.
Click to expand it.
diner_installer.py
0 → 100644
+
40
−
0
View file @
fcffd468
from
sys
import
stderr
from
sqlalchemy.exc
import
SQLAlchemyError
from
diner
import
DinerDatabase
,
Menu
,
Item
,
Order
,
OrderItem
def
add_starter_data
(
session
):
beverage_menu
=
Menu
(
name
=
'
Beverage
'
)
food_menu
=
Menu
(
name
=
'
Food
'
)
orange_juice
=
Item
(
menu
=
beverage_menu
,
name
=
'
Orange Juice
'
,
price
=
149
)
short_stack
=
Item
(
menu
=
food_menu
,
name
=
'
Pancakes
'
,
price
=
399
)
regular_pancakes
=
Item
(
menu
=
food_menu
,
name
=
'
Pancakes
'
,
price
=
499
)
first_order
=
Order
(
order_items
=
[
OrderItem
(
item
=
orange_juice
,
amount
=
2
)])
session
.
add
(
beverage_menu
)
session
.
add
(
food_menu
)
session
.
add
(
orange_juice
)
session
.
add
(
short_stack
)
session
.
add
(
regular_pancakes
)
session
.
add
(
first_order
)
def
main
():
try
:
url
=
DinerDatabase
.
construct_mysql_url
(
'
localhost
'
,
3306
,
'
diner
'
,
'
root
'
,
'
cse1208
'
)
diner_database
=
DinerDatabase
(
url
)
diner_database
.
ensure_tables_exist
()
print
(
'
Tables created.
'
)
# session = diner_database.create_session()
# add_starter_data(session)
# session.commit()
# print('Records created.')
except
SQLAlchemyError
as
exception
:
print
(
'
Database setup failed!
'
,
file
=
stderr
)
print
(
f
'
Cause:
{
exception
}
'
,
file
=
stderr
)
exit
(
1
)
if
__name__
==
'
__main__
'
:
main
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment