1 29 package com.caucho.db.jdbc; 30 31 import com.caucho.db.Database; 32 import com.caucho.db.sql.Query; 33 import com.caucho.db.sql.QueryContext; 34 import com.caucho.db.sql.SelectQuery; 35 import com.caucho.db.store.Transaction; 36 import com.caucho.util.L10N; 37 38 import java.sql.SQLException ; 39 import java.sql.SQLWarning ; 40 41 44 public class StatementImpl implements java.sql.Statement { 45 private final static L10N L = new L10N(StatementImpl.class); 46 47 protected Database _db; 48 protected final ConnectionImpl _conn; 49 50 protected ResultSetImpl _rs; 51 52 protected QueryContext _queryContext; 53 54 private boolean _isClosed; 55 56 StatementImpl(ConnectionImpl conn) 57 { 58 _conn = conn; 59 _db = conn.getDatabase(); 60 61 if (_db == null) 62 throw new NullPointerException (); 63 64 init(); 65 } 66 67 protected void init() 68 { 69 if (_queryContext == null) 70 _queryContext = QueryContext.allocate(); 71 } 72 73 protected QueryContext getQueryContext() 74 { 75 if (_queryContext == null) 76 _queryContext = QueryContext.allocate(); 77 78 return _queryContext; 79 } 80 81 public void addBatch(String sql) 82 { 83 } 84 85 public void cancel() 86 { 87 } 88 89 public void clearBatch() 90 { 91 } 92 93 public void clearWarnings() 94 { 95 } 96 97 public java.sql.ResultSet executeQuery(String sql) 98 throws SQLException 99 { 100 if (_db == null) 101 throw new SQLException (L.l("statement is closed")); 102 103 Query query = _db.parseQuery(sql); 104 105 return executeQuery(query); 106 } 107 108 private java.sql.ResultSet executeQuery(Query query) 109 throws SQLException 110 { 111 Transaction xa = _conn.getTransaction(); 112 113 boolean isOkay = false; 114 try { 115 query.execute(_queryContext, xa); 116 isOkay = true; 117 } finally { 118 if (! xa.isAutoCommit()) { 119 } 120 else if (isOkay) 121 xa.commit(); 122 else 123 xa.rollback(); 124 } 125 126 _rs = new ResultSetImpl(this, _queryContext.getResult()); 127 128 return _rs; 129 } 130 131 138 public int executeUpdate(String sql) 139 throws SQLException 140 { 141 Query query = _db.parseQuery(sql); 142 143 return executeUpdate(query); 144 } 145 146 public int executeUpdate(String sql, int autoGeneratedKeys) 147 throws SQLException 148 { 149 Query query = _db.parseQuery(sql); 150 151 _queryContext.setReturnGeneratedKeys(true); 152 153 return executeUpdate(query); 154 } 155 156 public int executeUpdate(String sql, int []columnIndexes) 157 throws SQLException 158 { 159 Query query = _db.parseQuery(sql); 160 161 _queryContext.setReturnGeneratedKeys(true); 162 163 return executeUpdate(query); 164 } 165 166 public int executeUpdate(String sql, String []columnNames) 167 throws SQLException 168 { 169 Query query = _db.parseQuery(sql); 170 171 _queryContext.setReturnGeneratedKeys(true); 172 173 return executeUpdate(query); 174 } 175 176 181 private int executeUpdate(Query query) 182 throws SQLException 183 { 184 Transaction xa = _conn.getTransaction(); 185 boolean isOkay = false; 186 _queryContext.setTransaction(xa); 187 188 try { 189 query.execute(_queryContext, xa); 190 isOkay = true; 191 } finally { 192 if (! xa.isAutoCommit()) { 193 } 194 else if (isOkay) 195 xa.commit(); 196 else 197 xa.rollback(); 198 } 199 200 return _queryContext.getRowUpdateCount(); 201 } 202 203 public java.sql.ResultSet getGeneratedKeys() 204 { 205 java.sql.ResultSet rs = _queryContext.getGeneratedKeysResultSet(); 206 207 if (rs != null) 208 return rs; 209 else 210 return new NullResultSet(); 211 } 212 213 public boolean execute(String sql) 214 throws SQLException 215 { 216 Query query = _db.parseQuery(sql); 217 218 if (query instanceof SelectQuery) { 219 executeQuery(query); 220 221 return true; 222 } 223 else { 224 executeUpdate(query); 225 226 return false; 227 } 228 } 229 230 public int[]executeBatch() 231 throws SQLException 232 { 233 return null; 234 } 235 236 public java.sql.ResultSet getResultSet() 237 { 238 return _rs; 239 } 240 241 public int getUpdateCount() 242 { 243 return _queryContext.getRowUpdateCount(); 244 } 245 246 public java.sql.Connection getConnection() 247 { 248 return _conn; 249 } 250 251 public int getFetchDirection() 252 { 253 return 0; 254 } 255 256 public int getFetchSize() 257 { 258 return 0; 259 } 260 261 public int getMaxFieldSize() 262 { 263 return 0; 264 } 265 266 public int getMaxRows() 267 { 268 return 0; 269 } 270 271 public void setMaxRows(int max) 272 { 273 } 274 275 public boolean getMoreResults() 276 { 277 return false; 278 } 279 280 public int getQueryTimeout() 281 { 282 return 0; 283 } 284 285 public int getResultSetConcurrency() 286 { 287 return 0; 288 } 289 290 public int getResultSetType() 291 { 292 return 0; 293 } 294 295 public SQLWarning getWarnings() 296 { 297 return null; 298 } 299 300 public void setCursorName(String name) 301 { 302 } 303 304 public void setEscapeProcessing(boolean enable) 305 { 306 } 307 308 public void setFetchDirection(int direction) 309 { 310 } 311 312 public void setFetchSize(int rows) 313 { 314 } 315 316 public void setMaxFieldSize(int max) 317 { 318 } 319 320 public void setQueryTimeout(int seconds) 321 { 322 } 323 324 public boolean getMoreResults(int count) 326 { 327 throw new UnsupportedOperationException (); 328 } 329 330 public boolean execute(String query, int foo) 331 { 332 throw new UnsupportedOperationException (); 333 } 334 335 public boolean execute(String query, int []foo) 336 { 337 throw new UnsupportedOperationException (); 338 } 339 340 public boolean execute(String query, String []foo) 341 { 342 throw new UnsupportedOperationException (); 343 } 344 345 public int getResultSetHoldability() 346 { 347 throw new UnsupportedOperationException (); 348 } 349 350 public void close() 351 throws SQLException 352 { 353 _db = null; 354 355 ResultSetImpl rs = _rs; 356 _rs = null; 357 358 QueryContext queryContext = _queryContext; 359 _queryContext = null; 360 361 if (rs != null) 362 rs.close(); 363 364 _conn.closeStatement(this); 365 366 if (queryContext != null) 367 QueryContext.free(queryContext); 368 } 369 } 370 | Popular Tags |