From 4fc3690238d03b8d0e4cc73934b188f3a3943afe Mon Sep 17 00:00:00 2001 From: Peter Mount Date: Wed, 26 Apr 2000 05:39:32 +0000 Subject: Attempt III --- src/interfaces/jdbc/org/postgresql/Field.java | 169 ++++++++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 src/interfaces/jdbc/org/postgresql/Field.java (limited to 'src/interfaces/jdbc/org/postgresql/Field.java') diff --git a/src/interfaces/jdbc/org/postgresql/Field.java b/src/interfaces/jdbc/org/postgresql/Field.java new file mode 100644 index 0000000000..b73c224e51 --- /dev/null +++ b/src/interfaces/jdbc/org/postgresql/Field.java @@ -0,0 +1,169 @@ +package org.postgresql; + +import java.lang.*; +import java.sql.*; +import java.util.*; +import org.postgresql.*; +import org.postgresql.util.*; + +/** + * org.postgresql.Field is a class used to describe fields in a PostgreSQL + * ResultSet + */ +public class Field +{ + public int length; // Internal Length of this field + public int oid; // OID of the type + public int mod; // type modifier of this field + public String name; // Name of this field + + protected Connection conn; // Connection Instantation + + public int sql_type = -1; // The entry in java.sql.Types for this field + public String type_name = null;// The sql type name + + /** + * Construct a field based on the information fed to it. + * + * @param conn the connection this field came from + * @param name the name of the field + * @param oid the OID of the field + * @param len the length of the field + */ + public Field(Connection conn, String name, int oid, int length,int mod) + { + this.conn = conn; + this.name = name; + this.oid = oid; + this.length = length; + this.mod = mod; + } + + /** + * Constructor without mod parameter. + * + * @param conn the connection this field came from + * @param name the name of the field + * @param oid the OID of the field + * @param len the length of the field + */ + public Field(Connection conn, String name, int oid, int length) + { + this(conn,name,oid,length,0); + } + + /** + * @return the oid of this Field's data type + */ + public int getOID() + { + return oid; + } + + /** + * the ResultSet and ResultMetaData both need to handle the SQL + * type, which is gained from another query. Note that we cannot + * use getObject() in this, since getObject uses getSQLType(). + * + * @return the entry in Types that refers to this field + * @exception SQLException if a database access error occurs + */ + public int getSQLType() throws SQLException + { + if(sql_type == -1) { + type_name = (String)conn.fieldCache.get(new Integer(oid)); + + // it's not in the cache, so perform a query, and add the result to + // the cache + if(type_name==null) { + ResultSet result = (org.postgresql.ResultSet)conn.ExecSQL("select typname from pg_type where oid = " + oid); + if (result.getColumnCount() != 1 || result.getTupleCount() != 1) + throw new PSQLException("postgresql.unexpected"); + result.next(); + type_name = result.getString(1); + conn.fieldCache.put(new Integer(oid),type_name); + result.close(); + } + + sql_type = getSQLType(type_name); + } + return sql_type; + } + + /** + * This returns the SQL type. It is called by the Field and DatabaseMetaData classes + * @param type_name PostgreSQL type name + * @return java.sql.Types value for oid + */ + public static int getSQLType(String type_name) + { + int sql_type = Types.OTHER; // default value + for(int i=0;i