Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.api.core.ApiFunction;
import com.google.api.services.bigquery.model.Dataset.Access;
import com.google.cloud.StringEnumType;
import com.google.cloud.StringEnumValue;

import java.io.Serializable;
import java.util.Objects;
Expand All @@ -43,21 +46,61 @@ public final class Acl implements Serializable {
*
* @see <a href="https://cloud.google.com/bigquery/access-control#datasetroles">Dataset Roles</a>
*/
public enum Role {
public static final class Role extends StringEnumValue {
private static final long serialVersionUID = -1992679397135956912L;

private static final ApiFunction<String, Role> CONSTRUCTOR =
new ApiFunction<String, Role>() {
@Override
public Role apply(String constant) {
return new Role(constant);
}
};

private static final StringEnumType<Role> type = new StringEnumType(
Role.class,
CONSTRUCTOR);

/**
* Can read, query, copy or export tables in the dataset.
*/
READER,
public static final Role READER = type.createAndRegister("READER");

/**
* Same as {@link #READER} plus can edit or append data in the dataset.
*/
WRITER,
public static final Role WRITER = type.createAndRegister("WRITER");

/**
* Same as {@link #WRITER} plus can update and delete the dataset.
*/
OWNER
public static final Role OWNER = type.createAndRegister("OWNER");

private Role(String constant) {
super(constant);
}

/**
* Get the Role for the given String constant, and throw an exception if the constant is
* not recognized.
*/
public static Role valueOfStrict(String constant) {
return type.valueOfStrict(constant);
}

/**
* Get the Role for the given String constant, and allow unrecognized values.
*/
public static Role valueOf(String constant) {
return type.valueOf(constant);
}

/**
* Return the known values for Role.
*/
public static Role[] values() {
return type.values();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package com.google.cloud.bigquery;

import com.google.api.core.ApiFunction;
import com.google.cloud.StringEnumType;
import com.google.cloud.StringEnumValue;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
Expand All @@ -35,22 +38,62 @@ public class JobStatus implements Serializable {
/**
* Possible states that a BigQuery Job can assume.
*/
public enum State {
public static final class State extends StringEnumValue {
private static final long serialVersionUID = 818920627219751204L;

private static final ApiFunction<String, State> CONSTRUCTOR =
new ApiFunction<String, State>() {
@Override
public State apply(String constant) {
return new State(constant);
}
};

private static final StringEnumType<State> type = new StringEnumType(
State.class,
CONSTRUCTOR);

/**
* The BigQuery Job is waiting to be executed.
*/
PENDING,
public static final State PENDING = type.createAndRegister("PENDING");

/**
* The BigQuery Job is being executed.
*/
RUNNING,
public static final State RUNNING = type.createAndRegister("RUNNING");

/**
* The BigQuery Job has completed either succeeding or failing. If failed {@link #getError()}
* will be non-null.
*/
DONE
public static final State DONE = type.createAndRegister("DONE");

private State(String constant) {
super(constant);
}

/**
* Get the State for the given String constant, and throw an exception if the constant is
* not recognized.
*/
public static State valueOfStrict(String constant) {
return type.valueOfStrict(constant);
}

/**
* Get the State for the given String constant, and allow unrecognized values.
*/
public static State valueOf(String constant) {
return type.valueOf(constant);
}

/**
* Return the known values for State.
*/
public static State[] values() {
return type.values();
}
}

private final State state;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,63 @@

package com.google.cloud.bigquery;

import com.google.api.core.ApiFunction;
import com.google.cloud.StringEnumType;
import com.google.cloud.StringEnumValue;

/**
* A type used in legacy SQL contexts. NOTE: some contexts use a mix of types; for example,
* for queries that use standard SQL, the return types are the legacy SQL types.
*
* @see <a href="https://cloud.google.com/bigquery/data-types">https://cloud.google.com/bigquery/data-types</a>
*/
public enum LegacySQLTypeName {
public final class LegacySQLTypeName extends StringEnumValue {
private static final long serialVersionUID = 1421040468991161123L;

private static final ApiFunction<String, LegacySQLTypeName> CONSTRUCTOR =
new ApiFunction<String, LegacySQLTypeName>() {
@Override
public LegacySQLTypeName apply(String constant) {
return new LegacySQLTypeName(constant);
}
};

private static final StringEnumType<LegacySQLTypeName> type = new StringEnumType(
LegacySQLTypeName.class,
CONSTRUCTOR);

/** Variable-length binary data. */
BYTES(StandardSQLTypeName.BYTES),
public static final LegacySQLTypeName BYTES = type.createAndRegister("BYTES").setStandardType(StandardSQLTypeName.BYTES);
/** Variable-length character (Unicode) data. */
STRING(StandardSQLTypeName.STRING),
public static final LegacySQLTypeName STRING = type.createAndRegister("STRING").setStandardType(StandardSQLTypeName.STRING);
/** A 64-bit signed integer value. */
INTEGER(StandardSQLTypeName.INT64),
public static final LegacySQLTypeName INTEGER = type.createAndRegister("INTEGER").setStandardType(StandardSQLTypeName.INT64);
/** A 64-bit IEEE binary floating-point value. */
FLOAT(StandardSQLTypeName.FLOAT64),
public static final LegacySQLTypeName FLOAT = type.createAndRegister("FLOAT").setStandardType(StandardSQLTypeName.FLOAT64);
/** A Boolean value (true or false). */
BOOLEAN(StandardSQLTypeName.BOOL),
public static final LegacySQLTypeName BOOLEAN = type.createAndRegister("BOOLEAN").setStandardType(StandardSQLTypeName.BOOL);
/** Represents an absolute point in time, with microsecond precision. */
TIMESTAMP(StandardSQLTypeName.TIMESTAMP),
public static final LegacySQLTypeName TIMESTAMP = type.createAndRegister("TIMESTAMP").setStandardType(StandardSQLTypeName.TIMESTAMP);
/** Represents a logical calendar date. Note, support for this type is limited in legacy SQL. */
DATE(StandardSQLTypeName.DATE),
public static final LegacySQLTypeName DATE = type.createAndRegister("DATE").setStandardType(StandardSQLTypeName.DATE);
/**
* Represents a time, independent of a specific date, to microsecond precision. Note, support for
* this type is limited in legacy SQL.
*/
TIME(StandardSQLTypeName.TIME),
public static final LegacySQLTypeName TIME = type.createAndRegister("TIME").setStandardType(StandardSQLTypeName.TIME);
/**
* Represents a year, month, day, hour, minute, second, and subsecond (microsecond precision).
* Note, support for this type is limited in legacy SQL.
*/
DATETIME(StandardSQLTypeName.DATETIME),
public static final LegacySQLTypeName DATETIME = type.createAndRegister("DATETIME").setStandardType(StandardSQLTypeName.DATETIME);
/** A record type with a nested schema. */
RECORD(StandardSQLTypeName.STRUCT);
public static final LegacySQLTypeName RECORD = type.createAndRegister("RECORD").setStandardType(StandardSQLTypeName.STRUCT);

private StandardSQLTypeName equivalent;

LegacySQLTypeName(StandardSQLTypeName equivalent) {
private LegacySQLTypeName setStandardType(StandardSQLTypeName equivalent) {
this.equivalent = equivalent;
return this;
}

/**
Expand All @@ -62,4 +81,30 @@ public enum LegacySQLTypeName {
public StandardSQLTypeName getStandardType() {
return equivalent;
}

private LegacySQLTypeName(String constant) {
super(constant);
}

/**
* Get the LegacySQLTypeName for the given String constant, and throw an exception if the constant is
* not recognized.
*/
public static LegacySQLTypeName valueOfStrict(String constant) {
return type.valueOfStrict(constant);
}

/**
* Get the LegacySQLTypeName for the given String constant, and allow unrecognized values.
*/
public static LegacySQLTypeName valueOf(String constant) {
return type.valueOf(constant);
}

/**
* Return the known values for LegacySQLTypeName.
*/
public static LegacySQLTypeName[] values() {
return type.values();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.api.core.ApiFunction;
import com.google.api.services.bigquery.model.Table;
import com.google.cloud.StringEnumType;
import com.google.cloud.StringEnumValue;
import com.google.common.base.MoreObjects;

import java.io.Serializable;
Expand All @@ -37,20 +40,34 @@ public abstract class TableDefinition implements Serializable {
/**
* The table type.
*/
public enum Type {
public static final class Type extends StringEnumValue {
private static final long serialVersionUID = -551560816480511474L;

private static final ApiFunction<String, Type> CONSTRUCTOR =
new ApiFunction<String, Type>() {
@Override
public Type apply(String constant) {
return new Type(constant);
}
};

private static final StringEnumType<Type> type = new StringEnumType(
Type.class,
CONSTRUCTOR);

/**
* A normal BigQuery table. Instances of {@code TableDefinition} for this type are implemented
* by {@link StandardTableDefinition}.
*/
TABLE,
public static final Type TABLE = type.createAndRegister("TABLE");

/**
* A virtual table defined by a SQL query. Instances of {@code TableDefinition} for this type
* are implemented by {@link ViewDefinition}.
*
* @see <a href="https://cloud.google.com/bigquery/querying-data#views">Views</a>
*/
VIEW,
public static final Type VIEW = type.createAndRegister("VIEW");

/**
* A BigQuery table backed by external data. Instances of {@code TableDefinition} for this type
Expand All @@ -59,7 +76,33 @@ public enum Type {
* @see <a href="https://cloud.google.com/bigquery/federated-data-sources">Federated Data
* Sources</a>
*/
EXTERNAL
public static final Type EXTERNAL = type.createAndRegister("EXTERNAL");

private Type(String constant) {
super(constant);
}

/**
* Get the Type for the given String constant, and throw an exception if the constant is
* not recognized.
*/
public static Type valueOfStrict(String constant) {
return type.valueOfStrict(constant);
}

/**
* Get the Type for the given String constant, and allow unrecognized values.
*/
public static Type valueOf(String constant) {
return type.valueOf(constant);
}

/**
* Return the known values for Type.
*/
public static Type[] values() {
return type.values();
}
}

/**
Expand Down Expand Up @@ -170,12 +213,12 @@ Table toPb() {

@SuppressWarnings("unchecked")
static <T extends TableDefinition> T fromPb(Table tablePb) {
switch (Type.valueOf(tablePb.getType())) {
case TABLE:
switch (Type.valueOf(tablePb.getType()).toString()) {
case "TABLE":
return (T) StandardTableDefinition.fromPb(tablePb);
case VIEW:
case "VIEW":
return (T) ViewDefinition.fromPb(tablePb);
case EXTERNAL:
case "EXTERNAL":
return (T) ExternalTableDefinition.fromPb(tablePb);
default:
// never reached
Expand Down