CS346: Database Programming http://warwick.ac.uk/cs346 1
Database programming Issue: inclusionofdatabasestatementsinaprogram combination host language (general-purpose programming language, e.g. Java) data sublanguage (database language, e.g. SQL) stand-alone database programming language, e.g. Oracle s PL/SQL (Programming Language/SQL) Mixing is more common: there is often need to provide database access to existing code. New database PL is a good idea for intensive database interactions. Problems: vendor-specificvariations,evolvingstandards 2
Embedded SQL Database commands embedded into the host language C EXEC SQL... ; SQLJ #sql {... } ; Preprocessor extracts database statements for processing by the DBMS. DBMS generates code, which will be called in the original program instead of the database statements. 3
Host/data language integration issues Impedance mismatch attribute types (database) vs data types (PL) mapping needed (binding) different for each PL! query result structure (multiset of rows) vs data structure (variables) looping construct: cursor/iterator variables data extraction: tuple into distinct program variables These problems do not arise for special database PL. 4
Embedded SQL Advantages Query text is part of the source code. SQL syntax can be checked at compile time. Query plan can be created at compile time. Succinct and readable (queries are separated from the rest) Disadvantage Inability to change/create queries at runtime SQLJ: standard adopted by several vendors (IBM, Oracle, Compaq, Informix, ) Generic interaction pattern: connect,query,update,,close 5
SQLJ Default context public static DefaultContext getconnection(string url, String user, String password, Boolean autocommit) throws SQLException ; Error handling via exceptions (SQLException) try {<operation >} catch (<exception >) {<exception handling >} <continuation code> #sql and into clause (variable names preceded with :) 6
Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Towards JDBC Embedded SQL: static,querytextinsidesourcecode,changes require recompilation Dynamic SQL:moreflexibility,queriesconstructedatruntime(prepared), queries passed as argument strings, SQL checks happen at runtime, more difficult to access query results, numbers of attributes may not be known in advance Library of function calls approach (API): JDBCAPI Class.forName("oracle.jdbc.driver.OracleDriver"); Connection Statement ResultSet 7
Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Local setup "jdbc:oracle:thin:@daisy.warwick.ac.uk:1521:daisy" This will work from daisy. SSH authentication is needed otherwise. ssh -L 7100:daisy:1521 csu @daisy.csv.warwick.ac.uk "jdbc:oracle:thin:@localhost:7100:daisy" The username and password required by getconnection are the database login credentials, i.e. ops$csu. The password can be changed using SQLPLUS ALTER USER ops$csu IDENTIFIED BY new_password; 8
Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
SQL/PSM SQL/Persistent Stored Modules Program modules stored by the DBMS at the database server Can be functions or procedures Example of a database programming language based around SQL Includes general-purpose programming constructs in SQL Usefulness When database program is needed by several applications Reduces data transfer and communication cost between client and server Enhances modelling power provided by views (more complexity) 9
Stored code (either SQL or external) for future calls CREATE PROCEDURE <procedure name> (<parameters>) <local declarations> <procedure body> ; CREATE FUNCTION <function name> (<parameters>) RETURNS <return type> <local declarations> <function body> ; CREATE PROCEDURE <procedure name> (<parameters>) LANGUAGE <programming language name> EXTERNAL NAME <file path name> ; CALL <procedure or function name> (<argument list>) ; 10
Additional SQL/PSM constructs IF <condition> THEN <statement list> ELSEIF <condition> THEN <statement list>... ELSEIF <condition> THEN <statement list> ELSE <statement list> END IF ; WHILE <condition> DO <statement list> END WHILE ; REPEAT <statement list> UNTIL <condition> END REPEAT ; FOR <loop name> AS <cursor name> CURSOR FOR <query> DO <statement list> END FOR ; 11
Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley