*
* 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.]
*
* ---------------
* Command.java
* ---------------
* Author: Volker Berlin
*
*/
package smallsql.database;
import java.sql.*;
abstract class Command {
int type;
String catalog;
String name;
SSResultSet rs;
int updateCount = -1;
final Expressions columnExpressions;
Expressions params = new Expressions();
final Logger log;
Command(Logger log){
this.log = log;
this.columnExpressions = new Expressions();
}
Command(Logger log, Expressions columnExpressions){
this.log = log;
this.columnExpressions = columnExpressions;
}
void addColumnExpression( Expression column ){
columnExpressions.add( column );
}
void addParameter( ExpressionValue param ){
params.add( param );
}
void verifyParams() throws SQLException{
for(int p=0; p<params.size(); p++){
if(((ExpressionValue)params.get(p)).isEmpty())
throw Utils.createSQLException("Parameter " + (p+1) + " is empty.");
}
}
void clearParams(){
for(int p=0; p<params.size(); p++){
((ExpressionValue)params.get(p)).clear();
}
}
private ExpressionValue getParam(int idx) throws SQLException{
if(idx < 1 || idx > params.size())
throw Utils.createSQLException("Parameter index " +idx+ " out of range. The value must be between 1 and " + params.size());
return ((ExpressionValue)params.get(idx-1));
}
void setParamValue(int idx, Object value, int dataType) throws SQLException{
getParam(idx).set( value, dataType );
if(log.isLogging()){
log.println("param"+idx+'='+value+"; type="+dataType);
}
}
void setParamValue(int idx, Object value, int dataType, int length) throws SQLException{
getParam(idx).set( value, dataType, length );
if(log.isLogging()){
log.println("param"+idx+'='+value+"; type="+dataType+"; length="+length);
}
}
final void execute(SSConnection con, SSStatement st) throws SQLException{
int savepoint = con.getSavepoint();
try{
executeImpl( con, st );
}catch(Throwable e){
con.rollback(savepoint);
throw Utils.createSQLException(e);
}finally{
if(con.getAutoCommit()) con.commit();
}
}
abstract void executeImpl(SSConnection con, SSStatement st) throws Exception;
SSResultSet getQueryResult() throws SQLException{
if(rs == null)
throw Utils.createSQLException("No ResultSet was produce.");
return rs;
}
SSResultSet getResultSet(){
return rs;
}
int getUpdateCount(){
return updateCount;
}
void setMaxRows(int max){}
}