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.Connection; 022import java.sql.PreparedStatement; 023import java.sql.SQLException; 024import java.sql.Statement; 025 026import lucee.commons.lang.ExceptionUtil; 027import lucee.runtime.PageContext; 028import lucee.runtime.db.DataSource; 029import lucee.runtime.db.DatasourceConnection; 030import lucee.runtime.db.DatasourceConnectionImpl; 031import lucee.runtime.db.SQL; 032import lucee.runtime.exp.PageException; 033import lucee.runtime.exp.PageRuntimeException; 034import lucee.runtime.op.Caster; 035 036public class ORMDatasourceConnection implements DatasourceConnection { 037 038 private DataSource datasource; 039 private ORMConnection connection; 040 private Boolean supportsGetGeneratedKeys; 041 042 public ORMDatasourceConnection(PageContext pc, ORMSession session, DataSource ds) throws PageException { 043 datasource=ds; 044 // this should never happen 045 if(datasource==null) { 046 try { 047 datasource=ORMUtil.getDefaultDataSource(pc); 048 } 049 catch (PageException pe) { 050 throw new PageRuntimeException(pe); 051 } 052 } 053 connection=new ORMConnection(pc,session,datasource,false); 054 } 055 056 public Connection getConnection() { 057 connection.begin(); 058 return connection; 059 } 060 061 public boolean isAutoCommit() throws SQLException { 062 return connection.getAutoCommit(); 063 } 064 065 public void setAutoCommit(boolean setting) throws SQLException { 066 connection.setAutoCommit(setting); 067 } 068 069 @Override 070 public DataSource getDatasource() { 071 return datasource; 072 } 073 074 @Override 075 public String getPassword() { 076 return datasource.getPassword(); 077 } 078 079 @Override 080 public String getUsername() { 081 return datasource.getUsername(); 082 } 083 084 @Override 085 public boolean isTimeout() { 086 return false; 087 } 088 089 090 091 @Override 092 public boolean equals(Object obj) { 093 if(this==obj) return true; 094 if(!(obj instanceof ORMDatasourceConnection)) return false; 095 return DatasourceConnectionImpl.equals(this, (DatasourceConnection) obj); 096 } 097 098 public boolean supportsGetGeneratedKeys() { 099 if(supportsGetGeneratedKeys==null){ 100 try { 101 supportsGetGeneratedKeys=Caster.toBoolean(getConnection().getMetaData().supportsGetGeneratedKeys()); 102 } catch (Throwable t) { 103 ExceptionUtil.rethrowIfNecessary(t); 104 return false; 105 } 106 } 107 return supportsGetGeneratedKeys.booleanValue(); 108 } 109 110 public PreparedStatement getPreparedStatement(SQL sql, boolean createGeneratedKeys, boolean allowCaching) throws SQLException { 111 if(createGeneratedKeys) return getConnection().prepareStatement(sql.getSQLString(),Statement.RETURN_GENERATED_KEYS); 112 return getConnection().prepareStatement(sql.getSQLString()); 113 } 114 115 @Override 116 public PreparedStatement getPreparedStatement(SQL sql, int resultSetType, int resultSetConcurrency) throws SQLException { 117 return getConnection().prepareStatement(sql.getSQLString(),resultSetType,resultSetConcurrency); 118 } 119 120 @Override 121 public void close() throws SQLException { 122 getConnection().close(); 123 } 124 125}