sqlj: java and relational databases sqlj - the technology – part 0: sqlj embeded sql • mostly...

37
1 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

Upload: donhan

Post on 29-May-2018

229 views

Category:

Documents


0 download

TRANSCRIPT

1

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

2

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

3

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 workablestandards 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 aset of conformance tests

4

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 typesint[] types = {Types.JAVA_OBJECT};

ResultSet rs = dmd.getUDTs("catalog-name", "schema-name", "%", types);

5

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

6

SQLJ Part 0:SQL Embedded in Java✔ Objectives

– Simple, concise language for embedding SQLstatements in Java programs

– Standard to allow for assembly of binary componentsproduced by different tools

– Standard to allow for binary portability across differentdatabase systems

Advantages

– Ahead-of-time syntax and type checking

– Strongly typed cursors (iterators)

– Offline pre-compilation (for performance)

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

7

SQLJ clauses

– SQLJ statements start with “#sql”

– 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();

8

Strongly typed cursors

#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();

– Positional binding to columns

Strongly typed cursors (cont.)

#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();

– Named binding to columns

9

Connection context

– SQLJ statements are associated with a connectioncontext

– 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 andsemantic analyzers using SQLChecker framework

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

– Default SQLJ binaries run on any JDBC driver

10

SQLJ translator framework

SQLJprogram

SQLChecker

Java Frontend

SQLJ Translator

Java Class Files

SQLJ Profiles

SQLJ JAR FILE

Profile Customizer

Utility

SQLJ Customizations

SQLJ portability layers

SQL DB SQL DBSQL DB

SQLJ Program

Profile Entries

JDBC

11

Custom SQL execution

SQL DB SQL DBSQL DBStored procedure TP serviceSQL Module

SQLJ Program

Profile Entries

JDBC

Customizations

Profile customization selection

CustomizationsProfileData source URLs

12

SQLJ profile objectsProfile ProfileData EntryInfo TypeInfo

ConnectedProfileCustomization RTStatement

SQLJ compilation phases

SQL

J Translator

Foo.sqlj Foo.java Foo.classJava Com

piler

13

SQLJ translation phase

SQL

J Translator

Foo.sqlj

SQLJ semantic analysis

SQL

J Translator

Foo.sqlj[Ctx0]{SQL0}

describe(SQL0)

SQLChecker0

14

SQLJ semantic analysis

SQL

J Translator

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

describe(SQL1)

SQLChecker0

SQLJ semantic analysis

SQL

J Translator

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

SQLChecker0

(Ctx1){SQL2}

describe(SQL2)

SQLChecker1

15

SQLJ code generation

SQL

J Translator

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

Foo.java

SQLJ code generation

SQL

J Translator

Foo.jsql[Ctx0]{SQL0}[Ctx0]{SQL1}[Ctx1)]SQL2}

Foo.java

Entry0

Profile0.ser

Profile0:Entry0

16

SQLJ code generation

SQL

J Translator

Foo.jsql[Ctx0]{SQL0}[Ctx0]{SQL1}[Ctx1]{SQL2}

Foo.java

Entry0

Profile0.ser

Profile0:Entry0

Profile0:Entry1

Entry1

SQLJ code generation

SQL

J Translator

Foo.jsql[Ctx0]{SQL0}[Ctx0]{SQL1}[Ctx1]{SQL2}

Foo.java

Entry0

Profile0.ser

Profile0:Entry0

Profile0:Entry1

Entry1

Entry0

Profile1.ser

Profile1:Entry0

17

Java compilation

SQL

J Translator

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

Foo.java

Entry0

Profile0.ser

Profile0:Entry0

Profile0:Entry1

Entry1

Entry0

Profile1.ser

Profile1:Entry0

Java Com

piler

Foo.classProfile0:Entry0

Profile0:Entry1

Profile1:Entry0

SQLJ packaging

SQL

J Translator

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

Foo.java

Entry0

Profile0.ser

Profile0:Entry0

Profile0:Entry1

Entry1

Entry0

Profile1.ser

Profile1:Entry0

Java Com

piler

Foo.classProfile0:Entry0

Profile0:Entry1

Profile1:Entry0

Foo.jar

18

SQLJ installation phase

Profile0.ser

Profile1.ser

Foo.class

Foo.jar

SQLJ installation phase

Custom

izer1

Profile0.ser

Profile1.ser

Foo.class

Foo.jar

Profile0.ser

Profile1.ser

Foo.class

Foo.jar

Customization

19

SQLJ installation phase

Custom

izer1

Custom

izer2

Profile0.ser

Profile1.ser

Foo.class

Foo.jar

Profile0.ser

Profile1.ser

Foo.class

Foo.jar

Profile0.ser

Profile1.ser

Foo.class

Foo.jar

Customization1 Customization1

Customization2

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

20

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 Javalibraries.

✔ 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 storedprocedures.

– Convenience and performance comparable with SQLroutines.

– Callable from CLI/ODBC, from other SQL storedprocedures, from JDBC/JSQL, and directly from Java.

• Caller needn't know the SQLJ stored procedure is in Java.

21

Technical objectives (cont.)

– Any Java static method callable as a stored procedure:

• Initially support only parameter and result types mappable toSQL.

• Extensible to support arbitrary Java types, for Java caller andcallee.

– Body of SQLJ stored procedure routines can use JDBCand/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

22

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 statecodes.

• Routines2.bestEmps—Returns the top two employees as outputparameters.

• 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;

}

}

23

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, andsignatures

– 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 jarfiles, etc.

Defining SQL names for Javamethods

– A form of the SQL create procedure/functionstatement:.

create procedure correct_states(old char(20), new char(20))modifies sql dataexternal name ’routines1_jar:Routines1.correctStates’language java parameter style java;

create function region_of(state char(20)) returns integerno sqlexternal name ’routines1_jar:Routines1.region’language java parameter style java;

– The create procedure statement and the externallanguage X are standard.

• The language alternative java is an SQLJ extension.

24

Defining SQL names for Javamethods

– The procedure/function names "correct_states" and"region_of" are normal SQL 3-part names, with normaldefaults.

– You can do multiple create procedure statementspointing to the same Java method.

– The key role of create procedure is to define an SQLsynonym for the Java method.

– Why use an SQL name?

• Java names have different syntax: case-sensitive, packagenames, 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".

25

Invoking Java methods

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

select name, region_of(state) as regionfrom empswhere 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 withOUT parameters, those parameters are declared as Javaarrays, 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 draftspecs, for comparison.

26

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 inparameter:

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 dataexternal name ’Routines2.bestTwoEmps’language java parameter style java;

27

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 neitherparameters nor function results.

• An SQL “result set” is a set of rows generated by the callee forthe caller. The caller processes the result set iteratively.

– SQLJ models this as follows:

• An SQL3 clause on create procedure specifies that the proc hasresult sets.

• Such an SQL proc can be defined on a Java method with aresult set return value.

– Example (below):

• The orderedEmps method returns a result set with theemployees of a given region ordered by sales.

28

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, salesFROM emp WHERE region_of(state) > ?AND sales IS NOT NULLORDER 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 1reads sql dataexternal name ’Routines3.orderedEmps’language java parameter style java;

– The dynamic result sets clause is standard ISO/ANSISQL3.

– Initially the dynamic result sets clause will only allow"1".

29

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 storedprocedure are internal to Java.

• Exceptions that are uncaught when you return from a Javamethod become SQLSTATE error codes.

• The message text of the SQLSTATE is the string specified inthe Java throw.

30

Paths– In Java, resolution of class names is done with the

operating system CLASSPATH.

– The CLASSPATH mechanism uses the operatingsystem directory structure.

– SQLJ defines a similar mechanism for name resolution.

– Assume you have three jar files that reference classes ineach 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 ine.g. the “admin_jar”, it will invoke the class loadersupplied by the SQL system, which will use the SQL pathto resolve the name.

31

Deployment descriptors

– A deployment descriptor is a text file containing thecreate and grant statements to do on install_jar, and thedrop and revoke statements to do on remove_jar.

– A deployment descriptor is contained in a jar file withthe classes it describes.

– The install_jar procedure will implicitly perform thecreate and grant statements indicated by the deploymentdescriptor.

– The remove_jar procedure will implicitly perform thedrop and revoke statements indicated by thedeployment 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 thefollowing 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”}

32

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.

33

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;

}

};

34

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 addrexternal 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 addrexternal 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’

)

35

CREATE TYPE (cont.)– CREATE for Address2

create type addr_2_lineunder addrexternal 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;

36

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 Javainstances in SQL.

– This avoids ambiguities with SQL dot-qualified names.

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

37

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.