|
12 | 12 | */
|
13 | 13 | public class Field
|
14 | 14 | {
|
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 |
19 | 19 |
|
20 |
| - protected Connection conn; // Connection Instantation |
| 20 | + private Connection conn; // Connection Instantation |
21 | 21 |
|
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(); |
26 | 22 |
|
27 | 23 | /**
|
28 | 24 | * Construct a field based on the information fed to it.
|
@@ -63,140 +59,49 @@ public int getOID()
|
63 | 59 | }
|
64 | 60 |
|
65 | 61 | /**
|
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 |
72 | 63 | */
|
73 |
| - public int getSQLType() throws SQLException |
| 64 | + public int getMod() |
74 | 65 | {
|
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; |
93 | 67 | }
|
94 | 68 |
|
95 | 69 | /**
|
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 |
99 | 71 | */
|
100 |
| - public static int getSQLType(String type_name) |
| 72 | + public String getName() |
101 | 73 | {
|
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; |
107 | 75 | }
|
108 | 76 |
|
109 | 77 | /**
|
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 |
113 | 79 | */
|
114 |
| - public int getOID( String type_name ) throws SQLException |
| 80 | + public int getLength() |
115 | 81 | {
|
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; |
134 | 83 | }
|
135 | 84 |
|
136 | 85 | /**
|
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. |
165 | 87 | *
|
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 |
169 | 90 | */
|
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 | + } |
187 | 95 |
|
188 | 96 | /**
|
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. |
193 | 98 | *
|
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 |
195 | 100 | * @exception SQLException if a database access error occurs
|
196 | 101 | */
|
197 |
| - public String getTypeName() throws SQLException |
| 102 | + public int getSQLType() throws SQLException |
198 | 103 | {
|
199 |
| - int sql = getSQLType(); |
200 |
| - return type_name; |
| 104 | + return conn.getSQLType(oid); |
201 | 105 | }
|
| 106 | + |
202 | 107 | }
|
0 commit comments