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

protobuf4j.orm.sql.expr.AbstractBinaryExpr Maven / Gradle / Ivy

The newest version!
package protobuf4j.orm.sql.expr;

import lombok.Data;
import lombok.NonNull;
import protobuf4j.orm.sql.IBinaryExpr;
import protobuf4j.orm.sql.IExpression;
import protobuf4j.orm.sql.ISqlOperator;
import protobuf4j.orm.sql.ISqlValue;

import javax.annotation.Nonnull;
import java.util.List;
import java.util.Objects;

/**
 * author: yuanwq
 * date: 2018/7/16
 */
@Data
public abstract class AbstractBinaryExpr extends AbstractExpression
    implements IBinaryExpr {
  private static final String WRAP_LEFT = "(";
  private static final String WRAP_RIGHT = ")";

  protected final IExpression left;
  @NonNull
  protected final T op;
  protected final IExpression right;

  public AbstractBinaryExpr(IExpression left, T op, IExpression right) {
    op.checkExpression(left, right);
    this.left = left;
    this.op = op;
    this.right = right;
  }

  @Override
  public int comparePrecedence(@Nonnull ISqlOperator outerOp) {
    // 默认:运算符不同,就需要包裹括号
    return Objects.equals(getOp(), outerOp) ? 0 : -1;
  }

  @Override
  public IExpression getLeft() {
    return left;
  }

  @Override
  public IExpression getRight() {
    return right;
  }

  @Override
  public T getOp() {
    return op;
  }

  @Override
  public StringBuilder toSqlTemplate(@Nonnull StringBuilder sb) {
    if (this.left != null) {
      boolean needWrap = this.left.comparePrecedence(this.op) < 0;
      if (needWrap) {
        sb.append(WRAP_LEFT);
      }
      this.left.toSqlTemplate(sb);
      if (needWrap) {
        sb.append(WRAP_RIGHT);
      }
    }
    sb.append(this.op.getOp());
    if (this.right != null) {
      boolean needWrap = this.right.comparePrecedence(this.op) < 0;
      if (needWrap) {
        sb.append(WRAP_LEFT);
      }
      this.right.toSqlTemplate(sb);
      if (needWrap) {
        sb.append(WRAP_RIGHT);
      }
    }
    return sb;
  }

  @Override
  public StringBuilder toSolidSql(@Nonnull StringBuilder sb) {
    if (this.left != null) {
      boolean needWrap = this.left.comparePrecedence(this.op) < 0;
      if (needWrap) {
        sb.append(WRAP_LEFT);
      }
      this.left.toSolidSql(sb);
      if (needWrap) {
        sb.append(WRAP_RIGHT);
      }
    }
    sb.append(this.op.getOp());
    if (this.right != null) {
      boolean needWrap = this.right.comparePrecedence(this.op) < 0;
      if (needWrap) {
        sb.append(WRAP_LEFT);
      }
      this.right.toSolidSql(sb);
      if (needWrap) {
        sb.append(WRAP_RIGHT);
      }
    }
    return sb;
  }

  @Override
  public List collectSqlValue(@Nonnull List sqlValues) {
    if (this.left != null) {
      this.left.collectSqlValue(sqlValues);
    }
    if (this.right != null) {
      this.right.collectSqlValue(sqlValues);
    }
    return sqlValues;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy