/*
* Copyright 2014-2015 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the “License”);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an “AS IS” BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wplatform.ddal.command.dml;
import java.util.ArrayList;
import com.wplatform.ddal.command.CommandInterface;
import com.wplatform.ddal.command.Prepared;
import com.wplatform.ddal.command.expression.Expression;
import com.wplatform.ddal.command.expression.Parameter;
import com.wplatform.ddal.engine.Procedure;
import com.wplatform.ddal.engine.Session;
import com.wplatform.ddal.result.ResultInterface;
import com.wplatform.ddal.util.New;
/**
* This class represents the statement
* EXECUTE
*/
public class ExecuteProcedure extends Prepared {
private final ArrayList<Expression> expressions = New.arrayList();
private Procedure procedure;
public ExecuteProcedure(Session session) {
super(session);
}
public void setProcedure(Procedure procedure) {
this.procedure = procedure;
}
/**
* Set the expression at the given index.
*
* @param index the index (0 based)
* @param expr the expression
*/
public void setExpression(int index, Expression expr) {
expressions.add(index, expr);
}
private void setParameters() {
Prepared prepared = procedure.getPrepared();
ArrayList<Parameter> params = prepared.getParameters();
for (int i = 0; params != null && i < params.size() &&
i < expressions.size(); i++) {
Expression expr = expressions.get(i);
Parameter p = params.get(i);
p.setValue(expr.getValue(session));
}
}
@Override
public boolean isQuery() {
Prepared prepared = procedure.getPrepared();
return prepared.isQuery();
}
@Override
public int update() {
setParameters();
Prepared prepared = procedure.getPrepared();
return prepared.update();
}
@Override
public ResultInterface query(int limit) {
setParameters();
Prepared prepared = procedure.getPrepared();
return prepared.query(limit);
}
@Override
public boolean isTransactional() {
return true;
}
@Override
public ResultInterface queryMeta() {
Prepared prepared = procedure.getPrepared();
return prepared.queryMeta();
}
@Override
public int getType() {
return CommandInterface.EXECUTE;
}
}