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

com.scalar.db.sql.statement.CreateUserStatement Maven / Gradle / Ivy

There is a newer version: 3.14.0
Show newest version
package com.scalar.db.sql.statement;

import com.google.common.collect.ImmutableList;
import com.scalar.db.sql.UserOption;
import java.util.Objects;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

@Immutable
public class CreateUserStatement implements DclStatement {

  public final String username;
  @Nullable public final String password;
  public final ImmutableList userOptions;

  private CreateUserStatement(
      String username, @Nullable String password, ImmutableList userOptions) {
    this.username = username;
    this.password = password;
    this.userOptions = userOptions;
  }

  @Override
  public String toSql() {
    StringBuilder builder = new StringBuilder("CREATE USER ");
    StatementUtils.appendObjectName(builder, username);
    if (password != null || !userOptions.isEmpty()) {
      builder.append(" WITH");
    }
    if (password != null) {
      builder.append(" PASSWORD ");
      StatementUtils.appendStringLiteral(builder, password);
    }
    if (!userOptions.isEmpty()) {
      builder.append(' ');

      boolean first = true;
      for (UserOption userOption : userOptions) {
        if (first) {
          first = false;
        } else {
          builder.append(" ");
        }
        builder.append(userOption.name());
      }
    }
    return builder.toString();
  }

  @Override
  public  R accept(DclStatementVisitor visitor, C context) {
    return visitor.visit(this, context);
  }

  @Override
  public  R accept(StatementVisitor visitor, C context) {
    return visitor.visit(this, context);
  }

  @Override
  public String toString() {
    return toSql();
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (!(o instanceof CreateUserStatement)) {
      return false;
    }
    CreateUserStatement that = (CreateUserStatement) o;
    return Objects.equals(username, that.username)
        && Objects.equals(password, that.password)
        && Objects.equals(userOptions, that.userOptions);
  }

  @Override
  public int hashCode() {
    return Objects.hash(username, password, userOptions);
  }

  public static CreateUserStatement create(
      String username, String password, ImmutableList userOptions) {
    return new CreateUserStatement(username, password, userOptions);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy