*** empty log message ***
[csql.git] / src / jdbc / JdbcSqlConnection.java
blob43aa1554a9226a757748d35d8634d05272bace3d
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 String url = "jdbc:csql";
36 private JdbcSqlDatabaseMetaData dbmtd;
37 public JSqlConnection getConnection()
39 return jniConn;
42 JdbcSqlConnection(int imode,String hostname,int portno, String username, String password) throws SQLException
44 if( password == null || username == null ) throw getException(CSQL_AUTHEN_ERR);
45 if (username.length() > 64 || password.length() > 64) throw getException(CSQL_AUTHEN_ERR);
46 jniConn = new JSqlConnection();
47 if(imode>3){
48 hName=hostname;
49 portNo=portno;
51 jniConn.alloc(imode,hostname,portNo);
52 mode = imode;
53 int rv = jniConn.connect(username, password);
54 //int rv = jniConn.connect("root", "manager");
55 if (rv != 0)
57 jniConn.free();
58 throw getException(CSQL_CON_REJECTED);
60 isClosed = false;
61 rv = jniConn.beginTrans(isoLevel);
62 if (rv != 0)
64 throw getException(CSQL_TRANS_NOT_STARTED);
66 dbmtd = null;
67 stmtList = new LinkedList();
70 protected void finalize ()
72 try
74 if(!isClosed)
75 close();
77 catch(SQLException e)
79 System.out.println( e );
83 public void setUrl(String url)
85 this.url = url;
87 public String getUrl() { return this.url; }
88 public void close() throws SQLException
90 if(isClosed) return;
91 if(stmtList.size() > 0)
93 Statement stmt;
94 ListIterator lstIter = stmtList.listIterator(0);
95 while(lstIter.hasNext())
97 stmt = (Statement) lstIter.next();
98 stmt.close();
99 lstIter.remove();
102 stmtList = null;
104 if( jniConn.rollback() != 0 )
105 throw getException(CSQL_TRANS_NOT_ROLLBACK);
106 if(jniConn.disconnect() != 0 )
107 throw getException(CSQL_NOT_DISCONNECT);
109 jniConn.free();
110 isClosed = true;
113 public void commit() throws SQLException
115 if(isClosed) throw getException(CSQL_INVALID_STATE);
116 if( jniConn.commit() != 0 )
117 throw getException(CSQL_TRANS_NOT_COMMIT);
118 if (jniConn.beginTrans(isoLevel) != 0)
119 throw getException(CSQL_TRANS_NOT_STARTED);
120 return;
123 public Statement createStatement() throws SQLException
125 if(isClosed) throw getException(CSQL_INVALID_STATE);
126 JdbcSqlStatement stmt = new JdbcSqlStatement(this);
127 stmtList.add(stmt);
128 return(stmt);
131 public boolean getAutoCommit()
133 return(autoCommit);
136 public int getTransactionIsolation() throws SQLException
138 if(isClosed) throw getException(CSQL_INVALID_STATE);
139 int level = TRANSACTION_READ_COMMITTED;
140 switch(isoLevel)
142 case 1:
143 level = TRANSACTION_READ_UNCOMMITTED; break;
144 case 2:
145 level = TRANSACTION_READ_COMMITTED; break;
146 case 3:
147 level = TRANSACTION_REPEATABLE_READ; break;
148 default:
149 throw getException(CSQL_INVALID_ISOLATION);
151 return level;
154 public boolean isClosed() throws SQLException
156 return isClosed;
159 public String nativeSQL (String query) throws SQLException
161 return query;
164 public PreparedStatement prepareStatement(String query) throws SQLException
166 if(isClosed) throw getException(CSQL_INVALID_STATE);
167 JdbcSqlPreparedStatement stmt = new JdbcSqlPreparedStatement(this);
168 stmt.prepareInt(query);
169 stmtList.add(stmt);
170 return(stmt);
173 public void rollback() throws SQLException
175 if(isClosed) throw getException(CSQL_INVALID_STATE);
176 if( jniConn.rollback() != 0 )
177 throw getException(CSQL_TRANS_NOT_ROLLBACK);
178 if (jniConn.beginTrans(isoLevel) != 0)
179 throw getException(CSQL_TRANS_NOT_STARTED);
180 return;
183 public void setAutoCommit(boolean aCommit) throws SQLException
185 //TODO
186 if(isClosed) throw getException(CSQL_INVALID_STATE);
187 autoCommit = aCommit;
190 public void setTransactionIsolation(int level) throws SQLException
192 if(isClosed) throw getException(CSQL_INVALID_STATE);
193 switch(level)
195 case TRANSACTION_READ_UNCOMMITTED:
196 isoLevel = 1; break;
197 case TRANSACTION_READ_COMMITTED:
198 isoLevel = 2; break;
199 case TRANSACTION_REPEATABLE_READ:
200 isoLevel = 3; break;
201 case TRANSACTION_SERIALIZABLE:
202 throw getException(CSQL_NOT_SUPPORTED);
203 case TRANSACTION_NONE:
204 default:
205 throw getException(CSQL_INVALID_ISOLATION);
209 /* public java.sql.Statement createAutoStatement() throws SQLException
211 throw getException(CSQL_NOT_SUPPORTED);
215 //Unsupported Methods
216 public void clearWarnings() throws SQLException
218 //TODO:
219 return;
221 public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException
223 if (JSqlError.isDebug) System.out.println("createStatement called");
224 throw getException(CSQL_NOT_SUPPORTED);
226 public Statement createStatement(int resultSetType, int resultSetConcurrency,
227 int resultSetHoldability) throws SQLException
229 if (JSqlError.isDebug) System.out.println("createStatement called");
230 throw getException(CSQL_NOT_SUPPORTED);
232 public String getCatalog() throws SQLException
234 return "csql";
236 public int getHoldability() throws SQLException
238 if (JSqlError.isDebug) System.out.println("getHoldability called");
239 throw getException(CSQL_NOT_SUPPORTED);
241 public DatabaseMetaData getMetaData() throws SQLException
243 //System.out.println("In connection create Metadata");
244 if(dbmtd==null)
246 dbmtd = new JdbcSqlDatabaseMetaData(this);
247 return dbmtd;
248 }else return dbmtd;
250 public Map getTypeMap() throws SQLException
252 if (JSqlError.isDebug) System.out.println("getTypeMap called");
253 throw getException(CSQL_NOT_SUPPORTED);
255 public SQLWarning getWarnings() throws SQLException
257 //TODO:
258 //if (JSqlError.isDebug) System.out.println("getWarnings called");
259 return null;
261 public boolean isReadOnly() throws SQLException
263 return false;
265 public CallableStatement prepareCall(String query ) throws SQLException
267 if(isClosed) throw getException(CSQL_INVALID_STATE);
268 JdbcSqlCallableStatement stmt = new JdbcSqlCallableStatement(this);
269 stmt.prepareProc(query);
270 stmtList.add(stmt);
271 return(stmt);
273 public CallableStatement prepareCall(String query, int resultSetType,
274 int resultSetConcurrency) throws SQLException
276 if (JSqlError.isDebug) System.out.println("prepareCall called");
277 throw getException(CSQL_NOT_SUPPORTED);
279 public CallableStatement prepareCall(String query, int resultSetType,
280 int resultSetConcurrency, int resultSetHoldability) throws SQLException
282 if (JSqlError.isDebug) System.out.println("prepareCall called");
283 throw getException(CSQL_NOT_SUPPORTED);
285 public PreparedStatement prepareStatement(String query,
286 int autoGeneratedKeys) throws SQLException
288 if (JSqlError.isDebug) System.out.println("prepareStatement called");
289 throw getException(CSQL_NOT_SUPPORTED);
291 public PreparedStatement prepareStatement(String query,
292 int[] columnIndexes) throws SQLException
294 if (JSqlError.isDebug) System.out.println("prepareStatement called");
295 throw getException(CSQL_NOT_SUPPORTED);
297 public PreparedStatement prepareStatement( String query, int resultSetType,
298 int resultSetConcurrency) throws SQLException
300 if (JSqlError.isDebug) System.out.println("prepareStatement called");
301 throw getException(CSQL_NOT_SUPPORTED);
303 public PreparedStatement prepareStatement( String query, int resultSetType,
304 int resultSetConcurrency, int resultSetHoldability) throws SQLException
306 if (JSqlError.isDebug) System.out.println("prepareStatement called");
307 throw getException(CSQL_NOT_SUPPORTED);
309 public PreparedStatement prepareStatement(String query,
310 String[] columnNames) throws SQLException
312 if (JSqlError.isDebug) System.out.println("prepareStatement called");
313 throw getException(CSQL_NOT_SUPPORTED);
315 public void releaseSavepoint(Savepoint savepoint) throws SQLException
317 if (JSqlError.isDebug) System.out.println("releaseSavepoint called");
318 throw getException(CSQL_NOT_SUPPORTED);
320 public void rollback(Savepoint savepoint) throws SQLException
322 if (JSqlError.isDebug) System.out.println("rollback called");
323 throw getException(CSQL_NOT_SUPPORTED);
325 public void setCatalog(String catalog) throws SQLException
327 if (JSqlError.isDebug) System.out.println("setCatalog called");
328 throw getException(CSQL_NOT_SUPPORTED);
330 public void setHoldability(int holdability) throws SQLException
332 if (JSqlError.isDebug) System.out.println("setHoldability called");
333 throw getException(CSQL_NOT_SUPPORTED);
335 public void setReadOnly(boolean rOnly) throws SQLException
337 return;
339 public Savepoint setSavepoint() throws SQLException
341 if (JSqlError.isDebug) System.out.println("setSavepoint called");
342 throw getException(CSQL_NOT_SUPPORTED);
344 public Savepoint setSavepoint(String query) throws SQLException
346 if (JSqlError.isDebug) System.out.println("setSavepoint called");
347 throw getException(CSQL_NOT_SUPPORTED);
349 public void setTypeMap(Map map) throws SQLException
351 if (JSqlError.isDebug) System.out.println("setTypeMap called");
352 throw getException(CSQL_NOT_SUPPORTED);
354 //Java 1.6 methods
355 public Struct createStruct(String typeName, Object[] attributes)
356 throws SQLException
358 if (JSqlError.isDebug) System.out.println("createStruct called");
359 throw getException(CSQL_NOT_SUPPORTED);
361 public Array createArrayOf(String typeName, Object[] elements)
362 throws SQLException
364 if (JSqlError.isDebug) System.out.println("createArrayOf called");
365 throw getException(CSQL_NOT_SUPPORTED);
367 public Blob createBlob() throws SQLException
369 if (JSqlError.isDebug) System.out.println("createBlob called");
370 throw getException(CSQL_NOT_SUPPORTED);
372 public Clob createClob() throws SQLException
374 if (JSqlError.isDebug) System.out.println("createClob called");
375 throw getException(CSQL_NOT_SUPPORTED);
377 public NClob createNClob() throws SQLException
379 if (JSqlError.isDebug) System.out.println("createNClob called");
380 throw getException(CSQL_NOT_SUPPORTED);
382 public SQLXML createSQLXML() throws SQLException
384 if (JSqlError.isDebug) System.out.println("createSQLXML called");
385 throw getException(CSQL_NOT_SUPPORTED);
387 public Properties getClientInfo() throws SQLException
389 if (JSqlError.isDebug) System.out.println("getClientInfo called");
390 throw getException(CSQL_NOT_SUPPORTED);
392 public String getClientInfo(String name) throws SQLException
394 if (JSqlError.isDebug) System.out.println("getClientInfo called");
395 throw getException(CSQL_NOT_SUPPORTED);
397 public void setClientInfo(String name , String value)
399 return;
401 public void setClientInfo(Properties properties)
403 if (JSqlError.isDebug) System.out.println("setClientInfo called");
404 return;
406 public boolean isValid(int timeout) throws SQLException
408 if (JSqlError.isDebug) System.out.println("isValid called");
409 throw getException(CSQL_NOT_SUPPORTED);
411 public boolean isWrapperFor(Class ifact) throws SQLException
413 if (JSqlError.isDebug) System.out.println("isWrapperFor called");
414 throw getException(CSQL_NOT_SUPPORTED);
416 public Class unwrap(Class iface) throws SQLException
418 if (JSqlError.isDebug) System.out.println("unwrap called");
419 throw getException(CSQL_NOT_SUPPORTED);