1
1
package org.postgresql.jdbc2;
3
import org.postgresql.core.BaseConnection;
4
import org.postgresql.core.BaseResultSet;
5
import org.postgresql.core.BaseStatement;
6
import org.postgresql.core.Field;
7
import org.postgresql.util.PSQLException;
8
import org.postgresql.util.PSQLState;
6
10
import java.math.BigDecimal;
7
import org.postgresql.Field;
8
import org.postgresql.util.*;
11
import java.sql.ResultSet;
12
import java.sql.SQLException;
13
import java.sql.Timestamp;
14
import java.sql.Types;
15
import java.util.ArrayList;
17
import java.util.Vector;
11
20
* Array is used collect one column of query result data.
26
35
public class Array implements java.sql.Array
28
private org.postgresql.Connection conn = null;
29
private org.postgresql.Field field = null;
30
private org.postgresql.jdbc2.ResultSet rs = null;
37
private BaseConnection conn = null;
38
private Field field = null;
39
private BaseResultSet rs;
31
40
private int idx = 0;
32
41
private String rawString = null;
39
48
* @param field the Field descriptor for the field to load into this Array
40
49
* @param rs the ResultSet from which to get the data for this Array
42
public Array( org.postgresql.Connection conn, int idx, Field field, org.postgresql.jdbc2.ResultSet rs )
51
public Array(BaseConnection conn, int idx, Field field, BaseResultSet rs )
43
52
throws SQLException
70
79
throw org.postgresql.Driver.notImplemented();
73
throw new PSQLException("postgresql.arr.range");
82
throw new PSQLException("postgresql.arr.range", PSQLState.DATA_ERROR);
74
83
Object retVal = null;
76
85
ArrayList array = new ArrayList();
77
if ( rawString != null )
87
/* Check if the String is also not an empty array
88
* otherwise there will be an exception thrown below
89
* in the ResultSet.toX with an empty string.
90
* -- Doug Fields <dfields-pg-jdbc@pexicom.com> Feb 20, 2002 */
92
if ( rawString != null && !rawString.equals("{}") )
79
94
char[] chars = rawString.toCharArray();
80
95
StringBuffer sbuf = new StringBuffer();
82
97
boolean insideString = false;
83
98
for ( int i = 0; i < chars.length; i++ )
85
if ( chars[i] == '{' )
100
if ( chars[i] == '\\' )
101
//escape character that we need to skip
103
else if (!insideString && chars[i] == '{' )
87
105
if ( foundOpen ) // Only supports 1-D arrays for now
88
106
throw org.postgresql.Driver.notImplemented();
92
if ( chars[i] == '"' )
110
else if (chars[i] == '"')
94
112
insideString = !insideString;
97
if ( (!insideString && chars[i] == ',') || chars[i] == '}' || i == chars.length - 1)
115
else if (!insideString && (chars[i] == ',' || chars[i] == '}') ||
116
i == chars.length - 1)
99
118
if ( chars[i] != '"' && chars[i] != '}' && chars[i] != ',' )
100
119
sbuf.append(chars[i]);
110
129
count = arrayContents.length;
112
131
if ( index + count > arrayContents.length )
113
throw new PSQLException("postgresql.arr.range");
132
throw new PSQLException("postgresql.arr.range", PSQLState.DATA_ERROR);
116
135
switch ( getBaseType() )
119
138
retVal = new boolean[ count ];
120
139
for ( ; count > 0; count-- )
121
((boolean[])retVal)[i++] = ResultSet.toBoolean( arrayContents[(int)index++] );
140
((boolean[])retVal)[i++] = AbstractJdbc2ResultSet.toBoolean( arrayContents[(int)index++] );
123
142
case Types.SMALLINT:
124
143
case Types.INTEGER:
125
144
retVal = new int[ count ];
126
145
for ( ; count > 0; count-- )
127
((int[])retVal)[i++] = ResultSet.toInt( arrayContents[(int)index++] );
146
((int[])retVal)[i++] = AbstractJdbc2ResultSet.toInt( arrayContents[(int)index++] );
129
148
case Types.BIGINT:
130
149
retVal = new long[ count ];
131
150
for ( ; count > 0; count-- )
132
((long[])retVal)[i++] = ResultSet.toLong( arrayContents[(int)index++] );
151
((long[])retVal)[i++] = AbstractJdbc2ResultSet.toLong( arrayContents[(int)index++] );
134
153
case Types.NUMERIC:
135
154
retVal = new BigDecimal[ count ];
136
155
for ( ; count > 0; count-- )
137
((BigDecimal[])retVal)[i] = ResultSet.toBigDecimal( arrayContents[(int)index++], 0 );
156
((BigDecimal[])retVal)[i++] = AbstractJdbc2ResultSet.toBigDecimal( arrayContents[(int)index++], -1 );
140
159
retVal = new float[ count ];
141
160
for ( ; count > 0; count-- )
142
((float[])retVal)[i++] = ResultSet.toFloat( arrayContents[(int)index++] );
161
((float[])retVal)[i++] = AbstractJdbc2ResultSet.toFloat( arrayContents[(int)index++] );
144
163
case Types.DOUBLE:
145
164
retVal = new double[ count ];
146
165
for ( ; count > 0; count-- )
147
((double[])retVal)[i++] = ResultSet.toDouble( arrayContents[(int)index++] );
166
((double[])retVal)[i++] = AbstractJdbc2ResultSet.toDouble( arrayContents[(int)index++] );
150
169
case Types.VARCHAR:
156
175
retVal = new java.sql.Date[ count ];
157
176
for ( ; count > 0; count-- )
158
((java.sql.Date[])retVal)[i++] = ResultSet.toDate( arrayContents[(int)index++] );
177
((java.sql.Date[])retVal)[i++] = AbstractJdbc2ResultSet.toDate( arrayContents[(int)index++] );
161
180
retVal = new java.sql.Time[ count ];
162
181
for ( ; count > 0; count-- )
163
((java.sql.Time[])retVal)[i++] = ResultSet.toTime( arrayContents[(int)index++] );
182
((java.sql.Time[])retVal)[i++] = AbstractJdbc2ResultSet.toTime( arrayContents[(int)index++], rs, getBaseTypeName() );
165
184
case Types.TIMESTAMP:
166
185
retVal = new Timestamp[ count ];
167
StringBuffer sbuf = null;
168
186
for ( ; count > 0; count-- )
169
((java.sql.Timestamp[])retVal)[i++] = ResultSet.toTimestamp( arrayContents[(int)index], rs );
187
((java.sql.Timestamp[])retVal)[i++] = AbstractJdbc2ResultSet.toTimestamp( arrayContents[(int)index++], rs, getBaseTypeName() );
172
190
// Other datatypes not currently supported. If you are really using other types ask
210
228
Object array = getArray( index, count, map );
211
229
Vector rows = new Vector();
212
230
Field[] fields = new Field[2];
213
fields[0] = new Field(conn, "INDEX", conn.getOID("int2"), 2);
231
fields[0] = new Field(conn, "INDEX", conn.getPGType("int2"), 2);
214
232
switch ( getBaseType() )
217
235
boolean[] booleanArray = (boolean[]) array;
218
fields[1] = new Field(conn, "VALUE", conn.getOID("bool"), 1);
236
fields[1] = new Field(conn, "VALUE", conn.getPGType("bool"), 1);
219
237
for ( int i = 0; i < booleanArray.length; i++ )
221
239
byte[][] tuple = new byte[2][0];
224
242
rows.addElement(tuple);
226
244
case Types.SMALLINT:
227
fields[1] = new Field(conn, "VALUE", conn.getOID("int2"), 2);
245
fields[1] = new Field(conn, "VALUE", conn.getPGType("int2"), 2);
228
246
case Types.INTEGER:
229
247
int[] intArray = (int[]) array;
230
248
if ( fields[1] == null )
231
fields[1] = new Field(conn, "VALUE", conn.getOID("int4"), 4);
249
fields[1] = new Field(conn, "VALUE", conn.getPGType("int4"), 4);
232
250
for ( int i = 0; i < intArray.length; i++ )
234
252
byte[][] tuple = new byte[2][0];
240
258
case Types.BIGINT:
241
259
long[] longArray = (long[]) array;
242
fields[1] = new Field(conn, "VALUE", conn.getOID("int8"), 8);
260
fields[1] = new Field(conn, "VALUE", conn.getPGType("int8"), 8);
243
261
for ( int i = 0; i < longArray.length; i++ )
245
263
byte[][] tuple = new byte[2][0];
251
269
case Types.NUMERIC:
252
270
BigDecimal[] bdArray = (BigDecimal[]) array;
253
fields[1] = new Field(conn, "VALUE", conn.getOID("numeric"), -1);
271
fields[1] = new Field(conn, "VALUE", conn.getPGType("numeric"), -1);
254
272
for ( int i = 0; i < bdArray.length; i++ )
256
274
byte[][] tuple = new byte[2][0];
263
281
float[] floatArray = (float[]) array;
264
fields[1] = new Field(conn, "VALUE", conn.getOID("float4"), 4);
282
fields[1] = new Field(conn, "VALUE", conn.getPGType("float4"), 4);
265
283
for ( int i = 0; i < floatArray.length; i++ )
267
285
byte[][] tuple = new byte[2][0];
273
291
case Types.DOUBLE:
274
292
double[] doubleArray = (double[]) array;
275
fields[1] = new Field(conn, "VALUE", conn.getOID("float8"), 8);
293
fields[1] = new Field(conn, "VALUE", conn.getPGType("float8"), 8);
276
294
for ( int i = 0; i < doubleArray.length; i++ )
278
296
byte[][] tuple = new byte[2][0];
285
fields[1] = new Field(conn, "VALUE", conn.getOID("char"), 1);
303
fields[1] = new Field(conn, "VALUE", conn.getPGType("char"), 1);
286
304
case Types.VARCHAR:
287
305
String[] strArray = (String[]) array;
288
306
if ( fields[1] == null )
289
fields[1] = new Field(conn, "VALUE", conn.getOID("varchar"), -1);
307
fields[1] = new Field(conn, "VALUE", conn.getPGType("varchar"), -1);
290
308
for ( int i = 0; i < strArray.length; i++ )
292
310
byte[][] tuple = new byte[2][0];
299
317
java.sql.Date[] dateArray = (java.sql.Date[]) array;
300
fields[1] = new Field(conn, "VALUE", conn.getOID("date"), 4);
318
fields[1] = new Field(conn, "VALUE", conn.getPGType("date"), 4);
301
319
for ( int i = 0; i < dateArray.length; i++ )
303
321
byte[][] tuple = new byte[2][0];
310
328
java.sql.Time[] timeArray = (java.sql.Time[]) array;
311
fields[1] = new Field(conn, "VALUE", conn.getOID("time"), 8);
329
fields[1] = new Field(conn, "VALUE", conn.getPGType("time"), 8);
312
330
for ( int i = 0; i < timeArray.length; i++ )
314
332
byte[][] tuple = new byte[2][0];
320
338
case Types.TIMESTAMP:
321
339
java.sql.Timestamp[] timestampArray = (java.sql.Timestamp[]) array;
322
fields[1] = new Field(conn, "VALUE", conn.getOID("timestamp"), 8);
340
fields[1] = new Field(conn, "VALUE", conn.getPGType("timestamp"), 8);
323
341
for ( int i = 0; i < timestampArray.length; i++ )
325
343
byte[][] tuple = new byte[2][0];
335
353
throw org.postgresql.Driver.notImplemented();
337
return new ResultSet((org.postgresql.jdbc2.Connection)conn, fields, rows, "OK", 1 );
355
BaseStatement stat = (BaseStatement) conn.createStatement();
356
return (ResultSet) stat.createResultSet(fields, rows, "OK", 1, 0);
340
359
public String toString()