
/*
 * Copyright (C) 2014 Archie L. Cobbs. All rights reserved.
 *
 * $Id$
 */

package org.jsimpledb.cli.cmd;

import java.io.PrintWriter;
import java.util.Map;

import org.jsimpledb.cli.CliSession;
import org.jsimpledb.parse.ParseContext;
import org.jsimpledb.parse.expr.EvalException;
import org.jsimpledb.parse.expr.Node;
import org.jsimpledb.parse.expr.Value;

@Command
public class EvalCommand extends AbstractCommand {

    public EvalCommand() {
        super("eval expr:expr");
    }

    @Override
    public String getHelpSummary() {
        return "evaluates the specified Java expression";
    }

    @Override
    public CliSession.Action getAction(CliSession session, ParseContext ctx, boolean complete, Map<String, Object> params) {
        final Node expr = (Node)params.get("expr");
        return new CliSession.Action() {
            @Override
            public void run(CliSession session) throws Exception {
                final PrintWriter writer = session.getWriter();
                final Value value;
                final Object result;
                try {
                    result = (value = expr.evaluate(session)).get(session);
                } catch (EvalException e) {
                    writer.println("Error: " + e.getMessage());
                    if (session.isVerbose())
                        e.printStackTrace(writer);
                    return;
                }
                if (value != Value.NO_VALUE)
                    writer.println(result);
            }
        };
    }
}


@@ -467,16 +467,25 @@
 r(%22eval 
+-f:force 
 expr:exp
@@ -485,24 +485,24 @@
 xpr:expr%22);%0A
-
     %7D%0A%0A    @
@@ -604,32 +604,301 @@
 ession%22;%0A    %7D%0A%0A
+    @Override%0A    public String getHelpDetail() %7B%0A        return %22The expression is evaluated within a transaction. If an exception occurs, the transaction is rolled back%22%0A          + %22 unless the %60-f' flag is given, in which case it will be committed anyway.%22;%0A    %7D%0A%0A
     @Override%0A  
@@ -1068,24 +1068,83 @@
 et(%22expr%22);%0A
+        final boolean force = params.containsKey(%22force%22);%0A
         retu
@@ -1495,16 +1495,16 @@
 ssion);%0A
-
         
@@ -1535,24 +1535,124 @@
 eption e) %7B%0A
+                    if (!force)%0A                        session.getTransaction().setRollbackOnly();%0A
             

Add `-f' flag to the `eval' command.