Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Application Development Database Programming Embedded SQL Dynamic SQL Embedded SQL in Java Database APIs: Alternative to embedding Embedded SQL.

Similar presentations


Presentation on theme: "Database Application Development Database Programming Embedded SQL Dynamic SQL Embedded SQL in Java Database APIs: Alternative to embedding Embedded SQL."— Presentation transcript:

1 Database Application Development Database Programming Embedded SQL Dynamic SQL Embedded SQL in Java Database APIs: Alternative to embedding Embedded SQL in Java using SQLJ - an example Database Stored Procedures SQL Persistent Stored Modules (SQL/PSM) Client-Server a Modern Database Architectures Client-Server Computing Two-Tier Architecture Multiple-Tier Architecture Active Database Concepts and Triggers – an introduction Generalized (ECA) Model for Active DB Database Triggers context Summary Course outlines

2 2 Database Programming (1/2)  Objective: To access a database from an application program (as opposed to interactive interfaces)  Why? An interactive interface is convenient but not sufficient A majority of database operations are made thru application programs (increasingly thru web applications)  SQL commands can be called from within a host language (e.g., C++ or Java ) program.  SQL statements can refer to host variables (including special variables used to return status).  Must include a statement to connect to the right database.

3 3 Database Programming (2/2)  Different Approaches (mainly two)  Embedded commands: Database commands are embedded in a general-purpose programming language  Library of database functions: Available to the host language for database calls; known as an API  A brand new, full-fledged language: Minimizes impedance mismatch  Impedance Mismatch  Incompatibilities between a host programming language and the database model, e.g.,  type mismatch and incompatibilities; requires a new binding for each language  set vs. record-at-a-time processing - need special iterators to loop over query results and manipulate individual values SQL supports a mechanism called a cursor to handle this.

4 4 Embedded SQL  Most SQL statements can be embedded in a general-purpose host programming language such as COBOL, C, Java  An embedded SQL statement is distinguished from the host language statements by enclosing it between  EXEC SQL or EXEC SQL BEGIN  and a matching END-EXEC or EXEC SQL END (or semicolon)  Syntax may vary with language  A preprocessor converts the SQL statements into special API calls.  Then a regular compiler is used to compile the code.  Language constructs: ! Connecting to a database:EXEC SQL CONNECT ! Declaring variables: EXEC SQL BEGIN (END) DECLARE SECTION ! Statements:EXEC SQL Statement;

5 5 Embedded SQL in C - an example  Variables inside DECLARE are shared and can appear (while prefixed by a colon) in SQL statements  SQLCODE is used to communicate errors/exceptions between the database and the program int loop; EXEC SQL BEGIN DECLARE SECTION; varchar dname[16], fname[16], …; char ssn[10], bdate[11], …; int dno, dnumber, SQLCODE, …; EXEC SQL END DECLARE SECTION; loop = 1; while (loop) { prompt (“Enter SSN: “, ssn); EXEC SQL select FNAME, LNAME, ADDRESS, SALARY into :fname, :lname, :address, :salary from EMPLOYEE where SSN == :ssn; if (SQLCODE == 0) printf(fname, …); else printf(“SSN does not exist: “, ssn); prompt(“More SSN? (1=yes, 0=no): “, loop); END-EXEC }

6 6 Why is cursor needed? Embedded SQL cursor A program variable can hold one value at a time host program DBMS result  When you write code for a transaction where the result set includes several rows of data, you must declare and use a cursor.  Cursor bridges the gap between value-oriented host program and set-oriented DBMS.

7 7 Cursor; definition  An entity that maps over a result set and establishes a position on a single row within the result set.  After the cursor is positioned on a row, operations can be performed on that row, or on a block of rows starting at that position.  The most common operation is to fetch (retrieve) the current row or block of rows. DECLARE CURSOR - a syntax based on the SQL-92 standard DECLARE cursor_name [ INSENSITIVE ] [ SCROLL ] CURSOR FOR select_statement [ FOR { READ ONLY | UPDATE [ OF column_name [,...n ] ] } ] A cursor is a mechanism you can use to fetch rows one at a time.

8 8 Embedding SQL using cursors DECLARE @sid varchar(5), @sfname varchar(20), @slname varchar(40), … DECLARE st_cursor CURSOR FOR SELECT sid, sfname, slname FROM student WHERE … OPEN st_cursor FETCH NEXT FROM st_cursor INTO @sid, @sfname, @slname WHILE @@FETCH_STATUS = 0 BEGIN … SELECT … --Get the next student. FETCH NEXT FROM st_cursor INTO @sid, @sfname, @slname END CLOSE st_cursor DEALLOCATE st_cursor GO Embedding SQL in C - an Example char SQLSTATE[6]; EXEC SQL BEGIN DECLARE SECTION char c_sname[20]; short c_minrating; float c_age; EXEC SQL END DECLARE SECTION c_minrating = random(); EXEC SQL DECLARE sinfo CURSOR FOR SELECT S.sname, S.age FROM Sailors S WHERE S.rating > :c_minrating ORDER BY S.sname; do { EXEC SQL FETCH sinfo INTO :c_sname, :c_age; printf(“%s is %d years old\n”, c_sname, c_age); } while (SQLSTATE != ‘02000’); EXEC SQL CLOSE sinfo;

9 9 Dynamic SQL Objective: Allows programs to construct and submit/executing SQL statements/queries at run time ( on-the-fly).  a program accepts SQL statements from the keyboard at run-time  a point-and-click operation translates to certain SQL query Dynamic SQL: An Example EXEC SQL BEGIN DECLARE SECTION; varchar sqlupdatestring [256]; EXEC SQL END DECLARE SECTION; … prompt (“Enter update command:“, sqlupdatestring ); EXEC SQL PREPARE sqlcommand FROM : sqlupdatestring ; EXEC SQL EXECUTE sqlcommand; Example of dynamic SQL within a C program. char* sqlcmd = “update account set balance = balance * 1.05 where account-number= ?” EXEC SQL prepare myprog from :sqlcmd; char account[10] = “A101”; EXEC SQL execute myprog using :account; The dynamic SQL program contains a ?, which is a place holder for a value that is provided when the SQL program is executed.

10 10 Embedded SQL in Java SQLJ: a standard (part of the SQL) for embedding SQL in Java  An SQLJ translator converts SQL statements into Java  These are executed thru the JDBC interface  Certain classes have to be imported - E.g., java.sql JDBC: Java Database Connectivity (Sun’s JDBC: Java API) SQL connection function calls for Java programming  A Java program with JDBC functions can access any relational DBMS that has a JDBC driver  JDBC allows a program to connect to several databases (known as data sources )

11 11 Database APIs: Alternative to embedding  Embedded SQL provides static database programming  API: Dynamic database programming with a library of functions  Advantage: No preprocessor needed (thus more flexible)  Disadvantage: SQL syntax checks to be done at run-time Rather than modify compiler, add library with database calls (API) (Database Programming with Functional Calls)  Special standardized interface: procedures/objects  Pass SQL strings from language, presents result sets in a language-friendly way  Sun’s JDBC: Java API SQL Call Level Interface - A part of the SQL standard  Provides easy access to several databases within the same program  Certain libraries (e.g., sqlcli.h for C) have to be installed and available  SQL statements are dynamically created and passed as string parameters in the calls

12 12 Steps in JDBC Database Access 1. Import JDBC library (java.sql.*) 2. Load JDBC driver: Class.forname(“oracle.jdbc.driver.OracleDriver”) 3.Define appropriate variables 4. Create a connect object (via getConnection ) 5. Create a statement object from the Statement class: 6.Identify statement parameters (designated by question marks) 7.Bound parameters to program variables 8. Execute SQL statement (referenced by an object) via JDBC’s executeQuery 9.Process query results (returned in an object of type ResultSet) ResultSet is a 2-dimentional table

13 13 Embedded SQL in Java using SQLJ - an example Compiler can perform syntax checks, strong type checks, consistency of the query with the schema All arguments always bound to the same variable: Int sid; String name; Int rating; // named iterator #sql iterator Sailors(Int sid, String name, Int rating); Sailors sailors; // assume that the application sets rating #sailors = { SELECT sid, sname INTO :sid, :name FROM Sailors WHERE rating = :rating }; // retrieve results while (sailors.next()) { System.out.println(sailors.sid + “ “ + sailors.sname)); } sailors.close();

14 14 SQLJ Iterators SQLJ supports two types of iterators (“cursors”): Named iterator - associated with a query result  Need both variable type and name, and then allows retrieval of columns by name.  Refer to previous slide. Positional iterator - lists only attribute types in a query result Need only variable type, and then uses FETCH.. INTO construct: #sql iterator Sailors(Int, String, Int); Sailors sailors; #sailors = … while (true) { #sql {FETCH :sailors INTO :sid, :name} ; if (sailors.endFetch()) { break; } // process the sailor }

15 15 Database Stored Procedures  What is a stored procedure:  Program executed through a single SQL statement  Executed in the process space of the server  Persistent procedures/functions (modules) are stored locally and executed by the database server  Advantages:  Can encapsulate application logic while staying “close” to the data  A procedure can be invoked by any applications (thus reduce duplications - reuse of application logic by different users)  Execution by the server reduces communication (traffic) costs  Enhance the modeling power of views  Disadvantages:  Every DBMS has its own syntax and this can make the system less portable

16 16 Stored Procedure Constructs  A stored procedure CREATE PROCEDURE procedure-name (params) local-declarations procedure-body;  A stored function CREATE FUNCTION fun-name (params) RETRUNS return-type local-declarations function-body;  Calling a procedure or function CALL procedure-name/fun-name (arguments);

17 17 Stored Procedures: Examples CREATE PROCEDURE ShowNumReservations SELECT S.sid, S.sname, COUNT(*) FROM Sailors S, Reserves R WHERE S.sid = R.sid GROUP BY S.sid, S.sname Stored procedures can have parameters - three different modes: IN, OUT, INOUT CREATE PROCEDURE IncreaseRating( IN sailor_sid INTEGER, IN increase INTEGER) UPDATE Sailors SET rating = rating + increase WHERE sid = sailor_sid Stored procedure do not have to be written in SQL: CREATE PROCEDURE TopSailors( IN num INTEGER) LANGUAGE JAVA EXTERNAL NAME “file:///c:/storedProcs/rank.jar”

18 18 Calling Stored Procedures EXEC SQL BEGIN DECLARE SECTION Int sid; Int rating; EXEC SQL END DECLARE SECTION // now increase the rating of this sailor EXEC CALL IncreaseRating(:sid,:rating); JDBC: CallableStatement cstmt= con.prepareCall(“{call ShowSailors}); ResultSet rs = cstmt.executeQuery(); while (rs.next()) { … } SQLJ: #sql iterator ShowSailors(…); ShowSailors showsailors; #sql showsailors={CALL ShowSailors}; while (showsailors.next()) { … }

19 19 SQL Persistent Stored Modules (SQL/PSM) Most DBMSs allow users to write stored procedures in a simple, general-purpose language (close to SQL)  SQL/PSM standard is a representative  SQL/PSM: Part of the SQL standard for writing persistent stored modules  SQL + stored procedures/functions + additional programming constructs  E.g., branching and looping statements  Enhance the power of SQL Declare a stored procedure: CREATE PROCEDURE name(p1, p2, …, pn) local variable declarations procedure code; Declare a function: CREATE FUNCTION name (p1, …, pn) RETURNS sqlDataType local variable declarations function code; An Example CREATE FUNCTION DEPT_SIZE (IN deptno INTEGER) RETURNS VARCHAR[7] DECLARE TOT_EMPS INTEGER; SELECT COUNT (*) INTO TOT_EMPS FROM SELECT EMPLOYEE WHERE DNO = deptno; IF TOT_EMPS > 100 THEN RETURN “HUGE” ELSEIF TOT_EMPS > 50 THEN RETURN “LARGE” ELSEIF TOT_EMPS > 30 THEN RETURN “MEDIUM” ELSE RETURN “SMALL” ENDIF;

20 Client-Server a Modern Database Architectures  Client-Server Computing Components of Data-Intensive Systems  Client-Server Computing with middleware  Two-Tier Architecture  Multiple-Tier Architecture Multiple-Tier Architecture with Web Server Case study – an example Advantages – an overview

21 21 Components of Data-Intensive Systems Client-Server Database Architectures (1/2)  Three separate types of functionality:  Presentation Logic  Application or business logic  Data management The system architecture determines whether these three components reside on a single system (“tier) or are distributed across several tiers  Tasks to Distribute  Presentation: code to maintain the graphical user interface  Validation: code to ensure the consistency of the database and user inputs  Business logic: code to perform business functions  Workflow: code to ensure completion of business processes  Data access: code to extract data to answer queries and modify a database Client-server computing (1990s)

22 22 Components of Data-Intensive Systems Client-Server Database Architectures (2/2)  Client-Server Architecture  is an arrangement of components (clients and servers) among computers connected by a network.  supports efficient processing of messages (requests for service) between clients and servers.  Design Issues  Division of processing: the allocation of tasks to clients and servers.  Process management: interoperability among clients and servers and efficiently processing messages between clients and servers.  Middleware: software for process management Client-server computing (1990s)

23 23 Client-Server Computing with Middleware  Middleware  A software component that performs process management.  Allow clients and servers to exist on different platforms.  Allows servers to efficiently process messages from a large number of clients.  Often located on a dedicated computer. Client-server computing

24 24 Two-Tier Architecture (1/2) Two-Tier Architecture  A PC client and a database server interact directly to request and transfer data.  The PC client contains the user interface code.  The server contains the data access logic.  The PC client and the server share the validation and business logic. Client-server computing

25 25 Two-Tier Architecture (2/2) Thin client  Client implements only the graphical user interface  Server implements business logic and data management Thick client  Client implements both the graphical user interface and the business logic  Server implements data management Disadvantages  No central place to update the business logic  Security issues: Server needs to trust clients  Access control and authentication needs to be managed at the server  Clients need to leave server database in consistent state  One possibility: Encapsulate all database access into stored procedures  Does not scale to more than several 100s of clients  Large data transfer between server and client  More than one server creates a problem: x clients, y servers: x*y connections Client-Server Architectures – Work division Extra

26 26 Three-Tier Architecture ( Application Server )  To improve performance, the three-tier architecture adds another server layer either by a middleware server or an application server.  The additional server software can reside on a separate computer.  Alternatively, the additional server software can be distributed between the database server and PC clients. Client-server computing

27 27 Multiple-Tier Architecture  A client-server architecture with more than three layers: a PC client, a backend database server, an intervening middleware server, and application servers.  Provides more flexibility on division of processing  The application servers perform business logic and manage specialized kinds of data such as images. Client-server computing

28 28 Multiple-Tier Architecture with Web Server Web server Middleware Server with listener SQL Results SQL statements and formatting requirements Database Server Database request HTML Page request HTML Client-server computing

29 29 Case study – Course Enrollment Build a system using which students can enroll in courses  Database System Student info, course info, instructor info, course availability, pre-requisites, etc.  Application Server Logic to add a course, drop a course, create a new course, etc.  Client Program Log in different users (students, staff, faculty), display forms and human- readable output Multiple-Tier Architecture / Web Application Database System (MS SQLServer) Application Server (Apache) Client Program (Web Browser) HTML, Javascript XSLT PhP, CGI XML Stored Procedures Technologies

30 30 Advantages of the Three-Tier Architecture  Heterogeneous systems Tiers can be independently maintained, modified, and replaced  Thin clients Only presentation layer at clients (web browsers)  Integrated data access  Several database systems can be handled transparently at the middle tier  Central management of connections  Scalability Replication at middle tier permits scalability of business logic  Software development  Code for business logic is centralized  Interaction between tiers through well-defined APIs: Can reuse standard components at each tier Multiple-Tier Architecture / Web Application Extra

31 Active Database Concepts and Triggers – an introduction  Generalized (ECA) Model for Active DB  Creating Triggers  Database Triggers context

32 32 Generalized Model for Active DB Event-Condition-Action (ECA model) rules  Allow for decisions to be made in the database instead of a separate application  Event : trigger the rule, Usually database update Example: – Inserting new employee tuples – Changing the salary of existing employees – Deleting employee tuples`  Condition : determines whether the rule action should be executed  No condition – executed once the event occurs  With condition (WHEN clause) Example -DNO for new tuple is not null  Action : usually a sequence of SQL statements Example - Automatically update TOTAL_SAL (1,2,& 4)  Implemented as triggers

33 33 Application SQL> INSERT INTO EMP 2...; SQL> INSERT INTO EMP 2...; EMP table EMPNO 7838 7698 7369 7788 ENAME KING BLAKE SMITH SCOTT JOB PRESIDENT MANAGER CLERK ANALYST SAL 5000 2850 800 3000 CHECK_SAL trigger Database Trigger: Example

34 34 Creating Triggers  Trigger timing - When should the trigger fire?  BEFORE: The code in the trigger body will execute before the triggering DML event.  AFTER: The code in the trigger body will execute after the triggering DML event.  Triggering event - What DML operation will cause the trigger to execute?  INSERT, UPDATE, DELETE  Any combination of the above  Table name: On table  Trigger Type - How many times should the trigger body execute when the triggering event takes place?  Statement: The trigger body executes once for the triggering event. This is the default.  Row: The trigger body executes once for each row affected by the triggering event.  When clause: Restricting condition  Trigger body: DECLARE BEGIN END;

35 35 SQL Triggers: An Example A trigger to compare an employee’s salary to his/her supervisor during insert or update operations: Create trigger inform_supervisor before INSERT OR UPDATE OF salary, supervisor_snn ON employee for each row when (NEW.SALARY> ( SELECT SALARY FROM EMPLOYEE WHERE SSN=NEW.SUPERVISOR_SSN) ) inform_supervisor (NEW.SUPERVISOR_SSN,NEW.SSN);

36 36 Database Triggers context  Implementation of Triggers Enforcing  Security,  Auditing  Data integrity  Referential integrity Within the Server  Table replication,  Derived data  Event logging  Benefits of Database Triggers  Improved data security  Provide value-based security checks  Provide value-based auditing  Improved data integrity  Enforce dynamic data integrity constraints  Enforce complex referential integrity constraints  Ensure related operations are performed together implicitly

37 37 Summary  A database may be accessed in an interactive mode  Most often data in a database is manipulate via application programs  Several methods of database programming:  Embedded SQL  Dynamic SQL  Stored procedure and function  Embedded SQL allows execution of parametrized static queries within a host language  Dynamic SQL allows execution of completely ad-hoc queries within a host language  Cursor mechanism allows retrieval of one record at a time and bridges impedance mismatch between host language and SQL  APIs such as JDBC introduce a layer of abstraction between application and DBMS  SQLJ: Static model, queries checked a compile-time.  Stored procedures execute application logic directly at the server  SQL/PSM standard for writing stored procedures  Client-Server computing architecture is largely adopted by database development community  Active databases materialize Event-Condition-Action (ECA model) rules via triggers


Download ppt "Database Application Development Database Programming Embedded SQL Dynamic SQL Embedded SQL in Java Database APIs: Alternative to embedding Embedded SQL."

Similar presentations


Ads by Google