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