/*
* 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 com.wplatform.ddal.command.CommandInterface;
import com.wplatform.ddal.command.Prepared;
import com.wplatform.ddal.command.expression.Expression;
import com.wplatform.ddal.dbobject.table.PlanItem;
import com.wplatform.ddal.dbobject.table.TableFilter;
import com.wplatform.ddal.engine.Session;
import com.wplatform.ddal.result.ResultInterface;
import com.wplatform.ddal.util.StringUtils;
/**
* This class represents the statement
* DELETE
*/
public class Delete extends Prepared {
private Expression condition;
private TableFilter tableFilter;
/**
* The limit expression as specified in the LIMIT or TOP clause.
*/
private Expression limitExpr;
public Delete(Session session) {
super(session);
}
public void setTableFilter(TableFilter tableFilter) {
this.tableFilter = tableFilter;
}
public void setCondition(Expression condition) {
this.condition = condition;
}
@Override
public String getPlanSQL() {
StringBuilder buff = new StringBuilder();
buff.append("DELETE ");
buff.append("FROM ").append(tableFilter.getPlanSQL(false));
if (condition != null) {
buff.append("\nWHERE ").append(StringUtils.unEnclose(
condition.getSQL()));
}
if (limitExpr != null) {
buff.append("\nLIMIT (").append(StringUtils.unEnclose(
limitExpr.getSQL())).append(')');
}
return buff.toString();
}
@Override
public void prepare() {
if (condition != null) {
condition.mapColumns(tableFilter, 0);
condition = condition.optimize(session);
condition.createIndexConditions(session, tableFilter);
}
PlanItem item = tableFilter.getBestPlanItem(session, 1);
tableFilter.setPlanItem(item);
tableFilter.prepare();
}
@Override
public boolean isTransactional() {
return true;
}
@Override
public ResultInterface queryMeta() {
return null;
}
@Override
public int getType() {
return CommandInterface.DELETE;
}
public void setLimit(Expression limit) {
this.limitExpr = limit;
}
@Override
public boolean isCacheable() {
return true;
}
// getter
public Expression getCondition() {
return condition;
}
public TableFilter getTableFilter() {
return tableFilter;
}
public Expression getLimitExpr() {
return limitExpr;
}
}