📄 ssstatement.java
字号:
/* =============================================================
* SmallSQL : a free Java DBMS library for the Java(tm) platform
* =============================================================
*
* (C) Copyright 2004-2006, by Volker Berlin.
*
* Project Info: http://www.smallsql.de/
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ---------------
* SSStatement.java
* ---------------
* Author: Volker Berlin
*
*/
package smallsql.database;
import java.sql.*;
import java.util.ArrayList;
class SSStatement implements Statement {
final SSConnection con;
Command cmd;
int rsType;
int rsConcurrency;
private int fetchDirection;
private int fetchSize;
private int queryTimeout;
private int maxRows;
private int maxFieldSize;
private ArrayList batches;
private boolean needGeneratedKeys;
private ResultSet generatedKeys;
private int[] generatedKeyIndexes;
private String[] generatedKeyNames;
SSStatement( SSConnection con ) throws SQLException{
this( con, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY );
}
SSStatement( SSConnection con, int rsType, int rsConcurrency ) throws SQLException{
this.con = con;
this.rsType = rsType;
this.rsConcurrency = rsConcurrency;
con.testClosedConnection();
}
final public ResultSet executeQuery(String sql) throws SQLException {
executeImpl(sql);
return cmd.getQueryResult();
}
final public int executeUpdate(String sql) throws SQLException {
executeImpl(sql);
return cmd.getUpdateCount();
}
final public boolean execute(String sql) throws SQLException {
executeImpl(sql);
return cmd.getResultSet() != null;
}
final private void executeImpl(String sql) throws SQLException {
generatedKeys = null;
try{
con.log.println(sql);
SQLParser parser = new SQLParser();
cmd = parser.parse( con, sql );
if(maxRows != 0 && (cmd.getMaxRows() == -1 || cmd.getMaxRows() > maxRows))
cmd.setMaxRows(maxRows);
cmd.execute( con, this);
}catch(Exception e){
throw Utils.createSQLException(e);
}
needGeneratedKeys = false;
generatedKeyIndexes = null;
generatedKeyNames = null;
}
final public void close() throws SQLException {
con.log.println("Statement.close");
cmd = null;
//TODO make Ressources free;
//TODO Exception handling after close
}
final public int getMaxFieldSize() throws SQLException {
return maxFieldSize;
}
final public void setMaxFieldSize(int max) throws SQLException {
maxFieldSize = max;
}
final public int getMaxRows(){
return maxRows;
}
final public void setMaxRows(int max) throws SQLException{
if(max<0)
throw Utils.createSQLException("Wrong max rows value:"+max);
maxRows = max;
}
final public void setEscapeProcessing(boolean enable) throws SQLException {
//TODO enable/disable escape processing
}
final public int getQueryTimeout() throws SQLException {
return queryTimeout;
}
final public void setQueryTimeout(int seconds) throws SQLException {
queryTimeout = seconds;
}
final public void cancel(){
//TODO Statement.cancel()
}
final public SQLWarning getWarnings(){
return null;
}
final public void clearWarnings(){
//TODO support for warnings
}
final public void setCursorName(String name) throws SQLException {
/**@todo: Implement this java.sql.Statement.setCursorName method*/
throw Utils.createUnsupportedException("setCursorName");
}
final public ResultSet getResultSet(){
return cmd.getResultSet();
}
final public int getUpdateCount() throws SQLException {
return cmd.getUpdateCount();
}
final public boolean getMoreResults() throws SQLException {
return getMoreResults(CLOSE_CURRENT_RESULT);
}
final public void setFetchDirection(int direction) throws SQLException {
fetchDirection = direction;
}
final public int getFetchDirection() throws SQLException {
return fetchDirection;
}
final public void setFetchSize(int rows) throws SQLException {
fetchSize = rows;
}
final public int getFetchSize() throws SQLException {
return fetchSize;
}
final public int getResultSetConcurrency() throws SQLException {
return rsConcurrency;
}
final public int getResultSetType() throws SQLException {
return rsType;
}
final public void addBatch(String sql){
if(batches == null) batches = new ArrayList();
batches.add(sql);
}
public void clearBatch(){
if(batches == null) return;
batches.clear();
}
public int[] executeBatch() throws BatchUpdateException {
if(batches == null) return new int[0];
final int[] result = new int[batches.size()];
BatchUpdateException failed = null;
for(int i=0; i<result.length; i++){
try {
result[i] = executeUpdate((String)batches.get(i));
} catch (SQLException ex) {
result[i] = EXECUTE_FAILED;
if(failed == null){
failed = new BatchUpdateException(ex.getMessage(), ex.getSQLState(), ex.getErrorCode(), result);
failed.initCause(ex);
}
failed.setNextException(ex);
}
}
batches.clear();
if(failed != null)
throw failed;
return result;
}
final public Connection getConnection(){
return con;
}
final public boolean getMoreResults(int current) throws SQLException {
switch(current){
case CLOSE_ALL_RESULTS:
//currently there exists only one ResultSet
case CLOSE_CURRENT_RESULT:
ResultSet rs = cmd.getResultSet();
cmd.rs = null;
if(rs != null) rs.close();
break;
case KEEP_CURRENT_RESULT:
break;
default:
throw Utils.createSQLException("Invalid flag value in method getMoreResults:"+current);
}
return cmd.getMoreResults();
}
final void setNeedGeneratedKeys(int autoGeneratedKeys) throws SQLException{
switch(autoGeneratedKeys){
case NO_GENERATED_KEYS:
break;
case RETURN_GENERATED_KEYS:
needGeneratedKeys = true;
break;
default:
throw Utils.createSQLException("Invalid argument:"+autoGeneratedKeys);
}
}
final void setNeedGeneratedKeys(int[] columnIndexes) throws SQLException{
needGeneratedKeys = columnIndexes != null;
generatedKeyIndexes = columnIndexes;
}
final void setNeedGeneratedKeys(String[] columnNames) throws SQLException{
needGeneratedKeys = columnNames != null;
generatedKeyNames = columnNames;
}
final boolean needGeneratedKeys(){
return needGeneratedKeys;
}
final int[] getGeneratedKeyIndexes(){
return generatedKeyIndexes;
}
final String[] getGeneratedKeyNames(){
return generatedKeyNames;
}
/**
* Set on execution the result with the generated keys.
* @param rs
*/
final void setGeneratedKeys(ResultSet rs){
generatedKeys = rs;
}
final public ResultSet getGeneratedKeys() throws SQLException {
if(generatedKeys == null)
throw Utils.createSQLException("GeneratedKeys not requested");
return generatedKeys;
}
final public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
setNeedGeneratedKeys(autoGeneratedKeys);
return executeUpdate(sql);
}
final public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
setNeedGeneratedKeys(columnIndexes);
return executeUpdate(sql);
}
final public int executeUpdate(String sql, String[] columnNames) throws SQLException {
setNeedGeneratedKeys(columnNames);
return executeUpdate(sql);
}
final public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
setNeedGeneratedKeys(autoGeneratedKeys);
return execute(sql);
}
final public boolean execute(String sql, int[] columnIndexes) throws SQLException {
setNeedGeneratedKeys(columnIndexes);
return execute(sql);
}
final public boolean execute(String sql, String[] columnNames) throws SQLException {
setNeedGeneratedKeys(columnNames);
return execute(sql);
}
final public int getResultSetHoldability() throws SQLException {
/**@todo: Implement this java.sql.Statement method*/
throw new java.lang.UnsupportedOperationException("Method getResultSetHoldability() not yet implemented.");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -