@@ -26,13 +26,16 @@
import com .google .cloud .Timestamp ;
import com .google .common .base .Function ;
import com .google .common .collect .ImmutableList ;
import com .google .common .collect .ImmutableMap ;
import com .google .common .collect .Lists ;
import com .google .common .io .BaseEncoding ;
import java .io .Serializable ;
import java .math .BigDecimal ;
import java .util .ArrayList ;
import java .util .Date ;
import java .util .HashMap ;
import java .util .List ;
import java .util .Map ;
import javax .annotation .Nullable ;
import org .threeten .bp .Instant ;
import org .threeten .bp .ZoneOffset ;
@@ -127,12 +130,27 @@ public Builder setArrayValues(List<QueryParameterValue> arrayValues) {
abstract Builder setArrayValuesInner (ImmutableList <QueryParameterValue > arrayValues );
/** Sets struct values. The type must set to STRUCT. */
public Builder setStructValues (Map <String , QueryParameterValue > structValues ) {
setStructTypes (ImmutableMap .copyOf (structValues ));
return setStructValuesInner (ImmutableMap .copyOf (structValues ));
}
abstract Builder setStructValuesInner (Map <String , QueryParameterValue > structValues );
/** Sets the parameter data type. */
public abstract Builder setType (StandardSQLTypeName type );
/** Sets the data type of the array elements. The type must set to ARRAY. */
public abstract Builder setArrayType (StandardSQLTypeName arrayType );
/** Sets the data type of the struct elements. The type must set to STRUCT. */
public Builder setStructTypes (Map <String , QueryParameterValue > structTypes ) {
return setStructTypesInner (structTypes );
}
abstract Builder setStructTypesInner (Map <String , QueryParameterValue > structTypes );
/** Creates a {@code QueryParameterValue} object. */
public abstract QueryParameterValue build ();
}
@@ -154,13 +172,31 @@ public List<QueryParameterValue> getArrayValues() {
@ Nullable
abstract ImmutableList <QueryParameterValue > getArrayValuesInner ();
/** Returns the struct values of this parameter. The returned map, if not null, is immutable. */
@ Nullable
public Map <String , QueryParameterValue > getStructValues () {
return getStructValuesInner ();
}
@ Nullable
abstract Map <String , QueryParameterValue > getStructValuesInner ();
/** Returns the data type of this parameter. */
public abstract StandardSQLTypeName getType ();
/** Returns the data type of the array elements. */
@ Nullable
public abstract StandardSQLTypeName getArrayType ();
/** Returns the data type of the struct elements. */
@ Nullable
public Map <String , QueryParameterValue > getStructTypes () {
return getStructTypesInner ();
}
@ Nullable
abstract Map <String , QueryParameterValue > getStructTypesInner ();
/** Creates a {@code QueryParameterValue} object with the given value and type. */
public static <T > QueryParameterValue of (T value , Class <T > type ) {
return of (value , classToType (type ));
@@ -274,6 +310,17 @@ public static <T> QueryParameterValue array(T[] array, StandardSQLTypeName type)
.build ();
}
/**
* Creates a map with {@code QueryParameterValue} object and a type of STRUCT the given struct
* element type.
*/
public static QueryParameterValue struct (Map <String , QueryParameterValue > struct ) {
return QueryParameterValue .newBuilder ()
.setStructValues (struct )
.setType (StandardSQLTypeName .STRUCT )
.build ();
}
private static <T > StandardSQLTypeName classToType (Class <T > type ) {
if (Boolean .class .isAssignableFrom (type )) {
return StandardSQLTypeName .BOOL ;
@@ -398,6 +445,14 @@ com.google.api.services.bigquery.model.QueryParameterValue toValuePb() {
valuePb .setArrayValues (
Lists .transform (getArrayValues (), QueryParameterValue .TO_VALUE_PB_FUNCTION ));
}
if (getStructValues () != null ) {
Map <String , com .google .api .services .bigquery .model .QueryParameterValue > structValues =
new HashMap <>();
for (Map .Entry <String , QueryParameterValue > structValue : getStructValues ().entrySet ()) {
structValues .put (structValue .getKey (), structValue .getValue ().toValuePb ());
}
valuePb .setStructValues (structValues );
}
return valuePb ;
}
@@ -409,14 +464,24 @@ QueryParameterType toTypePb() {
arrayTypePb .setType (getArrayType ().toString ());
typePb .setArrayType (arrayTypePb );
}
if (getStructTypes () != null ) {
List <QueryParameterType .StructTypes > structTypes = new ArrayList <>();
for (Map .Entry <String , QueryParameterValue > entry : getStructTypes ().entrySet ()) {
QueryParameterType .StructTypes structType = new QueryParameterType .StructTypes ();
structType .setName (entry .getKey ());
structType .setType (entry .getValue ().toTypePb ());
structTypes .add (structType );
}
typePb .setStructTypes (structTypes );
}
return typePb ;
}
static QueryParameterValue fromPb (
com .google .api .services .bigquery .model .QueryParameterValue valuePb ,
QueryParameterType typePb ) {
Builder valueBuilder = newBuilder ();
Map < String , QueryParameterType > parameterTypes = new HashMap <>();
StandardSQLTypeName type = StandardSQLTypeName .valueOf (typePb .getType ());
valueBuilder .setType (type );
if (type == StandardSQLTypeName .ARRAY ) {
@@ -431,10 +496,35 @@ static QueryParameterValue fromPb(
}
valueBuilder .setArrayValues (arrayValues .build ());
}
} else if (type == StandardSQLTypeName .STRUCT ) {
Map <String , QueryParameterValue > structTypes = new HashMap <>();
for (QueryParameterType .StructTypes types : typePb .getStructTypes ()) {
structTypes .put (
types .getName (),
QueryParameterValue .newBuilder ()
.setType (StandardSQLTypeName .valueOf (types .getType ().getType ()))
.build ());
}
valueBuilder .setStructTypes (structTypes );
if (valuePb == null || valuePb .getStructValues () == null ) {
valueBuilder .setStructValues (ImmutableMap .<String , QueryParameterValue >of ());
} else {
Map <String , QueryParameterValue > structValues = new HashMap <>();
for (QueryParameterType .StructTypes structType : typePb .getStructTypes ()) {
parameterTypes .put (structType .getName (), structType .getType ());
}
for (Map .Entry <String , com .google .api .services .bigquery .model .QueryParameterValue >
structValue : valuePb .getStructValues ().entrySet ()) {
structValues .put (
structValue .getKey (),
QueryParameterValue .fromPb (
structValue .getValue (), parameterTypes .get (structValue .getKey ())));
}
valueBuilder .setStructValues (structValues );
}
} else {
valueBuilder .setValue (valuePb == null ? "" : valuePb .getValue ());
}
return valueBuilder .build ();
}
}