Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 76a6da8

Browse files
committed
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
1 parent 968d773 commit 76a6da8

12 files changed

+294
-171
lines changed

src/interfaces/jdbc/org/postgresql/Connection.java

Lines changed: 86 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import org.postgresql.core.Encoding;
1212

1313
/**
14-
* $Id: Connection.java,v 1.25 2001/08/10 14:42:07 momjian Exp $
14+
* $Id: Connection.java,v 1.26 2001/08/24 16:50:12 momjian Exp $
1515
*
1616
* This abstract class is used by org.postgresql.Driver to open either the JDBC1 or
1717
* JDBC2 versions of the Connection class.
@@ -69,11 +69,10 @@ public abstract class Connection
6969
// New for 6.3, salt value for crypt authorisation
7070
private String salt;
7171

72-
// This is used by Field to cache oid -> names.
73-
// It's here, because it's shared across this connection only.
74-
// Hence it cannot be static within the Field class, because it would then
75-
// be across all connections, which could be to different backends.
76-
public Hashtable fieldCache = new Hashtable();
72+
// These are used to cache oids, PGTypes and SQLTypes
73+
private static Hashtable sqlTypeCache = new Hashtable(); // oid -> SQLType
74+
private static Hashtable pgTypeCache = new Hashtable(); // oid -> PGType
75+
private static Hashtable typeOidCache = new Hashtable(); //PGType -> oid
7776

7877
// Now handle notices as warnings, so things like "show" now work
7978
public SQLWarning firstWarning = null;
@@ -1108,5 +1107,86 @@ public boolean haveMinimumServerVersion(String ver) throws SQLException
11081107
{
11091108
return (getDBVersionNumber().compareTo(ver) >= 0);
11101109
}
1110+
1111+
1112+
/**
1113+
* This returns the java.sql.Types type for a PG type oid
1114+
*
1115+
* @param oid PostgreSQL type oid
1116+
* @return the java.sql.Types type
1117+
* @exception SQLException if a database access error occurs
1118+
*/
1119+
public int getSQLType(int oid) throws SQLException
1120+
{
1121+
Integer sqlType = (Integer)typeOidCache.get(new Integer(oid));
1122+
1123+
// it's not in the cache, so perform a query, and add the result to the cache
1124+
if(sqlType==null) {
1125+
ResultSet result = (org.postgresql.ResultSet)ExecSQL("select typname from pg_type where oid = " + oid);
1126+
if (result.getColumnCount() != 1 || result.getTupleCount() != 1)
1127+
throw new PSQLException("postgresql.unexpected");
1128+
result.next();
1129+
String pgType = result.getString(1);
1130+
Integer iOid = new Integer(oid);
1131+
sqlType = new Integer(getSQLType(result.getString(1)));
1132+
sqlTypeCache.put(iOid,sqlType);
1133+
pgTypeCache.put(iOid,pgType);
1134+
result.close();
1135+
}
1136+
1137+
return sqlType.intValue();
1138+
}
1139+
1140+
/**
1141+
* This returns the java.sql.Types type for a PG type
1142+
*
1143+
* @param pgTypeName PostgreSQL type name
1144+
* @return the java.sql.Types type
1145+
*/
1146+
public abstract int getSQLType(String pgTypeName);
1147+
1148+
/**
1149+
* This returns the oid for a given PG data type
1150+
* @param typeName PostgreSQL type name
1151+
* @return PostgreSQL oid value for a field of this type
1152+
*/
1153+
public int getOID(String typeName) throws SQLException
1154+
{
1155+
int oid = -1;
1156+
if(typeName != null) {
1157+
Integer oidValue = (Integer) typeOidCache.get(typeName);
1158+
if(oidValue != null) {
1159+
oid = oidValue.intValue();
1160+
} else {
1161+
// it's not in the cache, so perform a query, and add the result to the cache
1162+
ResultSet result = (org.postgresql.ResultSet)ExecSQL("select oid from pg_type where typname='"
1163+
+ typeName + "'");
1164+
if (result.getColumnCount() != 1 || result.getTupleCount() != 1)
1165+
throw new PSQLException("postgresql.unexpected");
1166+
result.next();
1167+
oid = Integer.parseInt(result.getString(1));
1168+
typeOidCache.put(typeName, new Integer(oid));
1169+
result.close();
1170+
}
1171+
}
1172+
return oid;
1173+
}
1174+
1175+
/**
1176+
* We also need to get the PG type name as returned by the back end.
1177+
*
1178+
* @return the String representation of the type of this field
1179+
* @exception SQLException if a database access error occurs
1180+
*/
1181+
public String getPGType(int oid) throws SQLException
1182+
{
1183+
String pgType = (String) pgTypeCache.get(new Integer(oid));
1184+
if(pgType == null) {
1185+
getSQLType(oid);
1186+
pgType = (String) pgTypeCache.get(new Integer(oid));
1187+
}
1188+
return pgType;
1189+
}
1190+
11111191
}
11121192

src/interfaces/jdbc/org/postgresql/Field.java

Lines changed: 26 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,13 @@
1212
*/
1313
public class Field
1414
{
15-
public int length; // Internal Length of this field
16-
public int oid; // OID of the type
17-
public int mod; // type modifier of this field
18-
public String name; // Name of this field
15+
private int length; // Internal Length of this field
16+
private int oid; // OID of the type
17+
private int mod; // type modifier of this field
18+
private String name; // Name of this field
1919

20-
protected Connection conn; // Connection Instantation
20+
private Connection conn; // Connection Instantation
2121

22-
public int sql_type = -1; // The entry in java.sql.Types for this field
23-
public String type_name = null;// The sql type name
24-
25-
private static Hashtable oidCache = new Hashtable();
2622

2723
/**
2824
* Construct a field based on the information fed to it.
@@ -63,140 +59,49 @@ public int getOID()
6359
}
6460

6561
/**
66-
* the ResultSet and ResultMetaData both need to handle the SQL
67-
* type, which is gained from another query. Note that we cannot
68-
* use getObject() in this, since getObject uses getSQLType().
69-
*
70-
* @return the entry in Types that refers to this field
71-
* @exception SQLException if a database access error occurs
62+
* @return the mod of this Field's data type
7263
*/
73-
public int getSQLType() throws SQLException
64+
public int getMod()
7465
{
75-
if(sql_type == -1) {
76-
type_name = (String)conn.fieldCache.get(new Integer(oid));
77-
78-
// it's not in the cache, so perform a query, and add the result to
79-
// the cache
80-
if(type_name==null) {
81-
ResultSet result = (org.postgresql.ResultSet)conn.ExecSQL("select typname from pg_type where oid = " + oid);
82-
if (result.getColumnCount() != 1 || result.getTupleCount() != 1)
83-
throw new PSQLException("postgresql.unexpected");
84-
result.next();
85-
type_name = result.getString(1);
86-
conn.fieldCache.put(new Integer(oid),type_name);
87-
result.close();
88-
}
89-
90-
sql_type = getSQLType(type_name);
91-
}
92-
return sql_type;
66+
return mod;
9367
}
9468

9569
/**
96-
* This returns the SQL type. It is called by the Field and DatabaseMetaData classes
97-
* @param type_name PostgreSQL type name
98-
* @return java.sql.Types value for oid
70+
* @return the name of this Field's data type
9971
*/
100-
public static int getSQLType(String type_name)
72+
public String getName()
10173
{
102-
int sql_type = Types.OTHER; // default value
103-
for(int i=0;i<types.length;i++)
104-
if(type_name.equals(types[i]))
105-
sql_type=typei[i];
106-
return sql_type;
74+
return name;
10775
}
10876

10977
/**
110-
* This returns the oid for a field of a given data type
111-
* @param type_name PostgreSQL type name
112-
* @return PostgreSQL oid value for a field of this type
78+
* @return the length of this Field's data type
11379
*/
114-
public int getOID( String type_name ) throws SQLException
80+
public int getLength()
11581
{
116-
int oid = -1;
117-
if(type_name != null) {
118-
Integer oidValue = (Integer) oidCache.get( type_name );
119-
if( oidValue != null )
120-
oid = oidValue.intValue();
121-
else {
122-
// it's not in the cache, so perform a query, and add the result to the cache
123-
ResultSet result = (org.postgresql.ResultSet)conn.ExecSQL("select oid from pg_type where typname='"
124-
+ type_name + "'");
125-
if (result.getColumnCount() != 1 || result.getTupleCount() != 1)
126-
throw new PSQLException("postgresql.unexpected");
127-
result.next();
128-
oid = Integer.parseInt(result.getString(1));
129-
oidCache.put( type_name, new Integer(oid) );
130-
result.close();
131-
}
132-
}
133-
return oid;
82+
return length;
13483
}
13584

13685
/**
137-
* This table holds the org.postgresql names for the types supported.
138-
* Any types that map to Types.OTHER (eg POINT) don't go into this table.
139-
* They default automatically to Types.OTHER
140-
*
141-
* Note: This must be in the same order as below.
142-
*
143-
* Tip: keep these grouped together by the Types. value
144-
*/
145-
private static final String types[] = {
146-
"int2",
147-
"int4","oid",
148-
"int8",
149-
"cash","money",
150-
"numeric",
151-
"float4",
152-
"float8",
153-
"bpchar","char","char2","char4","char8","char16",
154-
"varchar","text","name","filename",
155-
"bool",
156-
"date",
157-
"time",
158-
"abstime","timestamp",
159-
"_bool", "_char", "_int2", "_int4", "_text", "_oid", "_varchar", "_int8",
160-
"_float4", "_float8", "_abstime", "_date", "_time", "_timestamp", "_numeric"
161-
};
162-
163-
/**
164-
* This table holds the JDBC type for each entry above.
86+
* We also need to get the PG type name as returned by the back end.
16587
*
166-
* Note: This must be in the same order as above
167-
*
168-
* Tip: keep these grouped together by the Types. value
88+
* @return the String representation of the PG type of this field
89+
* @exception SQLException if a database access error occurs
16990
*/
170-
private static final int typei[] = {
171-
Types.SMALLINT,
172-
Types.INTEGER,Types.INTEGER,
173-
Types.BIGINT,
174-
Types.DOUBLE,Types.DOUBLE,
175-
Types.NUMERIC,
176-
Types.REAL,
177-
Types.DOUBLE,
178-
Types.CHAR,Types.CHAR,Types.CHAR,Types.CHAR,Types.CHAR,Types.CHAR,
179-
Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,
180-
Types.BIT,
181-
Types.DATE,
182-
Types.TIME,
183-
Types.TIMESTAMP,Types.TIMESTAMP,
184-
Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY,
185-
Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY
186-
};
91+
public String getPGType() throws SQLException
92+
{
93+
return conn.getPGType(oid);
94+
}
18795

18896
/**
189-
* We also need to get the type name as returned by the back end.
190-
* This is held in type_name AFTER a call to getSQLType. Since
191-
* we get this information within getSQLType (if it isn't already
192-
* done), we can just call getSQLType and throw away the result.
97+
* We also need to get the java.sql.types type.
19398
*
194-
* @return the String representation of the type of this field
99+
* @return the int representation of the java.sql.types type of this field
195100
* @exception SQLException if a database access error occurs
196101
*/
197-
public String getTypeName() throws SQLException
102+
public int getSQLType() throws SQLException
198103
{
199-
int sql = getSQLType();
200-
return type_name;
104+
return conn.getSQLType(oid);
201105
}
106+
202107
}

src/interfaces/jdbc/org/postgresql/jdbc1/Connection.java

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import org.postgresql.util.*;
1818

1919
/**
20-
* $Id: Connection.java,v 1.7 2001/07/30 14:51:19 momjian Exp $
20+
* $Id: Connection.java,v 1.8 2001/08/24 16:50:15 momjian Exp $
2121
*
2222
* A Connection represents a session with a specific database. Within the
2323
* context of a Connection, SQL statements are executed and results are
@@ -137,6 +137,73 @@ protected java.sql.ResultSet getResultSet(org.postgresql.Connection conn,java.sq
137137
return new org.postgresql.jdbc1.ResultSet((org.postgresql.jdbc1.Connection)conn,fields,tuples,status,updateCount,insertOID);
138138
}
139139

140+
141+
/* An implementation of the abstract method in the parent class.
142+
* This implemetation uses the jdbc1Types array to support the jdbc1
143+
* datatypes. Basically jdbc1 and jdbc2 are the same, except that
144+
* jdbc2 adds the Array types.
145+
*/
146+
public int getSQLType(String pgTypeName)
147+
{
148+
int sqlType = Types.OTHER; // default value
149+
for(int i=0;i<jdbc1Types.length;i++) {
150+
if(pgTypeName.equals(jdbc1Types[i])) {
151+
sqlType=jdbc1Typei[i];
152+
break;
153+
}
154+
}
155+
return sqlType;
156+
}
157+
158+
/**
159+
* This table holds the org.postgresql names for the types supported.
160+
* Any types that map to Types.OTHER (eg POINT) don't go into this table.
161+
* They default automatically to Types.OTHER
162+
*
163+
* Note: This must be in the same order as below.
164+
*
165+
* Tip: keep these grouped together by the Types. value
166+
*/
167+
private static final String jdbc1Types[] = {
168+
"int2",
169+
"int4","oid",
170+
"int8",
171+
"cash","money",
172+
"numeric",
173+
"float4",
174+
"float8",
175+
"bpchar","char","char2","char4","char8","char16",
176+
"varchar","text","name","filename",
177+
"bool",
178+
"date",
179+
"time",
180+
"abstime","timestamp"
181+
};
182+
183+
/**
184+
* This table holds the JDBC type for each entry above.
185+
*
186+
* Note: This must be in the same order as above
187+
*
188+
* Tip: keep these grouped together by the Types. value
189+
*/
190+
private static final int jdbc1Typei[] = {
191+
Types.SMALLINT,
192+
Types.INTEGER,Types.INTEGER,
193+
Types.BIGINT,
194+
Types.DOUBLE,Types.DOUBLE,
195+
Types.NUMERIC,
196+
Types.REAL,
197+
Types.DOUBLE,
198+
Types.CHAR,Types.CHAR,Types.CHAR,Types.CHAR,Types.CHAR,Types.CHAR,
199+
Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,
200+
Types.BIT,
201+
Types.DATE,
202+
Types.TIME,
203+
Types.TIMESTAMP,Types.TIMESTAMP
204+
};
205+
206+
140207
}
141208

142209
// ***********************************************************************

src/interfaces/jdbc/org/postgresql/jdbc1/DatabaseMetaData.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,7 +1963,7 @@ public java.sql.ResultSet getColumns(String catalog, String schemaPattern, Strin
19631963
dr.next();
19641964
String typname=dr.getString(1);
19651965
dr.close();
1966-
tuple[4] = Integer.toString(Field.getSQLType(typname)).getBytes(); // Data type
1966+
tuple[4] = Integer.toString(connection.getSQLType(typname)).getBytes(); // Data type
19671967
tuple[5] = typname.getBytes(); // Type name
19681968

19691969
// Column size
@@ -2596,7 +2596,7 @@ public java.sql.ResultSet getTypeInfo() throws SQLException
25962596
byte[][] tuple = new byte[18][];
25972597
String typname=rs.getString(1);
25982598
tuple[0] = typname.getBytes();
2599-
tuple[1] = Integer.toString(Field.getSQLType(typname)).getBytes();
2599+
tuple[1] = Integer.toString(connection.getSQLType(typname)).getBytes();
26002600
tuple[2] = b9; // for now
26012601
tuple[6] = bnn; // for now
26022602
tuple[7] = bf; // false for now - not case sensitive

0 commit comments

Comments
 (0)