001    package railo.runtime.orm;
002    
003    import java.sql.Connection;
004    import java.sql.PreparedStatement;
005    import java.sql.SQLException;
006    import java.sql.Statement;
007    
008    import railo.runtime.PageContext;
009    import railo.runtime.db.DataSource;
010    import railo.runtime.db.DatasourceConnection;
011    import railo.runtime.db.DatasourceConnectionImpl;
012    import railo.runtime.db.DatasourceConnectionPro;
013    import railo.runtime.db.SQL;
014    import railo.runtime.op.Caster;
015    
016    public class ORMDatasourceConnection implements DatasourceConnectionPro {
017    
018            private DataSource datasource;
019            private Connection connection;
020            private Boolean supportsGetGeneratedKeys;
021    
022            public ORMDatasourceConnection(PageContext pc, ORMSession session) {
023                    datasource=session.getEngine().getDataSource();
024                    connection=new ORMConnection(pc,session);
025            }
026    
027            public Connection getConnection() {
028                    // TODO Auto-generated method stub
029                    return connection;
030            }
031    
032            /**
033             * @see railo.runtime.db.DatasourceConnection#getDatasource()
034             */
035            public DataSource getDatasource() {
036                    return datasource;
037            }
038    
039            /**
040             * @see railo.runtime.db.DatasourceConnection#getPassword()
041             */
042            public String getPassword() {
043                    return datasource.getPassword();
044            }
045    
046            /**
047             * @see railo.runtime.db.DatasourceConnection#getUsername()
048             */
049            public String getUsername() {
050                    return datasource.getUsername();
051            }
052    
053            /**
054             * @see railo.runtime.db.DatasourceConnection#isTimeout()
055             */
056            public boolean isTimeout() {
057                    return false;
058            }
059            
060    
061    
062            /**
063             * @see java.lang.Object#equals(java.lang.Object)
064             */
065            public boolean equals(Object obj) {
066                    if(this==obj) return true;
067                    if(!(obj instanceof ORMDatasourceConnection)) return false;
068                    return DatasourceConnectionImpl.equals(this, (DatasourceConnection) obj);
069            }
070    
071            public boolean supportsGetGeneratedKeys() {
072                    if(supportsGetGeneratedKeys==null){
073                            try {
074                                    supportsGetGeneratedKeys=Caster.toBoolean(getConnection().getMetaData().supportsGetGeneratedKeys());
075                            } catch (Throwable t) {
076                                    return false;
077                            }
078                    }
079                    return supportsGetGeneratedKeys.booleanValue();
080            }
081    
082            public PreparedStatement getPreparedStatement(SQL sql, boolean createGeneratedKeys, boolean allowCaching) throws SQLException {
083                    if(createGeneratedKeys) return getConnection().prepareStatement(sql.getSQLString(),Statement.RETURN_GENERATED_KEYS);
084                    else return getConnection().prepareStatement(sql.getSQLString());
085            }
086    
087            @Override
088            public PreparedStatement getPreparedStatement(SQL sql, int resultSetType, int resultSetConcurrency) throws SQLException {
089                    return getConnection().prepareStatement(sql.getSQLString(),resultSetType,resultSetConcurrency);
090            }
091    
092            @Override
093            public void close() throws SQLException {
094                    getConnection().close();
095            }
096    
097    }