📄 i18nconnection.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.webdocwf.util.i18njdbc;
import java.io.File;
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.Map;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.Vector;
import org.webdocwf.util.i18njdbc.I18nDriver;
/**
* This class implements the Connection interface for the I18nJdbc driver.
*
* @author Zoran Milakovic
* @author Zeljko Kovacevic
*/
public class I18nConnection
implements Connection {
/** Directory where the i18n files to use are located */
private String path;
/** File extension to use */
private String charset = I18nDriver.DEFAULT_CHARSET;
private String nameColumn = I18nDriver.DEFAULT_NAMECOLUMN;
private String valueColumn = I18nDriver.DEFAULT_VALUECOLUMN;
private boolean create = I18nDriver.DEFAULT_CREATE;
private String extension = I18nDriver.DEFAULT_EXTENSION;
/** 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 I18n file will be saved after each query.Default value is true in JDBC compliant drivers.*/
private boolean autoCommit=true;
/**
* Creates a new I18nConnection that takes the supplied path
* @param path directory where the i18n files are located
*/
protected I18nConnection(String path) throws SQLException {
// validate argument(s)
init(path);
}
/**
* Creates a new I18nConnection that takes the supplied path and properties
* @param path directory where the i18n files are located
* @param info set of properties containing custom options
*/
protected I18nConnection(String path, Properties info) throws SQLException {
// check for properties
if (info != null) {
// set the file extension to be used
if (info.getProperty(I18nDriver.FILE_EXTENSION) != null) {
extension = info.getProperty(I18nDriver.FILE_EXTENSION);
if (!extension.startsWith("."))
this.extension = "." + extension;
}
//set the nameColumn
if (info.getProperty(I18nDriver.NAMECOLUMN) != null) {
this.nameColumn = info.getProperty(I18nDriver.NAMECOLUMN);
}
// set the valueColumn
if (info.getProperty(I18nDriver.VALUECOLUMN) != null) {
this.valueColumn = info.getProperty(I18nDriver.VALUECOLUMN);
}
// default charset
if (info.getProperty(I18nDriver.CHARSET) != null) {
this.charset = info.getProperty(I18nDriver.CHARSET);
}
// set create
if (info.getProperty(I18nDriver.CREATE) != null) {
this.create = Boolean.valueOf(info.getProperty(
I18nDriver.CREATE)).booleanValue();
}
}
init(path);
}
/**
* This method set init parameters
* @param path
* @throws SQLException
*/
protected void init(String path) throws SQLException {
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();
}
/**
* This method set parameters from property string
* @param propString
* @return
*/
private boolean setProperty(String propString) {
boolean retVal = true;
StringTokenizer st = new StringTokenizer(propString, "=");
String name = st.nextToken();
String value = st.nextToken();
if (name.equals(I18nDriver.FILE_EXTENSION)) {
if (!value.startsWith("."))
value = "." + value;
this.extension = value;
}
else if (name.equals(I18nDriver.CHARSET)) {
this.charset = value;
}
else if (name.equals(I18nDriver.CREATE)) {
this.create = Boolean.valueOf(value).booleanValue();
}
else if (name.equals(I18nDriver.NAMECOLUMN)) {
this.nameColumn = value;
}
else if (name.equals(I18nDriver.VALUECOLUMN)) {
this.valueColumn = 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 {
I18nStatement statement = new I18nStatement(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) + I18nPreparedStatement.PREPARE_SEPARATOR + sql.substring(index + 1);
index = sql.indexOf("?");
}
I18nPreparedStatement statement = new I18nPreparedStatement(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
* pre-compiled SQL statement
* @exception SQLException if a database access error occurs
*/
public CallableStatement prepareCall(String sql) throws SQLException {
throw new UnsupportedOperationException(
"Connection.prepareCall(String) unsupported");
}
/**
* Converts the given SQL statement into the system's native SQL grammar.
* A driver may convert the JDBC SQL grammar into its system's
* native SQL grammar prior to sending it. This method returns the
* native form of the statement that the driver would have sent.
*
* @param sql an SQL statement that may contain one or more '?'
* parameter placeholders
* @return the native form of this statement
* @exception SQLException if a database access error occurs
*/
public String nativeSQL(String sql) throws SQLException {
throw new UnsupportedOperationException(
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -