/*
 Licensed to Diennea S.r.l. under one
 or more contributor license agreements. See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership. Diennea S.r.l. licenses this file
 to you 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 herddb.model.commands;

import herddb.index.IndexOperation;
import herddb.index.PrimaryIndexSeek;
import herddb.model.ConstValueRecordFunction;
import herddb.model.DMLStatement;
import herddb.model.Predicate;
import herddb.model.Record;
import herddb.model.RecordFunction;
import herddb.model.StatementEvaluationContext;
import herddb.model.StatementExecutionException;
import herddb.utils.Bytes;

/**
 * Delete an existing record, if the record does not exist the updateCount will
 * return 0
 *
 * @author enrico.olivelli
 */
public class DeleteStatement extends DMLStatement {

    private final Predicate predicate;

    public DeleteStatement(String tableSpace, String table, Bytes key, Predicate predicate) {
        super(table, tableSpace);
        if (predicate == null) {
            if (key == null) {
                predicate = new Predicate() {
                    @Override
                    public boolean evaluate(Record record, StatementEvaluationContext context) throws StatementExecutionException {
                        return true;
                    }
                };
            } else {
                predicate = new Predicate() {
                    @Override
                    public boolean evaluate(Record record, StatementEvaluationContext context) throws StatementExecutionException {
                        return record.key.equals(key);
                    }
                };
            }
        }
        if (key != null) {
            predicate.setIndexOperation(new PrimaryIndexSeek(new ConstValueRecordFunction(key.data)));
        }
        this.predicate = predicate;
    }

    public Predicate getPredicate() {
        return predicate;
    }

}

@@ -1240,19 +1240,16 @@
 unt will
-%0A *
  return 
@@ -2458,10 +2458,130 @@
 %0A    %7D%0A%0A
+    @Override%0A    public String toString() %7B%0A        return %22DeleteStatement%7B%22 + %22predicate=%22 + predicate + '%7D';%0A    %7D%0A%0A
 %7D%0A

DELETE toString