001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.runtime.orm; 020 021import java.sql.Array; 022import java.sql.Blob; 023import java.sql.CallableStatement; 024import java.sql.Clob; 025import java.sql.Connection; 026import java.sql.DatabaseMetaData; 027import java.sql.NClob; 028import java.sql.PreparedStatement; 029import java.sql.SQLException; 030import java.sql.SQLFeatureNotSupportedException; 031import java.sql.SQLWarning; 032import java.sql.SQLXML; 033import java.sql.Savepoint; 034import java.sql.Statement; 035import java.sql.Struct; 036import java.util.Map; 037import java.util.Properties; 038import java.util.concurrent.Executor; 039 040import lucee.runtime.PageContext; 041import lucee.runtime.db.DataSource; 042import lucee.runtime.exp.PageException; 043 044public class ORMConnection implements Connection { 045 046 private ORMSession session; 047 private boolean autoCommit=false; 048 private int isolation=Connection.TRANSACTION_SERIALIZABLE; 049 private ORMTransaction trans; 050 private DataSource ds; 051 private boolean hasBegun; 052 053 /** 054 * Constructor of the class 055 * @param session 056 * @throws PageException 057 */ 058 public ORMConnection(PageContext pc,ORMSession session, DataSource ds, boolean begin) throws PageException { 059 this.session=session; 060 this.ds=ds; 061 trans = session.getTransaction(ds.getName(),session.getEngine().getConfiguration(pc).autoManageSession()); 062 if(begin)begin(); 063 } 064 065 @Override 066 public void clearWarnings() throws SQLException {} 067 068 public void close() throws SQLException { 069 // TODO Auto-generated method stub 070 } 071 072 public void begin() { 073 if(!hasBegun)trans.begin(); 074 hasBegun=true; 075 } 076 077 @Override 078 public void commit() { 079 trans.commit(); 080 } 081 082 @Override 083 public Statement createStatement() throws SQLException { 084 throw notSupported(); 085 } 086 087 @Override 088 public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { 089 throw notSupported(); 090 } 091 092 @Override 093 public Statement createStatement(int resultSetType,int resultSetConcurrency, int resultSetHoldability)throws SQLException { 094 throw notSupported(); 095 } 096 097 @Override 098 public boolean getAutoCommit() throws SQLException { 099 return autoCommit; 100 } 101 102 @Override 103 public int getTransactionIsolation() throws SQLException { 104 return isolation; 105 } 106 107 @Override 108 public void setTransactionIsolation(int isolation) throws SQLException { 109 this.isolation=isolation; 110 } 111 112 @Override 113 public String getCatalog() throws SQLException { 114 throw notSupported(); 115 } 116 117 @Override 118 public int getHoldability() throws SQLException { 119 throw notSupported(); 120 } 121 122 @Override 123 public DatabaseMetaData getMetaData() throws SQLException { 124 throw notSupported(); 125 } 126 127 @Override 128 public Map<String, Class<?>> getTypeMap() throws SQLException { 129 throw notSupported(); 130 } 131 132 @Override 133 public SQLWarning getWarnings() throws SQLException { 134 throw notSupported(); 135 } 136 137 @Override 138 public boolean isClosed() throws SQLException { 139 return !session.isValid(ds); 140 } 141 142 @Override 143 public boolean isReadOnly() throws SQLException { 144 return false; 145 } 146 147 @Override 148 public String nativeSQL(String sql) throws SQLException { 149 return sql; 150 } 151 152 @Override 153 public CallableStatement prepareCall(String sql) throws SQLException { 154 throw notSupported(); 155 } 156 157 @Override 158 public CallableStatement prepareCall(String sql, int resultSetType,int resultSetConcurrency) throws SQLException { 159 throw notSupported(); 160 } 161 162 @Override 163 public CallableStatement prepareCall(String sql, int resultSetType,int resultSetConcurrency, int resultSetHoldability)throws SQLException { 164 throw notSupported(); 165 } 166 167 @Override 168 public PreparedStatement prepareStatement(String sql) throws SQLException { 169 throw notSupported(); 170 } 171 172 @Override 173 public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)throws SQLException { 174 throw notSupported(); 175 } 176 177 @Override 178 public PreparedStatement prepareStatement(String sql, int[] columnIndexes)throws SQLException { 179 throw notSupported(); 180 } 181 182 @Override 183 public PreparedStatement prepareStatement(String sql, String[] columnNames)throws SQLException { 184 throw notSupported(); 185 } 186 187 @Override 188 public PreparedStatement prepareStatement(String sql, int resultSetType,int resultSetConcurrency) throws SQLException { 189 throw notSupported(); 190 } 191 192 @Override 193 public PreparedStatement prepareStatement(String sql, int resultSetType,int resultSetConcurrency, int resultSetHoldability)throws SQLException { 194 throw notSupported(); 195 } 196 197 @Override 198 public void releaseSavepoint(Savepoint savepoint) throws SQLException { 199 } 200 201 @Override 202 public void rollback() { 203 trans.rollback(); 204 } 205 206 @Override 207 public void rollback(Savepoint savepoint) throws SQLException { 208 rollback(); 209 } 210 211 public void setAutoCommit(boolean autoCommit) throws SQLException { 212 this.autoCommit=autoCommit; 213 if(autoCommit) 214 trans.end(); 215 } 216 217 @Override 218 public void setCatalog(String catalog) throws SQLException { 219 } 220 221 @Override 222 public void setHoldability(int holdability) throws SQLException { 223 } 224 225 @Override 226 public void setReadOnly(boolean readOnly) throws SQLException { 227 228 } 229 230 @Override 231 public Savepoint setSavepoint() throws SQLException { 232 throw notSupported(); 233 } 234 235 @Override 236 public Savepoint setSavepoint(String name) throws SQLException { 237 throw notSupported(); 238 } 239 240 @Override 241 public void setTypeMap(Map<String, Class<?>> map) throws SQLException { 242 throw notSupported(); 243 } 244 245 /*private SQLException toSQLException(PageException pe) { 246 SQLException e = new SQLException(pe.getMessage()); 247 e.initCause(pe); 248 return e; 249 }*/ 250 251 public <T> T unwrap(Class<T> iface) throws SQLException { 252 throw notSupported(); 253 } 254 255 public boolean isWrapperFor(Class<?> iface) throws SQLException { 256 throw notSupported(); 257 } 258 259 public Clob createClob() throws SQLException { 260 throw notSupported(); 261 } 262 263 public Blob createBlob() throws SQLException { 264 throw notSupported(); 265 } 266 267 public boolean isValid(int timeout) throws SQLException { 268 throw notSupported(); 269 } 270 271 public void setClientInfo(String name, String value) { 272 throw notSupportedEL(); 273 } 274 275 public void setClientInfo(Properties properties) { 276 throw notSupportedEL(); 277 } 278 279 public String getClientInfo(String name) throws SQLException { 280 throw notSupported(); 281 } 282 283 public Properties getClientInfo() throws SQLException { 284 throw notSupported(); 285 } 286 287 public Array createArrayOf(String typeName, Object[] elements) throws SQLException { 288 throw notSupported(); 289 } 290 291 @Override 292 public Struct createStruct(String typeName, Object[] attributes)throws SQLException { 293 throw notSupported(); 294 } 295 296 private SQLException notSupported() { 297 return new SQLFeatureNotSupportedException("this feature is not supported"); 298 } 299 private RuntimeException notSupportedEL() { 300 return new RuntimeException(new SQLFeatureNotSupportedException("this feature is not supported")); 301 } 302 303 public NClob createNClob() throws SQLException { 304 throw notSupported(); 305 } 306 307 public SQLXML createSQLXML() throws SQLException { 308 throw notSupported(); 309 } 310 311 // used only with java 7, do not set @Override 312 public void setSchema(String schema) throws SQLException { 313 throw notSupported(); 314 } 315 316 // used only with java 7, do not set @Override 317 public String getSchema() throws SQLException { 318 throw notSupported(); 319 } 320 321 // used only with java 7, do not set @Override 322 public void abort(Executor executor) throws SQLException { 323 throw notSupported(); 324 } 325 326 // used only with java 7, do not set @Override 327 public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException { 328 throw notSupported(); 329 } 330 331 // used only with java 7, do not set @Override 332 public int getNetworkTimeout() throws SQLException { 333 throw notSupported(); 334 } 335}