1 /***
2 Copyright (C) 2002-2003 Together
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18 */
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 /***
28 * This class implements the Driver interface for the CsvJdbc driver.
29 *
30 * @author Zoran Milakovic
31 */
32
33 public class CsvDriver implements Driver
34 {
35
36 //PARAMETER NAMES
37 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 public static final String TRIM_STRING = "trimString";
48
49 //DEFAULT VALUES
50 public static final String DEFAULT_EXTENSION = ".csv";
51 public static final char DEFAULT_SEPARATOR = ',';
52 public static final boolean DEFAULT_SUPPRESS = false;
53 public static final boolean DEFAULT_CREATE = false;
54 public static final long DEFAULT_FILE_MAXSIZE = 1500000000;
55 public static final String DEFAULT_LINE_BREAK_ESCAPE = "~CSVLB~";
56 public static final String DEFAULT_DOUBLE_QUOTE_ESCAPE = "\"\"";
57 public static final String DEFAULT_CARRIAGE_RETURN_ESCAPE = "~CSVCR~";
58 public static final boolean DEFAULT_USE_QUOTES = true;
59 public static final boolean DEFAULT_USE_QUOTES_ESCAPE = true;
60 public static final boolean DEFAULT_TRIM = false;
61
62 //data types
63 public static final String BINARY_TYPE = "BINARY";
64 public static final String VARCHAR_TYPE = "VARCHAR";
65
66
67 public static String FILE_NAME_EXT = "extension";
68 private final static String URL_PREFIX = "jdbc:relique:csv:";
69 private Properties info = null;
70
71 /* If set to true, driver will log into csvdriver.log file, in working directory */
72 private static boolean ENABLE_LOG = false;
73
74 /* If set to true, driver will show stack traces and other debug info */
75 public static boolean DEBUG = false;
76
77 /***
78 *Gets the propertyInfo attribute of the CsvDriver object
79 *
80 * @param url Description of Parameter
81 * @param info Description of Parameter
82 * @return The propertyInfo value
83 * @exception SQLException Description of Exception
84 * @since
85 */
86 public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
87 throws SQLException
88 {
89 return new DriverPropertyInfo[0];
90 }
91
92
93 /***
94 *Gets the majorVersion attribute of the CsvDriver object
95 *
96 * @return The majorVersion value
97 * @since
98 */
99 public int getMajorVersion()
100 {
101 return 1;
102 }
103
104
105 /***
106 *Gets the minorVersion attribute of the CsvDriver object
107 *
108 * @return The minorVersion value
109 * @since
110 */
111 public int getMinorVersion()
112 {
113 return 0;
114 }
115
116
117 /***
118 *Description of the Method
119 *
120 * @param url Description of Parameter
121 * @param info Description of Parameter
122 * @return Description of the Returned Value
123 * @exception SQLException Description of Exception
124 * @since
125 */
126 public Connection connect(String url, Properties info) throws SQLException
127 {
128 DriverManager.println("CsvJdbc - CsvDriver:connect() - url=" + url);
129 // check for correct url
130 if (!url.startsWith(URL_PREFIX))
131 {
132 return null;
133 }
134 // get filepath from url
135 String filePath = url.substring(URL_PREFIX.length());
136 String filePathAll = filePath;
137 StringTokenizer st = new StringTokenizer( filePath , ";" );
138 filePath = st.nextToken();
139 if (!filePath.endsWith(File.separator))
140 {
141 filePath += File.separator;
142 }
143 DriverManager.println("CsvJdbc - CsvDriver:connect() - filePath=" + filePath);
144 return new CsvConnection(filePathAll, info);
145 }
146
147
148 /***
149 * Description of the Method
150 *
151 * @param url Description of Parameter
152 * @return Description of the Returned Value
153 * @exception SQLException Description of Exception
154 * @since
155 */
156 public boolean acceptsURL(String url) throws SQLException
157 {
158 DriverManager.println("CsvJdbc - CsvDriver:accept() - url=" + url);
159 return url.startsWith(URL_PREFIX);
160 }
161
162
163 /***
164 *Description of the Method
165 *
166 * @return Description of the Returned Value
167 * @since
168 */
169 public boolean jdbcCompliant()
170 {
171 return false;
172 }
173 // This static block inits the driver when the class is loaded by the JVM.
174 static
175 {
176 try
177 {
178 java.sql.DriverManager.registerDriver(new CsvDriver());
179 }
180 catch (SQLException e)
181 {
182 throw new RuntimeException(
183 "FATAL ERROR: Could not initialise CSV driver ! Message was: "
184 + e.getMessage());
185 }
186 }
187
188 public static void log( String message) {
189 if ( CsvDriver.ENABLE_LOG ) {
190 try {
191 File file = new File("csvdriver.log");
192 if (!file.exists())
193 file.createNewFile();
194 java.io.RandomAccessFile fileLogr = new java.io.RandomAccessFile(file,
195 "rw");
196 fileLogr.seek(fileLogr.length());
197 fileLogr.writeBytes("CsvJdbc, "+message + "\r\n");
198 fileLogr.close();
199 }
200 catch (Exception ex) {
201 ex.printStackTrace();
202 }
203 }
204 }
205
206 }
207
This page was automatically generated by Maven