ma.vi.esql.parser.expression.Exponentiation Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of esql Show documentation
Show all versions of esql Show documentation
ESQL, SQL enhanced with metadata compiling to various relational databases
/*
* Copyright (c) 2020 Vikash Madhow
*/
package ma.vi.esql.parser.expression;
import ma.vi.esql.parser.Context;
import java.util.Map;
import static ma.vi.base.string.Escape.escapeJsonString;
import static ma.vi.esql.parser.Translatable.Target.JSON;
/**
* The exponentiation operator (^) in ESQL.
*
* @author Vikash Madhow ([email protected])
*/
public class Exponentiation extends ArithmeticOperator {
public Exponentiation(Context context, Expression> expr1, Expression> expr2) {
super(context, "^", expr1, expr2);
}
public Exponentiation(Exponentiation other) {
super(other);
}
@Override
public Exponentiation copy() {
if (!copying()) {
try {
copying(true);
return new Exponentiation(this);
} finally {
copying(false);
}
} else {
return this;
}
}
@Override
public String translate(Target target, Map parameters) {
if (target == Target.SQLSERVER) {
return "POWER(" + expr1().translate(target, parameters) + ", " + expr2().translate(target, parameters) + ")";
} else {
String e = expr1().translate(target, parameters) + " ^ " + expr2().translate(target, parameters);
return target == JSON ? '"' + escapeJsonString(e) + '"' : e;
}
}
}