com.scalar.db.sql.statement.builder.InsertStatementBuilder Maven / Gradle / Ivy
package com.scalar.db.sql.statement.builder;
import com.google.common.collect.ImmutableList;
import com.scalar.db.sql.TableRef;
import com.scalar.db.sql.Term;
import com.scalar.db.sql.statement.InsertStatement;
import java.util.List;
import javax.annotation.Nullable;
public final class InsertStatementBuilder {
private InsertStatementBuilder() {}
public static class Start extends InsertValues implements Columns {
Start(@Nullable String namespaceName, String tableName) {
super(namespaceName, tableName, ImmutableList.of());
}
@Override
public InsertValues columns(String... columnNames) {
return new InsertValues(namespaceName, tableName, ImmutableList.copyOf(columnNames));
}
@Override
public InsertValues columns(List columnNames) {
return new InsertValues(namespaceName, tableName, ImmutableList.copyOf(columnNames));
}
}
public static class InsertValues implements Values {
@Nullable protected final String namespaceName;
protected final String tableName;
protected final ImmutableList columnNames;
private InsertValues(
@Nullable String namespaceName, String tableName, ImmutableList columnNames) {
this.namespaceName = namespaceName;
this.tableName = tableName;
this.columnNames = columnNames;
}
@Override
public Buildable values(Term... values) {
return new Buildable(
namespaceName,
tableName,
columnNames,
ImmutableList.>builder().add(ImmutableList.copyOf(values)));
}
@Override
public Buildable values(List values) {
return new Buildable(
namespaceName,
tableName,
columnNames,
ImmutableList.>builder().add(ImmutableList.copyOf(values)));
}
}
public static class Buildable implements Values {
@Nullable protected final String namespaceName;
protected final String tableName;
protected final ImmutableList columnNames;
private final ImmutableList.Builder> valuesListBuilder;
private Buildable(
@Nullable String namespaceName,
String tableName,
ImmutableList columnNames,
ImmutableList.Builder> valuesListBuilder) {
this.namespaceName = namespaceName;
this.tableName = tableName;
this.columnNames = columnNames;
this.valuesListBuilder = valuesListBuilder;
}
@Override
public Buildable values(Term... values) {
valuesListBuilder.add(ImmutableList.copyOf(values));
return this;
}
@Override
public Buildable values(List values) {
valuesListBuilder.add(ImmutableList.copyOf(values));
return this;
}
/**
* Builds a InsertStatement object.
*
* @return a InsertStatement object
*/
public InsertStatement build() {
return InsertStatement.create(
TableRef.of(namespaceName, tableName), columnNames, valuesListBuilder.build());
}
}
}