All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ma.vi.esql.parser.expression.Exponentiation Maven / Gradle / Ivy

/*
 * 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
  protected String trans(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;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy