1988649 with jdk 1.6 build fails
[csql.git] / src / jdbc / JdbcSqlConnection.java
blob62df8fdd1cfc764b55b0d314cc6fb9c03d375c7d
1 package csql.jdbc;
2 import java.sql.Connection;
3 import java.sql.SQLException;
4 import java.sql.SQLWarning;
5 import java.sql.DatabaseMetaData;
6 import java.sql.Savepoint;
7 import java.sql.Statement;
8 import java.sql.PreparedStatement;
9 import java.sql.CallableStatement;
10 import java.lang.String;
11 import java.util.LinkedList;
12 import java.util.ListIterator;
13 import java.util.Map;
14 import java.util.Properties;
16 import java.sql.Clob;
17 import java.sql.NClob;
18 import java.sql.Blob;
19 import java.sql.SQLXML;
20 import java.sql.Struct;
21 import java.sql.Array;
23 public final class JdbcSqlConnection extends JSqlError implements Connection, JSqlErrorType
25 private boolean isClosed;
26 private JSqlConnection jniConn;
27 private LinkedList stmtList;
28 private boolean autoCommit = true; //TODO
29 private boolean transRunning = false;
30 private int isoLevel = 2; //->READ_COMMITTED
31 private boolean readOnly = false;
32 public int mode = 1;
34 public JSqlConnection getConnection()
36 return jniConn;
39 JdbcSqlConnection(int imode, String username, String password) throws SQLException
41 if( password == null || username == null ) throw getException(CSQL_AUTHEN_ERR);
42 if (username.length() > 64 || password.length() > 64) throw getException(CSQL_AUTHEN_ERR);
43 jniConn = new JSqlConnection();
44 jniConn.alloc(imode);
45 mode = imode;
46 int rv = jniConn.connect(username, password);
47 if (rv != 0)
49 jniConn.free();
50 throw getException(CSQL_CON_REJECTED);
52 isClosed = false;
53 rv = jniConn.beginTrans(isoLevel);
54 if (rv != 0)
56 throw getException(CSQL_TRANS_NOT_STARTED);
58 stmtList = new LinkedList();
61 protected void finalize ()
63 try
65 if(!isClosed)
66 close();
68 catch(SQLException e)
70 System.out.println( e );
74 public void close() throws SQLException
76 if(isClosed) return;
77 if(stmtList.size() > 0)
79 Statement stmt;
80 ListIterator lstIter = stmtList.listIterator(0);
81 while(lstIter.hasNext())
83 stmt = (Statement) lstIter.next();
84 stmt.close();
85 lstIter.remove();
88 stmtList = null;
90 if( jniConn.rollback() != 0 )
91 throw getException(CSQL_TRANS_NOT_ROLLBACK);
92 if(jniConn.disconnect() != 0 )
93 throw getException(CSQL_NOT_DISCONNECT);
95 jniConn.free();
96 isClosed = true;
99 public void commit() throws SQLException
101 if(isClosed) throw getException(CSQL_INVALID_STATE);
102 if( jniConn.commit() != 0 )
103 throw getException(CSQL_TRANS_NOT_COMMIT);
104 if (jniConn.beginTrans(isoLevel) != 0)
105 throw getException(CSQL_TRANS_NOT_STARTED);
106 return;
109 public Statement createStatement() throws SQLException
111 if(isClosed) throw getException(CSQL_INVALID_STATE);
112 JdbcSqlStatement stmt = new JdbcSqlStatement(this);
113 stmtList.add(stmt);
114 return(stmt);
117 public boolean getAutoCommit()
119 return(autoCommit);
122 public int getTransactionIsolation() throws SQLException
124 if(isClosed) throw getException(CSQL_INVALID_STATE);
125 int level = TRANSACTION_READ_COMMITTED;
126 switch(isoLevel)
128 case 1:
129 level = TRANSACTION_READ_UNCOMMITTED; break;
130 case 2:
131 level = TRANSACTION_READ_COMMITTED; break;
132 case 3:
133 level = TRANSACTION_REPEATABLE_READ; break;
134 default:
135 throw getException(CSQL_INVALID_ISOLATION);
137 return level;
140 public boolean isClosed() throws SQLException
142 return isClosed;
145 public String nativeSQL (String query) throws SQLException
147 return query;
150 public PreparedStatement prepareStatement(String query) throws SQLException
152 if(isClosed) throw getException(CSQL_INVALID_STATE);
153 JdbcSqlPreparedStatement stmt = new JdbcSqlPreparedStatement(this);
154 stmt.prepareInt(query);
155 stmtList.add(stmt);
156 return(stmt);
159 public void rollback() throws SQLException
161 if(isClosed) throw getException(CSQL_INVALID_STATE);
162 if( jniConn.rollback() != 0 )
163 throw getException(CSQL_TRANS_NOT_ROLLBACK);
164 if (jniConn.beginTrans(isoLevel) != 0)
165 throw getException(CSQL_TRANS_NOT_STARTED);
166 return;
169 public void setAutoCommit(boolean aCommit) throws SQLException
171 //TODO
172 if(isClosed) throw getException(CSQL_INVALID_STATE);
173 autoCommit = aCommit;
176 public void setTransactionIsolation(int level) throws SQLException
178 if(isClosed) throw getException(CSQL_INVALID_STATE);
179 switch(level)
181 case TRANSACTION_READ_UNCOMMITTED:
182 isoLevel = 1; break;
183 case TRANSACTION_READ_COMMITTED:
184 isoLevel = 2; break;
185 case TRANSACTION_REPEATABLE_READ:
186 isoLevel = 3; break;
187 case TRANSACTION_SERIALIZABLE:
188 throw getException(CSQL_NOT_SUPPORTED);
189 case TRANSACTION_NONE:
190 default:
191 throw getException(CSQL_INVALID_ISOLATION);
195 /* public java.sql.Statement createAutoStatement() throws SQLException
197 throw getException(CSQL_NOT_SUPPORTED);
201 //Unsupported Methods
202 public void clearWarnings() throws SQLException
204 throw getException(CSQL_NOT_SUPPORTED);
206 public Statement createStatement(int resultSetType, int resultSetConcurrency)
207 throws SQLException
209 throw getException(CSQL_NOT_SUPPORTED);
211 public Statement createStatement(int resultSetType, int resultSetConcurrency,
212 int resultSetHoldability) throws SQLException
214 throw getException(CSQL_NOT_SUPPORTED);
216 public String getCatalog() throws SQLException
218 throw getException(CSQL_NOT_SUPPORTED);
220 public int getHoldability() throws SQLException
222 throw getException(CSQL_NOT_SUPPORTED);
224 public DatabaseMetaData getMetaData() throws SQLException
226 throw getException(CSQL_NOT_SUPPORTED);
228 public Map getTypeMap() throws SQLException
230 throw getException(CSQL_NOT_SUPPORTED);
232 public SQLWarning getWarnings() throws SQLException
234 throw getException(CSQL_NOT_SUPPORTED);
236 public boolean isReadOnly() throws SQLException
238 throw getException(CSQL_NOT_SUPPORTED);
240 public CallableStatement prepareCall(String query ) throws SQLException
242 throw getException(CSQL_NOT_SUPPORTED);
244 public CallableStatement prepareCall(String query, int resultSetType,
245 int resultSetConcurrency) throws SQLException
247 throw getException(CSQL_NOT_SUPPORTED);
249 public CallableStatement prepareCall(String query, int resultSetType,
250 int resultSetConcurrency, int resultSetHoldability) throws SQLException
252 throw getException(CSQL_NOT_SUPPORTED);
254 public PreparedStatement prepareStatement(String query,
255 int autoGeneratedKeys) throws SQLException
257 throw getException(CSQL_NOT_SUPPORTED);
259 public PreparedStatement prepareStatement(String query,
260 int[] columnIndexes) throws SQLException
262 throw getException(CSQL_NOT_SUPPORTED);
264 public PreparedStatement prepareStatement( String query, int resultSetType,
265 int resultSetConcurrency) throws SQLException
267 throw getException(CSQL_NOT_SUPPORTED);
269 public PreparedStatement prepareStatement( String query, int resultSetType,
270 int resultSetConcurrency, int resultSetHoldability) throws SQLException
272 throw getException(CSQL_NOT_SUPPORTED);
274 public PreparedStatement prepareStatement(String query,
275 String[] columnNames) throws SQLException
277 throw getException(CSQL_NOT_SUPPORTED);
279 public void releaseSavepoint(Savepoint savepoint) throws SQLException
281 throw getException(CSQL_NOT_SUPPORTED);
283 public void rollback(Savepoint savepoint) throws SQLException
285 throw getException(CSQL_NOT_SUPPORTED);
287 public void setCatalog(String catalog) throws SQLException
289 throw getException(CSQL_NOT_SUPPORTED);
291 public void setHoldability(int holdability) throws SQLException
293 throw getException(CSQL_NOT_SUPPORTED);
295 public void setReadOnly(boolean rOnly) throws SQLException
297 throw getException(CSQL_NOT_SUPPORTED);
299 public Savepoint setSavepoint() throws SQLException
301 throw getException(CSQL_NOT_SUPPORTED);
303 public Savepoint setSavepoint(String query) throws SQLException
305 throw getException(CSQL_NOT_SUPPORTED);
307 public void setTypeMap(Map map) throws SQLException
309 throw getException(CSQL_NOT_SUPPORTED);
311 //Java 1.6 methods
312 public Struct createStruct(String typeName, Object[] attributes)
313 throws SQLException
315 throw getException(CSQL_NOT_SUPPORTED);
317 public Array createArrayOf(String typeName, Object[] elements)
318 throws SQLException
320 throw getException(CSQL_NOT_SUPPORTED);
322 public Blob createBlob() throws SQLException
324 throw getException(CSQL_NOT_SUPPORTED);
326 public Clob createClob() throws SQLException
328 throw getException(CSQL_NOT_SUPPORTED);
330 public NClob createNClob() throws SQLException
332 throw getException(CSQL_NOT_SUPPORTED);
334 public SQLXML createSQLXML() throws SQLException
336 throw getException(CSQL_NOT_SUPPORTED);
338 public Properties getClientInfo() throws SQLException
340 throw getException(CSQL_NOT_SUPPORTED);
342 public String getClientInfo(String name) throws SQLException
344 throw getException(CSQL_NOT_SUPPORTED);
346 public void setClientInfo(String name , String value)
348 return;
350 public void setClientInfo(Properties properties)
352 return;
354 public boolean isValid(int timeout) throws SQLException
356 throw getException(CSQL_NOT_SUPPORTED);
358 public boolean isWrapperFor(Class ifact) throws SQLException
360 throw getException(CSQL_NOT_SUPPORTED);
362 public Class unwrap(Class iface) throws SQLException
364 throw getException(CSQL_NOT_SUPPORTED);