KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nilostep > xlsql > jdbc > xlConnection


1 /*(Header: NiLOSTEP / xlSQL)
2
3  Copyright (C) 2004 NiLOSTEP
4    NiLOSTEP Information Sciences
5    http://nilostep.com
6    nilo.de.roock@nilostep.com
7
8  This program is free software; you can redistribute it and/or modify it under
9  the terms of the GNU General Public License as published by the Free Software
10  Foundation; either version 2 of the License, or (at your option) any later
11  version.
12
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16  more details. You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software Foundation,
18  Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */

20 package com.nilostep.xlsql.jdbc;
21
22 // import com.nilostep.xlsql.xlSqlDatabase;
23
import com.nilostep.xlsql.database.*;
24 import com.nilostep.xlsql.sql.*;
25 import com.nilostep.xlsql.sql.xlSql;
26
27 import java.io.*;
28
29 import java.sql.*;
30
31 import java.util.*;
32 import java.util.logging.*;
33
34
35 /**
36  * Class
37  *
38  * @version $Revision: 1.13 $
39  * @author Jim Caprioli
40  */

41 public abstract class xlConnection implements Connection, Constants {
42     /** DOCUMENT ME! */
43     public static final Logger logger = Logger.getAnonymousLogger();
44     protected com.nilostep.xlsql.database.xlDatabase datastore;
45     protected Connection dbCon;
46     protected String JavaDoc URL;
47     protected xlSqlWriter w;
48     protected xlSql xlsql;
49     protected xlSqlSelect query;
50     protected boolean closed;
51
52     /**
53      * TODO: javadoc
54      *
55      * @throws Exception
56      */

57     public abstract void shutdown() throws Exception JavaDoc;
58
59     /**
60      * DOCUMENT ME!
61      *
62      * @param url jdbc:subprotocol:driver:directory
63      * @param info Properties
64      *
65      * @return JDBC Connection
66      *
67      * @throws SQLException if a database error occurs
68      *
69      * @see com.nilostep.xlsql.jdbc.xlDriver#connect
70      */

71     public static xlConnection factory(String JavaDoc url, Properties info)
72                                 throws SQLException {
73         String JavaDoc sql_url = "jdbc:hsqldb:.";
74         String JavaDoc sql_user = "";
75         String JavaDoc sql_password = "";
76
77         try {
78             if (info.getProperty("sql.url") != null) {
79                 sql_url = info.getProperty("sql.url");
80             }
81
82             if (sql_url.indexOf("mysql") > 0) {
83                 logger.info("Using MySQL...");
84
85                 return new xlConnectionMySQL(url, info);
86             } else if (sql_url.indexOf("mckoi") > 0) {
87                 logger.info("Using McKoi...");
88
89                 return new xlConnectionMcKoi(url, info);
90             } else {
91                 logger.info("Using in-memory HSQLDB..");
92
93                 return new xlConnectionHSQLDB(url, sql_url);
94             }
95         } catch (Exception JavaDoc e) {
96             throw new SQLException("xlSQL: connect to engine failure:" +
97                                    e.getMessage());
98         }
99     }
100
101     /**
102      * Implements method in interface java.sql.Connection
103      *
104      * @see java.sql.Connection#rollback
105      */

106     public void setAutoCommit(boolean autoCommit) throws SQLException {
107         dbCon.setAutoCommit(autoCommit);
108     }
109
110     /**
111      * Implements method in interface java.sql.Connection
112      *
113      * @see java.sql.Connection#getAutoCommit
114      */

115     public boolean getAutoCommit() throws SQLException {
116         return dbCon.getAutoCommit();
117     }
118
119     /**
120      * Implements method in interface java.sql.Connection
121      *
122      * @see java.sql.Connection#setCatalog
123      */

124     public void setCatalog(String JavaDoc catalog) throws SQLException {
125         dbCon.setCatalog(catalog);
126     }
127
128     /**
129      * Implements method in interface java.sql.Connection
130      *
131      * @see java.sql.Connection#getCatalog
132      */

133     public String JavaDoc getCatalog() throws SQLException {
134         return dbCon.getCatalog();
135     }
136
137     /**
138      * Implements method in interface java.sql.Connection
139      *
140      * @see java.sql.Connection#isClosed
141      */

142     public boolean isClosed() throws SQLException {
143         return dbCon.isClosed();
144     }
145
146     /**
147      * Implements method in interface java.sql.Connection
148      *
149      * @see java.sql.Connection#setHoldability
150      */

151     public void setHoldability(int holdability) throws SQLException {
152         dbCon.setHoldability(holdability);
153     }
154
155     /**
156      * Implements method in interface java.sql.Connection
157      *
158      * @see java.sql.Connection#getHoldability
159      */

160     public int getHoldability() throws SQLException {
161         return dbCon.getHoldability();
162     }
163
164     /**
165      * Implements method in interface java.sql.Connection
166      *
167      * @see java.sql.Connection#getMetaData
168      */

169     public DatabaseMetaData getMetaData() throws SQLException {
170         DatabaseMetaData dbMeta = dbCon.getMetaData();
171         DatabaseMetaData meta = new xlDatabaseMetaData(this, dbMeta);
172
173         return meta;
174     }
175
176     /**
177      * Implements method in interface java.sql.Connection
178      *
179      * @see java.sql.Connection#setReadOnly
180      */

181     public void setReadOnly(boolean readOnly) throws SQLException {
182         //NiLOSTEP...
183
// [ ISSUE ]
184
dbCon.setReadOnly(readOnly);
185
186         //End
187
}
188
189     /**
190      * Implements method in interface java.sql.Connection
191      *
192      * @see java.sql.Connection#isReadOnly
193      */

194     public boolean isReadOnly() throws SQLException {
195         return dbCon.isReadOnly();
196     }
197
198     /**
199      * Implements method in interface java.sql.Connection
200      *
201      * @see java.sql.Connection#setSavepoint
202      */

203     public Savepoint setSavepoint() throws SQLException {
204         Savepoint dbSave = dbCon.setSavepoint();
205         Savepoint save = new xlSavepoint(this, dbSave);
206
207         return save;
208     }
209
210     /**
211      * Implements method in interface java.sql.Connection
212      *
213      * @see java.sql.Connection#setSavepoint
214      */

215     public Savepoint setSavepoint(String JavaDoc name) throws SQLException {
216         Savepoint dbSave = dbCon.setSavepoint(name);
217         Savepoint save = new xlSavepoint(this, dbSave);
218
219         return save;
220     }
221
222     /**
223      * Implements method in interface java.sql.Connection
224      *
225      * @see java.sql.Connection#setTransactionIsolation
226      */

227     public void setTransactionIsolation(int level) throws SQLException {
228         dbCon.setTransactionIsolation(level);
229     }
230
231     /**
232      * Implements method in interface java.sql.Connection
233      *
234      * @see java.sql.Connection#getTransactionIsolation
235      */

236     public int getTransactionIsolation() throws SQLException {
237         return dbCon.getTransactionIsolation();
238     }
239
240     /**
241      * Implements method in interface java.sql.Connection
242      *
243      * @see java.sql.Connection#setTypeMap
244      */

245     public void setTypeMap(java.util.Map JavaDoc map) throws SQLException {
246         dbCon.setTypeMap(map);
247     }
248
249     /**
250      * Implements method in interface java.sql.Connection
251      *
252      * @see java.sql.Connection#getTypeMap
253      */

254     public java.util.Map JavaDoc getTypeMap() throws SQLException {
255         return dbCon.getTypeMap();
256     }
257
258     /**
259      * Implements method in interface java.sql.Connection
260      *
261      * @see java.sql.Connection#getWarnings
262      */

263     public SQLWarning getWarnings() throws SQLException {
264         return dbCon.getWarnings();
265     }
266
267     /**
268      * Implements method in interface java.sql.Connection
269      *
270      * @see java.sql.Connection#clearWarnings
271      */

272     public void clearWarnings() throws SQLException {
273         dbCon.clearWarnings();
274     }
275
276     /**
277      * Implements method in interface java.sql.Connection
278      *
279      * @see java.sql.Connection#close
280      */

281     public void close() throws SQLException {
282         try {
283             datastore.flush(query);
284             shutdown();
285         } catch (Exception JavaDoc e) {
286             e.printStackTrace();
287             throw new SQLException(e.getMessage());
288         }
289     }
290
291     /**
292      * Implements method in interface java.sql.Connection
293      *
294      * @see java.sql.Connection#commit
295      */

296     public void commit() throws SQLException {
297         dbCon.commit();
298     }
299
300     /**
301      * Implements method in interface java.sql.Connection
302      *
303      * @see java.sql.Connection#createStatement
304      */

305     public Statement createStatement() throws SQLException {
306         Statement dbStm = dbCon.createStatement();
307         Statement stmt = new xlStatement(this, dbStm);
308
309         return stmt;
310     }
311
312     /**
313      * Implements method in interface java.sql.Connection
314      *
315      * @see java.sql.Connection#createStatement
316      */

317     public Statement createStatement(int resultSetType,
318                                      int resultSetConcurrency)
319                               throws SQLException {
320         Statement dbStm = dbCon.createStatement(resultSetType,
321                                                 resultSetConcurrency);
322         Statement stmt = new xlStatement(this, dbStm);
323
324         return stmt;
325     }
326
327     /**
328      * Implements method in interface java.sql.Connection
329      *
330      * @see java.sql.Connection#createStatement
331      */

332     public Statement createStatement(int resultSetType,
333                                      int resultSetConcurrency,
334                                      int resultSetHoldability)
335                               throws SQLException {
336         Statement dbStm = dbCon.createStatement(resultSetType,
337                                                 resultSetConcurrency);
338         Statement stmt = new xlStatement(this, dbStm);
339
340         return stmt;
341     }
342
343     /**
344      * Implements method in interface java.sql.Connection
345      *
346      * @see java.sql.Connection#nativeSQL
347      */

348     public String JavaDoc nativeSQL(String JavaDoc sql) throws SQLException {
349         return dbCon.nativeSQL(sql);
350     }
351
352     /**
353      * Implements method in interface java.sql.Connection
354      *
355      * @see java.sql.Connection#prepareCall
356      */

357     public CallableStatement prepareCall(String JavaDoc sql) throws SQLException {
358         CallableStatement dbClst = dbCon.prepareCall(sql);
359         CallableStatement clst = new xlCallableStatement(this, dbClst, sql);
360
361         return clst;
362     }
363
364     /**
365      * Implements method in interface java.sql.Connection
366      *
367      * @see java.sql.Connection#prepareCall
368      */

369     public CallableStatement prepareCall(String JavaDoc sql, int resultSetType,
370                                          int resultSetConcurrency)
371                                   throws SQLException {
372         CallableStatement dbClst = dbCon.prepareCall(sql, resultSetType,
373                                                      resultSetConcurrency);
374         CallableStatement clst = new xlCallableStatement(this, dbClst, sql);
375
376         return clst;
377     }
378
379     /**
380      * Implements method in interface java.sql.Connection
381      *
382      * @see java.sql.Connection#prepareCall
383      */

384     public CallableStatement prepareCall(String JavaDoc sql, int resultSetType,
385                                          int resultSetConcurrency,
386                                          int resultSetHoldability)
387                                   throws SQLException {
388         CallableStatement dbClst = dbCon.prepareCall(sql, resultSetType,
389                                                      resultSetConcurrency,
390                                                      resultSetHoldability);
391         CallableStatement clst = new xlCallableStatement(this, dbClst, sql);
392
393         return clst;
394     }
395
396     /**
397      * Implements method in interface java.sql.Connection
398      *
399      * @see java.sql.Connection#prepareStatement
400      */

401     public PreparedStatement prepareStatement(String JavaDoc sql)
402                                        throws SQLException {
403         PreparedStatement dbPstm = dbCon.prepareStatement(sql);
404         PreparedStatement pstm = new xlPreparedStatement(this, dbPstm, sql);
405
406         return pstm;
407     }
408
409     /**
410      * Implements method in interface java.sql.Connection
411      *
412      * @see java.sql.Connection#prepareStatement
413      */

414     public PreparedStatement prepareStatement(String JavaDoc sql, int autoGeneratedKeys)
415                                        throws SQLException {
416         PreparedStatement dbPstm = dbCon.prepareStatement(sql,
417                                                           autoGeneratedKeys);
418         PreparedStatement pstm = new xlPreparedStatement(this, dbPstm, sql);
419
420         return pstm;
421     }
422
423     /**
424      * Implements method in interface java.sql.Connection
425      *
426      * @see java.sql.Connection#prepareStatement
427      */

428     public PreparedStatement prepareStatement(String JavaDoc sql, int[] columnIndexes)
429                                        throws SQLException {
430         PreparedStatement dbPstm = dbCon.prepareStatement(sql, columnIndexes);
431         PreparedStatement pstm = new xlPreparedStatement(this, dbPstm, sql);
432
433         return pstm;
434     }
435
436     /**
437      * Implements method in interface java.sql.Connection
438      *
439      * @see java.sql.Connection#prepareStatement
440      */

441     public PreparedStatement prepareStatement(String JavaDoc sql, int resultSetType,
442                                               int resultSetConcurrency)
443                                        throws SQLException {
444         PreparedStatement dbPstm = dbCon.prepareStatement(sql, resultSetType,
445                                                           resultSetConcurrency);
446         PreparedStatement pstm = new xlPreparedStatement(this, dbPstm, sql);
447
448         return pstm;
449     }
450
451     /**
452      * Implements method in interface java.sql.Connection
453      *
454      * @see java.sql.Connection#prepareStatement
455      */

456     public PreparedStatement prepareStatement(String JavaDoc sql, int resultSetType,
457                                               int resultSetConcurrency,
458                                               int resultSetHoldability)
459                                        throws SQLException {
460         PreparedStatement dbPstm = dbCon.prepareStatement(sql, resultSetType,
461                                                           resultSetConcurrency,
462                                                           resultSetHoldability);
463         PreparedStatement pstm = new xlPreparedStatement(this, dbPstm, sql);
464
465         return pstm;
466     }
467
468     /**
469      * Implements method in interface java.sql.Connection
470      *
471      * @see java.sql.Connection#prepareStatement
472      */

473     public PreparedStatement prepareStatement(String JavaDoc sql, String JavaDoc[] columnNames)
474                                        throws SQLException {
475         PreparedStatement dbPstm = dbCon.prepareStatement(sql, columnNames);
476         PreparedStatement pstm = new xlPreparedStatement(this, dbPstm, sql);
477
478         return pstm;
479     }
480
481     /**
482      * Implements method in interface java.sql.Connection
483      *
484      * @see java.sql.Connection#releaseSavepoint
485      */

486     public void releaseSavepoint(Savepoint savepoint) throws SQLException {
487         dbCon.releaseSavepoint(savepoint);
488     }
489
490     /**
491      * Implements method in interface java.sql.Connection
492      *
493      * @see java.sql.Connection#rollback
494      */

495     public void rollback() throws SQLException {
496         dbCon.rollback();
497     }
498
499     /**
500      * Implements method in interface java.sql.Connection
501      *
502      * @see java.sql.Connection#rollback
503      */

504     public void rollback(Savepoint savepoint) throws SQLException {
505         dbCon.rollback(savepoint);
506     }
507
508     /**
509      * TODO: javadoc
510      * @throws SQLException DOCUMENT ME!
511      */

512     void startup() throws SQLException {
513         try {
514             String JavaDoc dir;
515
516             if (URL.indexOf("csv") > 0) {
517                 dir = URL.substring(URL_PFX_CSV.length());
518                 logger.info("mounting: " + dir + " using jdbc:nilostep:csv");
519                 datastore = xlDatabaseFactory.create(new File(dir), "csv");
520             } else {
521                 dir = URL.substring(URL_PFX_XLS.length());
522                 logger.info("mounting: " + dir +
523                             " using jdbc:nilostep:excel");
524                 datastore = xlDatabaseFactory.create(new File(dir), "xls");
525             }
526
527             Vector v = new Vector();
528
529             String JavaDoc[] s = datastore.getSchemas();
530
531             for (int i = 0; i < s.length; i++) {
532                 // -->
533
v.add(w.wCreateSchema(s[i]));
534
535                 String JavaDoc[] t = datastore.getTables(s[i]);
536
537                 for (int j = 0; j < t.length; j++) {
538                     String JavaDoc[] cn = datastore.getColumnNames(s[i], t[j]);
539                     String JavaDoc[] ty = datastore.getColumnTypes(s[i], t[j]);
540
541                     // -->
542
//NiLOSTEP...
543
//workaround for IF EXISTS known bug in McKoi 1.0.2
544
//v.add(w.wDropTable(s[i], t[j]));
545
if (w.wDropTable(s[i], t[j]) != null) {
546                         v.add(w.wDropTable(s[i], t[j]));
547                     }
548
549
550                     //End
551
v.add(w.wCreateTable(s[i], t[j], cn, ty));
552
553                     int R = datastore.getRows(s[i], t[j]);
554                     int C = cn.length;
555                     String JavaDoc[][] matrix = datastore.getValues(s[i], t[j]);
556                     String JavaDoc[] va = new String JavaDoc[C];
557
558                     if ((cn != null) && (ty != null)) {
559                         for (int k = 0; k < R; k++) {
560                             for (int m = 0; m < C; m++) {
561                                 va[m] = matrix[m][k];
562                             }
563
564                             v.add(w.wInsert(s[i], t[j], cn, ty, va));
565                         }
566                     }
567                 }
568             }
569
570             Statement stm = dbCon.createStatement();
571             ListIterator l = v.listIterator();
572             int i = 0;
573
574             while (l.hasNext()) {
575                 //String sql;
576
//sql = (String) l.next();
577
//System.out.println("sql=" + sql);
578
//stm.executeUpdate(sql);
579
stm.executeUpdate((String JavaDoc) l.next());
580                 i++;
581             }
582         } catch (xlException xe) {
583             throw new SQLException("xlSQL: -ERR: while reading datastore");
584         }
585     }
586 }
Popular Tags