Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. """ "Writing SQL is just as fast as using an ORM" proof of concept
  2.  
  3. Below is a simple Python object model, where we represent a database that
  4. stores the names of employees at a company, some of whom are "engineers",
  5. and a list of the jobs they do and the programming languages they use.
  6.  
  7. We'd like to persist the state represented by this Python object model
  8. in a relational database, using our Python objects as a start. Then we'd
  9. like to write SQL queries for rows in this database, and we get back instances
  10. of Python objects exactly as they were created.
  11.  
  12. This is of course the job that an ORM does. We'd like to show that ORMs
  13. are unnecessary if one knows SQL, that the work is just as fast and easy
  14. writing raw SQL as long as one knows SQL.
  15.  
  16. The assigment is as follows:
  17.  
  18. 1. create a schema to store the Python object model below relationally.
  19.  
  20. a. There should be six tables: "employee", "engineer", "employee_job",
  21. "job", "engineer_language", and "language".
  22. b. An object of type Engineer should have a row in the
  23. "employee" and "engineer" tables, which are associated via foreign
  24. key.
  25. c. "employee_job" has foreign key constraints referring to "employee"
  26. and "job".
  27. d. "engineer_language" has foreign key constraints referring to
  28. "engineer" and "language"
  29.  
  30. 2. store the "emp1" and "emp2" object structures in the database.
  31. Primary keys should use Postgresql SERIAL columns using an auto-incrementing
  32. integer that comes from the sequence implicitly associated with a SERIAL
  33. column. It's likely best to use RETURNING with INSERT statements to get
  34. these back.
  35.  
  36. 3. run this SQL query, or one like it, and get back "emp1" and "emp2" as Python
  37. objects:
  38.  
  39. SELECT emp.id, emp.name FROM employee
  40.  
  41. 4. The "engineer" table will also need to be queried in some way (either
  42. part of the first query, or as some additional query) to determine which row is
  43. an "Engineer" and which is an "Employee"; emp1 should come back as an Engineer
  44. object instance and emp2 should come back as an Employee object instance.
  45. The Python objects returned should also have the appropriate Job and
  46. Language collections present on them as well, which would require additional
  47. SQL to be emitted (again, either as joins or separate statements, whatever.
  48. With SQLAlchemy, these different kinds of schemes can be selected based
  49. on simple configuration settings.).
  50.  
  51. 5. Things that aren't part of the challenge: setting up transaction blocks, rolling
  52. back when errors occur, connection pooling, dealing with tricky datatypes,
  53. ensuring in-memory uniqueness of objects (after all if two parts of my program
  54. update ProgrammingLanguage in the same transaction do they conflict?), optimistic
  55. object versioning, all things that are also not "writing SQL" that the ORM does,
  56. we'll leave those out.
  57.  
  58. There's a psycopg2 Connection object near the end of this program.
  59.  
  60. Are you ready to show how easy writing raw SQL is and how there's nothing
  61. else you need you need when programming real applications? Great! Let's go!
  62.  
  63. """
  64.  
  65.  
  66. class Employee(object):
  67. def __init__(self, name, jobs):
  68. self.name = name
  69. self.jobs = jobs
  70.  
  71.  
  72. class Engineer(Employee):
  73. def __init__(self, name, jobs, languages):
  74. super(Engineer, self).__init__(name, jobs)
  75. self.languages = languages
  76.  
  77.  
  78. class Job(object):
  79. def __init__(self, name):
  80. self.name = name
  81.  
  82.  
  83. class ProgrammingLanguage(object):
  84. def __init__(self, name):
  85. self.name = name
  86.  
  87.  
  88. j1, j2 = Job("programming"), Job("meeting")
  89. python, java = ProgrammingLanguage("python"), ProgrammingLanguage("java")
  90. emp1 = Engineer(name="some engineer", languages=[python, java], jobs=[j1, j2])
  91. emp2 = Employee(name="some employee", jobs=[j2])
  92.  
  93.  
  94. # here's a start:
  95. import psycopg2
  96.  
  97. connection = psycopg2.connect(
  98. user="scott", password="tiger", host="localhost", database="test"
  99. )
  100.  
  101. # go! create the schema, store the data, load it back! efficiently!
  102.  
  103.  
  104. # lets read the data now
  105. for emp in employees:
  106. print(emp.name)
  107. print([j.name for j in emp.jobs])
  108. if isinstance(emp, Engineer):
  109. print([l.name for l in emp.languages])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement