Create an opaque type using support functions you supply
In this example, the Java™ class Circle2 on
the client is mapped to an SQL opaque type named circle2. The circle2 opaque
type uses support functions provided by the programmer.
import java.sql.*;
import java.text.*;
import com.informix.jdbc.IfmxUDTSQLInput;
import com.informix.jdbc.IfmxUDTSQLOutput;
public class Circle2 implements SQLData
{
private static double PI = 3.14159;
double x; // x coordinate
double y; // y coordinate
double radius;
private String type = "circle2";
public String getSQLTypeName() { return type; }
public void readSQL(SQLInput stream, String typeName)
throws SQLException
{
/* commented out - because the first release of the UDT/UDR Manager feature
* does not support mixing user-supplied support functions
* with server DEFAULT support functions.
* However, once the mix is supported, this code needs to be used to
* replace the existing code.
*
// To be able to use the DEFAULT support functions (other than
// Input/Output) supplied by the server, you must cast the stream
// to IfmxUDTSQLInput.
IfmxUDTSQLInput in = (IfmxUDTSQLInput) stream;
x = in.readDouble();
y = in.readDouble();
radius = in.readDouble();
*/
x = stream.readDouble();
y = stream.readDouble();
radius = stream.readDouble();
}
public void writeSQL(SQLOutput stream) throws SQLException
{
/* commented out - because the 1st release of UDT/UDR Manager feature
* doesn't support the mixing of user support functions
* with server DEFAULT support functions.
* However, once the mix is supported, this code needs to be used to
* replace the existing code.
*
// To be able to use the DEFAULT support functions (other than
// Input/Output) supplied by the server, you must cast the stream
// to IfmxUDTSQLOutput.
IfmxUDTSQLOutput out = (IfmxUDTSQLOutput) stream;
out.writeDouble(x);
out.writeDouble(y);
out.writeDouble(radius);
*/
stream.writeDouble(x);
stream.writeDouble(y);
stream.writeDouble(radius);
}
/**
* Input function - return the object from the String representation -
* 'x y radius'.
*/
public static Circle2 fromString(String text)
{
Number a = null;
Number b = null;
Number r = null;
try
{
ParsePosition ps = new ParsePosition(0);
a = NumberFormat.getInstance().parse(text, ps);
ps.setIndex(ps.getIndex() + 1);
b = NumberFormat.getInstance().parse(text, ps);
ps.setIndex(ps.getIndex() + 1);
r = NumberFormat.getInstance().parse(text, ps);
}
catch (Exception e)
{
System.out.println("In exception : " + e.getMessage());
}
Circle2 c = new Circle2();
c.x = a.doubleValue();
c.y = b.doubleValue();
c.radius = r.doubleValue();
return c;
}
/**
* Output function - return the string of the form 'x y radius'.
*/
public static String makeString(Circle2 c)
{
StringBuffer sbuff = new StringBuffer();
FieldPosition fp = new FieldPosition(NumberFormat.INTEGER_FIELD);
NumberFormat.getInstance().format(c.x, sbuff, fp);
sbuff.append(" ");
NumberFormat.getInstance().format(c.y, sbuff, fp);
sbuff.append(" ");
NumberFormat.getInstance().format(c.radius, sbuff, fp);
return sbuff.toString();
}
/**
* user function - get the area of a circle.
*/
public static double area(Circle2 c)
{
return PI * c.radius * c.radius;
}
}