1. /*
  2. * @(#)SQLData.java 1.19 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.sql;
  8. /**
  9. * The interface used for the custom mapping of an SQL user-defined type (UDT) to
  10. * a class in the Java programming language. The class object for a class
  11. * implementing the <code>SQLData</code> interface will be entered in the
  12. * appropriate <code>Connection</code> object's type map along with the SQL
  13. * name of the UDT for which it is a custom mapping.
  14. * <P>
  15. * Typically, a <code>SQLData</code> implementation
  16. * will define a field for each attribute of an SQL structured type or a
  17. * single field for an SQL <code>DISTINCT</code> type. When the UDT is
  18. * retrieved from a data source with the <code>ResultSet.getObject</code>
  19. * method, it will be mapped as an instance of this class. A programmer
  20. * can operate on this class instance just as on any other object in the
  21. * Java programming language and then store any changes made to it by
  22. * calling the <code>PreparedStatement.setObject</code> method,
  23. * which will map it back to the SQL type.
  24. * <p>
  25. * It is expected that the implementation of the class for a custom
  26. * mapping will be done by a tool. In a typical implementation, the
  27. * programmer would simply supply the name of the SQL UDT, the name of
  28. * the class to which it is being mapped, and the names of the fields to
  29. * which each of the attributes of the UDT is to be mapped. The tool will use
  30. * this information to implement the <code>SQLData.readSQL</code> and
  31. * <code>SQLData.writeSQL</code> methods. The <code>readSQL</code> method
  32. * calls the appropriate <code>SQLInput</code> methods to read
  33. * each attribute from an <code>SQLInput</code> object, and the
  34. * <code>writeSQL</code> method calls <code>SQLOutput</code> methods
  35. * to write each attribute back to the data source via an
  36. * <code>SQLOutput</code> object.
  37. * <P>
  38. * An application programmer will not normally call <code>SQLData</code> methods
  39. * directly, and the <code>SQLInput</code> and <code>SQLOutput</code> methods
  40. * are called internally by <code>SQLData</code> methods, not by application code.
  41. *
  42. * @since 1.2
  43. */
  44. public interface SQLData {
  45. /**
  46. * Returns the fully-qualified
  47. * name of the SQL user-defined type that this object represents.
  48. * This method is called by the JDBC driver to get the name of the
  49. * UDT instance that is being mapped to this instance of
  50. * <code>SQLData</code>.
  51. *
  52. * @return the type name that was passed to the method <code>readSql</code>
  53. * when this object was constructed and populated
  54. * @exception SQLException if there is a database access error
  55. * @since 1.2
  56. */
  57. String getSQLTypeName() throws SQLException;
  58. /**
  59. * Populates this object with data read from the database.
  60. * The implementation of the method must follow this protocol:
  61. * <UL>
  62. * <LI>It must read each of the attributes or elements of the SQL
  63. * type from the given input stream. This is done
  64. * by calling a method of the input stream to read each
  65. * item, in the order that they appear in the SQL definition
  66. * of the type.
  67. * <LI>The method <code>readSQL</code> then
  68. * assigns the data to appropriate fields or
  69. * elements (of this or other objects).
  70. * Specifically, it must call the appropriate <i>reader</i> method
  71. * (<code>SQLInput.readString</code>, <code>SQLInput.readBigDecimal</code>,
  72. * and so on) method(s) to do the following:
  73. * for a distinct type, read its single data element;
  74. * for a structured type, read a value for each attribute of the SQL type.
  75. * </UL>
  76. * The JDBC driver initializes the input stream with a type map
  77. * before calling this method, which is used by the appropriate
  78. * <code>SQLInput</code> reader method on the stream.
  79. *
  80. * @param stream the <code>SQLInput</code> object from which to read the data for
  81. * the value that is being custom mapped
  82. * @param typeName the SQL type name of the value on the data stream
  83. * @exception SQLException if there is a database access error
  84. * @see SQLInput
  85. */
  86. void readSQL (SQLInput stream, String typeName) throws SQLException;
  87. /**
  88. * Writes this object to the given SQL data stream, converting it back to
  89. * its SQL value in the data source.
  90. * The implementation of the method must follow this protocol:<BR>
  91. * It must write each of the attributes of the SQL type
  92. * to the given output stream. This is done by calling a
  93. * method of the output stream to write each item, in the order that
  94. * they appear in the SQL definition of the type.
  95. * Specifically, it must call the appropriate <code>SQLOutput</code> writer
  96. * method(s) (<code>writeInt</code>, <code>writeString</code>, and so on)
  97. * to do the following: for a Distinct Type, write its single data element;
  98. * for a Structured Type, write a value for each attribute of the SQL type.
  99. *
  100. * @param stream the <code>SQLOutput</code> object to which to write the data for
  101. * the value that was custom mapped
  102. * @exception SQLException if there is a database access error
  103. * @see SQLOutput
  104. * @since 1.2
  105. */
  106. void writeSQL (SQLOutput stream) throws SQLException;
  107. }