~ubuntu-branches/ubuntu/hardy/libpgjava/hardy

« back to all changes in this revision

Viewing changes to src/interfaces/jdbc/org/postgresql/jdbc2/Array.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2005-04-21 14:25:11 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20050421142511-wibh5vc31fkrorx7
Tags: 7.4.7-3
Built with sources...

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
package org.postgresql.jdbc2;
2
2
 
3
 
import java.text.*;
4
 
import java.sql.*;
5
 
import java.util.*;
 
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;
 
9
 
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;
 
16
import java.util.Map;
 
17
import java.util.Vector;
9
18
 
10
19
/*
11
20
 * Array is used collect one column of query result data.
25
34
 
26
35
public class Array implements java.sql.Array
27
36
{
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;
33
42
 
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
41
50
         */
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
44
53
        {
45
54
                this.conn = conn;
70
79
                        throw org.postgresql.Driver.notImplemented();
71
80
 
72
81
                if (index < 1)
73
 
                        throw new PSQLException("postgresql.arr.range");
 
82
                        throw new PSQLException("postgresql.arr.range", PSQLState.DATA_ERROR);
74
83
                Object retVal = null;
75
84
 
76
85
                ArrayList array = new ArrayList();
77
 
                if ( rawString != null )
 
86
 
 
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 */
 
91
 
 
92
                if ( rawString != null && !rawString.equals("{}") )
78
93
                {
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++ )
84
99
                        {
85
 
                                if ( chars[i] == '{' )
 
100
                                if ( chars[i] == '\\' )
 
101
                                        //escape character that we need to skip
 
102
                                        i++;
 
103
                                else if (!insideString && chars[i] == '{' )
86
104
                                {
87
105
                                        if ( foundOpen )  // Only supports 1-D arrays for now
88
106
                                                throw org.postgresql.Driver.notImplemented();
89
107
                                        foundOpen = true;
90
108
                                        continue;
91
109
                                }
92
 
                                if ( chars[i] == '"' )
 
110
                                else if (chars[i] == '"')
93
111
                                {
94
112
                                        insideString = !insideString;
95
113
                                        continue;
96
114
                                }
97
 
                                if ( (!insideString && chars[i] == ',') || chars[i] == '}' || i == chars.length - 1)
 
115
                                else if (!insideString && (chars[i] == ',' || chars[i] == '}') || 
 
116
                                                        i == chars.length - 1)
98
117
                                {
99
118
                                        if ( chars[i] != '"' && chars[i] != '}' && chars[i] != ',' )
100
119
                                                sbuf.append(chars[i]);
110
129
                        count = arrayContents.length;
111
130
                index--;
112
131
                if ( index + count > arrayContents.length )
113
 
                        throw new PSQLException("postgresql.arr.range");
 
132
                        throw new PSQLException("postgresql.arr.range", PSQLState.DATA_ERROR);
114
133
 
115
134
                int i = 0;
116
135
                switch ( getBaseType() )
118
137
                        case Types.BIT:
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++] );
122
141
                                break;
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++] );
128
147
                                break;
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++] );
133
152
                                break;
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 );
138
157
                                break;
139
158
                        case Types.REAL:
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++] );
143
162
                                break;
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++] );
148
167
                                break;
149
168
                        case Types.CHAR:
150
169
                        case Types.VARCHAR:
155
174
                        case Types.DATE:
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++] );
159
178
                                break;
160
179
                        case Types.TIME:
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() );
164
183
                                break;
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() );
170
188
                                break;
171
189
 
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() )
215
233
                {
216
234
                        case Types.BIT:
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++ )
220
238
                                {
221
239
                                        byte[][] tuple = new byte[2][0];
224
242
                                        rows.addElement(tuple);
225
243
                                }
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++ )
233
251
                                {
234
252
                                        byte[][] tuple = new byte[2][0];
239
257
                                break;
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++ )
244
262
                                {
245
263
                                        byte[][] tuple = new byte[2][0];
250
268
                                break;
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++ )
255
273
                                {
256
274
                                        byte[][] tuple = new byte[2][0];
261
279
                                break;
262
280
                        case Types.REAL:
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++ )
266
284
                                {
267
285
                                        byte[][] tuple = new byte[2][0];
272
290
                                break;
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++ )
277
295
                                {
278
296
                                        byte[][] tuple = new byte[2][0];
282
300
                                }
283
301
                                break;
284
302
                        case Types.CHAR:
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++ )
291
309
                                {
292
310
                                        byte[][] tuple = new byte[2][0];
297
315
                                break;
298
316
                        case Types.DATE:
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++ )
302
320
                                {
303
321
                                        byte[][] tuple = new byte[2][0];
308
326
                                break;
309
327
                        case Types.TIME:
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++ )
313
331
                                {
314
332
                                        byte[][] tuple = new byte[2][0];
319
337
                                break;
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++ )
324
342
                                {
325
343
                                        byte[][] tuple = new byte[2][0];
334
352
                        default:
335
353
                                throw org.postgresql.Driver.notImplemented();
336
354
                }
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);
338
357
        }
339
358
 
340
359
        public String toString()