1 19 20 package org.relique.jdbc.csv; 21 22 import java.io.File ; 23 import java.sql.CallableStatement ; 24 import java.sql.Connection ; 25 import java.sql.DatabaseMetaData ; 26 import java.sql.PreparedStatement ; 27 import java.sql.SQLException ; 28 import java.sql.SQLWarning ; 29 import java.sql.Savepoint ; 30 import java.sql.Statement ; 31 import java.util.Enumeration ; 32 import java.util.Map ; 33 import java.util.Properties ; 34 import java.util.StringTokenizer ; 35 import java.util.Vector ; 36 37 42 public class CsvConnection 43 implements Connection { 44 45 46 private String path; 47 48 49 private String extension = CsvDriver.DEFAULT_EXTENSION; 50 51 52 private char separator = CsvDriver.DEFAULT_SEPARATOR; 53 54 55 private boolean suppressHeaders = CsvDriver.DEFAULT_SUPPRESS; 56 57 58 private boolean create = CsvDriver.DEFAULT_CREATE; 59 60 61 private long maxFileSize = CsvDriver.DEFAULT_FILE_MAXSIZE; 62 63 64 private String lineBrakesEscape = CsvDriver.DEFAULT_LINE_BREAK_ESCAPE; 65 66 67 private String carriageReturnEscape = CsvDriver.DEFAULT_CARRIAGE_RETURN_ESCAPE; 68 69 70 private boolean useQuotes = CsvDriver.DEFAULT_USE_QUOTES; 71 72 73 private boolean useQuotesEscape = CsvDriver.DEFAULT_USE_QUOTES_ESCAPE; 74 75 76 private Vector statements = new Vector (); 77 78 79 private String charset = null; 80 81 82 private boolean closed; 83 84 85 private boolean autoCommit=true; 86 87 88 92 protected CsvConnection(String path) throws SQLException { 93 if (path == null || path.length() == 0) { 95 throw new IllegalArgumentException ( 96 "'path' argument may not be empty or null"); 97 } 98 StringTokenizer st = new StringTokenizer (path, ";"); 100 this.path = st.nextToken(); 101 if (!this.path.endsWith(File.separator)) { 102 this.path += File.separator; 103 } 104 while (st.hasMoreTokens()) { 105 String next = st.nextToken(); 106 if (!this.setProperty(next)) { 107 throw new IllegalArgumentException ( 108 "unknown property " + next); 109 } 110 } 111 File filePath = new File (this.path); 112 113 if (!this.create && !filePath.exists()) { 114 throw new SQLException ( 115 "Specified path '" + filePath.getAbsolutePath() + 116 "' does not exist !"); 117 } 118 119 if (this.create && !filePath.exists()) 120 filePath.mkdirs(); 121 122 } 123 124 129 protected CsvConnection(String path, Properties info) throws SQLException { 130 this(path); 131 if (info != null) { 133 if (info.getProperty(CsvDriver.FILE_EXTENSION) != null) { 135 extension = info.getProperty(CsvDriver.FILE_EXTENSION); 136 if (!extension.startsWith(".")) 137 extension = "." + extension; 138 } 139 if (info.getProperty(CsvDriver.SEPARATOR) != null) { 141 separator = info.getProperty(CsvDriver.SEPARATOR).charAt(0); 142 if(info.getProperty(CsvDriver.SEPARATOR).equalsIgnoreCase("TAB")) 144 this.separator = '\t'; 145 if(info.getProperty(CsvDriver.SEPARATOR).equalsIgnoreCase("SC")) 147 this.separator = ';'; 148 } 149 if (info.getProperty(CsvDriver.SUPPRESS_HEADERS) != null) { 151 suppressHeaders = Boolean.valueOf(info.getProperty( 152 CsvDriver.SUPPRESS_HEADERS)).booleanValue(); 153 } 154 if (info.getProperty(CsvDriver.CHARSET) != null) { 156 charset = info.getProperty(CsvDriver.CHARSET); 157 } 158 if (info.getProperty(CsvDriver.CREATE) != null) { 160 create = Boolean.valueOf(info.getProperty( 161 CsvDriver.SUPPRESS_HEADERS)).booleanValue(); 162 } 163 if (info.getProperty(CsvDriver.MAXFILESIZE) != null) { 165 maxFileSize = Long.valueOf(info.getProperty(CsvDriver.MAXFILESIZE)). 166 longValue(); 167 } 168 if (info.getProperty(CsvDriver.LINE_BREAK_ESCAPE) != null) { 170 lineBrakesEscape = info.getProperty(CsvDriver.LINE_BREAK_ESCAPE); 171 } 172 if (info.getProperty(CsvDriver.CARRIAGE_RETURN_ESCAPE) != null) { 174 carriageReturnEscape = info.getProperty(CsvDriver.CARRIAGE_RETURN_ESCAPE); 175 } 176 if (info.getProperty(CsvDriver.USE_QUOTES) != null) { 178 useQuotes = new Boolean (info.getProperty(CsvDriver.USE_QUOTES)).booleanValue(); 179 } 180 if (info.getProperty(CsvDriver.USE_QUOTES_ESCAPE) != null) { 182 useQuotesEscape = new Boolean (info.getProperty(CsvDriver.USE_QUOTES_ESCAPE)).booleanValue(); 183 } 184 } 185 } 186 187 private boolean setProperty(String propString) { 188 boolean retVal = true; 189 StringTokenizer st = new StringTokenizer (propString, "="); 190 String name = st.nextToken(); 191 String value = st.nextToken(); 192 if (name.equals(CsvDriver.SEPARATOR)) { 193 this.separator = value.charAt(0); 194 if(value.equalsIgnoreCase("TAB")) 196 this.separator = '\t'; 197 if(value.equalsIgnoreCase("SC")) 199 this.separator = ';'; 200 } 201 else if (name.equals(CsvDriver.FILE_EXTENSION)) { 202 if (!value.startsWith(".")) 203 value = "." + value; 204 this.extension = value; 205 } 206 else if (name.equals(CsvDriver.SUPPRESS_HEADERS)) { 207 this.suppressHeaders = Boolean.valueOf(value).booleanValue(); 208 } 209 else if (name.equals(CsvDriver.CHARSET)) { 210 this.charset = value; 211 } 212 else if (name.equals(CsvDriver.CREATE)) { 213 this.create = Boolean.valueOf(value).booleanValue(); 214 } 215 else if (name.equals(CsvDriver.MAXFILESIZE)) { 216 this.maxFileSize = Long.valueOf(value).longValue(); 217 } 218 else if (name.equals(CsvDriver.LINE_BREAK_ESCAPE)) { 219 this.lineBrakesEscape = value; 220 } 221 else if (name.equals(CsvDriver.CARRIAGE_RETURN_ESCAPE)) { 222 this.carriageReturnEscape = value; 223 } 224 else if (name.equals(CsvDriver.USE_QUOTES)) { 225 this.useQuotes = new Boolean ( value ).booleanValue(); 226 } 227 else if (name.equals(CsvDriver.USE_QUOTES_ESCAPE)) { 228 this.useQuotesEscape = new Boolean ( value ).booleanValue(); 229 } 230 else 231 retVal = false; 232 return retVal; 233 } 234 235 250 public Statement createStatement() throws SQLException { 251 CsvStatement statement = new CsvStatement(this); 252 statements.add(statement); 253 return statement; 254 } 255 256 285 public PreparedStatement prepareStatement(String sql) throws SQLException { 286 int index = sql.indexOf("?"); 287 while (index != -1) { 288 sql = sql.substring(0, index) + CsvPreparedStatement.PREPARE_SEPARATOR + sql.substring(index + 1); 289 index = sql.indexOf("?"); 290 } 291 292 CsvPreparedStatement statement = new CsvPreparedStatement(this, sql); 293 statements.add(statement); 294 return statement; 295 } 296 297 324 public CallableStatement prepareCall(String sql) throws SQLException { 325 throw new UnsupportedOperationException ( 326 "Connection.prepareCall(String) unsupported"); 327 } 328 329 340 public String nativeSQL(String sql) throws SQLException { 341 throw new UnsupportedOperationException ( 342 "Connection.nativeSQL(String) unsupported"); 343 } 344 345 373 public void setAutoCommit(boolean autoCommit) throws SQLException { 374 this.autoCommit = autoCommit; 375 } 378 379 388 public boolean getAutoCommit() throws SQLException { 389 return this.autoCommit; 393 } 394 395 406 public void commit() throws SQLException { 407 for (int i = 0; i < this.statements.size(); i++) { 408 ( (Statement ) statements.get(i)).close(); 409 } 410 } 411 412 422 public void rollback() throws SQLException { 423 throw new UnsupportedOperationException ( 424 "Connection.rollback() unsupported"); 425 } 426 427 441 public void close() throws SQLException { 442 for (Enumeration i = statements.elements(); i.hasMoreElements(); ) { 444 Statement statement = (Statement ) i.nextElement(); 445 statement.close(); 446 } 447 closed = true; 449 } 450 451 468 public boolean isClosed() throws SQLException { 469 return closed; 470 } 471 472 484 public DatabaseMetaData getMetaData() throws SQLException { 485 throw new UnsupportedOperationException ( 486 "Connection.getMetaData() unsupported"); 487 } 488 489 500 public void setReadOnly(boolean readOnly) throws SQLException { 501 throw new UnsupportedOperationException ( 502 "Connection.setReadOnly(boolean) unsupported"); 503 } 504 505 513 public boolean isReadOnly() throws SQLException { 514 return true; 515 } 516 517 530 public void setCatalog(String catalog) throws SQLException { 531 } 533 534 541 public String getCatalog() throws SQLException { 542 return null; 543 } 544 545 567 public void setTransactionIsolation(int level) throws SQLException { 568 throw new UnsupportedOperationException ( 569 "Connection.setTransactionIsolation(int) unsupported"); 570 } 571 572 586 public int getTransactionIsolation() throws SQLException { 587 return Connection.TRANSACTION_NONE; 588 } 589 590 611 public SQLWarning getWarnings() throws SQLException { 612 throw new UnsupportedOperationException ( 613 "Connection.getWarnings() unsupported"); 614 } 615 616 624 public void clearWarnings() throws SQLException { 625 throw new UnsupportedOperationException ( 626 "Connection.getWarnings() unsupported"); 627 } 628 629 631 652 public Statement createStatement(int resultSetType, int resultSetConcurrency) throws 653 SQLException { 654 throw new UnsupportedOperationException ( 655 "Connection.createStatement(int, int) unsupported"); 656 } 657 658 682 public PreparedStatement prepareStatement(String sql, int resultSetType, 683 int resultSetConcurrency) throws 684 SQLException { 685 throw new UnsupportedOperationException ( 686 "Connection.prepareStatement(String, int, int) unsupported"); 687 } 688 689 712 public CallableStatement prepareCall(String sql, int resultSetType, 713 int resultSetConcurrency) throws 714 SQLException { 715 throw new UnsupportedOperationException ( 716 "Connection.prepareCall(String, int, int) unsupported"); 717 } 718 719 730 public Map getTypeMap() throws SQLException { 731 throw new UnsupportedOperationException ( 732 "Connection.getTypeMap() unsupported"); 733 } 734 735 748 public void setTypeMap(Map map) throws SQLException { 749 throw new UnsupportedOperationException ( 750 "Connection.setTypeMap(Map) unsupported"); 751 } 752 753 769 public void setHoldability(int holdability) throws SQLException { 770 throw new UnsupportedOperationException ( 771 "Connection.setHoldability(int) unsupported"); 772 } 773 774 785 public int getHoldability() throws SQLException { 786 throw new UnsupportedOperationException ( 787 "Connection.getHoldability() unsupported"); 788 } 789 790 public Savepoint setSavepoint() throws SQLException { 792 throw new UnsupportedOperationException ( 793 "Connection.setSavepoint() unsupported"); 794 } 795 796 public Savepoint setSavepoint(String name) throws SQLException { 797 throw new UnsupportedOperationException ( 798 "Connection.setSavepoint(String) unsupported"); 799 } 800 801 public void rollback(Savepoint savepoint) throws SQLException { 802 throw new UnsupportedOperationException ( 803 "Connection.rollback(Savepoint) unsupported"); 804 } 805 806 public void releaseSavepoint(Savepoint savepoint) throws SQLException { 807 throw new UnsupportedOperationException ( 808 "Connection.releaseSavepoint(Savepoint) unsupported"); 809 } 810 811 public Statement createStatement(int resultSetType, 812 int resultSetConcurrency, 813 int resultSetHoldability) throws 814 SQLException { 815 throw new UnsupportedOperationException ( 816 "Connection.createStatement(int,int,int) unsupported"); 817 } 818 819 public PreparedStatement prepareStatement(String sql, 820 int resultSetType, 821 int resultSetConcurrency, 822 int resultSetHoldability) throws 823 SQLException { 824 throw new UnsupportedOperationException ( 825 "Connection.prepareStatement(String,int,int,int) unsupported"); 826 } 827 828 public CallableStatement prepareCall(String sql, 829 int resultSetType, 830 int resultSetConcurrency, 831 int resultSetHoldability) throws 832 SQLException { 833 throw new UnsupportedOperationException ( 834 "Connection.prepareCall(String,int,int,int) unsupported"); 835 } 836 837 public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws 838 SQLException { 839 throw new UnsupportedOperationException ( 840 "Connection.prepareStatement(String,int) unsupported"); 841 } 842 843 public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws 844 SQLException { 845 throw new UnsupportedOperationException ( 846 "Connection.prepareStatement(String,int[]) unsupported"); 847 } 848 849 public PreparedStatement prepareStatement(String sql, String [] columnNames) throws 850 SQLException { 851 throw new UnsupportedOperationException ( 852 "Connection.prepareStatement(String,String[]) unsupported"); 853 } 854 855 859 863 protected String getPath() { 864 return path; 865 } 866 867 871 protected String getExtension() { 872 return extension; 873 } 874 875 879 protected char getSeperator() { 880 return separator; 881 } 882 883 887 protected boolean isSuppressHeaders() { 888 return suppressHeaders; 889 } 890 891 895 protected String getCharset() { 896 return charset; 897 } 898 899 protected long getMaxFileSize() { 900 return maxFileSize; 901 } 902 903 protected String getLineBreakEscape() { 904 return lineBrakesEscape; 905 } 906 907 protected String getCarriageReturnEscape() { 908 return carriageReturnEscape; 909 } 910 911 protected boolean getUseQuotes() { 912 return this.useQuotes; 913 } 914 915 protected boolean getUseQuotesEscape() { 916 return this.useQuotesEscape; 917 } 918 919 } 920 | Popular Tags |