|
| 1 | +packageorg.postgresql.jdbc2; |
| 2 | + |
| 3 | +importjava.text.*; |
| 4 | +importjava.sql.*; |
| 5 | +importjava.util.*; |
| 6 | +importjava.math.BigDecimal; |
| 7 | +importorg.postgresql.Field; |
| 8 | +importorg.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 | +publicclassArrayimplementsjava.sql.Array |
| 27 | +{ |
| 28 | +privateorg.postgresql.Connectionconn =null; |
| 29 | +privateorg.postgresql.Fieldfield =null; |
| 30 | +privateorg.postgresql.jdbc2.ResultSetrs =null; |
| 31 | +privateintidx =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 | +publicArray(org.postgresql.Connectionconn,intidx,Fieldfield,org.postgresql.jdbc2.ResultSetrs ) { |
| 42 | +this.conn =conn; |
| 43 | +this.field =field; |
| 44 | +this.rs =rs; |
| 45 | +this.idx =idx; |
| 46 | +} |
| 47 | + |
| 48 | +publicObjectgetArray()throwsSQLException { |
| 49 | +returngetArray(1,0,null ); |
| 50 | +} |
| 51 | + |
| 52 | +publicObjectgetArray(longindex,intcount)throwsSQLException { |
| 53 | +returngetArray(index,count,null ); |
| 54 | +} |
| 55 | + |
| 56 | +publicObjectgetArray(Mapmap)throwsSQLException { |
| 57 | +returngetArray(1,0,map ); |
| 58 | +} |
| 59 | + |
| 60 | +publicObjectgetArray(longindex,intcount,Mapmap)throwsSQLException { |
| 61 | +if(map !=null )// For now maps aren't supported. |
| 62 | +throworg.postgresql.Driver.notImplemented(); |
| 63 | + |
| 64 | +if (index <1) |
| 65 | +thrownewPSQLException("postgresql.arr.range"); |
| 66 | +ObjectretVal =null; |
| 67 | + |
| 68 | +ArrayListarray =newArrayList(); |
| 69 | +Stringraw =rs.getFixedString(idx); |
| 70 | +if(raw !=null ) { |
| 71 | +char[]chars =raw.toCharArray(); |
| 72 | +StringBuffersbuf =newStringBuffer(); |
| 73 | +booleanfoundOpen =false; |
| 74 | +booleaninsideString =false; |
| 75 | +for(inti=0;i<chars.length;i++ ) { |
| 76 | +if(chars[i] =='{' ) { |
| 77 | +if(foundOpen )// Only supports 1-D arrays for now |
| 78 | +throworg.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 =newStringBuffer(); |
| 91 | +continue; |
| 92 | +} |
| 93 | +sbuf.append(chars[i] ); |
| 94 | +} |
| 95 | +} |
| 96 | +String[]arrayContents = (String[])array.toArray(newString[array.size()] ); |
| 97 | +if(count ==0 ) |
| 98 | +count =arrayContents.length; |
| 99 | +index--; |
| 100 | +if(index+count >arrayContents.length ) |
| 101 | +thrownewPSQLException("postgresql.arr.range"); |
| 102 | + |
| 103 | +inti =0; |
| 104 | +switch (getBaseType() ) |
| 105 | +{ |
| 106 | +caseTypes.BIT: |
| 107 | +retVal =newboolean[count ]; |
| 108 | +for( ;count >0;count-- ) |
| 109 | +((boolean[])retVal)[i++] =ResultSet.toBoolean(arrayContents[(int)index++] ); |
| 110 | +break; |
| 111 | +caseTypes.SMALLINT: |
| 112 | +caseTypes.INTEGER: |
| 113 | +retVal =newint[count ]; |
| 114 | +for( ;count >0;count-- ) |
| 115 | +((int[])retVal)[i++] =ResultSet.toInt(arrayContents[(int)index++] ); |
| 116 | +break; |
| 117 | +caseTypes.BIGINT: |
| 118 | +retVal =newlong[count ]; |
| 119 | +for( ;count >0;count-- ) |
| 120 | +((long[])retVal)[i++] =ResultSet.toLong(arrayContents[(int)index++] ); |
| 121 | +break; |
| 122 | +caseTypes.NUMERIC: |
| 123 | +retVal =newBigDecimal[count ]; |
| 124 | +for( ;count >0;count-- ) |
| 125 | +((BigDecimal[])retVal)[i] =ResultSet.toBigDecimal(arrayContents[(int)index++],0 ); |
| 126 | +break; |
| 127 | +caseTypes.REAL: |
| 128 | +retVal =newfloat[count ]; |
| 129 | +for( ;count >0;count-- ) |
| 130 | +((float[])retVal)[i++] =ResultSet.toFloat(arrayContents[(int)index++] ); |
| 131 | +break; |
| 132 | +caseTypes.DOUBLE: |
| 133 | +retVal =newdouble[count ]; |
| 134 | +for( ;count >0;count-- ) |
| 135 | +((double[])retVal)[i++] =ResultSet.toDouble(arrayContents[(int)index++] ); |
| 136 | +break; |
| 137 | +caseTypes.CHAR: |
| 138 | +caseTypes.VARCHAR: |
| 139 | +retVal =newString[count ]; |
| 140 | +for( ;count >0;count-- ) |
| 141 | +((String[])retVal)[i++] =arrayContents[(int)index++]; |
| 142 | +break; |
| 143 | +caseTypes.DATE: |
| 144 | +retVal =newjava.sql.Date[count ]; |
| 145 | +for( ;count >0;count-- ) |
| 146 | +((java.sql.Date[])retVal)[i++] =ResultSet.toDate(arrayContents[(int)index++] ); |
| 147 | +break; |
| 148 | +caseTypes.TIME: |
| 149 | +retVal =newjava.sql.Time[count ]; |
| 150 | +for( ;count >0;count-- ) |
| 151 | +((java.sql.Time[])retVal)[i++] =ResultSet.toTime(arrayContents[(int)index++] ); |
| 152 | +break; |
| 153 | +caseTypes.TIMESTAMP: |
| 154 | +retVal =newTimestamp[count ]; |
| 155 | +StringBuffersbuf =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 | +throworg.postgresql.Driver.notImplemented(); |
| 164 | +} |
| 165 | +returnretVal; |
| 166 | +} |
| 167 | + |
| 168 | +publicintgetBaseType()throwsSQLException { |
| 169 | +returnField.getSQLType(getBaseTypeName() ); |
| 170 | +} |
| 171 | + |
| 172 | +publicStringgetBaseTypeName()throwsSQLException { |
| 173 | +StringfType =field.getTypeName(); |
| 174 | +if(fType.charAt(0) =='_' ) |
| 175 | +fType =fType.substring(1); |
| 176 | +returnfType; |
| 177 | +} |
| 178 | + |
| 179 | +publicjava.sql.ResultSetgetResultSet()throwsSQLException { |
| 180 | +returngetResultSet(1,0,null ); |
| 181 | +} |
| 182 | + |
| 183 | +publicjava.sql.ResultSetgetResultSet(longindex,intcount)throwsSQLException { |
| 184 | +returngetResultSet(index,count,null ); |
| 185 | +} |
| 186 | + |
| 187 | +publicjava.sql.ResultSetgetResultSet(Mapmap)throwsSQLException { |
| 188 | +returngetResultSet(1,0,map ); |
| 189 | +} |
| 190 | + |
| 191 | +publicjava.sql.ResultSetgetResultSet(longindex,intcount,java.util.Mapmap)throwsSQLException { |
| 192 | +Objectarray =getArray(index,count,map ); |
| 193 | +Vectorrows =newVector(); |
| 194 | +Field[]fields =newField[2]; |
| 195 | +fields[0] =newField(conn,"INDEX",field.getOID("int2"),2); |
| 196 | +switch (getBaseType() ) |
| 197 | +{ |
| 198 | +caseTypes.BIT: |
| 199 | +boolean[]booleanArray = (boolean[])array; |
| 200 | +fields[1] =newField(conn,"VALUE",field.getOID("bool"),1); |
| 201 | +for(inti=0;i<booleanArray.length;i++ ) { |
| 202 | +byte[][]tuple =newbyte[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 | +caseTypes.SMALLINT: |
| 208 | +fields[1] =newField(conn,"VALUE",field.getOID("int2"),2); |
| 209 | +caseTypes.INTEGER: |
| 210 | +int[]intArray = (int[])array; |
| 211 | +if(fields[1] ==null ) |
| 212 | +fields[1] =newField(conn,"VALUE",field.getOID("int4"),4); |
| 213 | +for(inti=0;i<intArray.length;i++ ) { |
| 214 | +byte[][]tuple =newbyte[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 | +caseTypes.BIGINT: |
| 221 | +long[]longArray = (long[])array; |
| 222 | +fields[1] =newField(conn,"VALUE",field.getOID("int8"),8); |
| 223 | +for(inti=0;i<longArray.length;i++ ) { |
| 224 | +byte[][]tuple =newbyte[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 | +caseTypes.NUMERIC: |
| 231 | +BigDecimal[]bdArray = (BigDecimal[])array; |
| 232 | +fields[1] =newField(conn,"VALUE",field.getOID("numeric"), -1); |
| 233 | +for(inti=0;i<bdArray.length;i++ ) { |
| 234 | +byte[][]tuple =newbyte[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 | +caseTypes.REAL: |
| 241 | +float[]floatArray = (float[])array; |
| 242 | +fields[1] =newField(conn,"VALUE",field.getOID("float4"),4); |
| 243 | +for(inti=0;i<floatArray.length;i++ ) { |
| 244 | +byte[][]tuple =newbyte[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 | +caseTypes.DOUBLE: |
| 251 | +double[]doubleArray = (double[])array; |
| 252 | +fields[1] =newField(conn,"VALUE",field.getOID("float8"),8); |
| 253 | +for(inti=0;i<doubleArray.length;i++ ) { |
| 254 | +byte[][]tuple =newbyte[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 | +caseTypes.CHAR: |
| 261 | +fields[1] =newField(conn,"VALUE",field.getOID("char"),1); |
| 262 | +caseTypes.VARCHAR: |
| 263 | +String[]strArray = (String[])array; |
| 264 | +if(fields[1] ==null ) |
| 265 | +fields[1] =newField(conn,"VALUE",field.getOID("varchar"), -1); |
| 266 | +for(inti=0;i<strArray.length;i++ ) { |
| 267 | +byte[][]tuple =newbyte[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 | +caseTypes.DATE: |
| 274 | +java.sql.Date[]dateArray = (java.sql.Date[])array; |
| 275 | +fields[1] =newField(conn,"VALUE",field.getOID("date"),4); |
| 276 | +for(inti=0;i<dateArray.length;i++ ) { |
| 277 | +byte[][]tuple =newbyte[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 | +caseTypes.TIME: |
| 284 | +java.sql.Time[]timeArray = (java.sql.Time[])array; |
| 285 | +fields[1] =newField(conn,"VALUE",field.getOID("time"),8); |
| 286 | +for(inti=0;i<timeArray.length;i++ ) { |
| 287 | +byte[][]tuple =newbyte[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 | +caseTypes.TIMESTAMP: |
| 294 | +java.sql.Timestamp[]timestampArray = (java.sql.Timestamp[])array; |
| 295 | +fields[1] =newField(conn,"VALUE",field.getOID("timestamp"),8); |
| 296 | +for(inti=0;i<timestampArray.length;i++ ) { |
| 297 | +byte[][]tuple =newbyte[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 | +throworg.postgresql.Driver.notImplemented(); |
| 308 | +} |
| 309 | +returnnewResultSet((org.postgresql.jdbc2.Connection)conn,fields,rows,"OK",1 ); |
| 310 | +} |
| 311 | +} |
| 312 | + |