chapter 3 – user-defined routines and object behaviorlgis.informatik.uni-kl.de/archiv/ ·...
TRANSCRIPT
1
Recent Developments for Data Models - WS 2006/2007
Prof. Dr.-Ing. Stefan DeßlochAG Heterogene InformationssystemeGeb. 36, Raum 329Tel. 0631/205 [email protected]
Chapter 3 – User-defined Routines and Object Behavior
Recent Developments for Data Models - WS 2006/2007
2© Prof.Dr.-Ing. Stefan Deßloch
Outline
OverviewI. Object-Relational Database Concepts1. User-defined Data Types and Typed Tables2. Object-relational Views and Collection Types3. User-defined Routines and Object Behavior4. Application Programs and Object-relational CapabilitiesII. Online Analytic Processing5. Data Analysis in SQL6. Windows and Query Functions in SQLIII. XML7. XML and Databases8. SQL/XML9. XQueryIV. More Developments (if there is time left)temporal data models, data streams, databases and uncertainty, …
2
Recent Developments for Data Models - WS 2006/2007
3© Prof.Dr.-Ing. Stefan Deßloch
stored proceduresuser-defined functions
structured typessubtypingmethods
advanced datatypes SQL RoutinesPSMExternal RoutinesSQL/JRT
The "Big Picture"
SQL99
ISO
2.0 SQL92
JDBC
SQL OLBANSI
dynamic SQL
static SQL
Client DB Server Server-sideLogic
Recent Developments for Data Models - WS 2006/2007
4© Prof.Dr.-Ing. Stefan Deßloch
User-defined Routines
Named persistent code to be invoked from SQLCan be implemented using
procedural SQL extensions (PSM)external programming language
Created directly in a schemaHave schema-qualified names
Supported DDLCREATE and DROP statements ALTER statement (limited in functionality)
PrivilegesEXECUTE privilege controlled through GRANT and REVOKE statements
Described by corresponding information schema views
3
Recent Developments for Data Models - WS 2006/2007
5© Prof.Dr.-Ing. Stefan Deßloch
Procedures, Functions and Methods
Procedureinvoked exclusively using the SQL CALL statementCALL getPropertiesCloseTo ('1234 Cherry Lane …', 50, :number);may return additional results in form of result sets
Functionsinvoked in an expressions within other SQL statements (e.g., a SELECT or UPDATE statement) using function invocation syntaxSELECT price, location, distance(location, address('1234 Cherry Lane', …)) AS distFROM propertiesORDER BY dist
Methodsare regarded as a "special kind of function", associated with structured typesinvocation similar to function, but using method invocation syntaxSELECT price, location, location.longitude(), location.latitude()FROM properties
Reflected in additional differences in terms ofroutine signature (header)
parameter mode, resultoverloading, overridingroutine resolution/dispatch
Recent Developments for Data Models - WS 2006/2007
6© Prof.Dr.-Ing. Stefan Deßloch
Routines – General Structure
Routine header (SQL)consists of a name and a (possibly empty) list of parameters.parameters of procedures may specify parameter mode
INOUTINOUT
parameters of functions/methods are always INfunctions/methods return a single value (which may be complex)
header must specify data type of return value via RETURNS clause
Routine body, specified inSQL (SQL routines), using SQL procedural language extensions (PSM)a host programming language (external routines)
may contain SQL by embedding SQL statements in host language programs or using CLI
4
Recent Developments for Data Models - WS 2006/2007
7© Prof.Dr.-Ing. Stefan Deßloch
SQL Procedural Language Extensions
Compound statementSQL variable declarationIf statementCase statement
Loop statementWhile statementRepeat statementFor statementLeave statementReturn statementCall statementAssignment statementSignal/resignal statement
BEGIN ... END;DECLARE var CHAR (6);IF subject (var <> 'urgent') THEN ... ELSE ...;CASE subject (var)
WHEN 'SQL' THEN ...WHEN ...;
LOOP < SQL statement list> END LOOP;WHILE i<100 DO .... END WHILE;REPEAT ... UNTIL i>=100 END REPEAT;FOR result AS ... DO ... END FOR;LEAVE ...;RETURN 'urgent';CALL procedure_x (1,3,5);SET x = 'abc';SIGNAL division_by_zero
Recent Developments for Data Models - WS 2006/2007
8© Prof.Dr.-Ing. Stefan Deßloch
Procedures
Creating a stored procedureParameter modes OUT, INOUT designate parameters that
are set/modified by the procedure itselfare accessible by the calling application after the CALL invocation
mechanism depends on programming language binding
CREATE PROCEDURE getPropertiesCloseTo(IN addr VARCHAR(50),IN distance INTEGER,OUT results INTEGER)
… -- additional routine characteristicsDYNAMIC RESULT SETS 1;
A procedure can return one or more result sets to a calling applicationspecified using the DYNAMIC RESULT SETS clauseprocedure body can
declare cursors, indicating WITH RESULTSopen the cursor (to execute a SELECT statement), and leave the cursor open
calling application can process the results sets after invocation is completed
5
Recent Developments for Data Models - WS 2006/2007
9© Prof.Dr.-Ing. Stefan Deßloch
User-defined Functions
Creating a user-defined functionparameter mode must be IN (optional)must specify a result data type (RETURNS)CREATE FUNCTION distance
(loc1 VARCHAR(50),loc2 VARCHAR (50))
RETURNS INTEGER …;
Cannot return result sets, but may issue SQL statements
Recent Developments for Data Models - WS 2006/2007
10© Prof.Dr.-Ing. Stefan Deßloch
User-defined Table Functions
MotivationTransform non-relational data into a relational table "on-the-fly" for further SQL processing
semi-structured, …stored as BLOB, external file, …provided by external service
search engine, web service, …
Function returns a "multiset of rows"Example: "docmatch" access an external search service, returns document idsCREATE FUNCTION docmatch(idx VARCHAR(30), pattern VARCHAR(255))
RETURNS TABLE(doc_id CHAR(16)) …;
Function is used in the FROM clause of a queryExample: join the ids of matching documents with the DOCS tableSELECT T.AUTHOR, T.DOCTEXTFROM DOCS AS T,
TABLE(docmatch('MATHEMATICS', 'ZORN''S LEMMA')) AS FWHERE T.DOCID = F.DOC_ID
6
Recent Developments for Data Models - WS 2006/2007
11© Prof.Dr.-Ing. Stefan Deßloch
Table Functions vs. Views
View definitions don't support parametersAlternative: use SQL table functionsCREATE FUNCTION propertiesCloseTo (loc VARCHAR(40), dist INTEGER)
RETURNS TABLE ( price INTEGER, owner REF(person))…
RETURN TABLE(SELECT price, ownerFROM properties pWHERE distance(p.location, loc) < dist)
Use table function invocation in the FROM clause, instead of a view referenceSELECT *FROM TABLE(propertiesCloseTo('1234 Cherry Lane …', 50)) AS propsWHERE props.price < 500000
Recent Developments for Data Models - WS 2006/2007
12© Prof.Dr.-Ing. Stefan Deßloch
Privilege Requirements
Routine invocation requires EXECUTE privilegeWhose privileges are used when the routine itself invokes SQL statements?
security characteristic can be specified when creating a routineINVOKER
the invoker (user/role) must have the privileges to execute the statements in the routine body
DEFINERthe definer or creator must have the required privilegesroutine is dropped, if the definer loses any of these privileges at a later pointthe definer also has to possess these privileges WITH GRANT OPTION in order to grant the EXECUTE privilege to other users
7
Recent Developments for Data Models - WS 2006/2007
13© Prof.Dr.-Ing. Stefan Deßloch
Routine Overloading
Overloading -- multiple routines with the same unqualified nameS1.F (p1 INT, p2 REAL)S1.F (p1 REAL, p2 INT)S2.F (p1 INT, p2 REAL)
Within the same schemaEvery overloaded routine must have a unique signature, i.e., different number of parameters or different types for the same parameters
S1.F (p1 INT, p2 REAL)S1.F (p1 REAL, p2 INT)
Across schemasOverloaded routines may have the same signature
S1.F (p1 INT, p2 REAL)S2.F (p1 INT, p2 REAL)
Functions can be overloaded by type. Procedures can only be overloaded based on number of parameters.
Recent Developments for Data Models - WS 2006/2007
14© Prof.Dr.-Ing. Stefan Deßloch
Subject Routine Determination
Decides the function to invoke for a given invocation based on theCompile-time data types of all argumentsType precedence list of the data types of the argumentsSQL path
Always succeeds in finding a unique subject function, if one exists.Type precedence list is a list of data type names
Predefined types -- defined by the standard based on increasing precision/length
SMALLINT: SMALLINT, INTEGER, DECIMAL, NUMERIC, REAL, FLOAT, DOUBLECHAR: CHAR, VARCHAR, CLOB
User-defined types -- determined by the subtype-supertype relationshipif B is a subtype of A and C is a subtype of B, then the type precedence list for C is (C, B, A).
Invocation requires the invoker to have EXECUTE privilege on the routine --otherwise no routine will be found for the invocationIt is not an authorization violation!!!
8
Recent Developments for Data Models - WS 2006/2007
15© Prof.Dr.-Ing. Stefan Deßloch
Subject Routine Determination - Path
Path is a list of schema names.Can be specified during the creation of a schema, SQL-client module, or a SQL-server module
CREATE SCHEMA schema5PATH schema1,schema3...;
Every session has a default path, which can be changed using the SET statement.
SET PATH 'schema1, schema2'
Recent Developments for Data Models - WS 2006/2007
16© Prof.Dr.-Ing. Stefan Deßloch
Subject Routine Determination Algorithm
1. Determine the set of candidate functions for a given function invocation, F(a1, a2, ..., an):
Every function contained in S1 that has name F and has n parameters if the function name is fully qualified, i.e., the function invocation is of the form S1.F(a1, a2, ..., an), where S1 is a schema name.Every function in every schema of the applicable path that has name F and has n parameters if the function name is not fully qualified.
2. Eliminate unsuitable candidate functionsThe invoker has no EXECUTE privilegeThe data type of i-th parameter of the function is not in the type precedence list of the static type of the i-th argument (for parameter)
3. Select the best match from the remaining functionsExamine the type of the 1st parameter of each function and keep only those functions such that the type of their 1st parameter matches best the static type of the 1st argument (i.e., occurs earliest in the type precedence list of the static type of the argument), and eliminate the rest. Repeat Step b for the 2nd and subsequent parameters. Stop whenever there is only one function remaining or all parameters are considered.
4. Select the "subject function"From the remaining functions take the one whose schema appears first in the applicable path (if there is only one function, then it is the "subject function")
9
Recent Developments for Data Models - WS 2006/2007
17© Prof.Dr.-Ing. Stefan Deßloch
Subject Routine Determination - Example
Assume Y is a subtype of X. Assume the following three functions (with specific names F1, F2, and F3):F1: F(p1 X, p2 Y)F2: F (p1 Y, p2 Y)F3: F(p1 X, p2 REAL)
The subject function for F(y,y) where the static type of y is Y is F2.Now, assume the following three functions (with specific names F4, F5, and F6):F4: F(p1 X, p2 Y)F5: F(p1 X, p2 X)F6: F(p1 X, p2 REAL)
The subject function for F(y,y) where the static type of y is Y is F4.
Recent Developments for Data Models - WS 2006/2007
18© Prof.Dr.-Ing. Stefan Deßloch
Methods
What are methods?SQL-invoked functions "attached" to user-defined types
How are they different from functions?Implicit SELF parameter (called subject parameter)Two-step creation process: signature and body specified separately.Must be created in the type's schemaDifferent style of invocation, using dot-notation (e.g., UDT-value.method(...))
CREATE TYPE employee AS(name CHAR(40),base_salary DECIMAL(9,2),bonus DECIMAL(9,2))INSTANTIABLE NOT FINALMETHOD salary() RETURNS DECIMAL(9,2);
CREATE METHOD salary() FOR employeeBEGIN....END;
10
Recent Developments for Data Models - WS 2006/2007
19© Prof.Dr.-Ing. Stefan Deßloch
Methods (cont.)
Three kinds of methods: instance, constructor, static methodsTwo types of instance methods:
Original methods: methods attached to (super) typeOverriding methods: methods attached to subtypes, redefining original behavior
Signature must match with the signature of an original method, except for the subject parameter
CREATE TYPE employee AS(name CHAR(40),base_salary DECIMAL(9,2),bonus DECIMAL(9,2))INSTANTIABLE NOT FINALMETHOD salary() RETURNS DECIMAL(9,2);
CREATE TYPE manager UNDER employee AS(stock_option INTEGER)INSTANTIABLE NOT FINALOVERRIDING METHOD salary() RETURNS DECIMAL(9,2), -- overridingMETHOD vested() RETURNS INTEGER -- original;
Recent Developments for Data Models - WS 2006/2007
20© Prof.Dr.-Ing. Stefan Deßloch
Instance Methods
Invoked using dot syntax (assume dept table has mgr column):SELECT mgr.salary() FROM dept;
Subject routine determination picks the "best" method to invoke.Same algorithm as used for regular functionsSQL path is temporarily set to a list with the schemas of the supertypes of the static type of the self argument.
Dynamic dispatch executed at runtimeOverriding methods considered at execution timeOverriding method with the best match for the dynamic type of the self argument is selected.Schema evolution affects the actual method that gets invoked. If there is a new overriding method defined it may be picked for execution.
11
Recent Developments for Data Models - WS 2006/2007
21© Prof.Dr.-Ing. Stefan Deßloch
Method Reference
References can be used to invoke methods on the corresponding structured type
assumption: type 'person' has a method 'income' with the appropriate signatureSELECT prop.price, prop.owner->income(1998) FROM properties propSELECT name, DEREF(oid).income(1998)FROM people
Invocation of methods given a reference value require select privilege on the method for the target typed tableGRANT SELECT (METHOD income FOR person) ON TABLE people TO PUBLIC
Allows the table owner to control who is authorized to invoke methods on the rows of his/her table
Recent Developments for Data Models - WS 2006/2007
22© Prof.Dr.-Ing. Stefan Deßloch
Manipulating Structured Type Attributes
Attributes of structured types are implicitly associated with a pair of instance methodsObserver and mutator methods are used to access and modify attributes
Automatically generated when type is definedCREATE TYPE address AS (street CHAR (30), city CHAR (20), state CHAR (2), zip INTEGER) NOT FINAL
address_expression.street () -> CHAR (30)address_expression.city () -> CHAR (20)address_expression.state () -> CHAR (2)address_expression.zip () -> INTEGERaddress_expression.street (CHAR (30)) -> addressaddress_expression.city (CHAR (20)) -> addressaddress_expression.state (CHAR (2)) -> addressaddress_expression.zip (INTEGER) -> address
12
Recent Developments for Data Models - WS 2006/2007
23© Prof.Dr.-Ing. Stefan Deßloch
Dot Notation
"Dot'' notation must be used to invoke methods (e.g., to access attributes) Methods without parameters do not require use of "()"
SELECT location.street, location.city (), location.state, location.zip ()FROM propertiesWHERE price < 100000
Support for several 'levels' of dot notation (a.b.c.d.e)Allow "navigational" access to structured typesSupport for "user-friendly" assignment syntax
DECLARE r real_estate;...SET r.size = 2540.50; -- same as r.size (2540.50) ...SET ... = r.location.state; -- same as r.location().state()SET r.location.city = `LA'; -- same as r.location(r.location.city(`LA'))
Dot notation does not 'reveal' physical representationallows the definition of 'derived' attributes
method 'longitude' is indistinguishable from attribute 'city' from an invocation perspective
Recent Developments for Data Models - WS 2006/2007
24© Prof.Dr.-Ing. Stefan Deßloch
Encapsulation
An object should encapsulate its state to the outside worldonly the methods of an object may access the object state directlyother objects must invoke interface methods, cannot directly access the stateseparate interface from implementation
Encapsulation through public interface definition (not supported by SQL!)strict
all attributes are encapsulatedall or subset of methods are part of the interface
flexibleindividual attributes and methods may be designated as private, public, protected
Encapsulation implemented through privileges (supported by SQL)use authorization concepts to achieve arbitrary 'levels' of encapsulationEXECUTE privilege may be granted/revoked on observer/mutator methods as well
13
Recent Developments for Data Models - WS 2006/2007
25© Prof.Dr.-Ing. Stefan Deßloch
Static Methods
Static Methodshave no subject (SELF) parameterbehavior associated with type, not instanceno overriding, dynamic dispatch
Created using keyword STATICCREATE TYPE employee ...STATIC METHOD totalSalary(base DECIMAL(9,2), bonus DECIMAL(9,2))
RETURNS DECIMAL(9,2);
Invocation uses structured type name, "::"syntax 'borrowed' from C++
VALUES (employee::totalSalary(70000, 10000));
Recent Developments for Data Models - WS 2006/2007
26© Prof.Dr.-Ing. Stefan Deßloch
Initializing Instances: Constructor
Instances are generated by the system-provided constructor function Attributes are initialized with their default values
Attributes are modified (further initialized) by invoking the mutator functions
BEGINDECLARE re real_estate;SET re = real_estate(); -- generation of a new instance SET re.rooms = 12; -- initialization of attribute rooms SET re.size = 2500; -- initialization of attribute size
END
BEGINDECLARE re real_estate;SET re = real_estate().rooms (12).size (2500); -- same as above
END
14
Recent Developments for Data Models - WS 2006/2007
27© Prof.Dr.-Ing. Stefan Deßloch
User-defined Constructor Methods
Users can define any number of constructor methods and invoke them with NEW operator
CREATE TYPE real_estate AS ( ....)CONSTRUCTOR METHOD real_estate (r INTEGER, s DECIMAL(8,2)) RETURNS real_estate
CREATE CONSTRUCTOR METHOD real_estate(r INTEGER, s DECIMAL(8,2)) RETURNS real_estate
BEGINSET self.rooms = r;SET self.size = s;RETURN re;
END
BEGINDECLARE re real_estate;SET re = NEW real_estate(12, 2500); -- same as previously
END
Recent Developments for Data Models - WS 2006/2007
28© Prof.Dr.-Ing. Stefan Deßloch
Methods That Modify Object State
In OO-programminga method that wants to modify the state of its object simply assigns new values to an attributechanges are reflected in the identical object
In SQLvalue-based operations
expressions (including method invocations) always return (copies of) valuespersistent data can only be updated by the respective DML operations (e.g., UPDATE), assigning the results of expressions to the columns to be modified
a method will always operate on a copy of a complex value (i.e., instance of a structured type)
modification of state as a pure side-effect of a method is not possible
modifying the object state will require an UPDATE statementmethod returns a modified copy of the original complex valueseparate UPDATE replaces old value with the new copy
15
Recent Developments for Data Models - WS 2006/2007
29© Prof.Dr.-Ing. Stefan Deßloch
Value-based Model in SQL
SQL functions/methods operate on a value-based modelmethod needs to return a modified copy of the SELF object
return type must be the ST
CREATE TYPE employee AS(…)INSTANTIABLE NOT FINALMETHOD salary (DECIMAL(9, 2)) RETURNS employee;
CREATE METHOD salary (newsal DECIMAL(9, 2)) FOR employeeBEGIN
SET self.base_salary = newsal;RETURN self;
END;
UPDATE Employees -- assumes the table has an emp column of type employee!SET emp = emp.salary(80000)WHERE emp.name = 'Smith';
UPDATE EmployeesSET emp.salary = 80000 -- same as above WHERE emp.name = 'Smith'
Recent Developments for Data Models - WS 2006/2007
30© Prof.Dr.-Ing. Stefan Deßloch
Substitutability and Value-based Model
Problems with static type checkingCREATE TYPE employee AS (…)
INSTANTIABLE NOT FINALMETHOD salary (DECIMAL(9, 2)) RETURNS employee;
CREATE TYPE manager UNDER employee AS (…)INSTANTIABLE NOT FINAL;
CREATE TABLE departments(…, mgr manager, …)
UPDATE departmentsSET mgr = mgr.salary(80000)WHERE depno = 'K55';
manager inhertsmethod salary
method salary has static result type 'employee';
but department.mgr has type 'manager'!
=> static type error!
16
Recent Developments for Data Models - WS 2006/2007
31© Prof.Dr.-Ing. Stefan Deßloch
Type-Preserving Functions/Methods
SQL-invoked function, one of whose parameters is a result SQL parameter.The most specific type of the value returned by an invocation of a type-preserving function is identical to the most specific type of the SQL argument value substituted for the result SQL parameterThis can be the SELF parameter for methods
Example:CREATE TYPE employee AS(…)INSTANTIABLE NOT FINALMETHOD salary (DECIMAL(9, 2)) RETURNS employee SELF AS RESULT;
UPDATE departmentsSET mgr = mgr.salary(80000)WHERE depno = 'K55';
Type-checking succeeds, although the return type of manager.salary() is employee!
All system-generated mutator methods are type-preserving
Recent Developments for Data Models - WS 2006/2007
32© Prof.Dr.-Ing. Stefan Deßloch
Additional Routine Characteristics
DETERMINISTIC or NOT DETERMINISTICDETERMINISTIC (default)
Routine is expected to return the same result/output values for a given list of input values. (However, no checks are done at run time.)Gives the SQL query engine full flexibility for rewrite and optimization purposes
NOT DETERMINISTIC routines not allowed inConstraint definitionsAssertionsIn the condition part of CASE expressions CASE statements
RETURNS NULL ON NULL INPUT or CALLED ON NULL INPUT (default)RETURNS NULL ON NULL INPUT
An invocation returns null result/output value if any of the input values is null without executing the routine body
17
Recent Developments for Data Models - WS 2006/2007
33© Prof.Dr.-Ing. Stefan Deßloch
Additional Routine Characteristics (cont.)
CONTAINS SQL, READS SQL DATA, or MODIFIES SQL DATAExternal routines may in addition specify NO SQLImplementation-defined defaultFor SQL routines -- check may be done at routine creation time For both SQL and external routines -- exception raised if a routine attempts to perform actions that violate the specified characteristicRoutines with MODIFIES SQL DATA not allowed in
Constraint definitionsAssertionsQuery expressions (SELECT …)Triggered actions of BEFORE triggers Condition part of CASE expressionsCASE statementssearched delete statementssearch condition of searched update statements (are allowed in SET clause)
Recent Developments for Data Models - WS 2006/2007
34© Prof.Dr.-Ing. Stefan Deßloch
External Routines
Motivationexternal programming language may have better performance for computationally extensive tasksleverage existing program libraries
CREATE statement does not contain a method bodyLANGUAGE clause
Identifies the host language in which the body is written
NAME clauseIdentifies the host language code, e.g., file path in Unix
CREATE FUNCTION get_balance( IN INTEGER) RETURNS DECIMAL(15,2))LANGUAGE C EXTERNAL NAME 'usr/McKnight/banking/balance'
18
Recent Developments for Data Models - WS 2006/2007
35© Prof.Dr.-Ing. Stefan Deßloch
External Routine Parameters and Implementation
ParametersNames are optional
are not used in the routine body
Permissible data types depend on the host language of the body
RETURNS clause may specify CAST FROM clauseCREATE FUNCTION get_balance( IN INT) RETURNS DECIMAL(15,2)) CAST FROM REALLANGUAGE C
C program returns a REAL value, which is then cast to DECIMAL(15,2) before returning to the caller.
Parameter StylesDefine the signature of the corresponding programming language routineSpecial provisions to handle null indicators and the status of execution (SQLSTATE)
PARAMETER STYLE SQL (is the default)PARAMETER STYLE GENERAL
Recent Developments for Data Models - WS 2006/2007
36© Prof.Dr.-Ing. Stefan Deßloch
void balance (int* acct_id, float* rtn_val, int* acct_id_ind, int* rtn_ind, char* sqlstate[6], char* rtn_name [512], char* spc_name [512], char* msg_text[512]){...}
PARAMETER STYLE SQL
Additional parameters necessary for null indicators and routine name, and for returning a function result, error message, and SQLSTATE valueExternal language program (i.e., the body) has 2n+4 parameters for procedures and 2n+6 parameters for functions where n is the number of parameters of the external routine
CREATE FUNCTION get_balance( IN INTEGER) RETURNS DECIMAL(15,2)) CAST FROM REALLANGUAGE CEXTERNAL NAME 'bank\balance'PARAMETER STYLE SQL
19
Recent Developments for Data Models - WS 2006/2007
37© Prof.Dr.-Ing. Stefan Deßloch
PARAMETER STYLE GENERAL
No additional parametersExternal language program (i.e., the body) must have exactly the same number of parametersCannot handle null values
Exception is raised if any of the arguments evaluate to null
Value is returned in an implementation-dependent manner
CREATE FUNCTION get_balance( IN INTEGER) RETURNS DECIMAL(15,2)) CAST FROM REALLANGUAGE CEXTERNAL NAME 'bank\balance'PARAMETER STYLE GENERAL
float* balance (int* acct_id){...}
Recent Developments for Data Models - WS 2006/2007
38© Prof.Dr.-Ing. Stefan Deßloch
SQLJ Part 1
SQL Routines using the Java™ Programming LanguageLANGUAGE JAVA PARAMETER STYLE JAVA
no additional parameters on Java method signatures required to handle null values, errors, etc.
Java static methods used to implement SQL stored procedures and user-defined functions
parameter type conversion, error/exception handlingstored procedures: output parameters, returning result setsbody can contain JDBC, SQLJ
SQL DDL statement changescreate procedure, create function
JAR file becomes a database "object"built-in procedures to install, replace, remove JAR file in DBusage privilege on JAR files
Accepted ANSI standardANSI NCITS 331.1:1999
Has been folded into SQL:2003 as SQL/JRT
20
Recent Developments for Data Models - WS 2006/2007
39© Prof.Dr.-Ing. Stefan Deßloch
Installing Java Classes in the DB
InstallationNew install_jar proceduresqlj.install_jar('file:~/classes/routines.jar', 'routines_jar')Parameters: URL of JAR file with Java class and string to identify the JAR in SQLInstall all classes in the JAR fileUses Java reflection to determinenames, methods, signatures Optionally uses deploymentdescriptor file found in JAR tocreate SQL routines
Removalsqlj.remove_jar ('routines_jar')
Replacementsqlj.replace_jar
('file:~/classes/routines.jar', 'routines_jar')
JAR fileclass 1
method 11 (...)method 12 (...)
class 2method 21 (...)method 22 (...)
class 3method 31method 32
class 4method 41
class 5class 6
method 61 (...)
Recent Developments for Data Models - WS 2006/2007
40© Prof.Dr.-Ing. Stefan Deßloch
Creating Procedures and UDFs
routines.jarpublic class addr{public static voidmodifyaddr (...)
public static integer zip (string s) ...
sqlj.install_jar ('file:~/classes/routines.jar', 'routines_jar')
Java return type 'void' -> stored procedureotherwise -> user-defined function
CREATE PROCEDURE modify_address (ssn INTEGER, addr CHAR (40))MODIFIES SQL DATA EXTERNAL NAME 'routines_jar:addr.modifyaddr'LANGUAGE JAVAPARAMETER STYLE JAVA
CREATE FUNCTION zip (addr CHAR (40)) RETURNS INTEGER NO SQLDETERMINISTICEXTERNAL NAME 'routines_jar:addr.zip'LANGUAGE JAVAPARAMETER STYLE JAVA
21
Recent Developments for Data Models - WS 2006/2007
41© Prof.Dr.-Ing. Stefan Deßloch
Stored Procedures
OUT and INOUT parametersCREATE PROCEDURE
avgSal (IN dept VARCHAR(30), OUT avg DECIMAL(10, 2)) ...Java method declares them as arraysArray acts as container that can be filled/replaced by the method implementation to return a valuepublic static void averageSalary (String dept, BigDecimal[ ] avg) ...
Returning result set(s)CREATE PROCEDURE ranked_emps (region INTEGER)
DYNAMIC RESULT SETS 1 ....Java method declares explicit parameters for returned result sets of type
array of (JDBC) ResultSetarray of (SQLJ) iterator class, prev. declared in "#sql iterator ..."
public static void ranked_emps (int region, ResultSet[ ] rs) ...Java method body assigns (open) result sets as array elements of result set parametersMultiple result sets can be returned
Recent Developments for Data Models - WS 2006/2007
42© Prof.Dr.-Ing. Stefan Deßloch
Error Handling
Java method throws an SQLException to indicate error to the SQL engine... throws new SQLException ("Invalid input parameter", "38001");SQLSTATE value provided has to be in the "38xxx" range
Any other uncaught Java exception is turned into a SQLException "Uncaught Java exception" with SQLSTATE "38000" by the SQL engineJava exceptions that are caught within an SQLJ routine are internal and do not affect SQL processing
22
Recent Developments for Data Models - WS 2006/2007
43© Prof.Dr.-Ing. Stefan Deßloch
Additional Features
Java "main" methodsJava signature has to have single parameter of type String[]Corresponding SQL routine has
Either 0 or more CHAR/VARCHAR parameters, or a single parameter of type array of CHAR/VARCHAR
NULL value treatmentUse Java object types as parameters (see JDBC)
SQL NULL turned into Java nullSpecify SQL routine to return NULL if an input parameter is NULL
CREATE FUNCTION foo(integer p) RETURNS INTEGERRETURNS NULL ON NULL INPUT
Otherwise run-time exception will be thrownStatic Java variables
Can be read inside SQL routineShould not be modified (result is implementation-defined)
OverloadingSQL rules may be more restrictiveMap Java methods with same name to different SQL routine names
Recent Developments for Data Models - WS 2006/2007
44© Prof.Dr.-Ing. Stefan Deßloch
SQLJ Part 2
SQL Types using the Java™ Programming LanguageUse of Java classes to define SQL structured typesMapping of object state and behavior
Java methods become SQL methods on SQL typeJava methods can be invoked in SQL statements
Automatic mapping to Java object on fetch and method invocationJava SerializationJDBC 2.0 SQLData interface
Uses the procedures introduced in SQLJ Part 1 to install, remove, and replace SQLJ JAR files Approved ANSI standard
ANSI/NCITS 331.2-2000
Folded into SQL:2003 as SQL/JRT
23
Recent Developments for Data Models - WS 2006/2007
45© Prof.Dr.-Ing. Stefan Deßloch
Mapping Java Classes to SQL
Described using extended CREATE TYPE syntaxDDL statement, or Mapping description in the deployment descriptor
Supported Mapping
SQL constructor methods Have the same name as the type for which they are definedAre invoked using the NEW operator (just like in Java)
SQL does not know static member variablesMapped to a static SQL method that returns the value of the static variableNo support for modifying the static variable
Java SQLclass user-defined (structured) typemember variable attributemethod methodconstructor constructor methodstatic method static methodstatic variable static observer method
Recent Developments for Data Models - WS 2006/2007
46© Prof.Dr.-Ing. Stefan Deßloch
Mapping Example
Java classpublic class Residence implements Serializable, SQLData {
public int door;public String street;public String city;public static String country = "USA";public String printAddress( ) { ...};public void changeResidence(String adr) { ... // parse and update fields ...}// SQLData methodspublic void readSQL(SQLInput in, String type) { ... };public void writeSQL(SQLOutput out) { ... };
}
SQL DDL/descriptor statementCREATE TYPE Address EXTERNAL NAME 'residence_jar:Residence' LANGUAGE JAVA (
number INTEGER EXTERNAL NAME 'door',street VARCHAR(100) EXTERNAL NAME 'street',city VARCHAR(50) EXTERNAL NAME 'city',STATIC METHOD country( ) RETURNS CHAR(3)
EXTERNAL VARIABLE NAME 'country',METHOD print() RETURNS VARCHAR(200) EXTERNAL NAME 'printAddress',METHOD changeAddress (varchar(200)) RETURNS Address
SELF AS RESULT EXTERNAL NAME 'changeResidence')
24
Recent Developments for Data Models - WS 2006/2007
47© Prof.Dr.-Ing. Stefan Deßloch
Instance Update Methods
Remember: Java and SQL have different object update modelsJava model is object-basedSQL model is value-based
SQLJ permits mapping without requiring modification of Java methodsSELF AS RESULT identifies an instance update methodJava class
public class Residence implements Serializable, SQLData {...public void changeResidence(String adr) { ... // parse and update fields ...}
}SQL type
CREATE TYPE Address EXTERNAL NAME 'Residence' LANGUAGE JAVA (...METHOD changeAddress(varchar(200)) RETURNS Address SELF AS RESULT
EXTERNAL NAME 'changeResidence')
At runtime, the SQL systemInvokes the original Java method (returning void) on (a copy of) the objectIs responsible for returning the modified object
Recent Developments for Data Models - WS 2006/2007
48© Prof.Dr.-Ing. Stefan Deßloch
Object "Conversion" between SQL and Java
Serializable vs. SQLDataCan be specified in CREATE TYPE statement (optional clause)
CREATE TYPE Address ... LANGUAGE JAVA USING SERIALIZABLE ...CREATE TYPE Address ... LANGUAGE JAVA USING SQLDATA ...
default is implementation-definedimplementation may only support one of the mechanisms
does not impact the application program itselfUSING SERIALIZABLE
persistent object state entirely defined by Java serializationSQL attributes have to correspond to Java public fields
Java field names have to be listed in the external name clausesJava serialization is used for materializing the object in Java
attribute access, method invocation
USING SQLDATApersistent state is defined by the attributes in the CREATE TYPE statementexternal attribute names do not have to be specifiedSQLData interface is used for materializing objects in Java
read/writeSQL methods have to read/write attributes in the order definedJava fields might be different then the SQL attributes
Recommendations for portabilityhave Java class implement both Serializable and SQLDataJava class should define the complete persistent state as public fieldsCREATE TYPE statement should have external names, omit USING
25
Recent Developments for Data Models - WS 2006/2007
49© Prof.Dr.-Ing. Stefan Deßloch
Summary
OODBMS featuresExtensibility
user-defined types (structure and operations) as first class citizens
Computational completenessuse DML to express any computable function (-> method implementation)
Encapsulationseparate specification (interface) from implementation
Overloading, overriding, late bindingsame name for different operations or implementations
SQLUser-defined routines
procedures, functions, methods (instance, static, constructor)
SQL routines and external routinesuse SQL/PSM procedural extensions or external PL to implement routine
Encapsulationobserver/mutator methods and EXECUTE privilege to achieve encapsulation
Overloadingfull support for functions, methodslimited support for procedures
Overriding, late bindingsupported for methods