| Oracle8i JPublisher User's Guide Release 2 (8.1.6) A81357-01 |
|
This section describes examples of JPublisher output when you translate an object type containing methods and a PL/SQL package containing methods.
This section describes an example of JPublisher output given the following definition of a database type containing methods. The example defines a type Rational with numerator and denominator attributes and these functions and procedures:
MEMBER FUNCTION toReal: given two integers, this function converts a rational number to a real number and returns a real number.
MEMBER PROCEDURE normalize: given two integers (representing a numerator and a denominator), this procedure reduces a fraction by dividing numerator and denominator by their greatest common divisor.
STATIC FUNCTION gcd: given two integers, this function returns their greatest common divisor.
MEMBER FUNCTION plus: adds two rational numbers and returns the result.
The code for rational.sql follows:
CREATE TYPE Rational AS OBJECT ( numerator INTEGER, denominator INTEGER, MAP MEMBER FUNCTION toReal RETURN REAL, MEMBER PROCEDURE normalize, STATIC FUNCTION gcd(x INTEGER, y INTEGER) RETURN INTEGER, MEMBER FUNCTION plus ( x Rational) RETURN Rational ); CREATE TYPE BODY Rational AS MAP MEMBER FUNCTION toReal RETURN REAL IS -- convert rational number to real number BEGIN RETURN numerator / denominator; END toReal; MEMBER PROCEDURE normalize IS g INTEGER; BEGIN g := Rational.gcd(numerator, denominator); numerator := numerator / g; denominator := denominator / g; END normalize; STATIC FUNCTION gcd(x INTEGER, y INTEGER) RETURN INTEGER IS -- find greatest common divisor of x and y ans INTEGER; z INTEGER; BEGIN IF x < y THEN ans := Rational.gcd(y, x); ELSIF (x MOD y = 0) THEN ans := y; ELSE z := x MOD y; ans := Rational.gcd(y, z); END IF; RETURN ans; END gcd; MEMBER FUNCTION plus (x Rational) RETURN Rational IS BEGIN return Rational(numerator * x.denominator + x.numerator * denominator, denominator * x.denominator); END plus; END;
In this example, you invoke JPublisher with the following command line:
jpub -user=scott/tiger -sql=Rational -methods=true
The -user parameter directs JPublisher to log into the database as user scott with password tiger. The -methods parameter directs JPublisher to generate wrappers for the methods contained in the type Rational. You can omit this parameter, because -methods=true is the default.
JPublisher generates the file Rational.sqlj. This file reads as follows:
import java.sql.SQLException; import oracle.jdbc.driver.OracleConnection; import oracle.jdbc.driver.OracleTypes; import oracle.sql.CustomDatum; import oracle.sql.CustomDatumFactory; import oracle.sql.Datum; import oracle.sql.STRUCT; import oracle.jpub.runtime.MutableStruct; import sqlj.runtime.ref.DefaultContext; import sqlj.runtime.ConnectionContext; import java.sql.Connection; public class Rational implements CustomDatum, CustomDatumFactory { public static final String _SQL_NAME = "SCOTT.RATIONAL"; public static final int _SQL_TYPECODE = OracleTypes.STRUCT; #sql static context _Ctx; _Ctx _ctx; MutableStruct _struct; static int[] _sqlType = { 4, 4 }; static CustomDatumFactory[] _factory = new CustomDatumFactory[2]; static final Rational _RationalFactory = new Rational(); public static CustomDatumFactory getFactory() { return _RationalFactory; } /* constructors */ public Rational() { _struct = new MutableStruct(new Object[2], _sqlType, _factory); try { _ctx = new _Ctx(DefaultContext.getDefaultContext()); } catch (Exception e) { _ctx = null; } } public Rational(ConnectionContext c) throws SQLException { _struct = new MutableStruct(new Object[2], _sqlType, _factory); _ctx = new _Ctx(c == null ? DefaultContext.getDefaultContext() : c); } public Rational(Connection c) throws SQLException { _struct = new MutableStruct(new Object[2], _sqlType, _factory); _ctx = new _Ctx(c); } /* CustomDatum interface */ public Datum toDatum(OracleConnection c) throws SQLException { _ctx = new _Ctx(c); return _struct.toDatum(c, _SQL_NAME); } /* CustomDatumFactory interface */ public CustomDatum create(Datum d, int sqlType) throws SQLException { if (d == null) return null; Rational o = new Rational(); o._struct = new MutableStruct((STRUCT) d, _sqlType, _factory); o._ctx = new _Ctx(((STRUCT) d).getConnection()); return o; } /* accessor methods */ public Integer getNumerator() throws SQLException { return (Integer) _struct.getAttribute(0); } public void setNumerator(Integer numerator) throws SQLException { _struct.setAttribute(0, numerator); } public Integer getDenominator() throws SQLException { return (Integer) _struct.getAttribute(1); } public void setDenominator(Integer denominator) throws SQLException { _struct.setAttribute(1, denominator); } public Integer gcd ( Integer x, Integer y) throws SQLException { Integer __jPt_result; #sql [_ctx] __jPt_result = { VALUES(RATIONAL.GCD( :x, :y)) }; return __jPt_result; } public Rational normalize () throws SQLException { Rational __jPt_temp = this; #sql [_ctx] { BEGIN :INOUT __jPt_temp.NORMALIZE(); END; }; return __jPt_temp; } public Rational plus ( Rational x) throws SQLException { Rational __jPt_temp = this; Rational __jPt_result; #sql [_ctx] { BEGIN :OUT __jPt_result := :__jPt_temp.PLUS( :x); END; }; return __jPt_result; } public Float toreal () throws SQLException { Rational __jPt_temp = this; Float __jPt_result; #sql [_ctx] { BEGIN :OUT __jPt_result := :__jPt_temp.TOREAL(); END; }; return __jPt_result; } }
All the methods JPublisher generates invoke the corresponding PL/SQL methods executing in the server.
JPublisher declares the sql_name for the object to be SCOTT.RATIONAL and its sql_type_code to be OracleTypes.STRUCT, and creates a SQLJ connection context _Ctx. It creates accessor methods get/setNumerator() and get/setDenominator() for the object attributes numerator and denominator.
JPublisher generates source code for the gcd static function, which takes two Integer values as input and returns an Integer result. This gcd function invokes the RATIONAL.GCD stored function with IN host variables :x and :y.
JPublisher generates source code for the normalize member procedure, which defines a PL/SQL block containing an IN OUT parameter inside the SQLJ statement. The this parameter passes the values to the PL/SQL block.
JPublisher generates source code for the plus member function, which takes an object x of type Rational and returns an object of type Rational. It defines a PL/SQL block inside the SQLJ statement. The IN host variables are :x and a copy of this. The result of the function is an OUT host variable.
JPublisher generates source code for the toReal member function, which returns a Float. It defines a host OUT variable that is assigned the value returned by the function. A copy of this is an IN parameter.
This section describes an example of JPublisher output given the following definition of a PL/SQL package containing methods. The example defines the package RationalP with these functions and procedures, which manipulate the numerators and denominators of fractions.
FUNCTION toReal: given two integers, this function converts a rational number to a real number and returns a real number.
PROCEDURE normalize: given two integers (representing a numerator and a denominator), this procedure reduces a fraction by dividing numerator and denominator by their greatest common divisor.
FUNCTION gcd: given two integers, this function returns their greatest common divisor.
PROCEDURE plus: adds two rational numbers and returns the result.
The code for RationalP.sql follows:
CREATE PACKAGE RationalP AS FUNCTION toReal(numerator INTEGER, denominator INTEGER) RETURN REAL; PROCEDURE normalize(numerator IN OUT INTEGER, denominator IN OUT INTEGER); FUNCTION gcd(x INTEGER, y INTEGER) RETURN INTEGER; PROCEDURE plus (n1 INTEGER, d1 INTEGER, n2 INTEGER, d2 INTEGER, n3 OUT INTEGER, d3 OUT INTEGER); END rationalP; / CREATE PACKAGE BODY rationalP AS FUNCTION toReal(numerator INTEGER, denominator INTEGER) RETURN real IS -- convert rational number to real number BEGIN RETURN numerator / denominator; END toReal; FUNCTION gcd(x INTEGER, y INTEGER) RETURN INTEGER IS -- find greatest common divisor of x and y ans INTEGER; BEGIN IF x < y THEN ans := gcd(y, x); ELSIF (x MOD y = 0) THEN ans := y; ELSE ans := gcd(y, x MOD y); END IF; RETURN ans; END gcd; PROCEDURE normalize( numerator IN OUT INTEGER, denominator IN OUT INTEGER) IS g INTEGER; BEGIN g := gcd(numerator, denominator); numerator := numerator / g; denominator := denominator / g; END normalize; PROCEDURE plus (n1 INTEGER, d1 INTEGER, n2 INTEGER, d2 INTEGER, n3 OUT INTEGER, d3 OUT INTEGER) IS BEGIN n3 := n1 * d2 + n2 * d1; d3 := d1 * d2; END plus; END rationalP;
In this example, you invoke JPublisher with the following command line:
jpub -user=scott/tiger -sql=RationalP -methods=true
The -user parameter directs JPublisher to log into the database as user scott with password tiger. The -methods parameter directs JPublisher to generate wrappers for the methods in the package RationalP. You can omit this parameter, because -methods=true is the default.
JPublisher generates the file RationalP.sqlj, which reads as follows:
import java.sql.SQLException; import sqlj.runtime.ref.DefaultContext; import sqlj.runtime.ConnectionContext; import java.sql.Connection; public class RationalP { #sql static context _Ctx; _Ctx _ctx; /* constructors */ public RationalP() throws SQLException { _ctx = new _Ctx(DefaultContext.getDefaultContext()); } public RationalP(ConnectionContext c) throws SQLException { _ctx = new _Ctx(c); } public RationalP(Connection c) throws SQLException { _ctx = new _Ctx(c); } public Integer gcd ( Integer x, Integer y) throws SQLException { Integer __jPt_result; #sql [_ctx] __jPt_result = { VALUES(RATIONALP.GCD( :x, :y)) }; return __jPt_result; } public void normalize ( Integer numerator[], Integer denominator[]) throws SQLException { #sql [_ctx] { CALL RATIONALP.NORMALIZE( :INOUT (numerator[0]), :INOUT (denominator[0])) }; } public void plus ( Integer n1, Integer d1, Integer n2, Integer d2, Integer n3[], Integer d3[]) throws SQLException { #sql [_ctx] { CALL RATIONALP.PLUS( :n1, :d1, :n2, :d2, :OUT (n3[0]), :OUT (d3[0])) }; } public Float toreal ( Integer numerator, Integer denominator) throws SQLException { Float __jPt_result; #sql [_ctx] __jPt_result = { VALUES(RATIONALP.TOREAL( :numerator, :denominator)) }; return __jPt_result; } }
All of the methods that JPublisher generates invoke the corresponding PL/SQL methods executing in the server.
JPublisher creates a SQLJ connection context _Ctx and associates it with the RationalP package.
JPublisher generates source code for the gcd function, which takes two BigDecimal values x and y, and returns a BigDecimal result. This gcd function invokes the stored function RATIONALP.GCD with IN host variables :x and :y.
JPublisher generates source code for the normalize procedure, which takes two BigDecimal values numerator and denominator. This normalize procedure invokes the stored procedure call RATIONALP.NORMALIZE with IN OUT host variables :numerator and :denominator. Because these are IN OUT parameters, JPublisher passes their values as the first element of an array.
JPublisher generates source code for the plus procedure, which takes four BigDecimal IN parameters and two BigDecimal OUT parameters. This plus procedure invokes the stored procedure call RATIONALP.PLUS, with IN host variables :n1, :d1, :n2, :d2. It also defines the OUT host variables :n3 and :d3. Because these are OUT variables, JPublisher passes their values as the first element of an array.
JPublisher generates source code for the toReal function, which takes two BigDecimal values numerator and denominator and returns a BigDecimal result. This toReal function invokes the stored function call RATIONALP.TOREAL, with IN host variables :numerator and :denominator.