1 55 package org.lateralnz.c3d; 56 57 import java.sql.Array ; 58 import java.sql.ParameterMetaData ; 59 import java.sql.PreparedStatement ; 60 import java.sql.ResultSet ; 61 import java.sql.ResultSetMetaData ; 62 import java.sql.Statement ; 63 import java.sql.SQLException ; 64 import java.sql.Types ; 65 66 import org.lateralnz.common.util.Constants; 67 import org.lateralnz.common.util.DAOUtils; 68 import org.lateralnz.common.util.StringUtils; 69 70 import org.lateralnz.c3d.util.Column; 71 import org.lateralnz.c3d.util.ResultWrapper; 72 73 public class DCPreparedStatement extends DCStatement implements PreparedStatement , Constants { 74 private static final byte[] NULL_BYTES = "null".getBytes(); 75 protected PreparedStatement realPS; 76 protected PreparedStatement queryUpdateRowsPS = null; 77 protected String queryUpdateRowsSQL = EMPTY; 78 79 private int paramCount = 0; 80 protected Column[] params; 81 82 public DCPreparedStatement(DCConnection conn, String sql, int resultsetType, int resultsetConcurrency, int resultsetHoldability, int autoGeneratedKeys, int[] columnIndexes, String [] columnNames) throws SQLException { 83 super(conn, resultsetType, resultsetConcurrency, resultsetHoldability); 84 this.statementType = PREPARED; 85 setSQL(sql); 86 87 if (resultsetType != Integer.MIN_VALUE && resultsetConcurrency != Integer.MIN_VALUE && resultsetHoldability != Integer.MIN_VALUE) { 88 realPS = conn.getRealConnection().prepareStatement(sql, resultsetType, resultsetConcurrency, resultsetHoldability); 89 } 90 else if (resultsetType != Integer.MIN_VALUE && resultsetConcurrency != Integer.MIN_VALUE) { 91 realPS = conn.getRealConnection().prepareStatement(sql, resultsetType, resultsetConcurrency); 92 } 93 else if (autoGeneratedKeys != Integer.MIN_VALUE) { 94 realPS = conn.getRealConnection().prepareStatement(sql, autoGeneratedKeys); 95 } 96 else if (columnIndexes != null) { 97 realPS = conn.getRealConnection().prepareStatement(sql, columnIndexes); 98 } 99 else if (columnNames != null) { 100 realPS = conn.getRealConnection().prepareStatement(sql, columnNames); 101 } 102 else { 103 realPS = conn.getRealConnection().prepareStatement(sql); 104 } 105 106 realStatement = realPS; 107 108 paramCount = StringUtils.countOccurrences(sql, '?'); 109 clearParameters(); 110 } 111 112 public void addBatch() throws SQLException { 113 } 114 115 public void clearParameters() throws SQLException { 116 realPS.clearParameters(); 117 params = new Column[paramCount]; 118 for (int i = 0; i < paramCount; i++) { 119 params[i] = new Column(); 120 } 121 } 122 123 public void close() throws SQLException { 124 try { 125 super.close(); 126 } 127 finally { 128 DAOUtils.close(queryUpdateRowsPS); 129 } 130 } 131 132 public boolean execute() throws SQLException { 133 ResultWrapper rw = conn.getDatabaseEngine().execute(this); 134 if (rw != null) { 135 rs = rw.rs; 136 updateCount = rw.updateCount; 137 } 138 139 if (rs != null) { 140 return true; 141 } 142 else { 143 return false; 144 } 145 } 146 147 public ResultSet executeQuery() throws SQLException { 148 if (execute()) { 149 return getResultSet(); 150 } 151 else { 152 throw new SQLException ("query failed"); 153 } 154 } 155 156 public int executeUpdate() throws SQLException { 157 if (!execute()) { 158 return getUpdateCount(); 159 } 160 else { 161 return -1; 162 } 163 } 164 165 public ResultSetMetaData getMetaData() throws SQLException { 166 if (getResultSet() != null) { 167 return getResultSet().getMetaData(); 168 } 169 else { 170 throw new SQLException ("operation not supported"); 171 } 172 } 173 174 public ParameterMetaData getParameterMetaData() throws SQLException { 175 return realPS.getParameterMetaData(); 176 } 177 178 public Statement getRealStatement() { 179 return realPS; 180 } 181 182 public void setArray(int param, Array array) throws SQLException { 183 realPS.setArray(param, array); 184 nocaching = true; 185 } 186 187 public void setAsciiStream(int param, java.io.InputStream inputStream, int param2) throws SQLException { 188 realPS.setAsciiStream(param, inputStream, param2); 189 nocaching = true; 190 } 191 192 public void setBigDecimal(int param, java.math.BigDecimal bigDecimal) throws SQLException { 193 realPS.setBigDecimal(param, bigDecimal); 194 param--; 195 params[param].setObjectValue(bigDecimal); 196 params[param].setType(Types.NUMERIC); 197 } 198 199 public void setBinaryStream(int param, java.io.InputStream inputStream, int param2) throws SQLException { 200 realPS.setBinaryStream(param, inputStream, param2); 201 nocaching = true; 202 } 203 204 public void setBlob(int param, java.sql.Blob blob) throws SQLException { 205 realPS.setBlob(param, blob); 206 nocaching = true; 207 } 208 209 public void setBoolean(int param, boolean param1) throws SQLException { 210 realPS.setBoolean(param, param1); 211 param--; 212 if (param1) { 213 params[param].setObjectValue(TRUE); 214 } 215 else { 216 params[param].setObjectValue(FALSE); 217 } 218 params[param].setType(Types.BOOLEAN); 219 } 220 221 public void setByte(int param, byte param1) throws SQLException { 222 realPS.setByte(param, param1); 223 param--; 224 params[param].setObjectValue(new Byte (param1)); 225 params[param].setType(Types.TINYINT); 226 } 227 228 public void setBytes(int param, byte[] values) throws SQLException { 229 realPS.setBytes(param, values); 230 param--; 231 params[param].setValue(values); 232 params[param].setType(Types.VARBINARY); 233 } 234 235 public void setCharacterStream(int param, java.io.Reader reader, int param2) throws SQLException { 236 realPS.setCharacterStream(param, reader, param2); 237 nocaching = true; 238 } 239 240 public void setClob(int param, java.sql.Clob clob) throws SQLException { 241 realPS.setClob(param, clob); 242 nocaching = true; 243 } 244 245 public void setDate(int param, java.sql.Date date) throws SQLException { 246 realPS.setDate(param, date); 247 param--; 248 String s = date.toString(); 249 params[param].setObjectValue(s); 250 params[param].setType(Types.DATE); 251 } 252 253 public void setDate(int param, java.sql.Date date, java.util.Calendar calendar) throws SQLException { 254 realPS.setDate(param, date, calendar); 255 param--; 256 String s = date.toString(); 257 params[param].setObjectValue(s); 258 params[param].setType(Types.DATE); 259 } 260 261 public void setDouble(int param, double param1) throws SQLException { 262 realPS.setDouble(param, param1); 263 param--; 264 params[param].setObjectValue(new Double (param1)); 265 params[param].setType(Types.DOUBLE); 266 } 267 268 public void setFloat(int param, float param1) throws SQLException { 269 realPS.setFloat(param, param1); 270 param--; 271 params[param].setObjectValue(new Float (param1)); 272 params[param].setType(Types.FLOAT); 273 } 274 275 public void setInt(int param, int param1) throws SQLException { 276 realPS.setInt(param, param1); 277 param--; 278 params[param].setObjectValue(new Integer (param1)); 279 params[param].setType(Types.INTEGER); 280 } 281 282 public void setLong(int param, long param1) throws SQLException { 283 realPS.setLong(param, param1); 284 param--; 285 params[param].setObjectValue(new Long (param1)); 286 params[param].setType(Types.BIGINT); 287 } 288 289 public void setNull(int param, int param1) throws SQLException { 290 realPS.setNull(param, param1); 291 param--; 292 params[param].setType(param1); 293 params[param].setNull(true); 294 } 295 296 public void setNull(int param, int param1, String str) throws SQLException { 297 realPS.setNull(param, param1, str); 298 param--; 299 params[param].setObjectValue(str); 300 params[param].setType(param1); 301 params[param].setNull(true); 302 } 303 304 public void setObject(int param, Object obj) throws SQLException { 305 realPS.setObject(param, obj); 306 nocaching = true; 307 } 308 309 public void setObject(int param, Object obj, int param2) throws SQLException { 310 realPS.setObject(param, obj, param2); 311 nocaching = true; 312 } 313 314 public void setObject(int param, Object obj, int param2, int param3) throws SQLException { 315 realPS.setObject(param, obj, param2, param3); 316 nocaching = true; 317 } 318 319 public void setRef(int param, java.sql.Ref ref) throws SQLException { 320 realPS.setRef(param, ref); 321 nocaching = true; 322 } 323 324 public void setShort(int param, short param1) throws SQLException { 325 realPS.setShort(param, param1); 326 param--; 327 params[param].setObjectValue(new Short (param1)); 328 params[param].setType(Types.SMALLINT); 329 } 330 331 public void setString(int param, String str) throws SQLException { 332 realPS.setString(param, str); 333 param--; 334 params[param].setObjectValue(str); 335 params[param].setType(Types.VARCHAR); 336 } 337 338 public void setTime(int param, java.sql.Time time) throws SQLException { 339 realPS.setTime(param, time); 340 param--; 341 params[param].setObjectValue(time); 342 params[param].setType(Types.TIME); 343 } 344 345 public void setTime(int param, java.sql.Time time, java.util.Calendar calendar) throws SQLException { 346 realPS.setTime(param, time, calendar); 347 param--; 348 params[param].setObjectValue(time); 349 params[param].setType(Types.TIME); 350 } 351 352 public void setTimestamp(int param, java.sql.Timestamp timestamp) throws SQLException { 353 realPS.setTimestamp(param, timestamp); 354 param--; 355 params[param].setObjectValue(timestamp); 356 params[param].setType(Types.TIMESTAMP); 357 } 358 359 public void setTimestamp(int param, java.sql.Timestamp timestamp, java.util.Calendar calendar) throws SQLException { 360 realPS.setTimestamp(param, timestamp, calendar); 361 param--; 362 params[param].setObjectValue(timestamp); 363 params[param].setType(Types.TIMESTAMP); 364 } 365 366 public void setURL(int param, java.net.URL url) throws SQLException { 367 realPS.setURL(param, url); 368 nocaching = true; 369 } 370 371 public void setUnicodeStream(int param, java.io.InputStream inputStream, int param2) throws SQLException { 372 realPS.setUnicodeStream(param, inputStream, param2); 373 nocaching = true; 374 } 375 376 } | Popular Tags |