Practical Session 13 Persistence Layer.

Slides:



Advertisements
Similar presentations
A Guide to SQL, Seventh Edition. Objectives Create a new table from an existing table Change data using the UPDATE command Add new data using the INSERT.
Advertisements

A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
A Guide to SQL, Seventh Edition. Objectives Embed SQL commands in PL/SQL programs Retrieve single rows using embedded SQL Update a table using embedded.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Data Access Patterns. Motivation Most software systems require persistent data (i.e. data that persists between program executions). In general, distributing.
Entity Framework Code First End to End
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
Data Access Patterns Some of the problems with data access from OO programs: 1.Data source and OO program use different data modelling concepts 2.Decoupling.
NHibernate in Action Web Seminar at UMLChina By Pierre Henri Kuaté 2008/08/27
Virtual Private Databases Dr. Gabriel. 2 Overview of Virtual Private Databases A VPD deals with data access VPD controls data access at the row or column.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
Object Persistence Design Chapter 13. Key Definitions Object persistence involves the selection of a storage format and optimization for performance.
CSCI 6962: Server-side Design and Programming Database Manipulation in ASP.
JDBC Java and Databases. RHS – SOC 2 JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/SQL.
Phonegap Bridge – File System CIS 136 Building Mobile Apps 1.
 It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data.
THE WEBMASTERS: SENG + WAVERING.  On account of construction, we will be having class in room 1248 next week.
Relational Databases: Basic Concepts BCHB Lecture 21 By Edwards & Li Slides:
Oracle11g: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
Task #1 Create a relational database on computers in computer classroom 308, using MySQL server and any client. Create the same database, using MS Access.
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
The Data Access Object Pattern (Structural – Not a GoF Pattern) ©SoftMoore ConsultingSlide 1.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
JDBC Java and Databases. SWC – JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
Introduction to ORM Hibernate Hibernate vs JDBC. May 12, 2011 INTRODUCTION TO ORM ORM is a programming technique for converting data between relational.
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
By: Eliav Menachi.  On Android, all application data (including files) are private to that application  Android provides a standard way for an application.
SQL CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Introduction to Database Programming with Python Gary Stewart
COMP 430 Intro. to Database Systems SQL from application code.
Don't Know Jack About Object-Relational Mapping?
ASP.NET Programming with C# and SQL Server First Edition
CompSci 280 S Introduction to Software Development
Database Access with SQL
Databases.
Rob Gleasure robgleasure.com
A Guide to SQL, Seventh Edition
INLS 623– Database Systems II– File Structures, Indexing, and Hashing
Indexes By Adrienne Watt.
Chapter 6 - Database Implementation and Use
Insert, Update and the rest…
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
© 2016, Mike Murach & Associates, Inc.
SQL MODELER - OPEN There are Three Ways to open the SQL Modeler
Session 4 PHP & MySQL.
SQL – Application Persistence Design Patterns
Persistence Database Management Systems
MS Access Database Connection
Chapter 8 Working with Databases and MySQL
Introduction To Structured Query Language (SQL)
Developing a Model-View-Controller Component for Joomla Part 3
Rob Gleasure robgleasure.com
Data Management Innovations 2017 High level overview of DB
SQL Application Persistence Design Patterns
Adding Multiple Logical Table Sources
Introduction To Structured Query Language (SQL)
Creating and Managing Database Tables
Tutorial 6 PHP & MySQL Li Xu
Chapter 8 Advanced SQL.
Rob Gleasure robgleasure.com
SQL – Application Persistence Design Patterns
M S COLLEGE OF ART’S, COMM., SCI. & BMS Advance Web Programming
Trainer: Bach Ngoc Toan– TEDU Website:
SPL – PS13 Persistence Layer.
Presentation transcript:

Practical Session 13 Persistence Layer

Persistence Layer Design pattern for managing storing and accessing of permanent data Manages communication between the application and its database Effectively creates a separation between the application logic and the database access Better code stability Better usability SQL code reuse - queries/commands are written only once

Persistence Layer Components Data Transfer Object – DTO An object that represents a record from a single table. Its variables represent the columns of the table. Data Access Object – DAO Contain methods for retrieving and storing DTOs. Each DAO is responsible for a single DTO. Repository Contains commands/queries that span over multiple tables. Effectively manages multiple related DTOs

Data Transfer Object DTOs are passed to and from the persistence layer. If transferred to application logic: They contain the data retrieved from the database. Contains query result. If transferred to persistence layer They contain the data that to be written to the database. Data will added using commands. Naming Convention: DTO name will be the singular of a plural table name. Example: Table named “grades”, DTO will be named “grade” Names of DTO constructor parameters == DTO fields names == table represented by the DTO column names Each DTO class represents a single table.

Implementation Example Students Contains student information Fields: name Questions Contains the questions information Fields: question, answer Grades: Contains grade for each student and his questions Fields: student, question, grade

DTO Implementation 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class Student(object): def __init__(self, id, name): self.id = id self.name = name class Question(object): def __init__(self, num, answer): self.num = num self.answer = answer class Grade(object): def __init__(self, student_id, question_num, grade): self.student_id = student_id self.question_num = question_num self.grade = grade

Student - DAO 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class _Students(object): #Underscore means singleton def __init__(self, conn): self._conn = conn def insert(self, student): self._conn.execute(""" INSERT INTO students (id, name) VALUES (?, ?) """, [student.id, student.name]) def find(self, student_id): c = self._conn.cursor() c.execute(""" SELECT id, name FROM students WHERE id = ? """, [student_id]) return Student(*c.fetchone())

Questions - DAO 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class _Questions(object): def __init__(self, conn): self._conn = conn def insert(self, question): self._conn.execute(""" INSERT INTO questions (num, answer) VALUES (?, ?) """, [question.num, question.answer]) def find(self, num): c = self._conn.cursor() c.execute(""" SELECT num,answer FROM questions WHERE num = ? """, [num]) return Question(*c.fetchone())

Grades - DAO 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class _Grades(object): def __init__(self, conn): self._conn = conn def insert(self, grade): self._conn.execute(""" INSERT INTO grades (student_id, question_num, grade) VALUES (?, ?, ?) """, [grade.student_id, grade.question_num, grade.grade]) def find_all(self): c = self._conn.cursor() all = c.execute(""" SELECT student_id, question_num, grade FROM grades """).fetchall() return [Grade(*row) for row in all]

Repository 1 2 3 4 5 6 7 8 9 class _Repository(object): def __init__(self): self._conn = sqlite3.connect('grades.db') self.students = _Students(self._conn) self.questions = _Questions(self._conn) self.grades = _Grades(self._conn) def _close(self): self._conn.commit() self._conn.close()

Repository - Continued 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 def create_tables(self): _conn.executescript(""“ CREATE TABLE students ( id INT PRIMARY KEY, name TEXT NOT NULL ); CREATE TABLE questions ( num INT PRIMARY KEY, answer TEXT NOT NULL ); CREATE TABLE grades ( student_id INT NOT NULL, question_num INT NOT NULL, grade INT NOT NULL, FOREIGN KEY(student_id) REFERENCES students(id), FOREIGN KEY(question_num) REFERENCES questions(num), PRIMARY KEY (student_id, question_num) ); ""“)

Application Logic- Adding Grades 1 2 3 4 5 6 7 8 9 10 11 12 13 def grade(questions_dir, question_num): answer = repo. questions.find(question_num).answer for question in os.listdir(questions_dir): (student_id, ext) = os.path.splitext(question) code = imp.load_source('test', questions_dir + '/' + question) student_grade = Grade(student_id, question_num, 0) if code.run_question() == answer: student_grade.grade = 100 repo.grades.insert(student_grade)

Application Logic – Printing Grades 1 2 3 4 5 6 7 def print_grades(): print 'grades:' for grade in repo.grades.find_all(): student = repo.students.find(grade.student_id) print 'grade of student {} on assignment {} is {}'\ .format(student.name, grade.assignment_num, grade.grade) This method goes over all the students to find the name of the student of a specific grades This is not an efficient solution. A better solution would use JOIN. Next, we add JOIN support to our later.

Adding JOIN to the Repository 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class StudentGradeWithName(object): def __init__(self, name, question_num, grade): self.name = name self. question_num = question_num self.grade = grade #this function is added to the Repository def get_grades_with_names(self): c = self._conn.cursor() all = c.execute(""" SELECT students.name, grades.assignment_num, grades.grade FROM grades JOIN students ON grades.student_id = students.student_id """).fetchall() return [StudentGradeWithName(*row) for row in all]

Updating the Application Logic 1 2 3 4 567 def print_grades(): print 'grades:' for studentGradeWithName in repo.get_grades_with_names(): print 'grade of student {} on assignment {} is {}'\ .format(studentGradeWithName.name, studentGradeWithName.assignment_num, studentGradeWithName.grade)

ORM - Object Relational Mapping What if we wish to update a grade? What if we wish to update a student name? Should we make an update function for each case? Solution: Generic functions. ORM method allows mapping between a DTO and its table. Using ORM we can implement a generic DAO containing: Generic delete Generic update

ORM Implementation 1 2 3 4 5 6 7 8 9 10 11 12 13 14 import inspect #allows reaching constructor arguments def orm(cursor, dto_type): #the following line retrieve the argument names of the constructor args = inspect.getargspec(dto_type.__init__).args #__init__ == constructor args = args[1:] #args[0] == ‘self’, so we ignore it #gets the names of the columns returned in the cursor, after SELECT was executed col_names = [column[0] for column in cursor.description] #map them into the position of the corresponding constructor argument col_mapping = [col_names.index(arg) for arg in args] return [row_map(row, col_mapping, dto_type) for row in cursor.fetchall()] def row_map(row, col_mapping, dto_type): ctor_args = [row[idx] for idx in col_mapping] return dto_type(*ctor_args)

Generic DOA implementation Code @ website

Generic Delete - DAO 1 2 3 4 5 6 7 8 9 def delete(self, **keyvals): column_names = keyvals.keys() params = keyvals.values() stmt = 'DELETE FROM {} WHERE {}' \ .format(self._table_name, ' AND '.join([col + '=?' for col in column_names])) c = self._conn.cursor() c.execute(stmt, params)

Generic Update - DAO 1 2 3 4 5 6 7 8 9 10 11 12 13 14 def update(self, set_values, cond): set_column_names = ','.join(set_values.keys()) set_params = set_values.values() cond_column_names = ','.join(cond.keys()) cond_params = cond.values() params = set_params + cond_params stmt = 'UPDATE {} SET ({}) WHERE ({})'\ .format(self._table_name, ' AND '.join([set + '=?' for set in set_column_names]), ' AND '.join([cond + '=?' for cond in cond_column_names])) self._conn.execute(stmt, params).