001: /**
002: Copyright (C) 2002-2003 Together
003:
004: This library is free software; you can redistribute it and/or
005: modify it under the terms of the GNU Lesser General Public
006: License as published by the Free Software Foundation; either
007: version 2.1 of the License, or (at your option) any later version.
008:
009: This library is distributed in the hope that it will be useful,
010: but WITHOUT ANY WARRANTY; without even the implied warranty of
011: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: Lesser General Public License for more details.
013:
014: You should have received a copy of the GNU Lesser General Public
015: License along with this library; if not, write to the Free Software
016: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: */package org.relique.jdbc.csv;
019:
020: import java.sql.*;
021: import java.util.Properties;
022: import java.util.StringTokenizer;
023: import java.io.File;
024:
025: /**
026: * This class implements the Driver interface for the CsvJdbc driver.
027: *
028: * @author Zoran Milakovic
029: */
030:
031: public class CsvDriver implements Driver {
032:
033: //PARAMETER NAMES
034: public static final String FILE_EXTENSION = "fileExtension";
035: public static final String SEPARATOR = "separator";
036: public static final String MAXFILESIZE = "maxFileSize";
037: public static final String CREATE = "create";
038: public static final String SUPPRESS_HEADERS = "suppressHeaders";
039: public static final String CHARSET = "charset";
040: public static final String LINE_BREAK_ESCAPE = "lineBreakEscape";
041: public static final String CARRIAGE_RETURN_ESCAPE = "carriageReturnEscape";
042: public static final String USE_QUOTES = "useQuotes";
043: public static final String USE_QUOTES_ESCAPE = "useQuotesEscape";
044: public static final String TRIM_STRING = "trimString";
045:
046: //DEFAULT VALUES
047: public static final String DEFAULT_EXTENSION = ".csv";
048: public static final char DEFAULT_SEPARATOR = ',';
049: public static final boolean DEFAULT_SUPPRESS = false;
050: public static final boolean DEFAULT_CREATE = false;
051: public static final long DEFAULT_FILE_MAXSIZE = 1500000000;
052: public static final String DEFAULT_LINE_BREAK_ESCAPE = "~CSVLB~";
053: public static final String DEFAULT_DOUBLE_QUOTE_ESCAPE = "\"\"";
054: public static final String DEFAULT_CARRIAGE_RETURN_ESCAPE = "~CSVCR~";
055: public static final boolean DEFAULT_USE_QUOTES = true;
056: public static final boolean DEFAULT_USE_QUOTES_ESCAPE = true;
057: public static final boolean DEFAULT_TRIM = false;
058:
059: //data types
060: public static final String BINARY_TYPE = "BINARY";
061: public static final String VARCHAR_TYPE = "VARCHAR";
062:
063: public static String FILE_NAME_EXT = "extension";
064: private final static String URL_PREFIX = "jdbc:relique:csv:";
065: private Properties info = null;
066:
067: /* If set to true, driver will log into csvdriver.log file, in working directory */
068: private static boolean ENABLE_LOG = false;
069:
070: /* If set to true, driver will show stack traces and other debug info */
071: public static boolean DEBUG = false;
072:
073: /**
074: *Gets the propertyInfo attribute of the CsvDriver object
075: *
076: * @param url Description of Parameter
077: * @param info Description of Parameter
078: * @return The propertyInfo value
079: * @exception SQLException Description of Exception
080: * @since
081: */
082: public DriverPropertyInfo[] getPropertyInfo(String url,
083: Properties info) throws SQLException {
084: return new DriverPropertyInfo[0];
085: }
086:
087: /**
088: *Gets the majorVersion attribute of the CsvDriver object
089: *
090: * @return The majorVersion value
091: * @since
092: */
093: public int getMajorVersion() {
094: return 1;
095: }
096:
097: /**
098: *Gets the minorVersion attribute of the CsvDriver object
099: *
100: * @return The minorVersion value
101: * @since
102: */
103: public int getMinorVersion() {
104: return 0;
105: }
106:
107: /**
108: *Description of the Method
109: *
110: * @param url Description of Parameter
111: * @param info Description of Parameter
112: * @return Description of the Returned Value
113: * @exception SQLException Description of Exception
114: * @since
115: */
116: public Connection connect(String url, Properties info)
117: throws SQLException {
118: DriverManager.println("CsvJdbc - CsvDriver:connect() - url="
119: + url);
120: // check for correct url
121: if (!url.startsWith(URL_PREFIX)) {
122: return null;
123: }
124: // get filepath from url
125: String filePath = url.substring(URL_PREFIX.length());
126: String filePathAll = filePath;
127: StringTokenizer st = new StringTokenizer(filePath, ";");
128: filePath = st.nextToken();
129: if (!filePath.endsWith(File.separator)) {
130: filePath += File.separator;
131: }
132: DriverManager
133: .println("CsvJdbc - CsvDriver:connect() - filePath="
134: + filePath);
135: return new CsvConnection(filePathAll, info);
136: }
137:
138: /**
139: * Description of the Method
140: *
141: * @param url Description of Parameter
142: * @return Description of the Returned Value
143: * @exception SQLException Description of Exception
144: * @since
145: */
146: public boolean acceptsURL(String url) throws SQLException {
147: DriverManager.println("CsvJdbc - CsvDriver:accept() - url="
148: + url);
149: return url.startsWith(URL_PREFIX);
150: }
151:
152: /**
153: *Description of the Method
154: *
155: * @return Description of the Returned Value
156: * @since
157: */
158: public boolean jdbcCompliant() {
159: return false;
160: }
161:
162: // This static block inits the driver when the class is loaded by the JVM.
163: static {
164: try {
165: java.sql.DriverManager.registerDriver(new CsvDriver());
166: } catch (SQLException e) {
167: throw new RuntimeException(
168: "FATAL ERROR: Could not initialise CSV driver ! Message was: "
169: + e.getMessage());
170: }
171: }
172:
173: public static void log(String message) {
174: if (CsvDriver.ENABLE_LOG) {
175: try {
176: File file = new File("csvdriver.log");
177: if (!file.exists())
178: file.createNewFile();
179: java.io.RandomAccessFile fileLogr = new java.io.RandomAccessFile(
180: file, "rw");
181: fileLogr.seek(fileLogr.length());
182: fileLogr.writeBytes("CsvJdbc, " + message + "\r\n");
183: fileLogr.close();
184: } catch (Exception ex) {
185: ex.printStackTrace();
186: }
187: }
188: }
189:
190: }
|