Skip to content

Commit 2a9bfb1

Browse files
committed
Add new jdbc array file.
1 parent 4859219 commit 2a9bfb1

File tree

1 file changed

+312
-0
lines changed
  • src/interfaces/jdbc/org/postgresql/jdbc2

1 file changed

+312
-0
lines changed
Lines changed: 312 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,312 @@
1+
package org.postgresql.jdbc2;
2+
3+
import java.text.*;
4+
import java.sql.*;
5+
import java.util.*;
6+
import java.math.BigDecimal;
7+
import org.postgresql.Field;
8+
import org.postgresql.util.*;
9+
10+
/**
11+
* Array is used collect one column of query result data.
12+
*
13+
* <p>Read a field of type Array into either a natively-typed
14+
* Java array object or a ResultSet. Accessor methods provide
15+
* the ability to capture array slices.
16+
*
17+
* <p>Other than the constructor all methods are direct implementations
18+
* of those specified for java.sql.Array. Please refer to the javadoc
19+
* for java.sql.Array for detailed descriptions of the functionality
20+
* and parameters of the methods of this class.
21+
*
22+
* @see ResultSet#getArray
23+
*/
24+
25+
26+
public class Array implements java.sql.Array
27+
{
28+
private org.postgresql.Connection conn = null;
29+
private org.postgresql.Field field = null;
30+
private org.postgresql.jdbc2.ResultSet rs = null;
31+
private int idx = 0;
32+
33+
/**
34+
* Create a new Array
35+
*
36+
* @param conn a database connection
37+
* @param idx 1-based index of the query field to load into this Array
38+
* @param field the Field descriptor for the field to load into this Array
39+
* @param rs the ResultSet from which to get the data for this Array
40+
*/
41+
public Array( org.postgresql.Connection conn, int idx, Field field, org.postgresql.jdbc2.ResultSet rs ) {
42+
this.conn = conn;
43+
this.field = field;
44+
this.rs = rs;
45+
this.idx = idx;
46+
}
47+
48+
public Object getArray() throws SQLException {
49+
return getArray( 1, 0, null );
50+
}
51+
52+
public Object getArray(long index, int count) throws SQLException {
53+
return getArray( index, count, null );
54+
}
55+
56+
public Object getArray(Map map) throws SQLException {
57+
return getArray( 1, 0, map );
58+
}
59+
60+
public Object getArray(long index, int count, Map map) throws SQLException {
61+
if( map != null ) // For now maps aren't supported.
62+
throw org.postgresql.Driver.notImplemented();
63+
64+
if (index < 1)
65+
throw new PSQLException("postgresql.arr.range");
66+
Object retVal = null;
67+
68+
ArrayList array = new ArrayList();
69+
String raw = rs.getFixedString(idx);
70+
if( raw != null ) {
71+
char[] chars = raw.toCharArray();
72+
StringBuffer sbuf = new StringBuffer();
73+
boolean foundOpen = false;
74+
boolean insideString = false;
75+
for( int i=0; i<chars.length; i++ ) {
76+
if( chars[i] == '{' ) {
77+
if( foundOpen ) // Only supports 1-D arrays for now
78+
throw org.postgresql.Driver.notImplemented();
79+
foundOpen = true;
80+
continue;
81+
}
82+
if( chars[i] == '"' ) {
83+
insideString = !insideString;
84+
continue;
85+
}
86+
if( (!insideString && chars[i] == ',') || chars[i] == '}' || i == chars.length-1) {
87+
if( chars[i] != '"' && chars[i] != '}' && chars[i] != ',' )
88+
sbuf.append(chars[i]);
89+
array.add( sbuf.toString() );
90+
sbuf = new StringBuffer();
91+
continue;
92+
}
93+
sbuf.append( chars[i] );
94+
}
95+
}
96+
String[] arrayContents = (String[]) array.toArray( new String[array.size()] );
97+
if( count == 0 )
98+
count = arrayContents.length;
99+
index--;
100+
if( index+count > arrayContents.length )
101+
throw new PSQLException("postgresql.arr.range");
102+
103+
int i = 0;
104+
switch ( getBaseType() )
105+
{
106+
case Types.BIT:
107+
retVal = new boolean[ count ];
108+
for( ; count > 0; count-- )
109+
((boolean[])retVal)[i++] = ResultSet.toBoolean( arrayContents[(int)index++] );
110+
break;
111+
case Types.SMALLINT:
112+
case Types.INTEGER:
113+
retVal = new int[ count ];
114+
for( ; count > 0; count-- )
115+
((int[])retVal)[i++] = ResultSet.toInt( arrayContents[(int)index++] );
116+
break;
117+
case Types.BIGINT:
118+
retVal = new long[ count ];
119+
for( ; count > 0; count-- )
120+
((long[])retVal)[i++] = ResultSet.toLong( arrayContents[(int)index++] );
121+
break;
122+
case Types.NUMERIC:
123+
retVal = new BigDecimal[ count ];
124+
for( ; count > 0; count-- )
125+
((BigDecimal[])retVal)[i] = ResultSet.toBigDecimal( arrayContents[(int)index++], 0 );
126+
break;
127+
case Types.REAL:
128+
retVal = new float[ count ];
129+
for( ; count > 0; count-- )
130+
((float[])retVal)[i++] = ResultSet.toFloat( arrayContents[(int)index++] );
131+
break;
132+
case Types.DOUBLE:
133+
retVal = new double[ count ];
134+
for( ; count > 0; count-- )
135+
((double[])retVal)[i++] = ResultSet.toDouble( arrayContents[(int)index++] );
136+
break;
137+
case Types.CHAR:
138+
case Types.VARCHAR:
139+
retVal = new String[ count ];
140+
for( ; count > 0; count-- )
141+
((String[])retVal)[i++] = arrayContents[(int)index++];
142+
break;
143+
case Types.DATE:
144+
retVal = new java.sql.Date[ count ];
145+
for( ; count > 0; count-- )
146+
((java.sql.Date[])retVal)[i++] = ResultSet.toDate( arrayContents[(int)index++] );
147+
break;
148+
case Types.TIME:
149+
retVal = new java.sql.Time[ count ];
150+
for( ; count > 0; count-- )
151+
((java.sql.Time[])retVal)[i++] = ResultSet.toTime( arrayContents[(int)index++] );
152+
break;
153+
case Types.TIMESTAMP:
154+
retVal = new Timestamp[ count ];
155+
StringBuffer sbuf = null;
156+
for( ; count > 0; count-- )
157+
((java.sql.Timestamp[])retVal)[i++] = ResultSet.toTimestamp( arrayContents[(int)index], rs );
158+
break;
159+
160+
// Other datatypes not currently supported. If you are really using other types ask
161+
// yourself if an array of non-trivial data types is really good database design.
162+
default:
163+
throw org.postgresql.Driver.notImplemented();
164+
}
165+
return retVal;
166+
}
167+
168+
public int getBaseType() throws SQLException {
169+
return Field.getSQLType( getBaseTypeName() );
170+
}
171+
172+
public String getBaseTypeName() throws SQLException {
173+
String fType = field.getTypeName();
174+
if( fType.charAt(0) == '_' )
175+
fType = fType.substring(1);
176+
return fType;
177+
}
178+
179+
public java.sql.ResultSet getResultSet() throws SQLException {
180+
return getResultSet( 1, 0, null );
181+
}
182+
183+
public java.sql.ResultSet getResultSet(long index, int count) throws SQLException {
184+
return getResultSet( index, count, null );
185+
}
186+
187+
public java.sql.ResultSet getResultSet(Map map) throws SQLException {
188+
return getResultSet( 1, 0, map );
189+
}
190+
191+
public java.sql.ResultSet getResultSet(long index, int count, java.util.Map map) throws SQLException {
192+
Object array = getArray( index, count, map );
193+
Vector rows = new Vector();
194+
Field[] fields = new Field[2];
195+
fields[0] = new Field(conn, "INDEX", field.getOID("int2"), 2);
196+
switch ( getBaseType() )
197+
{
198+
case Types.BIT:
199+
boolean[] booleanArray = (boolean[]) array;
200+
fields[1] = new Field(conn, "VALUE", field.getOID("bool"), 1);
201+
for( int i=0; i<booleanArray.length; i++ ) {
202+
byte[][] tuple = new byte[2][0];
203+
tuple[0] = Integer.toString((int)index+i).getBytes(); // Index
204+
tuple[1] = (booleanArray[i]?"YES":"NO").getBytes(); // Value
205+
rows.addElement(tuple);
206+
}
207+
case Types.SMALLINT:
208+
fields[1] = new Field(conn, "VALUE", field.getOID("int2"), 2);
209+
case Types.INTEGER:
210+
int[] intArray = (int[]) array;
211+
if( fields[1] == null )
212+
fields[1] = new Field(conn, "VALUE", field.getOID("int4"), 4);
213+
for( int i=0; i<intArray.length; i++ ) {
214+
byte[][] tuple = new byte[2][0];
215+
tuple[0] = Integer.toString((int)index+i).getBytes(); // Index
216+
tuple[1] = Integer.toString(intArray[i]).getBytes(); // Value
217+
rows.addElement(tuple);
218+
}
219+
break;
220+
case Types.BIGINT:
221+
long[] longArray = (long[]) array;
222+
fields[1] = new Field(conn, "VALUE", field.getOID("int8"), 8);
223+
for( int i=0; i<longArray.length; i++ ) {
224+
byte[][] tuple = new byte[2][0];
225+
tuple[0] = Integer.toString((int)index+i).getBytes(); // Index
226+
tuple[1] = Long.toString(longArray[i]).getBytes(); // Value
227+
rows.addElement(tuple);
228+
}
229+
break;
230+
case Types.NUMERIC:
231+
BigDecimal[] bdArray = (BigDecimal[]) array;
232+
fields[1] = new Field(conn, "VALUE", field.getOID("numeric"), -1);
233+
for( int i=0; i<bdArray.length; i++ ) {
234+
byte[][] tuple = new byte[2][0];
235+
tuple[0] = Integer.toString((int)index+i).getBytes(); // Index
236+
tuple[1] = bdArray[i].toString().getBytes(); // Value
237+
rows.addElement(tuple);
238+
}
239+
break;
240+
case Types.REAL:
241+
float[] floatArray = (float[]) array;
242+
fields[1] = new Field(conn, "VALUE", field.getOID("float4"), 4);
243+
for( int i=0; i<floatArray.length; i++ ) {
244+
byte[][] tuple = new byte[2][0];
245+
tuple[0] = Integer.toString((int)index+i).getBytes(); // Index
246+
tuple[1] = Float.toString(floatArray[i]).getBytes(); // Value
247+
rows.addElement(tuple);
248+
}
249+
break;
250+
case Types.DOUBLE:
251+
double[] doubleArray = (double[]) array;
252+
fields[1] = new Field(conn, "VALUE", field.getOID("float8"), 8);
253+
for( int i=0; i<doubleArray.length; i++ ) {
254+
byte[][] tuple = new byte[2][0];
255+
tuple[0] = Integer.toString((int)index+i).getBytes(); // Index
256+
tuple[1] = Double.toString(doubleArray[i]).getBytes(); // Value
257+
rows.addElement(tuple);
258+
}
259+
break;
260+
case Types.CHAR:
261+
fields[1] = new Field(conn, "VALUE", field.getOID("char"), 1);
262+
case Types.VARCHAR:
263+
String[] strArray = (String[]) array;
264+
if( fields[1] == null )
265+
fields[1] = new Field(conn, "VALUE", field.getOID("varchar"), -1);
266+
for( int i=0; i<strArray.length; i++ ) {
267+
byte[][] tuple = new byte[2][0];
268+
tuple[0] = Integer.toString((int)index+i).getBytes(); // Index
269+
tuple[1] = strArray[i].getBytes(); // Value
270+
rows.addElement(tuple);
271+
}
272+
break;
273+
case Types.DATE:
274+
java.sql.Date[] dateArray = (java.sql.Date[]) array;
275+
fields[1] = new Field(conn, "VALUE", field.getOID("date"), 4);
276+
for( int i=0; i<dateArray.length; i++ ) {
277+
byte[][] tuple = new byte[2][0];
278+
tuple[0] = Integer.toString((int)index+i).getBytes(); // Index
279+
tuple[1] = dateArray[i].toString().getBytes(); // Value
280+
rows.addElement(tuple);
281+
}
282+
break;
283+
case Types.TIME:
284+
java.sql.Time[] timeArray = (java.sql.Time[]) array;
285+
fields[1] = new Field(conn, "VALUE", field.getOID("time"), 8);
286+
for( int i=0; i<timeArray.length; i++ ) {
287+
byte[][] tuple = new byte[2][0];
288+
tuple[0] = Integer.toString((int)index+i).getBytes(); // Index
289+
tuple[1] = timeArray[i].toString().getBytes(); // Value
290+
rows.addElement(tuple);
291+
}
292+
break;
293+
case Types.TIMESTAMP:
294+
java.sql.Timestamp[] timestampArray = (java.sql.Timestamp[]) array;
295+
fields[1] = new Field(conn, "VALUE", field.getOID("timestamp"), 8);
296+
for( int i=0; i<timestampArray.length; i++ ) {
297+
byte[][] tuple = new byte[2][0];
298+
tuple[0] = Integer.toString((int)index+i).getBytes(); // Index
299+
tuple[1] = timestampArray[i].toString().getBytes(); // Value
300+
rows.addElement(tuple);
301+
}
302+
break;
303+
304+
// Other datatypes not currently supported. If you are really using other types ask
305+
// yourself if an array of non-trivial data types is really good database design.
306+
default:
307+
throw org.postgresql.Driver.notImplemented();
308+
}
309+
return new ResultSet((org.postgresql.jdbc2.Connection)conn, fields, rows, "OK", 1 );
310+
}
311+
}
312+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy