1 29 package com.caucho.db.jdbc; 30 31 import com.caucho.db.sql.Query; 32 import com.caucho.db.sql.QueryContext; 33 import com.caucho.db.store.Transaction; 34 35 import java.io.ByteArrayInputStream ; 36 import java.io.InputStream ; 37 import java.io.Reader ; 38 import java.sql.*; 39 import java.util.Calendar ; 40 41 44 public class PreparedStatementImpl extends StatementImpl 45 implements PreparedStatement { 46 47 private Query _query; 48 private int _updateCount; 49 private boolean _wasResultSet; 50 private ResultSet _resultSet; 51 52 private boolean _isReturnGeneratedKeys; 53 54 PreparedStatementImpl(ConnectionImpl conn, Query query) 55 { 56 super(conn); 57 58 _query = query; 59 } 60 61 void setReturnGeneratedKeys(boolean isReturnGeneratedKeys) 62 { 63 _isReturnGeneratedKeys = isReturnGeneratedKeys; 64 } 65 66 public java.sql.ResultSetMetaData getMetaData() 67 { 68 return null; 69 } 70 71 public void clearParameters() 72 throws SQLException 73 { 74 _query.clearParameters(); 75 } 77 78 public void setNull(int parameter, int sqlType) 79 throws SQLException 80 { 81 _query.setString(parameter, null); 82 } 83 84 public void setNull(int parameter, int sqlType, String typeName) 85 throws SQLException 86 { 87 _query.setString(parameter, null); 88 } 89 90 public void setBoolean(int parameter, boolean x) 91 throws SQLException 92 { 93 _query.setBoolean(parameter, x); 94 } 95 96 public void setByte(int parameter, byte x) 97 throws SQLException 98 { 99 _query.setLong(parameter, x); 100 } 101 102 public void setShort(int parameter, short x) 103 throws SQLException 104 { 105 _query.setLong(parameter, x); 106 } 107 108 public void setInt(int parameter, int x) 109 throws SQLException 110 { 111 _query.setLong(parameter, x); 112 } 113 114 public void setLong(int parameter, long x) 115 throws SQLException 116 { 117 _query.setLong(parameter, x); 118 } 119 120 public void setFloat(int parameter, float x) 121 throws SQLException 122 { 123 _query.setDouble(parameter, x); 124 } 125 126 public void setDouble(int parameter, double x) 127 throws SQLException 128 { 129 _query.setDouble(parameter, x); 130 } 131 132 public void setBigDecimal(int parameter, java.math.BigDecimal x) 133 throws SQLException 134 { 135 throw new UnsupportedOperationException (); 136 } 137 138 public void setString(int parameter, String x) 139 throws SQLException 140 { 141 _query.setString(parameter, x); 142 } 143 144 public void setBytes(int parameter, byte []x) 145 throws SQLException 146 { 147 if (x != null) { 148 ByteArrayInputStream bis = new ByteArrayInputStream (x); 149 150 _query.setBinaryStream(parameter, bis, x.length); 151 } 152 else 153 setNull(parameter, 0); 154 } 155 156 public void setDate(int parameter, java.sql.Date x, Calendar calendar) 157 throws SQLException 158 { 159 setDate(parameter, x); 160 } 161 162 public void setDate(int parameter, java.sql.Date x) 163 throws SQLException 164 { 165 if (x != null) 166 setTime(parameter, x.getTime()); 167 else 168 setNull(parameter, 0); 169 } 170 171 public void setTime(int parameter, Time x, Calendar calendar) 172 throws SQLException 173 { 174 setTime(parameter, x); 175 } 176 177 public void setTime(int parameter, Time x) 178 throws SQLException 179 { 180 if (x != null) 181 setTime(parameter, x.getTime()); 182 else 183 setNull(parameter, 0); 184 } 185 186 public void setTimestamp(int parameter, Timestamp x, Calendar calendar) 187 throws SQLException 188 { 189 setTimestamp(parameter, x); 190 } 191 192 public void setTimestamp(int parameter, Timestamp x) 193 throws SQLException 194 { 195 if (x != null) 196 setTime(parameter, x.getTime()); 197 else 198 setNull(parameter, 0); 199 } 200 201 private void setTime(int parameter, long now) 202 throws SQLException 203 { 204 _query.setDate(parameter, now); 205 } 206 207 public void setAsciiStream(int parameter, InputStream is, int len) 208 throws SQLException 209 { 210 throw new UnsupportedOperationException (); 211 } 212 213 public void setUnicodeStream(int parameter, InputStream is, int len) 214 throws SQLException 215 { 216 throw new UnsupportedOperationException (); 217 } 218 219 public void setBinaryStream(int parameter, InputStream is, int len) 220 throws SQLException 221 { 222 _query.setBinaryStream(parameter, is, len); 223 } 224 225 public void setCharacterStream(int parameter, Reader is, int len) 226 throws SQLException 227 { 228 throw new UnsupportedOperationException (); 229 } 230 231 public void setObject(int parameter, Object x, int target, int scale) 232 throws SQLException 233 { 234 throw new UnsupportedOperationException (); 235 } 236 237 public void setObject(int parameter, Object x, int target) 238 throws SQLException 239 { 240 throw new UnsupportedOperationException (); 241 } 242 243 public void setObject(int parameter, Object x) 244 throws SQLException 245 { 246 if (x instanceof String ) 247 setString(parameter, (String ) x); 248 else if (x instanceof Number ) { 249 Number number = (Number ) x; 250 251 if (x instanceof Double ) 252 setDouble(parameter, number.doubleValue()); 253 else if (x instanceof java.lang.Float ) 254 setDouble(parameter, number.doubleValue()); 255 else 256 setLong(parameter, number.longValue()); 257 } 258 else if (x instanceof java.sql.Time ) 259 setTime(parameter, (java.sql.Time ) x); 260 else if (x instanceof java.sql.Timestamp ) 261 setTimestamp(parameter, (java.sql.Timestamp ) x); 262 else if (x instanceof java.sql.Date ) 263 setDate(parameter, (java.sql.Date ) x); 264 else { 265 throw new UnsupportedOperationException (); 266 } 267 } 268 269 public void setRef(int parameter, Ref x) 270 throws SQLException 271 { 272 throw new UnsupportedOperationException (); 273 } 274 275 public void setBlob(int parameter, Blob x) 276 throws SQLException 277 { 278 throw new UnsupportedOperationException (); 279 } 280 281 public void setClob(int parameter, Clob x) 282 throws SQLException 283 { 284 throw new UnsupportedOperationException (); 285 } 286 287 public void setArray(int parameter, Array x) 288 throws SQLException 289 { 290 throw new UnsupportedOperationException (); 291 } 292 293 public void addBatch() 294 throws SQLException 295 { 296 } 297 298 public java.sql.ResultSet executeQuery() 299 throws SQLException 300 { 301 execute(); 302 303 if (_wasResultSet) 304 return _resultSet; 305 else 306 throw new SQLException("missing result set"); 307 } 308 309 public int executeUpdate() 310 throws SQLException 311 { 312 execute(); 313 314 return getUpdateCount(); 315 } 316 317 private int _count; 318 319 public boolean execute() 320 throws SQLException 321 { 322 _count++; 323 324 Transaction xa = null; 325 326 try { 327 if (_count != 1) 328 throw new IllegalStateException ("Multithreading execute"); 329 330 xa = _conn.getTransaction(); 331 QueryContext queryContext = getQueryContext(); 332 333 if (_query.isSelect()) { 334 com.caucho.db.ResultSetImpl rs = null; 335 336 _query.execute(queryContext, xa); 337 338 _wasResultSet = true; 339 _resultSet = new ResultSetImpl(this, queryContext.getResult()); 340 341 return true; 342 } 343 else { 344 queryContext.setReturnGeneratedKeys(_isReturnGeneratedKeys); 345 346 _query.execute(queryContext, xa); 347 348 _wasResultSet = false; 349 return false; 350 } 351 } finally { 352 _count--; 353 354 if (xa != null && xa.isAutoCommit()) 355 xa.rollback(); 356 } 357 } 358 359 public void setURL(int foo, java.net.URL url) 361 { 362 throw new UnsupportedOperationException (); 363 } 364 365 public ParameterMetaData getParameterMetaData() 366 { 367 throw new UnsupportedOperationException (); 368 } 369 } 370 | Popular Tags |