reverting back to 1.20
[csql.git] / src / jdbc / JdbcSqlConnection.java
blobba97a0efe4a808a8d089f909a00bda48e854112d
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;
33 public String hName="localhost";
34 public int portNo=5678;
35 public JSqlConnection getConnection()
37 return jniConn;
40 JdbcSqlConnection(int imode,String hostname,int portno, String username, String password) throws SQLException
42 if( password == null || username == null ) throw getException(CSQL_AUTHEN_ERR);
43 if (username.length() > 64 || password.length() > 64) throw getException(CSQL_AUTHEN_ERR);
44 jniConn = new JSqlConnection();
45 if(imode==3){
46 hName=hostname;
47 portNo=portno;
49 jniConn.alloc(imode,hostname,portNo);
50 mode = imode;
51 int rv = jniConn.connect(username, password);
52 if (rv != 0)
54 jniConn.free();
55 throw getException(CSQL_CON_REJECTED);
57 isClosed = false;
58 rv = jniConn.beginTrans(isoLevel);
59 if (rv != 0)
61 throw getException(CSQL_TRANS_NOT_STARTED);
63 stmtList = new LinkedList();
66 protected void finalize ()
68 try
70 if(!isClosed)
71 close();
73 catch(SQLException e)
75 System.out.println( e );
79 public void close() throws SQLException
81 if(isClosed) return;
82 if(stmtList.size() > 0)
84 Statement stmt;
85 ListIterator lstIter = stmtList.listIterator(0);
86 while(lstIter.hasNext())
88 stmt = (Statement) lstIter.next();
89 stmt.close();
90 lstIter.remove();
93 stmtList = null;
95 if( jniConn.rollback() != 0 )
96 throw getException(CSQL_TRANS_NOT_ROLLBACK);
97 if(jniConn.disconnect() != 0 )
98 throw getException(CSQL_NOT_DISCONNECT);
100 jniConn.free();
101 isClosed = true;
104 public void commit() throws SQLException
106 if(isClosed) throw getException(CSQL_INVALID_STATE);
107 if( jniConn.commit() != 0 )
108 throw getException(CSQL_TRANS_NOT_COMMIT);
109 if (jniConn.beginTrans(isoLevel) != 0)
110 throw getException(CSQL_TRANS_NOT_STARTED);
111 return;
114 public Statement createStatement() throws SQLException
116 if(isClosed) throw getException(CSQL_INVALID_STATE);
117 JdbcSqlStatement stmt = new JdbcSqlStatement(this);
118 stmtList.add(stmt);
119 return(stmt);
122 public boolean getAutoCommit()
124 return(autoCommit);
127 public int getTransactionIsolation() throws SQLException
129 if(isClosed) throw getException(CSQL_INVALID_STATE);
130 int level = TRANSACTION_READ_COMMITTED;
131 switch(isoLevel)
133 case 1:
134 level = TRANSACTION_READ_UNCOMMITTED; break;
135 case 2:
136 level = TRANSACTION_READ_COMMITTED; break;
137 case 3:
138 level = TRANSACTION_REPEATABLE_READ; break;
139 default:
140 throw getException(CSQL_INVALID_ISOLATION);
142 return level;
145 public boolean isClosed() throws SQLException
147 return isClosed;
150 public String nativeSQL (String query) throws SQLException
152 return query;
155 public PreparedStatement prepareStatement(String query) throws SQLException
157 if(isClosed) throw getException(CSQL_INVALID_STATE);
158 JdbcSqlPreparedStatement stmt = new JdbcSqlPreparedStatement(this);
159 stmt.prepareInt(query);
160 stmtList.add(stmt);
161 return(stmt);
164 public void rollback() throws SQLException
166 if(isClosed) throw getException(CSQL_INVALID_STATE);
167 if( jniConn.rollback() != 0 )
168 throw getException(CSQL_TRANS_NOT_ROLLBACK);
169 if (jniConn.beginTrans(isoLevel) != 0)
170 throw getException(CSQL_TRANS_NOT_STARTED);
171 return;
174 public void setAutoCommit(boolean aCommit) throws SQLException
176 //TODO
177 if(isClosed) throw getException(CSQL_INVALID_STATE);
178 autoCommit = aCommit;
181 public void setTransactionIsolation(int level) throws SQLException
183 if(isClosed) throw getException(CSQL_INVALID_STATE);
184 switch(level)
186 case TRANSACTION_READ_UNCOMMITTED:
187 isoLevel = 1; break;
188 case TRANSACTION_READ_COMMITTED:
189 isoLevel = 2; break;
190 case TRANSACTION_REPEATABLE_READ:
191 isoLevel = 3; break;
192 case TRANSACTION_SERIALIZABLE:
193 throw getException(CSQL_NOT_SUPPORTED);
194 case TRANSACTION_NONE:
195 default:
196 throw getException(CSQL_INVALID_ISOLATION);
200 /* public java.sql.Statement createAutoStatement() throws SQLException
202 throw getException(CSQL_NOT_SUPPORTED);
206 //Unsupported Methods
207 public void clearWarnings() throws SQLException
209 throw getException(CSQL_NOT_SUPPORTED);
211 public Statement createStatement(int resultSetType, int resultSetConcurrency)
212 throws SQLException
214 throw getException(CSQL_NOT_SUPPORTED);
216 public Statement createStatement(int resultSetType, int resultSetConcurrency,
217 int resultSetHoldability) throws SQLException
219 throw getException(CSQL_NOT_SUPPORTED);
221 public String getCatalog() throws SQLException
223 throw getException(CSQL_NOT_SUPPORTED);
225 public int getHoldability() throws SQLException
227 throw getException(CSQL_NOT_SUPPORTED);
229 public DatabaseMetaData getMetaData() throws SQLException
231 throw getException(CSQL_NOT_SUPPORTED);
233 public Map getTypeMap() throws SQLException
235 throw getException(CSQL_NOT_SUPPORTED);
237 public SQLWarning getWarnings() throws SQLException
239 throw getException(CSQL_NOT_SUPPORTED);
241 public boolean isReadOnly() throws SQLException
243 throw getException(CSQL_NOT_SUPPORTED);
245 public CallableStatement prepareCall(String query ) throws SQLException
247 if(isClosed) throw getException(CSQL_INVALID_STATE);
248 JdbcSqlCallableStatement stmt = new JdbcSqlCallableStatement(this);
249 stmt.prepareProc(query);
250 stmtList.add(stmt);
251 return(stmt);
253 public CallableStatement prepareCall(String query, int resultSetType,
254 int resultSetConcurrency) throws SQLException
256 throw getException(CSQL_NOT_SUPPORTED);
258 public CallableStatement prepareCall(String query, int resultSetType,
259 int resultSetConcurrency, int resultSetHoldability) throws SQLException
261 throw getException(CSQL_NOT_SUPPORTED);
263 public PreparedStatement prepareStatement(String query,
264 int autoGeneratedKeys) throws SQLException
266 throw getException(CSQL_NOT_SUPPORTED);
268 public PreparedStatement prepareStatement(String query,
269 int[] columnIndexes) throws SQLException
271 throw getException(CSQL_NOT_SUPPORTED);
273 public PreparedStatement prepareStatement( String query, int resultSetType,
274 int resultSetConcurrency) throws SQLException
276 throw getException(CSQL_NOT_SUPPORTED);
278 public PreparedStatement prepareStatement( String query, int resultSetType,
279 int resultSetConcurrency, int resultSetHoldability) throws SQLException
281 throw getException(CSQL_NOT_SUPPORTED);
283 public PreparedStatement prepareStatement(String query,
284 String[] columnNames) throws SQLException
286 throw getException(CSQL_NOT_SUPPORTED);
288 public void releaseSavepoint(Savepoint savepoint) throws SQLException
290 throw getException(CSQL_NOT_SUPPORTED);
292 public void rollback(Savepoint savepoint) throws SQLException
294 throw getException(CSQL_NOT_SUPPORTED);
296 public void setCatalog(String catalog) throws SQLException
298 throw getException(CSQL_NOT_SUPPORTED);
300 public void setHoldability(int holdability) throws SQLException
302 throw getException(CSQL_NOT_SUPPORTED);
304 public void setReadOnly(boolean rOnly) throws SQLException
306 throw getException(CSQL_NOT_SUPPORTED);
308 public Savepoint setSavepoint() throws SQLException
310 throw getException(CSQL_NOT_SUPPORTED);
312 public Savepoint setSavepoint(String query) throws SQLException
314 throw getException(CSQL_NOT_SUPPORTED);
316 public void setTypeMap(Map map) throws SQLException
318 throw getException(CSQL_NOT_SUPPORTED);
320 //Java 1.6 methods
321 public Struct createStruct(String typeName, Object[] attributes)
322 throws SQLException
324 throw getException(CSQL_NOT_SUPPORTED);
326 public Array createArrayOf(String typeName, Object[] elements)
327 throws SQLException
329 throw getException(CSQL_NOT_SUPPORTED);
331 public Blob createBlob() throws SQLException
333 throw getException(CSQL_NOT_SUPPORTED);
335 public Clob createClob() throws SQLException
337 throw getException(CSQL_NOT_SUPPORTED);
339 public NClob createNClob() throws SQLException
341 throw getException(CSQL_NOT_SUPPORTED);
343 public SQLXML createSQLXML() throws SQLException
345 throw getException(CSQL_NOT_SUPPORTED);
347 public Properties getClientInfo() throws SQLException
349 throw getException(CSQL_NOT_SUPPORTED);
351 public String getClientInfo(String name) throws SQLException
353 throw getException(CSQL_NOT_SUPPORTED);
355 public void setClientInfo(String name , String value)
357 return;
359 public void setClientInfo(Properties properties)
361 return;
363 public boolean isValid(int timeout) throws SQLException
365 throw getException(CSQL_NOT_SUPPORTED);
367 public boolean isWrapperFor(Class ifact) throws SQLException
369 throw getException(CSQL_NOT_SUPPORTED);
371 public Class unwrap(Class iface) throws SQLException
373 throw getException(CSQL_NOT_SUPPORTED);