1 30 31 package com.genimen.djeneric.jdbc; 32 33 import java.sql.CallableStatement ; 34 import java.sql.Connection ; 35 import java.sql.DatabaseMetaData ; 36 import java.sql.PreparedStatement ; 37 import java.sql.ResultSet ; 38 import java.sql.SQLException ; 39 import java.sql.SQLWarning ; 40 import java.sql.Savepoint ; 41 import java.sql.Statement ; 42 import java.util.ArrayList ; 43 import java.util.HashMap ; 44 import java.util.Iterator ; 45 import java.util.List ; 46 import java.util.Map ; 47 48 import com.genimen.djeneric.repository.exceptions.DjenericException; 49 import com.genimen.djeneric.repository.rdbms.RdbmsPersistenceManager; 50 import com.genimen.djeneric.repository.rdbms.RdbmsSession; 51 import com.genimen.djeneric.util.DjLogger; 52 53 59 60 public class DjConnection implements Connection 61 { 62 private static final String CLASSNAME = DjConnection.class.getName(); 63 private RdbmsPersistenceManager _persistMan = null; 64 private RdbmsSession _session = null; 65 private DjDriver _driver = null; 66 private boolean _closed = false; 67 69 private boolean _isClosing = false; 70 private List _allStatements = new ArrayList (); 71 72 75 DjConnection(DjDriver p_driver, RdbmsPersistenceManager p_mana) throws SQLException 76 { 77 _driver = p_driver; 78 _persistMan = p_mana; 79 try 80 { 81 _session = (RdbmsSession) _persistMan.createSession(); 82 } 83 catch (DjenericException ex) 84 { 85 DjLogger.log(ex); 86 throw new SQLException (ex.toString()); 87 } 88 } 89 90 public Statement createStatement() throws SQLException 91 { 92 DjStatement stat = new DjStatement(this); 93 _allStatements.add(stat); 94 return stat; 95 } 96 97 public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException 98 { 99 DjStatement stat = new DjStatement(this, resultSetType, resultSetConcurrency); 100 _allStatements.add(stat); 101 return stat; 102 } 103 104 public PreparedStatement prepareStatement(String sql) throws SQLException 105 { 106 DjPreparedStatement stat = new DjPreparedStatement(this, sql); 107 _allStatements.add(stat); 108 return stat; 109 } 110 111 public CallableStatement prepareCall(String sql) throws SQLException 112 { 113 throw new SQLException ("djeneric doesn't support callable statements"); 114 } 115 116 public String nativeSQL(String sql) throws SQLException 117 { 118 String st = translateSqlToPolymorph(sql); 119 return st; 120 } 121 122 public void setAutoCommit(boolean autoCommit) throws SQLException 123 { 124 throw new SQLException ("setAutoCommit not implemented"); 125 } 126 127 public boolean getAutoCommit() throws SQLException 128 { 129 return false; 130 } 131 132 public void commit() throws SQLException 133 { 134 try 135 { 136 _session.commit(); 137 } 138 catch (DjenericException ex) 139 { 140 DjLogger.log(ex); 141 throw new SQLException (ex.toString()); 142 } 143 } 144 145 public void rollback() throws SQLException 146 { 147 try 148 { 149 _session.rollback(); 150 } 151 catch (DjenericException ex) 152 { 153 DjLogger.log(ex); 154 throw new SQLException (ex.toString()); 155 } 156 } 157 158 public void close() throws SQLException 159 { 160 if (_closed) return; 161 _isClosing = true; 162 Iterator it = _allStatements.iterator(); 163 while (it.hasNext()) 164 { 165 Object stat = it.next(); 166 if (stat instanceof DjStatement) ((DjStatement) stat).close(); 167 else ((DjPreparedStatement) stat).close(); 168 } 169 _closed = true; 170 _allStatements.clear(); 171 _session.close(); 172 _persistMan.close(); 173 _driver.disconnect(this); 174 } 175 176 protected void finalize() 177 { 178 try 179 { 180 super.finalize(); 181 close(); 182 } 183 catch (Throwable e) 184 { 185 } 186 } 187 188 public boolean isClosed() throws SQLException 189 { 190 return _closed; 191 } 192 193 public DatabaseMetaData getMetaData() throws SQLException 194 { 195 return new DjDatabaseMetaData(this); 196 } 197 198 public void setReadOnly(boolean readOnly) throws SQLException 199 { 200 if (readOnly == false) throw new SQLException ("djeneric driver must be readonly!"); 201 } 202 203 public boolean isReadOnly() throws SQLException 204 { 205 return true; 206 } 207 208 public void setCatalog(String catalog) throws SQLException 209 { 210 throw new SQLException ("No catalog!"); 211 } 212 213 public String getCatalog() throws SQLException 214 { 215 return ""; 216 } 217 218 public void setTransactionIsolation(int level) throws SQLException 219 { 220 throw new SQLException ("No transaction isolation!"); 221 } 222 223 public int getTransactionIsolation() throws SQLException 224 { 225 return 0; 226 } 227 228 public SQLWarning getWarnings() throws SQLException 229 { 230 return null; 231 } 232 233 public void clearWarnings() throws SQLException 234 { 235 } 236 237 public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) 238 throws SQLException 239 { 240 DjPreparedStatement stat = new DjPreparedStatement(this, sql, resultSetType, resultSetConcurrency); 241 _allStatements.add(stat); 242 return stat; 243 } 244 245 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException 246 { 247 throw new SQLException ("prepareCall: this method is not implemented by the djeneric driver"); 248 } 249 250 public Map getTypeMap() throws SQLException 251 { 252 return new HashMap (); 253 } 254 255 public void setTypeMap(Map map) throws SQLException 256 { 257 } 258 259 public String toString() 260 { 261 StringBuffer sb = new StringBuffer (); 262 sb.append(CLASSNAME + "\n Connected to " + _persistMan.toString()); 263 return sb.toString(); 264 } 265 266 267 void close(DjStatement p_stat) 268 { 269 if (_isClosing == false) _allStatements.remove(p_stat); 270 } 271 272 273 void close(DjPreparedStatement p_stat) 274 { 275 if (_isClosing == false) _allStatements.remove(p_stat); 276 } 277 278 279 String translateSqlToPolymorph(String p_sqlToTranslate) throws SQLException 280 { 281 try 282 { 283 if (!p_sqlToTranslate.endsWith(";")) p_sqlToTranslate = p_sqlToTranslate + ";"; 284 String result = _persistMan.external2internalStatement(p_sqlToTranslate); 285 if (result.endsWith(";")) return result.substring(0, result.length() - 1); 286 else return result; 287 } 288 catch (DjenericException sqlc) 289 { 290 throw new SQLException (sqlc.getMessage()); 291 } 292 } 293 294 295 RdbmsPersistenceManager getPersistenceManager() 296 { 297 return _persistMan; 298 } 299 300 301 DjDriver getDriver() 302 { 303 return _driver; 304 } 305 306 308 323 public void setHoldability(int holdability) throws SQLException 324 { 325 throw new SQLException ("Not Supported"); 326 } 327 328 340 public int getHoldability() throws SQLException 341 { 342 throw new SQLException ("Not Supported"); 343 } 344 345 356 public Savepoint setSavepoint() throws SQLException 357 { 358 throw new SQLException ("Not Supported"); 359 } 360 361 374 public Savepoint setSavepoint(String name) throws SQLException 375 { 376 throw new SQLException ("Not Supported"); 377 } 378 379 395 public void rollback(Savepoint savepoint) throws SQLException 396 { 397 throw new SQLException ("Not Supported"); 398 } 399 400 412 public void releaseSavepoint(Savepoint savepoint) throws SQLException 413 { 414 throw new SQLException ("Not Supported"); 415 } 416 417 442 public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) 443 throws SQLException 444 { 445 throw new SQLException ("Not Supported"); 446 } 447 448 479 public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, 480 int resultSetHoldability) throws SQLException 481 { 482 throw new SQLException ("Not Supported"); 483 } 484 485 514 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) 515 throws SQLException 516 { 517 throw new SQLException ("Not Supported"); 518 } 519 520 556 public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException 557 { 558 throw new SQLException ("Not Supported"); 559 } 560 561 600 public PreparedStatement prepareStatement(String sql, int columnIndexes[]) throws SQLException 601 { 602 throw new SQLException ("Not Supported"); 603 } 604 605 643 public PreparedStatement prepareStatement(String sql, String columnNames[]) throws SQLException 644 { 645 throw new SQLException ("Not Supported"); 646 } 647 648 } | Popular Tags |