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