DATABASE PROGRAMMING IN SQLJ
DATABASE PROGRAMMING IN SQLJ
in SQLJ
Julie Basu, Ekkehard Rohwedder, and Brian Becker
Java Products Group, Oracle Corporation
• SQLJ Language
• SQLJ Translator
• SQLJ Runtime
• Connect to database(s)
• Query
• Update
• FAQ
1105023628 11
Querying One Row of Data
5 program steps:
1. Declare an iterator class ( in #sql )
2. Define an instance of the class
3. Populate the instance with query (in #sql)
4. Access the column data
– Use method calls for named iterators
– Use #sql syntax for positional iterators
Declares a class
• Class name: BugIter, modifier: public
• Done: close()
} finally {
if (b != null) b.close();
}
}
sqlj.runtime.ExecutionContext ec =
new sqlj.runtime.ExecutionContext();
#sql [ec] { UPDATE bugs SET picture = :pic
WHERE name = :bug };
System.out.println(ec.getUpdateCount() +
" rows updated");
• Stored procedures
#sql { call STORED_PROC(
:IN x, :OUT y, :INOUT z) };
• Stored functions
int a; String b;
#sql a = { VALUES ( STORED_FUNC(:IN b))};
• Compile
• Run
1105023628 24
One Step
SQLJ Translator
Bug.sqlj Bug.java Bug.class
Java Compiler
% sqlj Bug.sqlj
% java Bug
1105023628 25
SQL Checking
• Type mismatch!
– Must translate online to detect
SQLJ Translator
Bug.sqlj Bug.java Bug.class
Java Compiler
Checker
% sqlj Bug.sqlj -user=scott/tiger
DBMS % java Bug
1105023628 27
Offline Checking
1105023628 28
SQL Checkers
1105023628 29
SQL Checkers (Cont.)
• boolean
supportsDatabase(errlog,conn)
– For online checkers
• describeSQLOperation
(errlog,conn,sqlop)
– Determine role of SQL statement
– Check modes and types of host expressions
– Issue errors and warnings
1105023628 30
Standard Checkers
• Vendor-specific checkers
– e.g. Oracle-checker uses system tables for
metadata, parses SQL in database
1105023628 31
SQLJ Customizations
1105023628 32
SQL Stored in.ser Files
SQLJ Translator
Bug.sqlj Bug.java Bug.class
Java Compiler
#sql
#sql{{ Profile:
Profile:
INSERT…
INSERT… Entry0
Entry0
};};
BugProfile.ser
Entry0:
Entry0:
INSERT…
INSERT…
1105023628 33
Transparent Translation
1105023628 34
SQLJ Implementation
1105023628 35
Standard Components
• Customization utilities
– Profile pretty printer
– Runtime call auditor
1105023628 36
Free Stuff!!
1105023628 37
Part Three
The SQLJ Runtime
Brian Becker
1105023628 39
Generated Objects
SQLJ Translator
Bug.sqlj Bug.java Bug.class
Java Compiler
#sql
#sql{{ Profile:
Profile:
INSERT…
INSERT… Entry0
Entry0
};};
BugProfile.ser
Entry0:
Entry0:
INSERT…
INSERT…
1105023628 40
Generated .java Files
1105023628 41
Example: Bug.sqlj
1105023628 42
Example: Bug.java
1105023628 43
Generated Profile Files
Profile
Profile ProfileData
ProfileData EntryInfo
EntryInfo n TypeInfo
TypeInfo
data
data profile
profile n
params
params n
entries
entries results
results TypeInfo
TypeInfo
1105023628 44
Example: BugProfile
• Entries (1):
– SQLString:
“INSERT INTO bugs (name, numLegs) VALUES (?, ?)”
– Role: STATEMENT
– ExecutionType: EXECUTE_UPDATE
– Parameters (2):
• JavaType: String, SQLType: VARCHAR,
Mode: IN, Name: “bug”
• JavaType: int, SQLType: INTEGER,
Mode: IN, Name: null
1105023628 45
Runtime Call Stack
Standard Runtime
Runtime Implementation
DBMS
1105023628 46
Runtime Implementations
SQLJ Program
Standard Runtime
1105023628 47
Runtime Performance
1105023628 48
Profile Customization
Profile
Profile ProfileData
ProfileData EntryInfo
EntryInfo n TypeInfo
TypeInfo
data
data profile
profile n
params
params n
custs entries
entries results
results TypeInfo
TypeInfo
n
Oracle IBM SAP
1105023628 49
Customization Steps
Oracle Customizer
IBM Customizer
Profile0.ser Profile0.ser Profile0.ser
8i Cust 8i Cust
DB2 Cust
DBMS
1105023628 50
Customization Selection
Profile
Profile Connection
Connected
Connected
Customization
Customization Profile
Profile
1105023628 51
Extensibility
1105023628 52
Runtime Extensibility
SQLJ Program
• Examples:
– Bind expression
type extensions
Standard Runtime
– Debug call tracing
Call Auditor
– Y2K bug fixes
JDBC
DBMS
1105023628 53
Deploy-time Extensibility
1105023628 54
Example: Oracle 8i
• Extended Types
– ROWID, Ref CURSOR, LOBs, Native Types
– Object, Ref, Array, Custom Types
• Extended SQL
– PSM-style assignment statement
– PL/SQL
1105023628 55
Other Success Stories
1105023628 56
Benefits
1105023628 57
Resources
• http://www.sqlj.org
– Free download of source/binary for Ref Impl
• http://www.oracle.com/java/sqlj
– Free 8.0.5 and 7.3.4 versions of SQLJ
– 8i documentation and business collateral
• Books
– SQLJ chapter in “Client/Server Programming with
Java™ and CORBA,” by Orfali,Harkey et al
– Several others in the works
1105023628 58
1105023628 59