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

com.scalar.db.storage.dynamo.ConditionExpressionBuilder Maven / Gradle / Ivy

Go to download

A universal transaction manager that achieves database-agnostic transactions and distributed transactions that span multiple databases

There is a newer version: 3.14.0
Show newest version
package com.scalar.db.storage.dynamo;

import com.scalar.db.api.ConditionalExpression;
import com.scalar.db.api.DeleteIf;
import com.scalar.db.api.DeleteIfExists;
import com.scalar.db.api.MutationConditionVisitor;
import com.scalar.db.api.PutIf;
import com.scalar.db.api.PutIfExists;
import com.scalar.db.api.PutIfNotExists;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.NotThreadSafe;

/**
 * A builder to make a query statement for a stored procedure of Cosmos DB from conditions
 *
 * @author Yuji Ito
 */
@NotThreadSafe
public class ConditionExpressionBuilder implements MutationConditionVisitor {
  private final List expressions;
  private final String columnNameAlias;
  private final String valueAlias;
  private int index;

  public ConditionExpressionBuilder(String columnNameAlias, String valueAlias) {
    this.expressions = new ArrayList<>();
    this.columnNameAlias = columnNameAlias;
    this.valueAlias = valueAlias;
    this.index = 0;
  }

  @Nonnull
  public String build() {
    return String.join(" AND ", expressions);
  }

  /**
   * Adds {@code PutIf}-specific conditions to the query
   *
   * @param condition {@code PutIf} condition
   */
  @Override
  public void visit(PutIf condition) {
    condition.getExpressions().forEach(e -> expressions.add(createConditionWith(e)));
  }

  /**
   * Adds {@code PutIfExists}-specific conditions to the query
   *
   * @param condition {@code PutIfExists} condition
   */
  @Override
  public void visit(PutIfExists condition) {
    // nothing to do
  }

  /**
   * Adds {@code PutIfNotExists}-specific conditions to the query
   *
   * @param condition {@code PutIfNotExists} condition
   */
  @Override
  public void visit(PutIfNotExists condition) {
    // nothing to do
  }

  /**
   * Adds {@code DeleteIf}-specific conditions to the query
   *
   * @param condition {@code DeleteIf} condition
   */
  @Override
  public void visit(DeleteIf condition) {
    condition.getExpressions().forEach(e -> expressions.add(createConditionWith(e)));
  }

  /**
   * Adds {@code DeleteIfExists}-specific conditions to the query
   *
   * @param condition {@code DeleteIfExists} condition
   */
  @Override
  public void visit(DeleteIfExists condition) {
    // nothing to do
  }

  private String createConditionWith(ConditionalExpression e) {
    List elements;

    String columnName = columnNameAlias + index;
    String value = valueAlias + index;
    switch (e.getOperator()) {
      case EQ:
        elements = Arrays.asList(columnName, "=", value);
        break;
      case NE:
        elements = Arrays.asList("NOT", columnName, "=", value);
        break;
      case GT:
        elements = Arrays.asList(columnName, ">", value);
        break;
      case GTE:
        elements = Arrays.asList(columnName, ">=", value);
        break;
      case LT:
        elements = Arrays.asList(columnName, "<", value);
        break;
      case LTE:
        elements = Arrays.asList(columnName, "<=", value);
        break;
      case IS_NULL:
        elements =
            Arrays.asList(
                "(attribute_not_exists(" + columnName + ")", "OR", columnName, "=", value + ")");
        break;
      case IS_NOT_NULL:
        elements =
            Arrays.asList(
                "(attribute_exists(" + columnName + ")",
                "AND",
                "NOT",
                columnName,
                "=",
                value + ")");
        break;
      default:
        throw new AssertionError();
    }
    index++;

    return String.join(" ", elements);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy