1 20 package com.nilostep.xlsql.jdbc; 21 22 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 41 public abstract class xlConnection implements Connection, Constants { 42 43 public static final Logger logger = Logger.getAnonymousLogger(); 44 protected com.nilostep.xlsql.database.xlDatabase datastore; 45 protected Connection dbCon; 46 protected String URL; 47 protected xlSqlWriter w; 48 protected xlSql xlsql; 49 protected xlSqlSelect query; 50 protected boolean closed; 51 52 57 public abstract void shutdown() throws Exception ; 58 59 71 public static xlConnection factory(String url, Properties info) 72 throws SQLException { 73 String sql_url = "jdbc:hsqldb:."; 74 String sql_user = ""; 75 String 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 e) { 96 throw new SQLException("xlSQL: connect to engine failure:" + 97 e.getMessage()); 98 } 99 } 100 101 106 public void setAutoCommit(boolean autoCommit) throws SQLException { 107 dbCon.setAutoCommit(autoCommit); 108 } 109 110 115 public boolean getAutoCommit() throws SQLException { 116 return dbCon.getAutoCommit(); 117 } 118 119 124 public void setCatalog(String catalog) throws SQLException { 125 dbCon.setCatalog(catalog); 126 } 127 128 133 public String getCatalog() throws SQLException { 134 return dbCon.getCatalog(); 135 } 136 137 142 public boolean isClosed() throws SQLException { 143 return dbCon.isClosed(); 144 } 145 146 151 public void setHoldability(int holdability) throws SQLException { 152 dbCon.setHoldability(holdability); 153 } 154 155 160 public int getHoldability() throws SQLException { 161 return dbCon.getHoldability(); 162 } 163 164 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 181 public void setReadOnly(boolean readOnly) throws SQLException { 182 dbCon.setReadOnly(readOnly); 185 186 } 188 189 194 public boolean isReadOnly() throws SQLException { 195 return dbCon.isReadOnly(); 196 } 197 198 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 215 public Savepoint setSavepoint(String name) throws SQLException { 216 Savepoint dbSave = dbCon.setSavepoint(name); 217 Savepoint save = new xlSavepoint(this, dbSave); 218 219 return save; 220 } 221 222 227 public void setTransactionIsolation(int level) throws SQLException { 228 dbCon.setTransactionIsolation(level); 229 } 230 231 236 public int getTransactionIsolation() throws SQLException { 237 return dbCon.getTransactionIsolation(); 238 } 239 240 245 public void setTypeMap(java.util.Map map) throws SQLException { 246 dbCon.setTypeMap(map); 247 } 248 249 254 public java.util.Map getTypeMap() throws SQLException { 255 return dbCon.getTypeMap(); 256 } 257 258 263 public SQLWarning getWarnings() throws SQLException { 264 return dbCon.getWarnings(); 265 } 266 267 272 public void clearWarnings() throws SQLException { 273 dbCon.clearWarnings(); 274 } 275 276 281 public void close() throws SQLException { 282 try { 283 datastore.flush(query); 284 shutdown(); 285 } catch (Exception e) { 286 e.printStackTrace(); 287 throw new SQLException(e.getMessage()); 288 } 289 } 290 291 296 public void commit() throws SQLException { 297 dbCon.commit(); 298 } 299 300 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 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 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 348 public String nativeSQL(String sql) throws SQLException { 349 return dbCon.nativeSQL(sql); 350 } 351 352 357 public CallableStatement prepareCall(String sql) throws SQLException { 358 CallableStatement dbClst = dbCon.prepareCall(sql); 359 CallableStatement clst = new xlCallableStatement(this, dbClst, sql); 360 361 return clst; 362 } 363 364 369 public CallableStatement prepareCall(String 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 384 public CallableStatement prepareCall(String 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 401 public PreparedStatement prepareStatement(String 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 414 public PreparedStatement prepareStatement(String 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 428 public PreparedStatement prepareStatement(String 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 441 public PreparedStatement prepareStatement(String 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 456 public PreparedStatement prepareStatement(String 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 473 public PreparedStatement prepareStatement(String sql, String [] 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 486 public void releaseSavepoint(Savepoint savepoint) throws SQLException { 487 dbCon.releaseSavepoint(savepoint); 488 } 489 490 495 public void rollback() throws SQLException { 496 dbCon.rollback(); 497 } 498 499 504 public void rollback(Savepoint savepoint) throws SQLException { 505 dbCon.rollback(savepoint); 506 } 507 508 512 void startup() throws SQLException { 513 try { 514 String 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 [] s = datastore.getSchemas(); 530 531 for (int i = 0; i < s.length; i++) { 532 v.add(w.wCreateSchema(s[i])); 534 535 String [] t = datastore.getTables(s[i]); 536 537 for (int j = 0; j < t.length; j++) { 538 String [] cn = datastore.getColumnNames(s[i], t[j]); 539 String [] ty = datastore.getColumnTypes(s[i], t[j]); 540 541 if (w.wDropTable(s[i], t[j]) != null) { 546 v.add(w.wDropTable(s[i], t[j])); 547 } 548 549 550 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 [][] matrix = datastore.getValues(s[i], t[j]); 556 String [] va = new String [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 stm.executeUpdate((String ) l.next()); 580 i++; 581 } 582 } catch (xlException xe) { 583 throw new SQLException("xlSQL: -ERR: while reading datastore"); 584 } 585 } 586 } | Popular Tags |