📄 csvconnection.java
字号:
/**
Copyright (C) 2002-2003 Together
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.relique.jdbc.csv;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Savepoint;
import java.sql.Statement;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Map;
import java.util.Properties;
import java.util.Vector;
import java.util.StringTokenizer;
import java.io.File;
/**
* This class implements the Connection interface for the CsvJdbc driver.
*
* @author Zoran Milakovic
*/
public class CsvConnection
implements Connection {
/** Directory where the CSV files to use are located */
private String path;
/** File extension to use */
private String extension = CsvDriver.DEFAULT_EXTENSION;
/** Field separator to use */
private char separator = CsvDriver.DEFAULT_SEPARATOR;
/** Should headers be suppressed */
private boolean suppressHeaders = CsvDriver.DEFAULT_SUPPRESS;
/** If tree of directory should be created */
private boolean create = CsvDriver.DEFAULT_CREATE;
/** Max size of files*/
private long maxFileSize = CsvDriver.DEFAULT_FILE_MAXSIZE;
/** Escape for line brakes*/
private String lineBrakesEscape = CsvDriver.DEFAULT_LINE_BREAK_ESCAPE;
/** Escape for carriage return*/
private String carriageReturnEscape = CsvDriver.DEFAULT_CARRIAGE_RETURN_ESCAPE;
/** Escape for double quotes*/
// private String doubleQuotesEscape = CsvDriver.DEFAULT_DOUBLE_QUOTE_ESCAPE;
/** Collection of all created Statements */
private Vector statements = new Vector();
/** Charset that should be used to read the files */
private String charset = null;
/** Stores whether this Connection is closed or not */
private boolean closed;
/** If value is true csv file will be saved after each query.Default value is true in JDBC compliant drivers.*/
private boolean autoCommit=true;
/**
* Creates a new CsvConnection that takes the supplied path
* @param path directory where the CSV files are located
*/
protected CsvConnection(String path) throws SQLException {
// validate argument(s)
if (path == null || path.length() == 0) {
throw new IllegalArgumentException(
"'path' argument may not be empty or null");
}
//check for properties
StringTokenizer st = new StringTokenizer(path, ";");
this.path = st.nextToken();
if (!this.path.endsWith(File.separator)) {
this.path += File.separator;
}
while (st.hasMoreTokens()) {
String next = st.nextToken();
if (!this.setProperty(next)) {
throw new IllegalArgumentException(
"unknown property " + next);
}
}
File filePath = new File(this.path);
if (!this.create && !filePath.exists()) {
throw new SQLException(
"Specified path '" + filePath.getAbsolutePath() +
"' does not exist !");
}
if (this.create && !filePath.exists())
filePath.mkdirs();
}
/**
* Creates a new CsvConnection that takes the supplied path and properties
* @param path directory where the CSV files are located
* @param info set of properties containing custom options
*/
protected CsvConnection(String path, Properties info) throws SQLException {
this(path);
// check for properties
if (info != null) {
// set the file extension to be used
if (info.getProperty(CsvDriver.FILE_EXTENSION) != null) {
extension = info.getProperty(CsvDriver.FILE_EXTENSION);
if (!extension.startsWith("."))
extension = "." + extension;
}
// set the separator character to be used
if (info.getProperty(CsvDriver.SEPARATOR) != null) {
separator = info.getProperty(CsvDriver.SEPARATOR).charAt(0);
}
// set the header suppression flag
if (info.getProperty(CsvDriver.SUPPRESS_HEADERS) != null) {
suppressHeaders = Boolean.valueOf(info.getProperty(
CsvDriver.SUPPRESS_HEADERS)).booleanValue();
}
// default charset
if (info.getProperty(CsvDriver.CHARSET) != null) {
charset = info.getProperty(CsvDriver.CHARSET);
}
// set create
if (info.getProperty(CsvDriver.CREATE) != null) {
create = Boolean.valueOf(info.getProperty(
CsvDriver.SUPPRESS_HEADERS)).booleanValue();
}
// default max file size
if (info.getProperty(CsvDriver.MAXFILESIZE) != null) {
maxFileSize = Long.valueOf(info.getProperty(CsvDriver.MAXFILESIZE)).
longValue();
}
// default escape for line break
if (info.getProperty(CsvDriver.LINE_BREAK_ESCAPE) != null) {
lineBrakesEscape = info.getProperty(CsvDriver.LINE_BREAK_ESCAPE);
}
// default escape for carriage return
if (info.getProperty(CsvDriver.CARRIAGE_RETURN_ESCAPE) != null) {
carriageReturnEscape = info.getProperty(CsvDriver.CARRIAGE_RETURN_ESCAPE);
}
// default escape for double quotes
// if (info.getProperty(CsvDriver.DOUBLE_QUOTE_ESCAPE) != null) {
// doubleQuotesEscape = info.getProperty(CsvDriver.DOUBLE_QUOTE_ESCAPE);
// }
}
}
private boolean setProperty(String propString) {
boolean retVal = true;
StringTokenizer st = new StringTokenizer(propString, "=");
String name = st.nextToken();
String value = st.nextToken();
if (name.equals(CsvDriver.SEPARATOR)) {
this.separator = value.charAt(0);
}
else if (name.equals(CsvDriver.FILE_EXTENSION)) {
if (!value.startsWith("."))
value = "." + value;
this.extension = value;
}
else if (name.equals(CsvDriver.SUPPRESS_HEADERS)) {
this.suppressHeaders = Boolean.valueOf(value).booleanValue();
}
else if (name.equals(CsvDriver.CHARSET)) {
this.charset = value;
}
else if (name.equals(CsvDriver.CREATE)) {
this.create = Boolean.valueOf(value).booleanValue();
}
else if (name.equals(CsvDriver.MAXFILESIZE)) {
this.maxFileSize = Long.valueOf(value).longValue();
}
else if (name.equals(CsvDriver.LINE_BREAK_ESCAPE)) {
this.lineBrakesEscape = value;
}
else if (name.equals(CsvDriver.CARRIAGE_RETURN_ESCAPE)) {
this.carriageReturnEscape = value;
}
// else if (name.equals(CsvDriver.DOUBLE_QUOTE_ESCAPE)) {
// this.doubleQuotesEscape = value;
// }
else
retVal = false;
return retVal;
}
/**
* Creates a <code>Statement</code> object for sending
* SQL statements to the database.
* SQL statements without parameters are normally
* executed using <code>Statement</code> objects. If the same SQL statement
* is executed many times, it may be more efficient to use a
* <code>PreparedStatement</code> object.
* <P>
* Result sets created using the returned <code>Statement</code>
* object will by default be type <code>TYPE_FORWARD_ONLY</code>
* and have a concurrency level of <code>CONCUR_READ_ONLY</code>.
*
* @return a new default <code>Statement</code> object
* @exception SQLException if a database access error occurs
*/
public Statement createStatement() throws SQLException {
CsvStatement statement = new CsvStatement(this);
statements.add(statement);
return statement;
}
/**
* Creates a <code>PreparedStatement</code> object for sending
* parameterized SQL statements to the database.
* <P>
* A SQL statement with or without IN parameters can be
* pre-compiled and stored in a <code>PreparedStatement</code> object. This
* object can then be used to efficiently execute this statement
* multiple times.
*
* <P><B>Note:</B> This method is optimized for handling
* parametric SQL statements that benefit from precompilation. If
* the driver supports precompilation,
* the method <code>prepareStatement</code> will send
* the statement to the database for precompilation. Some drivers
* may not support precompilation. In this case, the statement may
* not be sent to the database until the <code>PreparedStatement</code>
* object is executed. This has no direct effect on users; however, it does
* affect which methods throw certain <code>SQLException</code> objects.
* <P>
* Result sets created using the returned <code>PreparedStatement</code>
* object will by default be type <code>TYPE_FORWARD_ONLY</code>
* and have a concurrency level of <code>CONCUR_READ_ONLY</code>.
*
* @param sql an SQL statement that may contain one or more '?' IN
* parameter placeholders
* @return a new default <code>PreparedStatement</code> object containing the
* pre-compiled SQL statement
* @exception SQLException if a database access error occurs
*/
public PreparedStatement prepareStatement(String sql) throws SQLException {
int index = sql.indexOf("?");
while (index != -1) {
sql = sql.substring(0, index) + CsvPreparedStatement.PREPARE_SEPARATOR + sql.substring(index + 1);
index = sql.indexOf("?");
}
CsvPreparedStatement statement = new CsvPreparedStatement(this, sql);
statements.add(statement);
return statement;
}
/**
* Creates a <code>CallableStatement</code> object for calling
* database stored procedures.
* The <code>CallableStatement</code> object provides
* methods for setting up its IN and OUT parameters, and
* methods for executing the call to a stored procedure.
*
* <P><B>Note:</B> This method is optimized for handling stored
* procedure call statements. Some drivers may send the call
* statement to the database when the method <code>prepareCall</code>
* is done; others
* may wait until the <code>CallableStatement</code> object
* is executed. This has no
* direct effect on users; however, it does affect which method
* throws certain SQLExceptions.
* <P>
* Result sets created using the returned <code>CallableStatement</code>
* object will by default be type <code>TYPE_FORWARD_ONLY</code>
* and have a concurrency level of <code>CONCUR_READ_ONLY</code>.
*
* @param sql an SQL statement that may contain one or more '?'
* parameter placeholders. Typically this statement is a JDBC
* function call escape string.
* @return a new default <code>CallableStatement</code> object containing the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -