1 19 20 package org.relique.jdbc.csv; 21 22 import java.sql.*; 23 import java.util.Properties ; 24 import java.util.StringTokenizer ; 25 import java.io.File ; 26 27 32 33 public class CsvDriver implements Driver 34 { 35 36 public static final String FILE_EXTENSION="fileExtension"; 38 public static final String SEPARATOR="separator"; 39 public static final String MAXFILESIZE = "maxFileSize"; 40 public static final String CREATE="create"; 41 public static final String SUPPRESS_HEADERS="suppressHeaders"; 42 public static final String CHARSET = "charset"; 43 public static final String LINE_BREAK_ESCAPE = "lineBreakEscape"; 44 public static final String CARRIAGE_RETURN_ESCAPE = "carriageReturnEscape"; 45 public static final String USE_QUOTES = "useQuotes"; 46 public static final String USE_QUOTES_ESCAPE = "useQuotesEscape"; 47 48 public static final String DEFAULT_EXTENSION = ".csv"; 50 public static final char DEFAULT_SEPARATOR = ','; 51 public static final boolean DEFAULT_SUPPRESS = false; 52 public static final boolean DEFAULT_CREATE = false; 53 public static final long DEFAULT_FILE_MAXSIZE = 1500000000; 54 public static final String DEFAULT_LINE_BREAK_ESCAPE = "~CSVLB~"; 55 public static final String DEFAULT_DOUBLE_QUOTE_ESCAPE = "\"\""; 56 public static final String DEFAULT_CARRIAGE_RETURN_ESCAPE = "~CSVCR~"; 57 public static final boolean DEFAULT_USE_QUOTES = true; 58 public static final boolean DEFAULT_USE_QUOTES_ESCAPE = true; 59 60 public static final String BINARY_TYPE = "BINARY"; 62 public static final String VARCHAR_TYPE = "VARCHAR"; 63 64 65 public static String FILE_NAME_EXT = "extension"; 66 private final static String URL_PREFIX = "jdbc:relique:csv:"; 67 private Properties info = null; 68 69 70 private static boolean ENABLE_LOG = false; 71 72 73 public static boolean DEBUG = false; 74 75 84 public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) 85 throws SQLException 86 { 87 return new DriverPropertyInfo[0]; 88 } 89 90 91 97 public int getMajorVersion() 98 { 99 return 1; 100 } 101 102 103 109 public int getMinorVersion() 110 { 111 return 0; 112 } 113 114 115 124 public Connection connect(String url, Properties info) throws SQLException 125 { 126 DriverManager.println("CsvJdbc - CsvDriver:connect() - url=" + url); 127 if (!url.startsWith(URL_PREFIX)) 129 { 130 return null; 131 } 132 String filePath = url.substring(URL_PREFIX.length()); 134 String filePathAll = filePath; 135 StringTokenizer st = new StringTokenizer ( filePath , ";" ); 136 filePath = st.nextToken(); 137 if (!filePath.endsWith(File.separator)) 138 { 139 filePath += File.separator; 140 } 141 DriverManager.println("CsvJdbc - CsvDriver:connect() - filePath=" + filePath); 142 return new CsvConnection(filePathAll, info); 143 } 144 145 146 154 public boolean acceptsURL(String url) throws SQLException 155 { 156 DriverManager.println("CsvJdbc - CsvDriver:accept() - url=" + url); 157 return url.startsWith(URL_PREFIX); 158 } 159 160 161 167 public boolean jdbcCompliant() 168 { 169 return false; 170 } 171 static 173 { 174 try 175 { 176 java.sql.DriverManager.registerDriver(new CsvDriver()); 177 } 178 catch (SQLException e) 179 { 180 throw new RuntimeException ( 181 "FATAL ERROR: Could not initialise CSV driver ! Message was: " 182 + e.getMessage()); 183 } 184 } 185 186 public static void log( String message) { 187 if ( CsvDriver.ENABLE_LOG ) { 188 try { 189 File file = new File ("csvdriver.log"); 190 if (!file.exists()) 191 file.createNewFile(); 192 java.io.RandomAccessFile fileLogr = new java.io.RandomAccessFile (file, 193 "rw"); 194 fileLogr.seek(fileLogr.length()); 195 fileLogr.writeBytes("CsvJdbc, "+message + "\r\n"); 196 fileLogr.close(); 197 } 198 catch (Exception ex) { 199 ex.printStackTrace(); 200 } 201 } 202 } 203 204 } 205 206 | Popular Tags |