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

## Example: JPublisher Type Mapping

This section describes an example of JPublisher output for a variety of object attribute types. The example demonstrates the various type mappings that JPublisher creates.

The example defines an address object (`address`) and then uses it as the basis of the definition of an address array (`Addr_Array`). The `alltypes` object definition also uses the address and address array objects to demonstrate the mappings that JPublisher creates for `REF`s and arrays (see `attr17`, `attr18`, and `attr19` in the `alltypes` object definition below).

    connect scott/tiger
    CREATE OR REPLACE TYPE address AS object
    (
      street varchar2(50),
      city   varchar2(50),
      state  varchar2(30),
      zip    number
    );

    CREATE OR REPLACE TYPE Addr_Array AS varray(10) OF address;
    CREATE OR REPLACE TYPE ntbl AS table OF Integer;
    CREATE TYPE alltypes AS object (
      attr1  bfile,
      attr2  blob,
      attr3  char(10),
      attr4  clob,
      attr5  date,
      attr6  decimal,
      attr7  double precision,
      attr8  float,
      attr9  integer,
      attr10 number,
      attr11 numeric,
      attr12 raw(20),
      attr13 real,
      attr14 smallint,
      attr15 varchar(10),
      attr16 varchar2(10),
      attr17 address,
      attr18 ref address,
      attr19 Addr_Array,
      attr20 ntbl);

In this example, invoke JPublisher with the following command line:

    jpub -user=scott/tiger -input=demoin -dir=demo -package=corp -mapping=objectjdbc
    -methods=false

It is not necessary to create the `demo` and `corp` directories in advance. JPublisher will create the directories for you.

The `demoin` file contains these declarations:

    SQL ADDRESS GENERATE Address
    SQL ALLTYPES AS all.Alltypes

JPublisher generates declarations of the types `Alltypes` and `Address,` because `demoin` explicitly lists them. It also generates declarations of the types `ntbl` and `AddrArray,` because the `Alltypes` type requires them.

Additionally, JPublisher generates declarations of the types `AlltypesRef` and `AddressRef,` because it generates a declaration of a `REF` type for each object type. A `REF` type is in the same package as the corresponding object type. `REF` types are not listed in the `INPUT` file or on the command line. The `Address` and `AddressRef` types are in package `corp,` because `-package=corp` appears on the command line. The `Alltypes` and `AlltypesRef` types are in package `all,` because the `all` in `all.Alltypes` overrides `-package=corp`. The remaining types were not explicitly mentioned, so they go in package `corp,` which was specified on the command line.

Therefore, JPublisher creates the following files in package `corp`:

    ./demo/corp/Address.java
    ./demo/corp/AddressRef.java
    ./demo/corp/Ntbl.java
    ./demo/corp/AddrArray.java

and the following files in package `all`:

    ./demo/all/Alltypes.java
    ./demo/all/AlltypesRef.java

### JPublisher Type Mapping Example Output

This section lists the contents of the files JPublisher produces.

#### Listing and Description of Address.java Generated by JPublisher

The file `./demo/corp/Address.java` reads as follows:

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;

    public class Address implements CustomDatum, CustomDatumFactory
    {
      public static final String _SQL_NAME = "SCOTT.ADDRESS";
      public static final int _SQL_TYPECODE = OracleTypes.STRUCT;

      MutableStruct _struct;

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

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

      static final Address _AddressFactory = new Address();
      public static CustomDatumFactory getFactory()
      {
        return _AddressFactory;
      }

      /* constructor */
      public Address()
      {
        _struct = new MutableStruct(new Object[4], _sqlType, _factory);
      }

      /* CustomDatum interface */
      public Datum toDatum(OracleConnection c) throws SQLException
      {
        return _struct.toDatum(c, _SQL_NAME);
      }

      /* CustomDatumFactory interface */
      public CustomDatum create(Datum d, int sqlType) throws SQLException
      {
        if (d == null) return null;
        Address o = new Address();
        o._struct = new MutableStruct((STRUCT) d, _sqlType, _factory);
        return o;
      }

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

      public void setStreet(String street) throws SQLException
      { _struct.setAttribute(0, street); }

      public String getCity() throws SQLException
      { return (String) _struct.getAttribute(1); }

      public void setCity(String city) throws SQLException
      { _struct.setAttribute(1, city); }

      public String getState() throws SQLException
      { return (String) _struct.getAttribute(2); }

      public void setState(String state) throws SQLException
      { _struct.setAttribute(2, state); }

      public java.math.BigDecimal getZip() throws SQLException
      { return (java.math.BigDecimal) _struct.getAttribute(3); }

      public void setZip(java.math.BigDecimal zip) throws SQLException
      { _struct.setAttribute(3, zip); }

    }

The `Address.java` file illustrates several points. Java source files JPublisher generates begin with a package declaration whenever the generated class is in a named package. Note that you can specify a package in any of these ways:

- `-package` parameter that you specify on the command line or in the properties file

- `AS` <Java identifier> clause in the `INPUT` file, where Java identifier includes a package name

Import declarations for specific classes and interfaces mentioned by the `Address` class follow the `package` declaration.

The class definition follows the `import` declarations. All classes JPublisher generates are declared `public`.

SQLJ uses the `_SQL_NAME` and `_SQL_TYPECODE` strings to identify the SQL type matching the `Address` class.

The no-argument constructor is used to create the `_AddressFactory` object, which will be returned by `getFactory()`. Other `Address` objects are constructed by the `create()` method. The static `_factory` field is an array of factories for the attributes of `Address`, because none of the attribute types of `Address` require a factory:

    _factory[0] == _factory[1] == null.

The actual data is stored in the `MutableStruct` `_struct`.

The `toDatum()` method converts an `Address` to a `Datum` (in this case, a `STRUCT`). JDBC requires the connection argument, although it might not be logically necessary.

The `get` and `set` accessor methods use the `objectjdbc` mapping for numeric attributes and the `jdbc` mapping for other attributes. The method names are in mixed case because `-case=mixed` is the default.

#### Listing and Description of AddressRef.java Generated by JPublisher

The file `./demo/corp/AddressRef.java` reads as follows:

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.REF;
    import oracle.sql.STRUCT;

    public class AddressRef implements CustomDatum, CustomDatumFactory
    {
      public static final String _SQL_BASETYPE = "SCOTT.ADDRESS";
      public static final int _SQL_TYPECODE = OracleTypes.REF;

      REF _ref;

      static final AddressRef _AddressRefFactory = new AddressRef();
      public static CustomDatumFactory getFactory()
      {
        return _AddressRefFactory;
      }

      /* constructor */
      public AddressRef()
      {
      }

      /* CustomDatum interface */
      public Datum toDatum(OracleConnection c) throws SQLException
      {
        return _ref;
      }

      /* CustomDatumFactory interface */
      public CustomDatum create(Datum d, int sqlType) throws SQLException
      {
        if (d == null) return null;
        AddressRef r = new AddressRef();
        r._ref = (REF) d;
        return r;
      }
      public Address getValue() throws SQLException
      {
         return (Address) Address.getFactory().create(
           _ref.getSTRUCT(), OracleTypes.REF);
      }

      public void setValue(Address c) throws SQLException
      {
        _ref.setValue((STRUCT) c.toDatum(_ref.getConnection()));
      }
    }

The `getValue` method in the `AddressRef` class returns the address referenced by an `AddressRef`, with its proper type. The `setValue()` method copies the contents of the `Address` argument into the database `Address` object to which the `AddressRef` refers.

#### Listing and Description of Alltypes.java Generated by JPublisher

The file `./demo/all/Alltypes.java` reads as follows:

    Note:

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

    package all;

    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;

    public class Alltypes implements CustomDatum, CustomDatumFactory
    {
      public static final String _SQL_NAME = "SCOTT.ALLTYPES";
      public static final int _SQL_TYPECODE = OracleTypes.STRUCT;

      MutableStruct _struct;

      static int[] _sqlType =
      {
        -13, 2004, 1, 2005, 91, 3, 8, 6, 4, 2,
        3, -2, 7, 5, 12, 12, 2002, 2006, 2003, 2003
      };

      static CustomDatumFactory[] _factory = new CustomDatumFactory[20];
      static
      {
        _factory[16] = corp.Address.getFactory();
        _factory[17] = corp.AddressRef.getFactory();
        _factory[18] = corp.AddrArray.getFactory();
        _factory[19] = corp.Ntbl.getFactory();
      }

      static final Alltypes _AlltypesFactory = new Alltypes();
      public static CustomDatumFactory getFactory()
      {
        return _AlltypesFactory;
      }

      /* constructor */
      public Alltypes()
      {
        _struct = new MutableStruct(new Object[20], _sqlType, _factory);
      }

      /* CustomDatum interface */
      public Datum toDatum(OracleConnection c) throws SQLException
      {
        return _struct.toDatum(c, _SQL_NAME);
      }

      /* CustomDatumFactory interface */
      public CustomDatum create(Datum d, int sqlType) throws SQLException
      {
        if (d == null) return null;
        Alltypes o = new Alltypes();
        o._struct = new MutableStruct((STRUCT) d, _sqlType, _factory);
        return o;
      }

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

      public void setAttr1(oracle.sql.BFILE attr1) throws SQLException
      { _struct.setOracleAttribute(0, attr1); }

      public oracle.sql.BLOB getAttr2() throws SQLException
      { return (oracle.sql.BLOB) _struct.getOracleAttribute(1); }

      public void setAttr2(oracle.sql.BLOB attr2) throws SQLException
      { _struct.setOracleAttribute(1, attr2); }

      public String getAttr3() throws SQLException
      { return (String) _struct.getAttribute(2); }

      public void setAttr3(String attr3) throws SQLException
      { _struct.setAttribute(2, attr3); }

      public oracle.sql.CLOB getAttr4() throws SQLException
      { return (oracle.sql.CLOB) _struct.getOracleAttribute(3); }

      public void setAttr4(oracle.sql.CLOB attr4) throws SQLException
      { _struct.setOracleAttribute(3, attr4); }

      public java.sql.Timestamp getAttr5() throws SQLException
      { return (java.sql.Timestamp) _struct.getAttribute(4); }

      public void setAttr5(java.sql.Timestamp attr5) throws SQLException
      { _struct.setAttribute(4, attr5); }

      public java.math.BigDecimal getAttr6() throws SQLException
      { return (java.math.BigDecimal) _struct.getAttribute(5); }

      public void setAttr6(java.math.BigDecimal attr6) throws SQLException
      { _struct.setAttribute(5, attr6); }

      public Double getAttr7() throws SQLException
      { return (Double) _struct.getAttribute(6); }

      public void setAttr7(Double attr7) throws SQLException
      { _struct.setAttribute(6, attr7); }

      public Double getAttr8() throws SQLException
      { return (Double) _struct.getAttribute(7); }

      public void setAttr8(Double attr8) throws SQLException
      { _struct.setAttribute(7, attr8); }

      public Integer getAttr9() throws SQLException
      { return (Integer) _struct.getAttribute(8); }

      public void setAttr9(Integer attr9) throws SQLException
      { _struct.setAttribute(8, attr9); }

      public java.math.BigDecimal getAttr10() throws SQLException
      { return (java.math.BigDecimal) _struct.getAttribute(9); }

      public void setAttr10(java.math.BigDecimal attr10) throws SQLException
      { _struct.setAttribute(9, attr10); }

      public java.math.BigDecimal getAttr11() throws SQLException
      { return (java.math.BigDecimal) _struct.getAttribute(10); }

      public void setAttr11(java.math.BigDecimal attr11) throws SQLException
      { _struct.setAttribute(10, attr11); }

      public byte[] getAttr12() throws SQLException
      { return (byte[]) _struct.getAttribute(11); }

      public void setAttr12(byte[] attr12) throws SQLException
      { _struct.setAttribute(11, attr12); }

      public Float getAttr13() throws SQLException
      { return (Float) _struct.getAttribute(12); }

      public void setAttr13(Float attr13) throws SQLException
      { _struct.setAttribute(12, attr13); }

      public Integer getAttr14() throws SQLException
      { return (Integer) _struct.getAttribute(13); }

      public void setAttr14(Integer attr14) throws SQLException
      { _struct.setAttribute(13, attr14); }

      public String getAttr15() throws SQLException
      { return (String) _struct.getAttribute(14); }

      public void setAttr15(String attr15) throws SQLException
      { _struct.setAttribute(14, attr15); }

      public String getAttr16() throws SQLException
      { return (String) _struct.getAttribute(15); }

      public void setAttr16(String attr16) throws SQLException
      { _struct.setAttribute(15, attr16); }

      public corp.Address getAttr17() throws SQLException
      { return (corp.Address) _struct.getAttribute(16); }

      public void setAttr17(corp.Address attr17) throws SQLException
      { _struct.setAttribute(16, attr17); }

      public corp.AddressRef getAttr18() throws SQLException
      { return (corp.AddressRef) _struct.getAttribute(17); }

      public void setAttr18(corp.AddressRef attr18) throws SQLException
      { _struct.setAttribute(17, attr18); }

      public corp.AddrArray getAttr19() throws SQLException
      { return (corp.AddrArray) _struct.getAttribute(18); }

      public void setAttr19(corp.AddrArray attr19) throws SQLException
      { _struct.setAttribute(18, attr19); }

      public corp.Ntbl getAttr20() throws SQLException
      { return (corp.Ntbl) _struct.getAttribute(19); }

      public void setAttr20(corp.Ntbl attr20) throws SQLException
      { _struct.setAttribute(19, attr20); }

    }

When a declared class requires user-defined classes from another package, JPublisher generates `import` declarations for those user-defined classes following the `import` declaration for `oracle.sql`. In this case, JDBC requires the `Address` and `AddressRef` classes from `package` `corp`.

The attributes with types `Address`, `AddressRef`, `AddrArray`, and `Ntbl` require the construction of factories. The static block puts the correct factories in the `_factory` array.

#### Listing and Description of AlltypesRef.java Generated by JPublisher

The file `./demo/corp/all/AlltypesRef.java` reads as follows:

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

    package all;

    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.REF;
    import oracle.sql.STRUCT;

    public class AlltypesRef implements CustomDatum, CustomDatumFactory
    {
      public static final String _SQL_BASETYPE = "SCOTT.ALLTYPES";
      public static final int _SQL_TYPECODE = OracleTypes.REF;

      REF _ref;

      static final AlltypesRef _AlltypesRefFactory = new AlltypesRef();
      public static CustomDatumFactory getFactory()
      {
        return _AlltypesRefFactory;
      }

      /* constructor */
      public AlltypesRef()
      {
      }

      /* CustomDatum interface */
      public Datum toDatum(OracleConnection c) throws SQLException
      {
        return _ref;
      }

      /* CustomDatumFactory interface */
      public CustomDatum create(Datum d, int sqlType) throws SQLException
      {
        if (d == null) return null;
        AlltypesRef r = new AlltypesRef();
        r._ref = (REF) d;
        return r;
      }
      public Alltypes getValue() throws SQLException
      {
         return (Alltypes) Alltypes.getFactory().create(
           _ref.getSTRUCT(), OracleTypes.REF);
      }

      public void setValue(Alltypes c) throws SQLException
      {
        _ref.setValue((STRUCT) c.toDatum(_ref.getConnection()));
      }
    }

#### Listing and Description of Ntbl.java Generated by JPublisher

The file `./demo/corp/Ntbl.java` reads as follows:

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.ARRAY;
    import oracle.sql.ArrayDescriptor;
    import oracle.jpub.runtime.MutableArray;

    public class Ntbl implements CustomDatum, CustomDatumFactory
    {
      public static final String _SQL_NAME = "SCOTT.NTBL";
      public static final int _SQL_TYPECODE = OracleTypes.ARRAY;

      MutableArray _array;

      static final Ntbl _NtblFactory = new Ntbl();
      public static CustomDatumFactory getFactory()
      {
        return _NtblFactory;
      }

      /* constructors */
      public Ntbl()
      {
        this((Integer[])null);
      }

      public Ntbl(Integer[] a)
      {
        _array = new MutableArray(a, 4, null);
      }

      /* CustomDatum interface */
      public Datum toDatum(OracleConnection c) throws SQLException
      {
        return _array.toDatum(c, _SQL_NAME);
      }

      /* CustomDatumFactory interface */
      public CustomDatum create(Datum d, int sqlType) throws SQLException
      {
        if (d == null) return null;
        Ntbl a = new Ntbl();
        a._array = new MutableArray((ARRAY) d, 4, null);
        return a;
      }

      public int length() throws SQLException
      {
        return _array.length();
      }

      public int getBaseType() throws SQLException
      {
        return _array.getBaseType();
      }

      public String getBaseTypeName() throws SQLException
      {
        return _array.getBaseTypeName();
      }

      public ArrayDescriptor getDescriptor() throws SQLException
      {
        return _array.getDescriptor();
      }

      /* array accessor methods */
      public Integer[] getArray() throws SQLException
      {
        return (Integer[]) _array.getObjectArray();
      }

      public void setArray(Integer[] a) throws SQLException
      {
        _array.setObjectArray(a);
      }

      public Integer[] getArray(long index, int count) throws SQLException
      {
        return (Integer[]) _array.getObjectArray(index, count);
      }

      public void setArray(Integer[] a, long index) throws SQLException
      {
        _array.setObjectArray(a, index);
      }

      public Integer getElement(long index) throws SQLException
      {
        return (Integer) _array.getObjectElement(index);
      }

      public void setElement(Integer a, long index) throws SQLException
      {
        _array.setObjectElement(a, index);
      }

    }

#### Listing and Description of AddrArray.java Generated by JPublisher

JPublisher generates declarations of the types `AddrArray,` because they are required by the `Alltypes` type. The file `./demo/corp/AddrArray.java` reads as follows:

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.ARRAY;
    import oracle.sql.ArrayDescriptor;
    import oracle.jpub.runtime.MutableArray;

    public class AddrArray implements CustomDatum, CustomDatumFactory
    {
      public static final String _SQL_NAME = "SCOTT.ADDR_ARRAY";
      public static final int _SQL_TYPECODE = OracleTypes.ARRAY;

      MutableArray _array;

      static final AddrArray _AddrArrayFactory = new AddrArray();
      public static CustomDatumFactory getFactory()
      {
        return _AddrArrayFactory;
      }

      /* constructors */
      public AddrArray()
      {
        this((Address[])null);
      }

      public AddrArray(Address[] a)
      {
        _array = new MutableArray(a, 2002, Address.getFactory());
      }

      /* CustomDatum interface */
      public Datum toDatum(OracleConnection c) throws SQLException
      {
        return _array.toDatum(c, _SQL_NAME);
      }

      /* CustomDatumFactory interface */
      public CustomDatum create(Datum d, int sqlType) throws SQLException
      {
        if (d == null) return null;
        AddrArray a = new AddrArray();
        a._array = new MutableArray((ARRAY) d, 2002, Address.getFactory());
        return a;
      }

      public int length() throws SQLException
      {
        return _array.length();
      }

      public int getBaseType() throws SQLException
      {
        return _array.getBaseType();
      }

      public String getBaseTypeName() throws SQLException
      {
        return _array.getBaseTypeName();
      }

      public ArrayDescriptor getDescriptor() throws SQLException
      {
        return _array.getDescriptor();
      }

      /* array accessor methods */
      public Address[] getArray() throws SQLException
      {
        return (Address[]) _array.getObjectArray(
          new Address[_array.length()]);
      }

      public void setArray(Address[] a) throws SQLException
      {
        _array.setObjectArray(a);
      }

      public Address[] getArray(long index, int count) throws SQLException
      {
        return (Address[]) _array.getObjectArray(index,
          new Address[_array.sliceLength(index, count)]);
      }

      public void setArray(Address[] a, long index) throws SQLException
      {
        _array.setObjectArray(a, index);
      }

      public Address getElement(long index) throws SQLException
      {
        return (Address) _array.getObjectElement(index);
      }

      public void setElement(Address a, long index) throws SQLException
      {
        _array.setObjectElement(a, index);
      }

    }

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