SQLJ: Java and Relational Databases

Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman, Oracle Corp. Richard Pledereder, Sybase Inc.

Agenda

➨ Introduction

✔ SQLJ Part 0: Embedded SQL and Portability Profile

✔ SQLJ Part 1: Java Methods as SQL Procedures

✔ SQLJ Part 2: Java Classes as SQL Types

1 Java and Databases

–JDBC • Java Database Connectivity API • Widely Implemented –SQLJ™ • Java-Relational Database Technology • Portability; Productivity; Java in the Database • Leverages JDBC technology –JavaBlend • Object/Relational Mapping for Java – The focus of this tutorial is on SQLJ

SQLJ - The Consortium

– Structure is informal • Participants include Oracle, IBM, Sybase, Tandem, JavaSoft, Microsoft, Informix, XDB • Open to other participants

– Meetings • Approximately every 3-4 weeks • Hosted by one of the Bay Area resident vendors (Oracle, Sybase, Tandem, JavaSoft, Informix, etc.) • Participants: Product Architects + SQL Standards people

2 SQLJ - The Technology

– Part 0: SQLJ Embeded SQL • Mostly reviewed and implemented • Integrated with JDBC API • Oracle has placed Translator source into public domain – Part 1: SQLJ Stored Procedures and UDFs • Using Java static methods as SQL stored procedures & functions • Leverages JDBC API – Part 2: SQLJ Data Types • Pure Java Classes as SQL ADTs • Alternative to SQL3 Abstract Data Types

SQLJ - The Standard

– Goal of the SQLJ Consortium is to create workable standards specifications in web time

– The Consortium is working with ANSI X3H2 on a fast- track process for adopting SQLJ as a standard

– The Consortium also works with The Open Group on a set of conformance tests

3 SQLJ - Implementation Status

– SQLJ Embedded SQL • Public-domain reference implementation available from Oracle http://www.oracle.com/st/products/jdbc/sqlj • Profile customizations available from Oracle, IBM, Sybase, Tandem, … – SQLJ Procedures • Specifications mostly reviewed • Implementations, e.g., Sybase Adaptive Server Anywhere 6.0, Oracle 8.1, IBM – SQLJ Data Types • Specifications through first pass • Implementations, e.g., Sybase Adaptive Server Anywhere 6.0

JDBC 2.0 - SQLJ Features

✔ Support for user-defined, object data types – Java Classes • Persistent Java objects stored in the DBMS – SQL3 types • BLOB, CLOB, array, reference • Structured and distinct types

✔ New type codes – JAVA_OBJECT, STRUCT, BLOB, etc. – Metadata for user-defined types int[] types = {Types.JAVA_OBJECT}; ResultSet rs = dmd.getUDTs("catalog-name", "schema-name", "%", types);

4 JDBC 2.0 - SQLJ Features

✔ Objects-by-Value – Java Classes as database types •this just works – SQL3 ADTs as database types • Java mapping maintained per Connection – Seamless extension of get/setObject()

Statement stmt; … ResultSet rs = stmt.executeQuery( "SELECT CUSTOMER FROM ACCOUNTS"); rs.next(); Customer cust = (Customer)rs.getObject(1);

Agenda

✔ Overview and JDBC 2.0: New Features

➨ SQLJ Part 0: Embedded SQL and Portability Profile

✔ SQLJ Part 1: Java Methods as SQL Procedures

✔ SQLJ Part 2: Java Classes as SQL Types

5 SQLJ Part 0: SQL Embedded in Java ✔ Objectives

– Simple, concise language for embedding SQL statements in Java programs

– Standard to allow for assembly of binary components produced by different tools

– Standard to allow for binary portability across different database systems

Advantages

– Ahead-of-time syntax and type checking

– Strongly typed cursors (iterators)

– Offline pre-compilation (for performance)

– Deployment-time customization (for binary portability and native pre-compilation)

6 SQLJ clauses

– SQLJ statements start with “#

– SQLJ statements terminate with “;”

– SQLJ host variables start with “:”

– SQL text is enclosed in curly braces “{..}”

int n; #sql { INSERT INTO emp VALUES (:n) };

SQLJ more concise than JDBC

// SQLJ

int n; #sql { INSERT INTO emp VALUES (:n)};

// JDBC

int n; Statement stmt = conn.prepareStatement (“INSERT INTO emp VALUES (?)”); stmt.setInt(1,n); stmt.execute (); stmt.close();

7 Strongly typed cursors

– Positional binding to columns

#sql public iterator ByPos (String, int); ByPos positer; String name = null; int year = 0;

#sql positer = { SELECT name, year FROM people}; while (true) { #sql { FETCH :positer INTO :name, :year}; if (positer.endFetch()) break; // process name, year } positer.close();

Strongly typed cursors (cont.)

– Named binding to columns

#sql public iterator ByName (int year, String name); ByName namiter; String name = null; int year = 0;

#sql namiter = { SELECT name, year FROM people}; while (namiter.next()) { name = namiter.name(); year = namiter.year(); // process name, year } namiter.close();

8 Connection context

– SQLJ statements are associated with a connection context

– Context type identifies exemplar schema, e.g. views, tables, privileges

#sql context Department; Department dept = newDepartment(“jdbc:odbc:acme.cs”);

int n; #sql [dept] { insert into EMP values (:n)};

Extensible SQLJ framework

– Database vendors plug-in SQL syntax checkers and semantic analyzers using SQLChecker framework

– Database vendors provide customizers to install SQLJ “binaries” (profiles) in target database

– Default SQLJ binaries run on any JDBC driver

9 SQLJ translator framework

Java Class Files SQLChecker SQLJ program Java Frontend SQLJ Profile SQLJ Customizations Customizer Profiles Utility SQLJ Translator

SQLJ JAR FILE

SQLJ portability layers

SQLJ Program Profile Entries

JDBC

SQL DBSQL DB SQL DB

10 Custom SQL execution

SQLJ Program Profile Entries

JDBC

Customizations

SQL Module Stored procedure TP service SQL DBSQL DB SQL DB

Profile customization selection

Profile Customizations Data source URLs

11 SQLJ profile objects

Profile ProfileData EntryInfo TypeInfo

Customization ConnectedProfile RTStatement

SQLJ compilation phases SQLJ Translator

Foo.sqlj Foo.java Java Compiler Foo.class

12 SQLJ translation phase

Foo.sqlj SQLJ Translator

SQLJ semantic analysis

Foo.sqlj SQLJ Translator [Ctx0] {SQL0}

describe(SQL0)

SQLChecker0

13 SQLJ semantic analysis

Foo.sqlj SQLJ Translator [Ctx0] {SQL0} [Ctx0] {SQL1}

describe(SQL1)

SQLChecker0

SQLJ semantic analysis

Foo.sqlj SQLJ Translator [Ctx0] {SQL0} [Ctx0] {SQL1} (Ctx1) {SQL2}

describe(SQL2)

SQLChecker0 SQLChecker1

14 SQLJ code generation

Foo.sqlj SQLJ Translator Foo.java [Ctx0] {SQL0} [Ctx0] {SQL1} [Ctx1] {SQL2}

SQLJ code generation

Foo.jsql SQLJ Translator Foo.java [Ctx0] Profile0: {SQL0} Entry0 [Ctx0] {SQL1} [Ctx1) ]SQL2} Profile0.ser Entry0

15 SQLJ code generation

Foo.jsql SQLJ Translator Foo.java [Ctx0] Profile0: {SQL0} Entry0 [Ctx0] Profile0: {SQL1} Entry1 [Ctx1] {SQL2} Profile0.ser Entry0 Entry1

SQLJ code generation

Foo.jsql SQLJ Translator Foo.java [Ctx0] Profile0: {SQL0} Entry0 [Ctx0] Profile0: {SQL1} Entry1 [Ctx1] Profile1: {SQL2} Entry0 Profile0.ser Entry0 Entry1 Profile1.ser Entry0

16 Java compilation SQLJ Translator

Foo.sqlj Foo.java Java Compiler Foo.class [Ctx0] Profile0: Profile0: {SQL0} Entry0 Entry0 [Ctx0] Profile0: Profile0: {SQL1} Entry1 Entry1 [Ctx1] Profile1: Profile1: {SQL2} Entry0 Entry0 Profile0.ser Entry0 Entry1 Profile1.ser Entry0

SQLJ packaging Foo.jar SQLJ Translator

Foo.sqlj Foo.java Java Compiler Foo.class [Ctx0] Profile0: Profile0: {SQL0} Entry0 Entry0 [Ctx0] Profile0: Profile0: {SQL1} Entry1 Entry1 [Ctx1] Profile1: Profile1: {SQL2} Entry0 Entry0 Profile0.ser Entry0 Entry1 Profile1.ser Entry0

17 SQLJ installation phase Foo.jar Foo.class

Profile0.ser

Profile1.ser

SQLJ installation phase Foo.jar Foo.jar Foo.class Foo.class Customizer1

Profile0.ser Profile0.ser

Customization

Profile1.ser Profile1.ser

18 SQLJ installation phase Foo.jar Foo.jar Foo.jar Foo.class Foo.class Foo.class Customizer1 Customizer2

Profile0.ser Profile0.ser Profile0.ser

Customization1 Customization1 Customization2 Profile1.ser Profile1.ser Profile1.ser

Customization2

Agenda

✔ JDBC 2.0: New Features

✔ SQLJ Part 0: Embedded SQL and Portability Profile

➨ SQLJ Part 1: Java Methods as SQL Procedures

✔ SQLJ Part 2: Java Classes as SQL Types

19 SQLJ Part 1: Java methods as SQL procedures ✔ Use Java static methods as SQL stored procedures and functions. – Advantage to SQL: Direct use of pre-written Java libraries.

✔ A procedural and scripting language for SQL. – Portable across DBMSs. – Deployable across tiers.

Technical objectives

– Convenient for Java programmers. • Not just aimed at SQL programmers. – Portable across DBMSs. – Same capability as regular SQL stored procedures. • Arbitrary SQL stored procedures re-codable as SQLJ stored procedures. – Convenience and performance comparable with SQL routines. – Callable from CLI/ODBC, from other SQL stored procedures, from JDBC/JSQL, and directly from Java. • Caller needn't know the SQLJ stored procedure is in Java.

20 Technical objectives (cont.)

– Any Java static method callable as a stored procedure: • Initially support only parameter and result types mappable to SQL. • Extensible to support arbitrary Java types, for Java caller and callee. – Body of SQLJ stored procedure routines can use JDBC and/or SQLJ to access SQL, or Java computation – Initially support persistence only for duration of a call. • Consider session and database persistence as follow-on.

Topics

– Example Java classes – Defining Java classes to SQL • Installing jar files • Specifying SQL names •SQL Permissions – OUT parameters – Result sets – Error handling –Paths – Deployment descriptors

21 Examples

– Example table: create table emps ( name varchar(50), id char(5), state char(20), sales decimal (6,2)); – Example classes and methods: • Routines1.region – Maps a state code to a region number. Plain Java (no SQL). • Routines1.correctStates –Performs an SQL update to correct the state codes. • Routines2.bestEmps—Returns the top two employees as output parameters. • Routines3.rankedEmps—Returns the employees as a result set.

Examples (cont.)

– The region and correctStates methods public class Routines1 { //The region method //An Integer method that will be called as a function public static Integer region(String s) throws SQLException { if (s == "MN" || s == "VT" || s == "NH" ) return 1; else if (s == "FL" || s == "GA" || s == "AL" ) return 2; else if (s == "CA" || s == "AZ" || s == "NV") return 3; else return 4; } //The correctStates method //A void method that will be called as a stored procedure public static void correctStates (String oldSpelling, String newSpelling) throws SQLException { Connection conn = DriverManager.getConnection ("JDBC:DEFAULT:CONNECTION"); PreparedStatement stmt = conn.prepareStatement ("UPDATE emps SET state = ? WHERE state = ?"); stmt.setString(1, newSpelling); stmt.setString(2, oldSpelling); stmt.executeUpdate(); return; } }

22 Installing Java Classes in SQL

– New install_jar procedure: sqlj.install_jar ('file:~/classes/Routines1.jar', 'routines1_jar' ) – Two parameters: • The URL of a jar file containing a set of Java classes • A character string that will be used to identify the Jar in SQL – Installs all classes in the jar file: • Uses Java reflection to determine their names, methods, and signatures – Retains the Jar file, the character string identifies it: • The jar name is specified in a later remove_jar procedure: • Follow-on facilities will address replacing and downloading jar files, etc.

Defining SQL names for Java methods – A form of the SQL create procedure/function statement:. create procedure correct_states(old char(20), new char(20)) modifies sql data external name 'routines1_jar:Routines1.correctStates' language java parameter style java;

create function region_of(state char(20)) returns integer no sql external name 'routines1_jar:Routines1.region' language java parameter style java; – The create procedure statement and the external language X are standard. • The language alternative java is an SQLJ extension.

23 Defining SQL names for Java methods – The procedure/function names "correct_states" and "region_of" are normal SQL 3-part names, with normal defaults. – You can do multiple create procedure statements pointing to the same Java method. – The key role of create procedure is to define an SQL synonym for the Java method. – Why use an SQL name? • Java names have different syntax: case-sensitive, package names, Unicode, etc. • SQL metadata and permissions are keyed to SQL names.

Privileges

– The usage privilege on the installed jar file is grantable: grant usage on routines1_jar to Smith – The execute permission on the SQL names is grantable. grant execute on correct_states to Smith – Methods run with "definer's rights".

24 Invoking Java methods

– Use the SQL names, with normal defaults for the first two parts:

select name, region_of(state) as region from emps where region_of(state) = 3

call correct_states ('CAL', 'CA');

OUT parameters

– SQL procedures have OUT and INOUT parameters; Java doesn't. – If a Java method will be used as an SQL proc with OUT parameters, those parameters are declared as Java arrays, to act as "containers". – Example (next page): • bestTwoEmps returns the two top employees in a given region. •The specific region is an in parameter. • The column values of the two top employees are out parameters. •The bestTwoEmps method is coded with JSQL. • A version of bestTwoEmps coded with JDBC is shown in the draft specs, for comparison.

25 OUT Parameters (cont.)

public class Routines2 { public static void bestTwoEmps (String[ ] n1, String[ ] id1, int[ ] r1, BigDecimal[ ] s1, String[ ] n2, String[ ] id2, int[ ] r2, BigDecimal[ ] s2, Integer regionParm) throws SQLException { #sql iterator ByNames (String name, int id, int region, BigDecimal sales); ByNames r; #sql r = {"SELECT name, id, region_of(state) as region, sales FROM emp WHERE region_of(state) > :regionParm AND sales IS NOT NULL ORDER BY sales DESC"}; if (r.next()) { n1[0] = r.name(); id1[0] = r.id(); r1[0] = r.region(); s1[0] = r.sales(); } else { n1[0] = "****"; return; } if (r.next()) { n2[0] = r.name(); id2[0] = r.id(); r2[0] = r.region(); s2[0] = r.sales(); } else { n2[0] = "****"; return; } } }

OUT parameters (cont.)

– CREATE PROC for the bestTwoEmps method •The bestTwoEmps method has eight out parameters and one in parameter:

create procedure best2 (out n1 varchar(50), out id1 varchar(5), out r1 integer, out s1 decimal(6,2), out n2 varchar(50), out id2 varchar(5), out r2 integer, out s2 decimal(6,2), region integer) reads sql data external name 'Routines2.bestTwoEmps' language java parameter style java;

26 OUT parameters (cont.)

– Invoking the best2 procedure

java.sql.CallableStatement stmt = conn.prepareCall ("{call best2(?,?,?,?,?,?,?,?,?)}"); stmt.registerOutParameter(1, java.sql.Types.String); stmt.registerOutParameter(2, java.sql.Types.String); stmt.registerOutParameter(3, java.sql.Types.Int); stmt.registerOutParameter(4, java.sql.Types.BigDecimal); stmt.registerOutParameter(5, java.sql.Types.String); stmt.registerOutParameter(6, java.sql.Types.String); stmt.registerOutParameter(7, java.sql.Types.Int); stmt.registerOutParameter(8, java.sql.Types.BigDecimal); stmt.setInt(9, 3); stmt.executeUpdate(); String n1 = stmt.getString(1); String id1 = stmt.getString(2); Integer r1 = stmt.getInt(3); BigDecimal s1 = stmt.getBigDecimal(4); String n2 = stmt.getString(5); String id2 = stmt.getString(6); Integer r2 = stmt.getInt(7); BigDecimal s2 = stmt.getBigDecimal(8);

Result sets

– SQL procedures can return result sets that are neither parameters nor function results. • An SQL “result set” is a set of rows generated by the callee for the caller. The caller processes the result set iteratively. – SQLJ models this as follows: • An SQL3 clause on create procedure specifies that the proc has result sets. • Such an SQL proc can be defined on a Java method with a result set return value. – Example (below): •The orderedEmps method returns a result set with the employees of a given region ordered by sales.

27 Result sets (cont.)

– Example: The orderedEmps method

public class Routines3 { public static orderedEmps(int regionParm, ResultSet[ ] rs ) throws SQLException { Connection conn = DriverManager.getConnection ("JDBC:DEFAULT:CONNECTION"); java.sql.PreparedStatement stmt = conn.prepareStatement ("SELECT name, region_of(state) as region, sales FROM emp WHERE region_of(state) > ? AND sales IS NOT NULL ORDER BY sales DESC"); stmt.setInteger(1, regionParm); rs[0] = stmt.executeQuery(); return; } }

Result sets (cont.)

– CREATE PROC for the orderedEmps method •The orderedEmps method returns one result set:

create procedure ranked_emps (region integer) dynamic result sets 1 reads sql data external name 'Routines3.orderedEmps' language java parameter style java;

– The dynamic result sets clause is standard ISO/ANSI SQL3. – Initially the dynamic result sets clause will only allow "1".

28 Result sets (cont.)

– Invoking the rankedEmps procedure

java.sql.CallableStatement stmt = conn.prepareCall( "{call ranked_Emps(?)}"); stmt.setInt(1, 3); ResultSet rs = stmt.executeQuery(); while (rs.next()) { String name = rs.getString(1); Integer region = rs.getInt(2); BigDecimal sales = rs.getBigDecimal(3); System.out.print(" Name = " + name); System.out.print(" Region = " + region); System.out.print(" Sales = " + sales); System.out.print("\n"); }

Error Handling

– General treatment: • Exceptions thrown and caught within an SQLJ stored procedure are internal to Java. • Exceptions that are uncaught when you return from a Java method become SQLSTATE error codes. • The message text of the SQLSTATE is the string specified in the Java throw.

29 Paths

– In Java, resolution of class names is done with the operating system CLASSPATH. – The CLASSPATH mechanism uses the operating system directory structure. – SQLJ defines a similar mechanism for name resolution. – Assume you have three jar files that reference classes in each other: • The “admin” jar references classes in the “property” and “project” jars. • The “property” jar references classes in the “project” jar. • The “project” jar references classes in the “property” and “admin” jars.

Paths (cont.)

– You install the jar files as usual: sqlj.install_jar (‘file:~/classes/admin.jar’, ‘admin_jar’); sqlj.install_jar (‘file:~/classes/property.jar’, ‘property_jar’); sqlj.install_jar (‘file:~/classes/project.jar’, ‘project_jar’); – Then you specify “paths” for the jar files: sqlj.alter_java_path (‘admin_jar’, ‘(property/*, property_jar) (project/*, project_jar)’); sqlj.alter_java_path (‘property_jar’, ‘(project/*, project_jar )’); sqlj.alter_java_path (‘project_jar’, ‘(*, property_jar) (*, admin_jar) ’); – When the Java VM encounteres an unloaded class name in e.g. the “admin_jar”, it will invoke the class loader supplied by the SQL system, which will use the SQL path to resolve the name.

30 Deployment descriptors

–A deployment descriptor is a text file containing the create and grant statements to do on install_jar, and the drop and revoke statements to do on remove_jar. – A deployment descriptor is contained in a jar file with the classes it describes. – The install_jar procedure will implicitly perform the create and grant statements indicated by the deployment descriptor. – The remove_jar procedure will implicitly perform the drop and revoke statements indicated by the deployment descriptor.

Deployment descriptors (cont.)

– Example deployment descriptor • Assume that all of the above example classes Routines1, Routines2, and Routines3 are in a single jar. • An example deployment descriptor for that jar would have the following form: SQLActions[ ] = { “BEGIN INSTALL // SQL create and grant statements // to be executed when the jar is installed. END INSTALL ”,

“BEGIN REMOVE //SQL drop and revoke statements // to be executed when the jar is removed. END REMOVE” }

31 Agenda

✔ Introduction

✔ SQLJ Part 0: Embedded SQL and Portability Profile

✔ SQLJ Part 1: Java Methods as SQL Procedures

➨ SQLJ Part 2: Java Classes as SQL Types

SQLJ Part 2: Java classes as SQL types – Use Java classes as SQL data types for: • Columns of SQL tables and views. • Parameters of SQL routines. – Especially SQL routines defined on Java methods (SQLJPart 1). – Advantage to SQL: • A type extension mechanism. • Either an alternative or supplement to SQL3 ADTs. – Advantage to Java: • Direct support for Java objects in SQL databases. • No need to map Java objects to SQL scalar or BLOB types.

32 Examples

– Example class: Address

public class Address implements java.io.Serializable { public String street; public String zip; public static int recommended_width = 25; // A default constructor public Address ( ) { street = "Unknown"; zip = "None"; } // A constructor with parameters public Address (String S, String Z) { street = S; zip = Z; } // A method to return a string representation of the full address public String toString( ) { return "Street= " + street + " ZIP= " + zip; } };

Examples (cont.)

– Example subclass: Address2Line

public class Address2Line extends Address implements java.io.Serializable { public String line2; // A default constructor public Address2Line ( ) { line2 = " "; } // A constructor with parameters public Address2Line (String S, String L2, String Z) { street = S; line2 = L2; zip = Z; } // A method to return a string representation of the full address public String toString( ) { return "Street= " + street + " Line2= " + line2 + " ZIP= " + zip; } };

33 CREATE TYPE

– The role of create type is like that of create procedure: • Specify SQL names for the type, the fields, and the methods. – Additional clauses for ordering specs, etc. • The above example uses the default ordering

CREATE TYPE (cont.)

– CREATE for Address

create type addr external name 'Address' language java (zip_attr char(10) external name 'zip', street_attr varchar(50) external name 'street', static rec_width_attr integer external name 'recommended_width', method addr ( ) returns addr external name 'Address', method addr (s_parm varchar(50), z_parm char(10)) returns addr external name 'Address', method to_string ( ) returns varchar(255) external name ‘toString’, method remove_leading_blanks ( ) external name ‘removeLeadingBlanks’; static method contiguous (A1 addr, A2 addr) returns char(3) external name 'contiguous' )

34 CREATE TYPE (cont.)

– CREATE for Address2

create type addr_2_line under addr external name 'Address2Line' language java (line2_attr varchar(100) external name 'line2', method addr_2_line ( ) returns addr_2_line external name 'Address2Line', method addr_2_line (s_parm varchar(50), s2_parm char(100), z_parm char(10)) returns addr_2_line external name 'Address2Line', method to_string ( ) returns varchar(255) external name 'toString', method remove_leading_blanks ( ) external name ‘removeLeadingBlanks’; method strip ( ) external name 'removeLeadingBlanks' )

Usage Privilege

– GRANTs for Address and Address2Line

grant usage on datatype addr to public;

grant usage on datatype addr2line to admin;

35 Java Classes as SQL datatypes

• Column data types:

create table emps ( name varchar(30), home_addr addr), mailing_addr addr_2_line); •Insert:

insert into emps values('Bob Smith', new Address('432 Elm Street', '99782'), new Address2Line('PO Box 99', 'attn: Bob Smith', '99678')); • Select:

select name, home_addr>>zip, home_addr>>street, mailing_addr>>zip from emps where home_addr>>zip <> mailing_addr>>zip • Methods and comparison:

select name, home_addr>>display(), mailing_addr>>display() from emps where home_addr <> mailing_addr

Java Classes as SQL datatypes (cont.) • Update:

update emps set home_addr>>zip = '99783' where name = 'Bob Smith'

update emps set home_address = mailing_address --Normal Java substitutability where home_address is null;

• Note the use of “>>” to reference fields and methods of Java instances in SQL.

– This avoids ambiguities with SQL dot-qualified names.

– The “>>” symbol is used in SQL3 for ADT references.

36 SQLJ: Java and Relational Databases

Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman, Oracle Corp. Richard Pledereder, Sybase Inc.

37