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 @@ -25,6 +25,7 @@
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Nullable;
import org.threeten.bp.Instant;
Expand Down Expand Up @@ -256,6 +257,8 @@ private static <T> StandardSQLTypeName classToType(Class<T> type) {
return StandardSQLTypeName.FLOAT64;
} else if (BigDecimal.class.isAssignableFrom(type)) {
return StandardSQLTypeName.NUMERIC;
} else if (Date.class.isAssignableFrom(type)) {
return StandardSQLTypeName.DATE;
}
throw new IllegalArgumentException("Unsupported object type for QueryParameter: " + type);
}
Expand Down Expand Up @@ -310,6 +313,9 @@ private static <T> String valueToStringOrNull(T value, StandardSQLTypeName type)
// verify that the String is in the right format
checkFormat(value, dateFormatter);
return (String) value;
} else if (value instanceof Date) {
com.google.cloud.Date date = com.google.cloud.Date.fromJavaUtilDate((Date) value);
return date.toString();
}
break;
case TIME:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import com.google.api.services.bigquery.model.QueryParameterType;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import org.junit.Test;

Expand Down Expand Up @@ -204,6 +206,17 @@ public void testDate() {
assertThat(value.getArrayValues()).isNull();
}

@Test
public void testStandardDate() throws ParseException {
com.google.cloud.Date gcDate = com.google.cloud.Date.parseDate("2016-09-18");
Date date = com.google.cloud.Date.toJavaUtilDate(gcDate);
QueryParameterValue value = QueryParameterValue.of(date, Date.class);
assertThat(value.getValue()).isEqualTo("2016-09-18");
assertThat(value.getType()).isEqualTo(StandardSQLTypeName.DATE);
assertThat(value.getArrayType()).isNull();
assertThat(value.getArrayValues()).isNull();
}

@Test(expected = IllegalArgumentException.class)
public void testInvalidDate() {
// not supposed to have the time
Expand Down