Oracle8i JPublisher User's Guide Release 2 (8.1.6) Part Number A81357-01 |
Library | Service | Contents | Index

## Example: JPublisher Translations with Different Mappings

This section presents sample output from JPublisher with the only difference in the translations being the value of the type mapping parameters. This section uses the following SQL type declaration presented in "Sample JPublisher Translation":

    CREATE TYPE employee AS OBJECT
    (
        name       VARCHAR2(30),
        empno      INTEGER,
        deptno     NUMBER,
        hiredate   DATE,
        salary     REAL
    );

with the original command line:

    jpub -user=scott/tiger -dir=demo -numbertypes=objectjdbc -builtintypes=jdbc
    -package=corp -case=mixed -sql=Employee

In the example on page 1-10, the types were translated with `-numbertypes=objectjdbc` and `-builtintypes=jdbc`.

In the following two examples, JPublisher translates the types using different type mapping options: first, with `-numbertypes=jdbc,` and second with `-numbertypes=oracle` and `-builtintypes=oracle`.

### JPublisher Translation with the JDBC mapping

The SQL program presented in "Sample JPublisher Translation" is translated here by JPublisher with `-numbertypes=jdbc`. No other changes have been made to the command line.

Because the user requests the JDBC mapping rather than the Object JDBC mapping for numeric types, the `get` and `set` accessor methods use the type `int` instead of `Integer` and the type `float` instead of `Float`.

Following are the contents of the `Employee.sqlj` file. The `EmployeeRef.java` file is unchanged because it does not depend on the types of the attributes.

Note: The details of method bodies generated by JPublisher might change in future releases.

    package corp;

    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 Employee implements CustomDatum, CustomDatumFactory
    {
      public static final String _SQL_NAME = "SCOTT.EMPLOYEE";
      public static final int _SQL_TYPECODE = OracleTypes.STRUCT;

      #sql static context _Ctx;
      _Ctx _ctx;

      MutableStruct _struct;

      static int[] _sqlType =
      {
        12, 4, 2, 91, 7
      };

      static CustomDatumFactory[] _factory = new CustomDatumFactory[5];

      static final Employee _EmployeeFactory = new Employee();
      public static CustomDatumFactory getFactory()
      {
        return _EmployeeFactory;
      }

      /* constructors */
      public Employee()
      {
        _struct = new MutableStruct(new Object[5], _sqlType, _factory);
        try
        {
          _ctx = new _Ctx(DefaultContext.getDefaultContext());
        }
        catch (Exception e)
        {
          _ctx = null;
        }
      }

      public Employee(ConnectionContext c) throws SQLException
      {
        _struct = new MutableStruct(new Object[5], _sqlType, _factory);
        _ctx = new _Ctx(c == null ? DefaultContext.getDefaultContext()
                                  : c);
      }
      public Employee(Connection c) throws SQLException
      {
        _struct = new MutableStruct(new Object[5], _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;
        Employee o = new Employee();
        o._struct = new MutableStruct((STRUCT) d, _sqlType, _factory);
        o._ctx = new _Ctx(((STRUCT) d).getConnection());
        return o;
      }

      /* accessor methods */
      public String getName() throws SQLException
      { return (String) _struct.getAttribute(0); }

      public void setName(String name) throws SQLException
      { _struct.setAttribute(0, name); }

      public int getEmpno() throws SQLException
      { return ((Integer) _struct.getAttribute(1)).intValue(); }

      public void setEmpno(int empno) throws SQLException
      { _struct.setAttribute(1, new Integer(empno)); }

      public java.math.BigDecimal getDeptno() throws SQLException
      { return (java.math.BigDecimal) _struct.getAttribute(2); }

      public void setDeptno(java.math.BigDecimal deptno) throws SQLException
      { _struct.setAttribute(2, deptno); }

      public java.sql.Timestamp getHiredate() throws SQLException
      { return (java.sql.Timestamp) _struct.getAttribute(3); }

      public void setHiredate(java.sql.Timestamp hiredate) throws SQLException
      { _struct.setAttribute(3, hiredate); }

      public float getSalary() throws SQLException
      { return ((Float) _struct.getAttribute(4)).floatValue(); }

      public void setSalary(float salary) throws SQLException
      { _struct.setAttribute(4, new Float(salary)); }

    }

### JPublisher Translation with the Oracle mapping

The SQL program presented in "Sample JPublisher Translation" is translated here by JPublisher with `-numbertypes=oracle` and `-builtintypes=oracle`.`` No other changes have been made to the command line.

Because the user requests Oracle type mappings, the `get` and `set` accessor methods employ the type `oracle.sql.CHAR` instead of `String`, the type `oracle.sql.DATE` instead of `java.sql.Timestamp`, and the type `oracle.sql.NUMBER` instead of `Integer`, `java.math.BigDecimal`, and `Float`.

Following are the contents of the `Employee.sqlj` file. The `EmployeeRef.java` file is unchanged, because it does not depend on the types of the attributes.

Note: The details of method bodies that JPublisher generates might change in future releases.

    package corp;

    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 Employee implements CustomDatum, CustomDatumFactory
    {
      public static final String _SQL_NAME = "SCOTT.EMPLOYEE";
      public static final int _SQL_TYPECODE = OracleTypes.STRUCT;

      #sql static context _Ctx;
      _Ctx _ctx;

      MutableStruct _struct;

      static int[] _sqlType =
      {
        12, 4, 2, 91, 7
      };

      static CustomDatumFactory[] _factory = new CustomDatumFactory[5];

      static final Employee _EmployeeFactory = new Employee();
      public static CustomDatumFactory getFactory()
      {
        return _EmployeeFactory;
      }

      /* constructors */
      public Employee()
      {
        _struct = new MutableStruct(new Object[5], _sqlType, _factory);
        try
        {
          _ctx = new _Ctx(DefaultContext.getDefaultContext());
        }
        catch (Exception e)
        {
          _ctx = null;
        }
      }

      public Employee(ConnectionContext c) throws SQLException
      {
        _struct = new MutableStruct(new Object[5], _sqlType, _factory);
        _ctx = new _Ctx(c == null ? DefaultContext.getDefaultContext()
                                  : c);
      }
      public Employee(Connection c) throws SQLException
      {
        _struct = new MutableStruct(new Object[5], _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;
        Employee o = new Employee();
        o._struct = new MutableStruct((STRUCT) d, _sqlType, _factory);
        o._ctx = new _Ctx(((STRUCT) d).getConnection());
        return o;
      }

      /* accessor methods */
      public oracle.sql.CHAR getName() throws SQLException
      { return (oracle.sql.CHAR) _struct.getOracleAttribute(0); }

      public void setName(oracle.sql.CHAR name) throws SQLException
      { _struct.setOracleAttribute(0, name); }

      public oracle.sql.NUMBER getEmpno() throws SQLException
      { return (oracle.sql.NUMBER) _struct.getOracleAttribute(1); }

      public void setEmpno(oracle.sql.NUMBER empno) throws SQLException
      { _struct.setOracleAttribute(1, empno); }

      public oracle.sql.NUMBER getDeptno() throws SQLException
      { return (oracle.sql.NUMBER) _struct.getOracleAttribute(2); }

      public void setDeptno(oracle.sql.NUMBER deptno) throws SQLException
      { _struct.setOracleAttribute(2, deptno); }

      public oracle.sql.DATE getHiredate() throws SQLException
      { return (oracle.sql.DATE) _struct.getOracleAttribute(3); }

      public void setHiredate(oracle.sql.DATE hiredate) throws SQLException
      { _struct.setOracleAttribute(3, hiredate); }

      public oracle.sql.NUMBER getSalary() throws SQLException
      { return (oracle.sql.NUMBER) _struct.getOracleAttribute(4); }

      public void setSalary(oracle.sql.NUMBER salary) throws SQLException
      { _struct.setOracleAttribute(4, salary); }

    }

###

| Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
Library | Service | Contents | Index