Field.java
上传用户:psq1974
上传日期:2007-01-06
资源大小:1195k
文件大小:4k
源码类别:

mpeg/mp3

开发平台:

C/C++

  1. package postgresql;
  2. import java.lang.*;
  3. import java.sql.*;
  4. import java.util.*;
  5. import postgresql.*;
  6. /**
  7.  * postgresql.Field is a class used to describe fields in a PostgreSQL
  8.  * ResultSet
  9.  */
  10. public class Field
  11. {
  12.   int length; // Internal Length of this field
  13.   int oid; // OID of the type
  14.   Connection conn; // Connection Instantation
  15.   String name; // Name of this field
  16.   
  17.   int sql_type = -1; // The entry in java.sql.Types for this field
  18.   String type_name = null;// The sql type name
  19.   
  20.   /**
  21.    * Construct a field based on the information fed to it.
  22.    *
  23.    * @param conn the connection this field came from
  24.    * @param name the name of the field
  25.    * @param oid the OID of the field
  26.    * @param len the length of the field
  27.    */
  28.   public Field(Connection conn, String name, int oid, int length)
  29.   {
  30.     this.conn = conn;
  31.     this.name = name;
  32.     this.oid = oid;
  33.     this.length = length;
  34.   }
  35.   
  36.   /**
  37.    * @return the oid of this Field's data type
  38.    */
  39.   public int getOID()
  40.   {
  41.     return oid;
  42.   }
  43.   
  44.   /**
  45.    * the ResultSet and ResultMetaData both need to handle the SQL
  46.    * type, which is gained from another query.  Note that we cannot
  47.    * use getObject() in this, since getObject uses getSQLType().
  48.    *
  49.    * @return the entry in Types that refers to this field
  50.    * @exception SQLException if a database access error occurs
  51.    */
  52.   public int getSQLType() throws SQLException
  53.   {
  54.     if(sql_type == -1) {
  55.       type_name = (String)conn.fieldCache.get(new Integer(oid));
  56.       
  57.       // it's not in the cache, so perform a query, and add the result to
  58.       // the cache
  59.       if(type_name==null) {
  60. ResultSet result = (postgresql.ResultSet)conn.ExecSQL("select typname from pg_type where oid = " + oid);
  61. if (result.getColumnCount() != 1 || result.getTupleCount() != 1)
  62.   throw new SQLException("Unexpected return from query for type");
  63. result.next();
  64. type_name = result.getString(1);
  65. conn.fieldCache.put(new Integer(oid),type_name);
  66. result.close();
  67.       }
  68.       
  69.       sql_type = getSQLType(type_name);
  70.     }
  71.     return sql_type;
  72.   }
  73.   
  74.   /**
  75.    * This returns the SQL type. It is called by the Field and DatabaseMetaData classes
  76.    * @param type_name PostgreSQL type name
  77.    * @return java.sql.Types value for oid
  78.    */
  79.   public static int getSQLType(String type_name)
  80.   {
  81.     int sql_type = Types.OTHER; // default value
  82.     for(int i=0;i<types.length;i++)
  83.       if(type_name.equals(types[i]))
  84. sql_type=typei[i];
  85.     return sql_type;
  86.   }
  87.   
  88.   /**
  89.    * This table holds the postgresql names for the types supported.
  90.    * Any types that map to Types.OTHER (eg POINT) don't go into this table.
  91.    * They default automatically to Types.OTHER
  92.    *
  93.    * Note: This must be in the same order as below.
  94.    *
  95.    * Tip: keep these grouped together by the Types. value
  96.    */
  97.   private static final String types[] = {
  98.     "int2",
  99.     "int4","oid",
  100.     "int8",
  101.     "cash","money",
  102.     "float4",
  103.     "float8",
  104.     "bpchar","char","char2","char4","char8","char16",
  105.     "varchar","text","name","filename",
  106.     "bool",
  107.     "date",
  108.     "time",
  109.     "abstime","timestamp"
  110.   };
  111.   
  112.   /**
  113.    * This table holds the JDBC type for each entry above.
  114.    *
  115.    * Note: This must be in the same order as above
  116.    *
  117.    * Tip: keep these grouped together by the Types. value
  118.    */
  119.   private static final int typei[] = {
  120.     Types.SMALLINT,
  121.     Types.INTEGER,Types.INTEGER,
  122.     Types.BIGINT,
  123.     Types.DECIMAL,Types.DECIMAL,
  124.     Types.REAL,
  125.     Types.DOUBLE,
  126.     Types.CHAR,Types.CHAR,Types.CHAR,Types.CHAR,Types.CHAR,Types.CHAR,
  127.     Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,
  128.     Types.BIT,
  129.     Types.DATE,
  130.     Types.TIME,
  131.     Types.TIMESTAMP,Types.TIMESTAMP
  132.   };
  133.   
  134.   /**
  135.    * We also need to get the type name as returned by the back end.
  136.    * This is held in type_name AFTER a call to getSQLType.  Since
  137.    * we get this information within getSQLType (if it isn't already
  138.    * done), we can just call getSQLType and throw away the result.
  139.    *
  140.    * @return the String representation of the type of this field
  141.    * @exception SQLException if a database access error occurs
  142.    */
  143.   public String getTypeName() throws SQLException
  144.   {
  145.     int sql = getSQLType();
  146.     return type_name;
  147.   }
  148. }