Commit 76a6da8a authored by Bruce Momjian's avatar Bruce Momjian

Attached is a patch to fix the current issues with building under jdbc1.

  This patch moves the logic that looks up TypeOid, PGTypeName, and
SQLTypeName from Field to Connection.  It is moved to connection since
it needs to differ from the jdbc1 to jdbc2 versions and Connection
already has different subclasses for the two driver versions.  It also
made sense to move the logic to Connection as some of the logic was
already there anyway.

Barry Lind
parent 968d7733
...@@ -11,7 +11,7 @@ import org.postgresql.util.*; ...@@ -11,7 +11,7 @@ import org.postgresql.util.*;
import org.postgresql.core.Encoding; import org.postgresql.core.Encoding;
/** /**
* $Id: Connection.java,v 1.25 2001/08/10 14:42:07 momjian Exp $ * $Id: Connection.java,v 1.26 2001/08/24 16:50:12 momjian Exp $
* *
* This abstract class is used by org.postgresql.Driver to open either the JDBC1 or * This abstract class is used by org.postgresql.Driver to open either the JDBC1 or
* JDBC2 versions of the Connection class. * JDBC2 versions of the Connection class.
...@@ -69,11 +69,10 @@ public abstract class Connection ...@@ -69,11 +69,10 @@ public abstract class Connection
// New for 6.3, salt value for crypt authorisation // New for 6.3, salt value for crypt authorisation
private String salt; private String salt;
// This is used by Field to cache oid -> names. // These are used to cache oids, PGTypes and SQLTypes
// It's here, because it's shared across this connection only. private static Hashtable sqlTypeCache = new Hashtable(); // oid -> SQLType
// Hence it cannot be static within the Field class, because it would then private static Hashtable pgTypeCache = new Hashtable(); // oid -> PGType
// be across all connections, which could be to different backends. private static Hashtable typeOidCache = new Hashtable(); //PGType -> oid
public Hashtable fieldCache = new Hashtable();
// Now handle notices as warnings, so things like "show" now work // Now handle notices as warnings, so things like "show" now work
public SQLWarning firstWarning = null; public SQLWarning firstWarning = null;
...@@ -1108,5 +1107,86 @@ public abstract class Connection ...@@ -1108,5 +1107,86 @@ public abstract class Connection
{ {
return (getDBVersionNumber().compareTo(ver) >= 0); return (getDBVersionNumber().compareTo(ver) >= 0);
} }
/**
* This returns the java.sql.Types type for a PG type oid
*
* @param oid PostgreSQL type oid
* @return the java.sql.Types type
* @exception SQLException if a database access error occurs
*/
public int getSQLType(int oid) throws SQLException
{
Integer sqlType = (Integer)typeOidCache.get(new Integer(oid));
// it's not in the cache, so perform a query, and add the result to the cache
if(sqlType==null) {
ResultSet result = (org.postgresql.ResultSet)ExecSQL("select typname from pg_type where oid = " + oid);
if (result.getColumnCount() != 1 || result.getTupleCount() != 1)
throw new PSQLException("postgresql.unexpected");
result.next();
String pgType = result.getString(1);
Integer iOid = new Integer(oid);
sqlType = new Integer(getSQLType(result.getString(1)));
sqlTypeCache.put(iOid,sqlType);
pgTypeCache.put(iOid,pgType);
result.close();
}
return sqlType.intValue();
}
/**
* This returns the java.sql.Types type for a PG type
*
* @param pgTypeName PostgreSQL type name
* @return the java.sql.Types type
*/
public abstract int getSQLType(String pgTypeName);
/**
* This returns the oid for a given PG data type
* @param typeName PostgreSQL type name
* @return PostgreSQL oid value for a field of this type
*/
public int getOID(String typeName) throws SQLException
{
int oid = -1;
if(typeName != null) {
Integer oidValue = (Integer) typeOidCache.get(typeName);
if(oidValue != null) {
oid = oidValue.intValue();
} else {
// it's not in the cache, so perform a query, and add the result to the cache
ResultSet result = (org.postgresql.ResultSet)ExecSQL("select oid from pg_type where typname='"
+ typeName + "'");
if (result.getColumnCount() != 1 || result.getTupleCount() != 1)
throw new PSQLException("postgresql.unexpected");
result.next();
oid = Integer.parseInt(result.getString(1));
typeOidCache.put(typeName, new Integer(oid));
result.close();
}
}
return oid;
}
/**
* We also need to get the PG type name as returned by the back end.
*
* @return the String representation of the type of this field
* @exception SQLException if a database access error occurs
*/
public String getPGType(int oid) throws SQLException
{
String pgType = (String) pgTypeCache.get(new Integer(oid));
if(pgType == null) {
getSQLType(oid);
pgType = (String) pgTypeCache.get(new Integer(oid));
}
return pgType;
}
} }
...@@ -12,17 +12,13 @@ import org.postgresql.util.*; ...@@ -12,17 +12,13 @@ import org.postgresql.util.*;
*/ */
public class Field public class Field
{ {
public int length; // Internal Length of this field private int length; // Internal Length of this field
public int oid; // OID of the type private int oid; // OID of the type
public int mod; // type modifier of this field private int mod; // type modifier of this field
public String name; // Name of this field private String name; // Name of this field
protected Connection conn; // Connection Instantation private 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
private static Hashtable oidCache = new Hashtable();
/** /**
* Construct a field based on the information fed to it. * Construct a field based on the information fed to it.
...@@ -63,140 +59,49 @@ public class Field ...@@ -63,140 +59,49 @@ public class Field
} }
/** /**
* the ResultSet and ResultMetaData both need to handle the SQL * @return the mod of this Field's data type
* 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 public int getMod()
{ {
if(sql_type == -1) { return mod;
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 * @return the name of this Field's data type
* @param type_name PostgreSQL type name
* @return java.sql.Types value for oid
*/ */
public static int getSQLType(String type_name) public String getName()
{ {
int sql_type = Types.OTHER; // default value return name;
for(int i=0;i<types.length;i++)
if(type_name.equals(types[i]))
sql_type=typei[i];
return sql_type;
} }
/** /**
* This returns the oid for a field of a given data type * @return the length of this Field's data type
* @param type_name PostgreSQL type name
* @return PostgreSQL oid value for a field of this type
*/ */
public int getOID( String type_name ) throws SQLException public int getLength()
{ {
int oid = -1; return length;
if(type_name != null) {
Integer oidValue = (Integer) oidCache.get( type_name );
if( oidValue != null )
oid = oidValue.intValue();
else {
// it's not in the cache, so perform a query, and add the result to the cache
ResultSet result = (org.postgresql.ResultSet)conn.ExecSQL("select oid from pg_type where typname='"
+ type_name + "'");
if (result.getColumnCount() != 1 || result.getTupleCount() != 1)
throw new PSQLException("postgresql.unexpected");
result.next();
oid = Integer.parseInt(result.getString(1));
oidCache.put( type_name, new Integer(oid) );
result.close();
}
}
return oid;
} }
/** /**
* This table holds the org.postgresql names for the types supported. * We also need to get the PG type name as returned by the back end.
* Any types that map to Types.OTHER (eg POINT) don't go into this table.
* They default automatically to Types.OTHER
* *
* Note: This must be in the same order as below. * @return the String representation of the PG type of this field
* * @exception SQLException if a database access error occurs
* Tip: keep these grouped together by the Types. value
*/
private static final String types[] = {
"int2",
"int4","oid",
"int8",
"cash","money",
"numeric",
"float4",
"float8",
"bpchar","char","char2","char4","char8","char16",
"varchar","text","name","filename",
"bool",
"date",
"time",
"abstime","timestamp",
"_bool", "_char", "_int2", "_int4", "_text", "_oid", "_varchar", "_int8",
"_float4", "_float8", "_abstime", "_date", "_time", "_timestamp", "_numeric"
};
/**
* This table holds the JDBC type for each entry above.
*
* Note: This must be in the same order as above
*
* Tip: keep these grouped together by the Types. value
*/ */
private static final int typei[] = { public String getPGType() throws SQLException
Types.SMALLINT, {
Types.INTEGER,Types.INTEGER, return conn.getPGType(oid);
Types.BIGINT, }
Types.DOUBLE,Types.DOUBLE,
Types.NUMERIC,
Types.REAL,
Types.DOUBLE,
Types.CHAR,Types.CHAR,Types.CHAR,Types.CHAR,Types.CHAR,Types.CHAR,
Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,
Types.BIT,
Types.DATE,
Types.TIME,
Types.TIMESTAMP,Types.TIMESTAMP,
Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY,
Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY
};
/** /**
* We also need to get the type name as returned by the back end. * We also need to get the java.sql.types type.
* This is held in type_name AFTER a call to getSQLType. Since
* we get this information within getSQLType (if it isn't already
* done), we can just call getSQLType and throw away the result.
* *
* @return the String representation of the type of this field * @return the int representation of the java.sql.types type of this field
* @exception SQLException if a database access error occurs * @exception SQLException if a database access error occurs
*/ */
public String getTypeName() throws SQLException public int getSQLType() throws SQLException
{ {
int sql = getSQLType(); return conn.getSQLType(oid);
return type_name;
} }
} }
...@@ -17,7 +17,7 @@ import org.postgresql.largeobject.*; ...@@ -17,7 +17,7 @@ import org.postgresql.largeobject.*;
import org.postgresql.util.*; import org.postgresql.util.*;
/** /**
* $Id: Connection.java,v 1.7 2001/07/30 14:51:19 momjian Exp $ * $Id: Connection.java,v 1.8 2001/08/24 16:50:15 momjian Exp $
* *
* A Connection represents a session with a specific database. Within the * A Connection represents a session with a specific database. Within the
* context of a Connection, SQL statements are executed and results are * context of a Connection, SQL statements are executed and results are
...@@ -137,6 +137,73 @@ public class Connection extends org.postgresql.Connection implements java.sql.Co ...@@ -137,6 +137,73 @@ public class Connection extends org.postgresql.Connection implements java.sql.Co
return new org.postgresql.jdbc1.ResultSet((org.postgresql.jdbc1.Connection)conn,fields,tuples,status,updateCount,insertOID); return new org.postgresql.jdbc1.ResultSet((org.postgresql.jdbc1.Connection)conn,fields,tuples,status,updateCount,insertOID);
} }
/* An implementation of the abstract method in the parent class.
* This implemetation uses the jdbc1Types array to support the jdbc1
* datatypes. Basically jdbc1 and jdbc2 are the same, except that
* jdbc2 adds the Array types.
*/
public int getSQLType(String pgTypeName)
{
int sqlType = Types.OTHER; // default value
for(int i=0;i<jdbc1Types.length;i++) {
if(pgTypeName.equals(jdbc1Types[i])) {
sqlType=jdbc1Typei[i];
break;
}
}
return sqlType;
}
/**
* This table holds the org.postgresql names for the types supported.
* Any types that map to Types.OTHER (eg POINT) don't go into this table.
* They default automatically to Types.OTHER
*
* Note: This must be in the same order as below.
*
* Tip: keep these grouped together by the Types. value
*/
private static final String jdbc1Types[] = {
"int2",
"int4","oid",
"int8",
"cash","money",
"numeric",
"float4",
"float8",
"bpchar","char","char2","char4","char8","char16",
"varchar","text","name","filename",
"bool",
"date",
"time",
"abstime","timestamp"
};
/**
* This table holds the JDBC type for each entry above.
*
* Note: This must be in the same order as above
*
* Tip: keep these grouped together by the Types. value
*/
private static final int jdbc1Typei[] = {
Types.SMALLINT,
Types.INTEGER,Types.INTEGER,
Types.BIGINT,
Types.DOUBLE,Types.DOUBLE,
Types.NUMERIC,
Types.REAL,
Types.DOUBLE,
Types.CHAR,Types.CHAR,Types.CHAR,Types.CHAR,Types.CHAR,Types.CHAR,
Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,
Types.BIT,
Types.DATE,
Types.TIME,
Types.TIMESTAMP,Types.TIMESTAMP
};
} }
// *********************************************************************** // ***********************************************************************
......
...@@ -1963,7 +1963,7 @@ public class DatabaseMetaData implements java.sql.DatabaseMetaData ...@@ -1963,7 +1963,7 @@ public class DatabaseMetaData implements java.sql.DatabaseMetaData
dr.next(); dr.next();
String typname=dr.getString(1); String typname=dr.getString(1);
dr.close(); dr.close();
tuple[4] = Integer.toString(Field.getSQLType(typname)).getBytes(); // Data type tuple[4] = Integer.toString(connection.getSQLType(typname)).getBytes(); // Data type
tuple[5] = typname.getBytes(); // Type name tuple[5] = typname.getBytes(); // Type name
// Column size // Column size
...@@ -2596,7 +2596,7 @@ public class DatabaseMetaData implements java.sql.DatabaseMetaData ...@@ -2596,7 +2596,7 @@ public class DatabaseMetaData implements java.sql.DatabaseMetaData
byte[][] tuple = new byte[18][]; byte[][] tuple = new byte[18][];
String typname=rs.getString(1); String typname=rs.getString(1);
tuple[0] = typname.getBytes(); tuple[0] = typname.getBytes();
tuple[1] = Integer.toString(Field.getSQLType(typname)).getBytes(); tuple[1] = Integer.toString(connection.getSQLType(typname)).getBytes();
tuple[2] = b9; // for now tuple[2] = b9; // for now
tuple[6] = bnn; // for now tuple[6] = bnn; // for now
tuple[7] = bf; // false for now - not case sensitive tuple[7] = bf; // false for now - not case sensitive
......
...@@ -782,7 +782,7 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu ...@@ -782,7 +782,7 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
case Types.BIGINT: case Types.BIGINT:
return new Long(getLong(columnIndex)); return new Long(getLong(columnIndex));
case Types.NUMERIC: case Types.NUMERIC:
return getBigDecimal(columnIndex, ((field.mod-4) & 0xffff)); return getBigDecimal(columnIndex, ((field.getMod()-4) & 0xffff));
case Types.REAL: case Types.REAL:
return new Float(getFloat(columnIndex)); return new Float(getFloat(columnIndex));
case Types.DOUBLE: case Types.DOUBLE:
...@@ -800,7 +800,7 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu ...@@ -800,7 +800,7 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
case Types.VARBINARY: case Types.VARBINARY:
return getBytes(columnIndex); return getBytes(columnIndex);
default: default:
return connection.getObject(field.getTypeName(), getString(columnIndex)); return connection.getObject(field.getPGType(), getString(columnIndex));
} }
} }
...@@ -836,7 +836,7 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu ...@@ -836,7 +836,7 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
int i; int i;
for (i = 0 ; i < fields.length; ++i) for (i = 0 ; i < fields.length; ++i)
if (fields[i].name.equalsIgnoreCase(columnName)) if (fields[i].getName().equalsIgnoreCase(columnName))
return (i+1); return (i+1);
throw new PSQLException ("postgresql.res.colname",columnName); throw new PSQLException ("postgresql.res.colname",columnName);
} }
......
...@@ -130,7 +130,7 @@ public class ResultSetMetaData implements java.sql.ResultSetMetaData ...@@ -130,7 +130,7 @@ public class ResultSetMetaData implements java.sql.ResultSetMetaData
*/ */
public boolean isCurrency(int column) throws SQLException public boolean isCurrency(int column) throws SQLException
{ {
String type_name = getField(column).getTypeName(); String type_name = getField(column).getPGType();
return type_name.equals("cash") || type_name.equals("money"); return type_name.equals("cash") || type_name.equals("money");
} }
...@@ -189,9 +189,9 @@ public class ResultSetMetaData implements java.sql.ResultSetMetaData ...@@ -189,9 +189,9 @@ public class ResultSetMetaData implements java.sql.ResultSetMetaData
public int getColumnDisplaySize(int column) throws SQLException public int getColumnDisplaySize(int column) throws SQLException
{ {
Field f = getField(column); Field f = getField(column);
String type_name = f.getTypeName(); String type_name = f.getPGType();
int sql_type = f.getSQLType(); int sql_type = f.getSQLType();
int typmod = f.mod; int typmod = f.getMod();
// I looked at other JDBC implementations and couldn't find a consistent // I looked at other JDBC implementations and couldn't find a consistent
// interpretation of the "display size" for numeric values, so this is our's // interpretation of the "display size" for numeric values, so this is our's
...@@ -219,7 +219,7 @@ public class ResultSetMetaData implements java.sql.ResultSetMetaData ...@@ -219,7 +219,7 @@ public class ResultSetMetaData implements java.sql.ResultSetMetaData
+ 1 + ( typmod & 0xffff ); // DECIMAL(p,s) = (p digits).(s digits) + 1 + ( typmod & 0xffff ); // DECIMAL(p,s) = (p digits).(s digits)
// if we don't know better // if we don't know better
return f.length; return f.getLength();
} }
/** /**
...@@ -246,7 +246,7 @@ public class ResultSetMetaData implements java.sql.ResultSetMetaData ...@@ -246,7 +246,7 @@ public class ResultSetMetaData implements java.sql.ResultSetMetaData
{ {
Field f = getField(column); Field f = getField(column);
if(f!=null) if(f!=null)
return f.name; return f.getName();
return "field"+column; return "field"+column;
} }
...@@ -293,7 +293,7 @@ public class ResultSetMetaData implements java.sql.ResultSetMetaData ...@@ -293,7 +293,7 @@ public class ResultSetMetaData implements java.sql.ResultSetMetaData
case Types.NUMERIC: case Types.NUMERIC:
Field f = getField(column); Field f = getField(column);
if(f != null) if(f != null)
return ((0xFFFF0000)&f.mod)>>16; return ((0xFFFF0000)&f.getMod())>>16;
else else
return 0; return 0;
default: default:
...@@ -330,7 +330,7 @@ public class ResultSetMetaData implements java.sql.ResultSetMetaData ...@@ -330,7 +330,7 @@ public class ResultSetMetaData implements java.sql.ResultSetMetaData
case Types.NUMERIC: case Types.NUMERIC:
Field f = getField(column); Field f = getField(column);
if(f != null) if(f != null)
return (((0x0000FFFF)&f.mod)-4); return (((0x0000FFFF)&f.getMod())-4);
else else
return 0; return 0;
default: default:
...@@ -389,7 +389,7 @@ public class ResultSetMetaData implements java.sql.ResultSetMetaData ...@@ -389,7 +389,7 @@ public class ResultSetMetaData implements java.sql.ResultSetMetaData
*/ */
public String getColumnTypeName(int column) throws SQLException public String getColumnTypeName(int column) throws SQLException
{ {
return getField(column).getTypeName(); return getField(column).getPGType();
} }
/** /**
......
...@@ -109,7 +109,7 @@ public class Statement extends org.postgresql.Statement implements java.sql.Stat ...@@ -109,7 +109,7 @@ public class Statement extends org.postgresql.Statement implements java.sql.Stat
public boolean execute(String sql) throws SQLException public boolean execute(String sql) throws SQLException
{ {
if (escapeProcessing) if (escapeProcessing)
sql = escapeSql(sql); sql = escapeSQL(sql);
result = connection.ExecSQL(sql); result = connection.ExecSQL(sql);
return (result != null && ((org.postgresql.ResultSet)result).reallyResultSet()); return (result != null && ((org.postgresql.ResultSet)result).reallyResultSet());
} }
......
...@@ -169,11 +169,11 @@ public class Array implements java.sql.Array ...@@ -169,11 +169,11 @@ public class Array implements java.sql.Array
} }
public int getBaseType() throws SQLException { public int getBaseType() throws SQLException {
return Field.getSQLType( getBaseTypeName() ); return conn.getSQLType(getBaseTypeName());
} }
public String getBaseTypeName() throws SQLException { public String getBaseTypeName() throws SQLException {
String fType = field.getTypeName(); String fType = field.getPGType();
if( fType.charAt(0) == '_' ) if( fType.charAt(0) == '_' )
fType = fType.substring(1); fType = fType.substring(1);
return fType; return fType;
...@@ -195,12 +195,12 @@ public class Array implements java.sql.Array ...@@ -195,12 +195,12 @@ public class Array implements java.sql.Array
Object array = getArray( index, count, map ); Object array = getArray( index, count, map );
Vector rows = new Vector(); Vector rows = new Vector();
Field[] fields = new Field[2]; Field[] fields = new Field[2];
fields[0] = new Field(conn, "INDEX", field.getOID("int2"), 2); fields[0] = new Field(conn, "INDEX", conn.getOID("int2"), 2);
switch ( getBaseType() ) switch ( getBaseType() )
{ {
case Types.BIT: case Types.BIT:
boolean[] booleanArray = (boolean[]) array; boolean[] booleanArray = (boolean[]) array;
fields[1] = new Field(conn, "VALUE", field.getOID("bool"), 1); fields[1] = new Field(conn, "VALUE", conn.getOID("bool"), 1);
for( int i=0; i<booleanArray.length; i++ ) { for( int i=0; i<booleanArray.length; i++ ) {
byte[][] tuple = new byte[2][0]; byte[][] tuple = new byte[2][0];
tuple[0] = conn.getEncoding().encode( Integer.toString((int)index+i) ); // Index tuple[0] = conn.getEncoding().encode( Integer.toString((int)index+i) ); // Index
...@@ -208,11 +208,11 @@ public class Array implements java.sql.Array ...@@ -208,11 +208,11 @@ public class Array implements java.sql.Array
rows.addElement(tuple); rows.addElement(tuple);
} }
case Types.SMALLINT: case Types.SMALLINT:
fields[1] = new Field(conn, "VALUE", field.getOID("int2"), 2); fields[1] = new Field(conn, "VALUE", conn.getOID("int2"), 2);
case Types.INTEGER: case Types.INTEGER:
int[] intArray = (int[]) array; int[] intArray = (int[]) array;
if( fields[1] == null ) if( fields[1] == null )
fields[1] = new Field(conn, "VALUE", field.getOID("int4"), 4); fields[1] = new Field(conn, "VALUE", conn.getOID("int4"), 4);
for( int i=0; i<intArray.length; i++ ) { for( int i=0; i<intArray.length; i++ ) {
byte[][] tuple = new byte[2][0]; byte[][] tuple = new byte[2][0];
tuple[0] = conn.getEncoding().encode( Integer.toString((int)index+i) ); // Index tuple[0] = conn.getEncoding().encode( Integer.toString((int)index+i) ); // Index
...@@ -222,7 +222,7 @@ public class Array implements java.sql.Array ...@@ -222,7 +222,7 @@ public class Array implements java.sql.Array
break; break;
case Types.BIGINT: case Types.BIGINT:
long[] longArray = (long[]) array; long[] longArray = (long[]) array;
fields[1] = new Field(conn, "VALUE", field.getOID("int8"), 8); fields[1] = new Field(conn, "VALUE", conn.getOID("int8"), 8);
for( int i=0; i<longArray.length; i++ ) { for( int i=0; i<longArray.length; i++ ) {
byte[][] tuple = new byte[2][0]; byte[][] tuple = new byte[2][0];
tuple[0] = conn.getEncoding().encode( Integer.toString((int)index+i) ); // Index tuple[0] = conn.getEncoding().encode( Integer.toString((int)index+i) ); // Index
...@@ -232,7 +232,7 @@ public class Array implements java.sql.Array ...@@ -232,7 +232,7 @@ public class Array implements java.sql.Array
break; break;
case Types.NUMERIC: case Types.NUMERIC:
BigDecimal[] bdArray = (BigDecimal[]) array; BigDecimal[] bdArray = (BigDecimal[]) array;
fields[1] = new Field(conn, "VALUE", field.getOID("numeric"), -1); fields[1] = new Field(conn, "VALUE", conn.getOID("numeric"), -1);
for( int i=0; i<bdArray.length; i++ ) { for( int i=0; i<bdArray.length; i++ ) {
byte[][] tuple = new byte[2][0]; byte[][] tuple = new byte[2][0];
tuple[0] = conn.getEncoding().encode( Integer.toString((int)index+i) ); // Index tuple[0] = conn.getEncoding().encode( Integer.toString((int)index+i) ); // Index
...@@ -242,7 +242,7 @@ public class Array implements java.sql.Array ...@@ -242,7 +242,7 @@ public class Array implements java.sql.Array
break; break;
case Types.REAL: case Types.REAL:
float[] floatArray = (float[]) array; float[] floatArray = (float[]) array;
fields[1] = new Field(conn, "VALUE", field.getOID("float4"), 4); fields[1] = new Field(conn, "VALUE", conn.getOID("float4"), 4);
for( int i=0; i<floatArray.length; i++ ) { for( int i=0; i<floatArray.length; i++ ) {
byte[][] tuple = new byte[2][0]; byte[][] tuple = new byte[2][0];
tuple[0] = conn.getEncoding().encode( Integer.toString((int)index+i) ); // Index tuple[0] = conn.getEncoding().encode( Integer.toString((int)index+i) ); // Index
...@@ -252,7 +252,7 @@ public class Array implements java.sql.Array ...@@ -252,7 +252,7 @@ public class Array implements java.sql.Array
break; break;
case Types.DOUBLE: case Types.DOUBLE:
double[] doubleArray = (double[]) array; double[] doubleArray = (double[]) array;
fields[1] = new Field(conn, "VALUE", field.getOID("float8"), 8); fields[1] = new Field(conn, "VALUE", conn.getOID("float8"), 8);
for( int i=0; i<doubleArray.length; i++ ) { for( int i=0; i<doubleArray.length; i++ ) {
byte[][] tuple = new byte[2][0]; byte[][] tuple = new byte[2][0];
tuple[0] = conn.getEncoding().encode( Integer.toString((int)index+i) ); // Index tuple[0] = conn.getEncoding().encode( Integer.toString((int)index+i) ); // Index
...@@ -261,11 +261,11 @@ public class Array implements java.sql.Array ...@@ -261,11 +261,11 @@ public class Array implements java.sql.Array
} }
break; break;
case Types.CHAR: case Types.CHAR:
fields[1] = new Field(conn, "VALUE", field.getOID("char"), 1); fields[1] = new Field(conn, "VALUE", conn.getOID("char"), 1);
case Types.VARCHAR: case Types.VARCHAR:
String[] strArray = (String[]) array; String[] strArray = (String[]) array;
if( fields[1] == null ) if( fields[1] == null )
fields[1] = new Field(conn, "VALUE", field.getOID("varchar"), -1); fields[1] = new Field(conn, "VALUE", conn.getOID("varchar"), -1);
for( int i=0; i<strArray.length; i++ ) { for( int i=0; i<strArray.length; i++ ) {
byte[][] tuple = new byte[2][0]; byte[][] tuple = new byte[2][0];
tuple[0] = conn.getEncoding().encode( Integer.toString((int)index+i) ); // Index tuple[0] = conn.getEncoding().encode( Integer.toString((int)index+i) ); // Index
...@@ -275,7 +275,7 @@ public class Array implements java.sql.Array ...@@ -275,7 +275,7 @@ public class Array implements java.sql.Array
break; break;
case Types.DATE: case Types.DATE:
java.sql.Date[] dateArray = (java.sql.Date[]) array; java.sql.Date[] dateArray = (java.sql.Date[]) array;
fields[1] = new Field(conn, "VALUE", field.getOID("date"), 4); fields[1] = new Field(conn, "VALUE", conn.getOID("date"), 4);
for( int i=0; i<dateArray.length; i++ ) { for( int i=0; i<dateArray.length; i++ ) {
byte[][] tuple = new byte[2][0]; byte[][] tuple = new byte[2][0];
tuple[0] = conn.getEncoding().encode( Integer.toString((int)index+i) ); // Index tuple[0] = conn.getEncoding().encode( Integer.toString((int)index+i) ); // Index
...@@ -285,7 +285,7 @@ public class Array implements java.sql.Array ...@@ -285,7 +285,7 @@ public class Array implements java.sql.Array
break; break;
case Types.TIME: case Types.TIME:
java.sql.Time[] timeArray = (java.sql.Time[]) array; java.sql.Time[] timeArray = (java.sql.Time[]) array;
fields[1] = new Field(conn, "VALUE", field.getOID("time"), 8); fields[1] = new Field(conn, "VALUE", conn.getOID("time"), 8);
for( int i=0; i<timeArray.length; i++ ) { for( int i=0; i<timeArray.length; i++ ) {
byte[][] tuple = new byte[2][0]; byte[][] tuple = new byte[2][0];
tuple[0] = conn.getEncoding().encode( Integer.toString((int)index+i) ); // Index tuple[0] = conn.getEncoding().encode( Integer.toString((int)index+i) ); // Index
...@@ -295,7 +295,7 @@ public class Array implements java.sql.Array ...@@ -295,7 +295,7 @@ public class Array implements java.sql.Array
break; break;
case Types.TIMESTAMP: case Types.TIMESTAMP:
java.sql.Timestamp[] timestampArray = (java.sql.Timestamp[]) array; java.sql.Timestamp[] timestampArray = (java.sql.Timestamp[]) array;
fields[1] = new Field(conn, "VALUE", field.getOID("timestamp"), 8); fields[1] = new Field(conn, "VALUE", conn.getOID("timestamp"), 8);
for( int i=0; i<timestampArray.length; i++ ) { for( int i=0; i<timestampArray.length; i++ ) {
byte[][] tuple = new byte[2][0]; byte[][] tuple = new byte[2][0];
tuple[0] = conn.getEncoding().encode( Integer.toString((int)index+i) ); // Index tuple[0] = conn.getEncoding().encode( Integer.toString((int)index+i) ); // Index
......
...@@ -17,7 +17,7 @@ import org.postgresql.largeobject.*; ...@@ -17,7 +17,7 @@ import org.postgresql.largeobject.*;
import org.postgresql.util.*; import org.postgresql.util.*;
/** /**
* $Id: Connection.java,v 1.9 2001/07/30 14:51:19 momjian Exp $ * $Id: Connection.java,v 1.10 2001/08/24 16:50:16 momjian Exp $
* *
* A Connection represents a session with a specific database. Within the * A Connection represents a session with a specific database. Within the
* context of a Connection, SQL statements are executed and results are * context of a Connection, SQL statements are executed and results are
...@@ -254,6 +254,77 @@ public class Connection extends org.postgresql.Connection implements java.sql.Co ...@@ -254,6 +254,77 @@ public class Connection extends org.postgresql.Connection implements java.sql.Co
// Default to the original method // Default to the original method
return super.getObject(type,value); return super.getObject(type,value);
} }
/* An implementation of the abstract method in the parent class.
* This implemetation uses the jdbc2Types array to support the jdbc2
* datatypes. Basically jdbc1 and jdbc2 are the same, except that
* jdbc2 adds the Array types.
*/
public int getSQLType(String pgTypeName)
{
int sqlType = Types.OTHER; // default value
for(int i=0;i<jdbc2Types.length;i++) {
if(pgTypeName.equals(jdbc2Types[i])) {
sqlType=jdbc2Typei[i];
break;
}
}
return sqlType;
}
/**
* This table holds the org.postgresql names for the types supported.
* Any types that map to Types.OTHER (eg POINT) don't go into this table.
* They default automatically to Types.OTHER
*
* Note: This must be in the same order as below.
*
* Tip: keep these grouped together by the Types. value
*/
private static final String jdbc2Types[] = {
"int2",
"int4","oid",
"int8",
"cash","money",
"numeric",
"float4",
"float8",
"bpchar","char","char2","char4","char8","char16",
"varchar","text","name","filename",
"bool",
"date",
"time",
"abstime","timestamp",
"_bool", "_char", "_int2", "_int4", "_text", "_oid", "_varchar", "_int8",
"_float4", "_float8", "_abstime", "_date", "_time", "_timestamp", "_numeric"
};
/**
* This table holds the JDBC type for each entry above.
*
* Note: This must be in the same order as above
*
* Tip: keep these grouped together by the Types. value
*/
private static final int jdbc2Typei[] = {
Types.SMALLINT,
Types.INTEGER,Types.INTEGER,
Types.BIGINT,
Types.DOUBLE,Types.DOUBLE,
Types.NUMERIC,
Types.REAL,
Types.DOUBLE,
Types.CHAR,Types.CHAR,Types.CHAR,Types.CHAR,Types.CHAR,Types.CHAR,
Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,
Types.BIT,
Types.DATE,
Types.TIME,
Types.TIMESTAMP,Types.TIMESTAMP,
Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY,
Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY
};
} }
// *********************************************************************** // ***********************************************************************
......
...@@ -1963,7 +1963,7 @@ public class DatabaseMetaData implements java.sql.DatabaseMetaData ...@@ -1963,7 +1963,7 @@ public class DatabaseMetaData implements java.sql.DatabaseMetaData
dr.next(); dr.next();
String typname=dr.getString(1); String typname=dr.getString(1);
dr.close(); dr.close();
tuple[4] = Integer.toString(Field.getSQLType(typname)).getBytes(); // Data type tuple[4] = Integer.toString(connection.getSQLType(typname)).getBytes(); // Data type
tuple[5] = typname.getBytes(); // Type name tuple[5] = typname.getBytes(); // Type name
// Column size // Column size
...@@ -2600,7 +2600,7 @@ public class DatabaseMetaData implements java.sql.DatabaseMetaData ...@@ -2600,7 +2600,7 @@ public class DatabaseMetaData implements java.sql.DatabaseMetaData
byte[][] tuple = new byte[18][]; byte[][] tuple = new byte[18][];
String typname=rs.getString(1); String typname=rs.getString(1);
tuple[0] = typname.getBytes(); tuple[0] = typname.getBytes();
tuple[1] = Integer.toString(Field.getSQLType(typname)).getBytes(); tuple[1] = Integer.toString(connection.getSQLType(typname)).getBytes();
tuple[2] = b9; // for now tuple[2] = b9; // for now
tuple[6] = bnn; // for now tuple[6] = bnn; // for now
tuple[7] = bf; // false for now - not case sensitive tuple[7] = bf; // false for now - not case sensitive
......
...@@ -657,7 +657,7 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu ...@@ -657,7 +657,7 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
return new Long(getLong(columnIndex)); return new Long(getLong(columnIndex));
case Types.NUMERIC: case Types.NUMERIC:
return getBigDecimal return getBigDecimal
(columnIndex, (field.mod==-1)?-1:((field.mod-4) & 0xffff)); (columnIndex, (field.getMod()==-1)?-1:((field.getMod()-4) & 0xffff));
case Types.REAL: case Types.REAL:
return new Float(getFloat(columnIndex)); return new Float(getFloat(columnIndex));
case Types.DOUBLE: case Types.DOUBLE:
...@@ -675,7 +675,7 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu ...@@ -675,7 +675,7 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
case Types.VARBINARY: case Types.VARBINARY:
return getBytes(columnIndex); return getBytes(columnIndex);
default: default:
return connection.getObject(field.getTypeName(), getString(columnIndex)); return connection.getObject(field.getPGType(), getString(columnIndex));
} }
} }
...@@ -711,7 +711,7 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu ...@@ -711,7 +711,7 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
int i; int i;
for (i = 0 ; i < fields.length; ++i) for (i = 0 ; i < fields.length; ++i)
if (fields[i].name.equalsIgnoreCase(columnName)) if (fields[i].getName().equalsIgnoreCase(columnName))
return (i+1); return (i+1);
throw new PSQLException ("postgresql.res.colname",columnName); throw new PSQLException ("postgresql.res.colname",columnName);
} }
......
...@@ -125,7 +125,7 @@ public class ResultSetMetaData implements java.sql.ResultSetMetaData ...@@ -125,7 +125,7 @@ public class ResultSetMetaData implements java.sql.ResultSetMetaData
*/ */
public boolean isCurrency(int column) throws SQLException public boolean isCurrency(int column) throws SQLException
{ {
String type_name = getField(column).getTypeName(); String type_name = getField(column).getPGType();
return type_name.equals("cash") || type_name.equals("money"); return type_name.equals("cash") || type_name.equals("money");
} }
...@@ -184,9 +184,9 @@ public class ResultSetMetaData implements java.sql.ResultSetMetaData ...@@ -184,9 +184,9 @@ public class ResultSetMetaData implements java.sql.ResultSetMetaData
public int getColumnDisplaySize(int column) throws SQLException public int getColumnDisplaySize(int column) throws SQLException
{ {
Field f = getField(column); Field f = getField(column);
String type_name = f.getTypeName(); String type_name = f.getPGType();
int sql_type = f.getSQLType(); int sql_type = f.getSQLType();
int typmod = f.mod; int typmod = f.getMod();
// I looked at other JDBC implementations and couldn't find a consistent // I looked at other JDBC implementations and couldn't find a consistent
// interpretation of the "display size" for numeric values, so this is our's // interpretation of the "display size" for numeric values, so this is our's
...@@ -214,7 +214,7 @@ public class ResultSetMetaData implements java.sql.ResultSetMetaData ...@@ -214,7 +214,7 @@ public class ResultSetMetaData implements java.sql.ResultSetMetaData
+ 1 + ( typmod & 0xffff ); // DECIMAL(p,s) = (p digits).(s digits) + 1 + ( typmod & 0xffff ); // DECIMAL(p,s) = (p digits).(s digits)
// if we don't know better // if we don't know better
return f.length; return f.getLength();
} }
/** /**
...@@ -241,7 +241,7 @@ public class ResultSetMetaData implements java.sql.ResultSetMetaData ...@@ -241,7 +241,7 @@ public class ResultSetMetaData implements java.sql.ResultSetMetaData
{ {
Field f = getField(column); Field f = getField(column);
if(f!=null) if(f!=null)
return f.name; return f.getName();
return "field"+column; return "field"+column;
} }
...@@ -288,7 +288,7 @@ public class ResultSetMetaData implements java.sql.ResultSetMetaData ...@@ -288,7 +288,7 @@ public class ResultSetMetaData implements java.sql.ResultSetMetaData
case Types.NUMERIC: case Types.NUMERIC:
Field f = getField(column); Field f = getField(column);
if(f != null) if(f != null)
return ((0xFFFF0000)&f.mod)>>16; return ((0xFFFF0000)&f.getMod())>>16;
else else
return 0; return 0;
default: default:
...@@ -325,7 +325,7 @@ public class ResultSetMetaData implements java.sql.ResultSetMetaData ...@@ -325,7 +325,7 @@ public class ResultSetMetaData implements java.sql.ResultSetMetaData
case Types.NUMERIC: case Types.NUMERIC:
Field f = getField(column); Field f = getField(column);
if(f != null) if(f != null)
return (((0x0000FFFF)&f.mod)-4); return (((0x0000FFFF)&f.getMod())-4);
else else
return 0; return 0;
default: default:
...@@ -384,7 +384,7 @@ public class ResultSetMetaData implements java.sql.ResultSetMetaData ...@@ -384,7 +384,7 @@ public class ResultSetMetaData implements java.sql.ResultSetMetaData
*/ */
public String getColumnTypeName(int column) throws SQLException public String getColumnTypeName(int column) throws SQLException
{ {
return getField(column).getTypeName(); return getField(column).getPGType();
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment