Array.java 11.5 KB
Newer Older
Bruce Momjian's avatar
Bruce Momjian committed
1 2 3 4 5 6 7 8 9
package org.postgresql.jdbc2;

import java.text.*;
import java.sql.*;
import java.util.*;
import java.math.BigDecimal;
import org.postgresql.Field;
import org.postgresql.util.*;

10
/*
Bruce Momjian's avatar
Bruce Momjian committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * Array is used collect one column of query result data.
 *
 * <p>Read a field of type Array into either a natively-typed
 * Java array object or a ResultSet.  Accessor methods provide
 * the ability to capture array slices.
 *
 * <p>Other than the constructor all methods are direct implementations
 * of those specified for java.sql.Array.  Please refer to the javadoc
 * for java.sql.Array for detailed descriptions of the functionality
 * and parameters of the methods of this class.
 *
 * @see ResultSet#getArray
 */


public class Array implements java.sql.Array
{
28 29 30 31 32
	private org.postgresql.Connection conn = null;
	private org.postgresql.Field field = null;
	private org.postgresql.jdbc2.ResultSet rs = null;
	private int idx = 0;
	private String rawString = null;
Bruce Momjian's avatar
Bruce Momjian committed
33

34
	/*
35 36 37 38 39 40 41 42 43 44
	 * Create a new Array
	 *
	 * @param conn a database connection
	 * @param idx 1-based index of the query field to load into this Array
	 * @param field the Field descriptor for the field to load into this Array
	 * @param rs the ResultSet from which to get the data for this Array
	 */
	public Array( org.postgresql.Connection conn, int idx, Field field, org.postgresql.jdbc2.ResultSet rs )
	throws SQLException
	{
Bruce Momjian's avatar
Bruce Momjian committed
45
		this.conn = conn;
46
		this.field = field;
Bruce Momjian's avatar
Bruce Momjian committed
47 48
		this.rs = rs;
		this.idx = idx;
49
		this.rawString = rs.getFixedString(idx);
Bruce Momjian's avatar
Bruce Momjian committed
50 51
	}

52 53
	public Object getArray() throws SQLException
	{
Bruce Momjian's avatar
Bruce Momjian committed
54 55 56
		return getArray( 1, 0, null );
	}

57 58
	public Object getArray(long index, int count) throws SQLException
	{
Bruce Momjian's avatar
Bruce Momjian committed
59 60 61
		return getArray( index, count, null );
	}

62 63
	public Object getArray(Map map) throws SQLException
	{
Bruce Momjian's avatar
Bruce Momjian committed
64 65 66
		return getArray( 1, 0, map );
	}

67 68 69 70
	public Object getArray(long index, int count, Map map) throws SQLException
	{
		if ( map != null ) // For now maps aren't supported.
			throw org.postgresql.Driver.notImplemented();
Bruce Momjian's avatar
Bruce Momjian committed
71

72 73
		if (index < 1)
			throw new PSQLException("postgresql.arr.range");
Bruce Momjian's avatar
Bruce Momjian committed
74 75 76
		Object retVal = null;

		ArrayList array = new ArrayList();
77

78 79 80 81
		/* Check if the String is also not an empty array
                 * otherwise there will be an exception thrown below
                 * in the ResultSet.toX with an empty string.
                 * -- Doug Fields <dfields-pg-jdbc@pexicom.com> Feb 20, 2002 */
82

83
		if ( rawString != null && !rawString.equals("{}") )
84
		{
85
			char[] chars = rawString.toCharArray();
Bruce Momjian's avatar
Bruce Momjian committed
86 87 88
			StringBuffer sbuf = new StringBuffer();
			boolean foundOpen = false;
			boolean insideString = false;
89 90 91 92 93
			for ( int i = 0; i < chars.length; i++ )
			{
				if ( chars[i] == '{' )
				{
					if ( foundOpen )  // Only supports 1-D arrays for now
Bruce Momjian's avatar
Bruce Momjian committed
94 95 96 97
						throw org.postgresql.Driver.notImplemented();
					foundOpen = true;
					continue;
				}
98 99
				if ( chars[i] == '"' )
				{
Bruce Momjian's avatar
Bruce Momjian committed
100 101 102
					insideString = !insideString;
					continue;
				}
103 104 105
				if ( (!insideString && chars[i] == ',') || chars[i] == '}' || i == chars.length - 1)
				{
					if ( chars[i] != '"' && chars[i] != '}' && chars[i] != ',' )
Bruce Momjian's avatar
Bruce Momjian committed
106 107 108 109 110 111 112 113 114
						sbuf.append(chars[i]);
					array.add( sbuf.toString() );
					sbuf = new StringBuffer();
					continue;
				}
				sbuf.append( chars[i] );
			}
		}
		String[] arrayContents = (String[]) array.toArray( new String[array.size()] );
115
		if ( count == 0 )
Bruce Momjian's avatar
Bruce Momjian committed
116 117
			count = arrayContents.length;
		index--;
118 119
		if ( index + count > arrayContents.length )
			throw new PSQLException("postgresql.arr.range");
Bruce Momjian's avatar
Bruce Momjian committed
120 121 122 123

		int i = 0;
		switch ( getBaseType() )
		{
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
			case Types.BIT:
				retVal = new boolean[ count ];
				for ( ; count > 0; count-- )
					((boolean[])retVal)[i++] = ResultSet.toBoolean( arrayContents[(int)index++] );
				break;
			case Types.SMALLINT:
			case Types.INTEGER:
				retVal = new int[ count ];
				for ( ; count > 0; count-- )
					((int[])retVal)[i++] = ResultSet.toInt( arrayContents[(int)index++] );
				break;
			case Types.BIGINT:
				retVal = new long[ count ];
				for ( ; count > 0; count-- )
					((long[])retVal)[i++] = ResultSet.toLong( arrayContents[(int)index++] );
				break;
			case Types.NUMERIC:
				retVal = new BigDecimal[ count ];
				for ( ; count > 0; count-- )
143
					((BigDecimal[])retVal)[i++] = ResultSet.toBigDecimal( arrayContents[(int)index++], 0 );
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
				break;
			case Types.REAL:
				retVal = new float[ count ];
				for ( ; count > 0; count-- )
					((float[])retVal)[i++] = ResultSet.toFloat( arrayContents[(int)index++] );
				break;
			case Types.DOUBLE:
				retVal = new double[ count ];
				for ( ; count > 0; count-- )
					((double[])retVal)[i++] = ResultSet.toDouble( arrayContents[(int)index++] );
				break;
			case Types.CHAR:
			case Types.VARCHAR:
				retVal = new String[ count ];
				for ( ; count > 0; count-- )
					((String[])retVal)[i++] = arrayContents[(int)index++];
				break;
			case Types.DATE:
				retVal = new java.sql.Date[ count ];
				for ( ; count > 0; count-- )
					((java.sql.Date[])retVal)[i++] = ResultSet.toDate( arrayContents[(int)index++] );
				break;
			case Types.TIME:
				retVal = new java.sql.Time[ count ];
				for ( ; count > 0; count-- )
169
					((java.sql.Time[])retVal)[i++] = ResultSet.toTime( arrayContents[(int)index++], rs, getBaseTypeName() );
170 171 172 173 174
				break;
			case Types.TIMESTAMP:
				retVal = new Timestamp[ count ];
				StringBuffer sbuf = null;
				for ( ; count > 0; count-- )
175
					((java.sql.Timestamp[])retVal)[i++] = ResultSet.toTimestamp( arrayContents[(int)index++], rs, getBaseTypeName() );
176
				break;
Bruce Momjian's avatar
Bruce Momjian committed
177

178 179 180 181
				// Other datatypes not currently supported.  If you are really using other types ask
				// yourself if an array of non-trivial data types is really good database design.
			default:
				throw org.postgresql.Driver.notImplemented();
Bruce Momjian's avatar
Bruce Momjian committed
182 183 184 185
		}
		return retVal;
	}

186 187 188
	public int getBaseType() throws SQLException
	{
		return conn.getSQLType(getBaseTypeName());
Bruce Momjian's avatar
Bruce Momjian committed
189 190
	}

191 192 193 194
	public String getBaseTypeName() throws SQLException
	{
		String fType = field.getPGType();
		if ( fType.charAt(0) == '_' )
Bruce Momjian's avatar
Bruce Momjian committed
195 196 197 198
			fType = fType.substring(1);
		return fType;
	}

199 200
	public java.sql.ResultSet getResultSet() throws SQLException
	{
Bruce Momjian's avatar
Bruce Momjian committed
201 202 203
		return getResultSet( 1, 0, null );
	}

204 205
	public java.sql.ResultSet getResultSet(long index, int count) throws SQLException
	{
Bruce Momjian's avatar
Bruce Momjian committed
206 207 208
		return getResultSet( index, count, null );
	}

209 210
	public java.sql.ResultSet getResultSet(Map map) throws SQLException
	{
Bruce Momjian's avatar
Bruce Momjian committed
211 212 213
		return getResultSet( 1, 0, map );
	}

214 215
	public java.sql.ResultSet getResultSet(long index, int count, java.util.Map map) throws SQLException
	{
Bruce Momjian's avatar
Bruce Momjian committed
216 217 218
		Object array = getArray( index, count, map );
		Vector rows = new Vector();
		Field[] fields = new Field[2];
219
		fields[0] = new Field(conn, "INDEX", conn.getOID("int2"), 2);
Bruce Momjian's avatar
Bruce Momjian committed
220 221
		switch ( getBaseType() )
		{
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
			case Types.BIT:
				boolean[] booleanArray = (boolean[]) array;
				fields[1] = new Field(conn, "VALUE", conn.getOID("bool"), 1);
				for ( int i = 0; i < booleanArray.length; i++ )
				{
					byte[][] tuple = new byte[2][0];
					tuple[0] = conn.getEncoding().encode( Integer.toString((int)index + i) ); // Index
					tuple[1] = conn.getEncoding().encode( (booleanArray[i] ? "YES" : "NO") ); // Value
					rows.addElement(tuple);
				}
			case Types.SMALLINT:
				fields[1] = new Field(conn, "VALUE", conn.getOID("int2"), 2);
			case Types.INTEGER:
				int[] intArray = (int[]) array;
				if ( fields[1] == null )
					fields[1] = new Field(conn, "VALUE", conn.getOID("int4"), 4);
				for ( int i = 0; i < intArray.length; i++ )
				{
					byte[][] tuple = new byte[2][0];
					tuple[0] = conn.getEncoding().encode( Integer.toString((int)index + i) ); // Index
					tuple[1] = conn.getEncoding().encode( Integer.toString(intArray[i]) ); // Value
					rows.addElement(tuple);
				}
				break;
			case Types.BIGINT:
				long[] longArray = (long[]) array;
				fields[1] = new Field(conn, "VALUE", conn.getOID("int8"), 8);
				for ( int i = 0; i < longArray.length; i++ )
				{
					byte[][] tuple = new byte[2][0];
					tuple[0] = conn.getEncoding().encode( Integer.toString((int)index + i) ); // Index
					tuple[1] = conn.getEncoding().encode( Long.toString(longArray[i]) ); // Value
					rows.addElement(tuple);
				}
				break;
			case Types.NUMERIC:
				BigDecimal[] bdArray = (BigDecimal[]) array;
				fields[1] = new Field(conn, "VALUE", conn.getOID("numeric"), -1);
				for ( int i = 0; i < bdArray.length; i++ )
				{
					byte[][] tuple = new byte[2][0];
					tuple[0] = conn.getEncoding().encode( Integer.toString((int)index + i) ); // Index
					tuple[1] = conn.getEncoding().encode( bdArray[i].toString() ); // Value
					rows.addElement(tuple);
				}
				break;
			case Types.REAL:
				float[] floatArray = (float[]) array;
				fields[1] = new Field(conn, "VALUE", conn.getOID("float4"), 4);
				for ( int i = 0; i < floatArray.length; i++ )
				{
					byte[][] tuple = new byte[2][0];
					tuple[0] = conn.getEncoding().encode( Integer.toString((int)index + i) ); // Index
					tuple[1] = conn.getEncoding().encode( Float.toString(floatArray[i]) ); // Value
					rows.addElement(tuple);
				}
				break;
			case Types.DOUBLE:
				double[] doubleArray = (double[]) array;
				fields[1] = new Field(conn, "VALUE", conn.getOID("float8"), 8);
				for ( int i = 0; i < doubleArray.length; i++ )
				{
					byte[][] tuple = new byte[2][0];
					tuple[0] = conn.getEncoding().encode( Integer.toString((int)index + i) ); // Index
					tuple[1] = conn.getEncoding().encode( Double.toString(doubleArray[i]) ); // Value
					rows.addElement(tuple);
				}
				break;
			case Types.CHAR:
				fields[1] = new Field(conn, "VALUE", conn.getOID("char"), 1);
			case Types.VARCHAR:
				String[] strArray = (String[]) array;
				if ( fields[1] == null )
					fields[1] = new Field(conn, "VALUE", conn.getOID("varchar"), -1);
				for ( int i = 0; i < strArray.length; i++ )
				{
					byte[][] tuple = new byte[2][0];
					tuple[0] = conn.getEncoding().encode( Integer.toString((int)index + i) ); // Index
					tuple[1] = conn.getEncoding().encode( strArray[i] ); // Value
					rows.addElement(tuple);
				}
				break;
			case Types.DATE:
				java.sql.Date[] dateArray = (java.sql.Date[]) array;
				fields[1] = new Field(conn, "VALUE", conn.getOID("date"), 4);
				for ( int i = 0; i < dateArray.length; i++ )
				{
					byte[][] tuple = new byte[2][0];
					tuple[0] = conn.getEncoding().encode( Integer.toString((int)index + i) ); // Index
					tuple[1] = conn.getEncoding().encode( dateArray[i].toString() ); // Value
					rows.addElement(tuple);
				}
				break;
			case Types.TIME:
				java.sql.Time[] timeArray = (java.sql.Time[]) array;
				fields[1] = new Field(conn, "VALUE", conn.getOID("time"), 8);
				for ( int i = 0; i < timeArray.length; i++ )
				{
					byte[][] tuple = new byte[2][0];
					tuple[0] = conn.getEncoding().encode( Integer.toString((int)index + i) ); // Index
					tuple[1] = conn.getEncoding().encode( timeArray[i].toString() ); // Value
					rows.addElement(tuple);
				}
				break;
			case Types.TIMESTAMP:
				java.sql.Timestamp[] timestampArray = (java.sql.Timestamp[]) array;
				fields[1] = new Field(conn, "VALUE", conn.getOID("timestamp"), 8);
				for ( int i = 0; i < timestampArray.length; i++ )
				{
					byte[][] tuple = new byte[2][0];
					tuple[0] = conn.getEncoding().encode( Integer.toString((int)index + i) ); // Index
					tuple[1] = conn.getEncoding().encode( timestampArray[i].toString() ); // Value
					rows.addElement(tuple);
				}
				break;
Bruce Momjian's avatar
Bruce Momjian committed
337

338 339 340 341
				// Other datatypes not currently supported.  If you are really using other types ask
				// yourself if an array of non-trivial data types is really good database design.
			default:
				throw org.postgresql.Driver.notImplemented();
Bruce Momjian's avatar
Bruce Momjian committed
342
		}
343
		return new ResultSet((org.postgresql.jdbc2.Connection)conn, fields, rows, "OK", 1 );
Bruce Momjian's avatar
Bruce Momjian committed
344
	}
345

346 347 348 349
	public String toString()
	{
		return rawString;
	}
Bruce Momjian's avatar
Bruce Momjian committed
350 351
}