Java Code Examples for com.google.cloud.spanner.Type#getCode()

The following examples show how to use com.google.cloud.spanner.Type#getCode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DdlToAvroSchemaConverter.java    From DataflowTemplates with Apache License 2.0 6 votes vote down vote up
private Schema avroType(Type spannerType) {
  switch (spannerType.getCode()) {
    case BOOL:
      return SchemaBuilder.builder().booleanType();
    case INT64:
      return SchemaBuilder.builder().longType();
    case FLOAT64:
      return SchemaBuilder.builder().doubleType();
    case STRING:
      return SchemaBuilder.builder().stringType();
    case BYTES:
      return SchemaBuilder.builder().bytesType();
    case TIMESTAMP:
      return SchemaBuilder.builder().stringType();
    case DATE:
      return SchemaBuilder.builder().stringType();
    case ARRAY:
      Schema avroItemsType = avroType(spannerType.getArrayElementType());
      return SchemaBuilder.builder().array().items().type(wrapAsNullable(avroItemsType));
    default:
      throw new IllegalArgumentException("Unknown spanner type " + spannerType);
  }
}
 
Example 2
Source File: Column.java    From DataflowTemplates with Apache License 2.0 6 votes vote down vote up
private static String typeString(Type type, Integer size) {
  switch (type.getCode()) {
    case BOOL:
      return "BOOL";
    case INT64:
      return "INT64";
    case FLOAT64:
      return "FLOAT64";
    case STRING:
      return "STRING(" + (size == -1 ? "MAX" : Integer.toString(size)) + ")";
    case BYTES:
      return "BYTES(" + (size == -1 ? "MAX" : Integer.toString(size)) + ")";
    case DATE:
      return "DATE";
    case TIMESTAMP:
      return "TIMESTAMP";
    case ARRAY:
      Type arrayType = type.getArrayElementType();
      return "ARRAY<" + typeString(arrayType, size) + ">";
  }

  throw new IllegalArgumentException("Unknown type " + type);
}
 
Example 3
Source File: RandomValueGenerator.java    From DataflowTemplates with Apache License 2.0 6 votes vote down vote up
private Value generate(Column column) {
  Type type = column.type();

  if (type.getCode() != Type.Code.ARRAY) {
    return generateScalar(column);
  }

  switch (type.getArrayElementType().getCode()) {
    case BOOL:
      return Value.boolArray(generateList(random::nextBoolean));
    case INT64:
      return Value.int64Array(generateList(random::nextLong));
    case FLOAT64:
      return Value.float64Array(generateList(random::nextDouble));
    case BYTES:
      return Value.bytesArray(generateList(() -> randomByteArray(column.size())));
    case STRING:
      return Value.stringArray(generateList(() -> randomString(column.size())));
    case DATE:
      return Value.dateArray(generateList(this::randomDate));
    case TIMESTAMP:
      return Value.timestampArray(generateList(this::randomTimestamp));
  }
  throw new IllegalArgumentException("Unexpected type " + type);
}
 
Example 4
Source File: RandomValueGenerator.java    From DataflowTemplates with Apache License 2.0 6 votes vote down vote up
private Value generateScalar(Column column) {
  Type type = column.type();
  switch (type.getCode()) {
    case BOOL:
      return Value.bool(random.nextBoolean());
    case INT64:
      return Value.int64(random.nextLong());
    case FLOAT64:
      return Value.float64(random.nextDouble());
    case BYTES: {
      return Value.bytes(randomByteArray(column.size()));
    }
    case STRING: {
      return Value.string(randomString(column.size()));
    }
    case DATE: {
      return Value.date(randomDate());
    }
    case TIMESTAMP: {
      return Value.timestamp(randomTimestamp());
    }
  }
  throw new IllegalArgumentException("Unexpected type " + type);
}
 
Example 5
Source File: CloudSpannerResultSet.java    From spanner-jdbc with MIT License 6 votes vote down vote up
private Object getObject(Type type, int columnIndex) throws SQLException {
  if (type == Type.bool())
    return getBoolean(columnIndex);
  if (type == Type.bytes())
    return getBytes(columnIndex);
  if (type == Type.date())
    return getDate(columnIndex);
  if (type == Type.float64())
    return getDouble(columnIndex);
  if (type == Type.int64())
    return getLong(columnIndex);
  if (type == Type.string())
    return getString(columnIndex);
  if (type == Type.timestamp())
    return getTimestamp(columnIndex);
  if (type.getCode() == Code.ARRAY)
    return getArray(columnIndex);
  throw new CloudSpannerSQLException("Unknown type: " + type.toString(),
      com.google.rpc.Code.INVALID_ARGUMENT);
}
 
Example 6
Source File: AbstractCloudSpannerWrapper.java    From spanner-jdbc with MIT License 6 votes vote down vote up
public static int extractColumnType(Type type) {
  if (type.equals(Type.bool()))
    return Types.BOOLEAN;
  if (type.equals(Type.bytes()))
    return Types.BINARY;
  if (type.equals(Type.date()))
    return Types.DATE;
  if (type.equals(Type.float64()))
    return Types.DOUBLE;
  if (type.equals(Type.int64()))
    return Types.BIGINT;
  if (type.equals(Type.string()))
    return Types.NVARCHAR;
  if (type.equals(Type.timestamp()))
    return Types.TIMESTAMP;
  if (type.getCode() == Code.ARRAY)
    return Types.ARRAY;
  return Types.OTHER;
}
 
Example 7
Source File: CloudSpannerResultSetMetaDataTest.java    From spanner-jdbc with MIT License 6 votes vote down vote up
private int getDefaultDisplaySize(Type type, int column) throws SQLException {
  if (type.getCode() == Code.ARRAY)
    return 50;
  if (type == Type.bool())
    return 5;
  if (type == Type.bytes())
    return 50;
  if (type == Type.date())
    return 10;
  if (type == Type.float64())
    return 14;
  if (type == Type.int64())
    return 10;
  if (type == Type.string()) {
    int length = subject.getPrecision(column);
    return length == 0 ? 50 : length;
  }
  if (type == Type.timestamp())
    return 16;
  return 10;
}
 
Example 8
Source File: CloudSpannerResultSetMetaDataTest.java    From spanner-jdbc with MIT License 6 votes vote down vote up
private int getSqlType(Type type) {
  if (type == Type.bool())
    return Types.BOOLEAN;
  if (type == Type.bytes())
    return Types.BINARY;
  if (type == Type.date())
    return Types.DATE;
  if (type == Type.float64())
    return Types.DOUBLE;
  if (type == Type.int64())
    return Types.BIGINT;
  if (type == Type.string())
    return Types.NVARCHAR;
  if (type == Type.timestamp())
    return Types.TIMESTAMP;
  if (type.getCode() == Code.ARRAY)
    return Types.ARRAY;
  return Types.OTHER;
}
 
Example 9
Source File: RandomValueGenerator.java    From DataflowTemplates with Apache License 2.0 5 votes vote down vote up
private Value generateNullValue(Type type) {
  switch (type.getCode()) {
    case BOOL:
      return Value.bool(null);
    case INT64:
      return Value.int64(null);
    case FLOAT64:
      return Value.float64(null);
    case BYTES:
      return Value.bytes(null);
    case STRING:
      return Value.string(null);
    case DATE:
      return Value.date(null);
    case TIMESTAMP:
      return Value.timestamp(null);
    case ARRAY:
      switch (type.getArrayElementType().getCode()) {
        case BOOL:
          return Value.boolArray((boolean[]) null);
        case INT64:
          return Value.int64Array((long[]) null);
        case FLOAT64:
          return Value.float64Array((double[]) null);
        case BYTES:
          return Value.bytesArray(null);
        case STRING:
          return Value.stringArray(null);
        case DATE:
          return Value.dateArray(null);
        case TIMESTAMP:
          return Value.timestampArray(null);
      }
  }
  throw new IllegalArgumentException("Unexpected type " + type);
}
 
Example 10
Source File: CloudSpannerResultSet.java    From spanner-jdbc with MIT License 5 votes vote down vote up
@Override
public Array getArray(String columnLabel) throws SQLException {
  Type type = resultSet.getColumnType(columnLabel);
  if (type.getCode() != Code.ARRAY)
    throw new CloudSpannerSQLException(
        "Column with label " + columnLabel + " does not contain an array",
        com.google.rpc.Code.INVALID_ARGUMENT);
  return getArray(resultSet.getColumnIndex(columnLabel) + 1);
}
 
Example 11
Source File: CloudSpannerResultSet.java    From spanner-jdbc with MIT License 5 votes vote down vote up
@Override
public Array getArray(int columnIndex) throws SQLException {
  if (isNull(columnIndex))
    return null;
  Type type = resultSet.getColumnType(columnIndex - 1);
  if (type.getCode() != Code.ARRAY)
    throw new CloudSpannerSQLException(
        "Column with index " + columnIndex + " does not contain an array",
        com.google.rpc.Code.INVALID_ARGUMENT);
  CloudSpannerDataType dataType =
      CloudSpannerDataType.getType(type.getArrayElementType().getCode());
  List<? extends Object> elements = dataType.getArrayElements(resultSet, columnIndex - 1);

  return CloudSpannerArray.createArray(dataType, elements);
}
 
Example 12
Source File: AbstractCloudSpannerWrapper.java    From spanner-jdbc with MIT License 5 votes vote down vote up
public static String getClassName(Type type) {
  if (type == Type.bool())
    return Boolean.class.getName();
  if (type == Type.bytes())
    return Byte[].class.getName();
  if (type == Type.date())
    return Date.class.getName();
  if (type == Type.float64())
    return Double.class.getName();
  if (type == Type.int64())
    return Long.class.getName();
  if (type == Type.string())
    return String.class.getName();
  if (type == Type.timestamp())
    return Timestamp.class.getName();
  if (type.getCode() == Code.ARRAY) {
    if (type.getArrayElementType() == Type.bool())
      return Boolean[].class.getName();
    if (type.getArrayElementType() == Type.bytes())
      return Byte[][].class.getName();
    if (type.getArrayElementType() == Type.date())
      return Date[].class.getName();
    if (type.getArrayElementType() == Type.float64())
      return Double[].class.getName();
    if (type.getArrayElementType() == Type.int64())
      return Long[].class.getName();
    if (type.getArrayElementType() == Type.string())
      return String[].class.getName();
    if (type.getArrayElementType() == Type.timestamp())
      return Timestamp[].class.getName();
  }
  return null;
}
 
Example 13
Source File: CloudSpannerResultSetMetaDataTest.java    From spanner-jdbc with MIT License 5 votes vote down vote up
private Value getDefaultValue(Type type, int row) {
  if (type == Type.bool())
    return Value.bool(Boolean.TRUE);
  if (type == Type.bytes())
    return Value.bytes(ByteArray.copyFrom("test byte array " + row));
  if (type == Type.date())
    return Value.date(com.google.cloud.Date.fromYearMonthDay(2018, 4, 1));
  if (type == Type.float64())
    return Value.float64(123.45D);
  if (type == Type.int64())
    return Value.int64(12345L);
  if (type == Type.string())
    return Value.string("test value " + row);
  if (type == Type.timestamp())
    return Value.timestamp(com.google.cloud.Timestamp.now());

  if (type.getCode() == Code.ARRAY && type.getArrayElementType() == Type.bool())
    return Value.boolArray(Arrays.asList(Boolean.TRUE, Boolean.FALSE));
  if (type.getCode() == Code.ARRAY && type.getArrayElementType() == Type.bytes())
    return Value.bytesArray(Arrays.asList(ByteArray.copyFrom("test byte array " + row),
        ByteArray.copyFrom("test byte array " + row)));
  if (type.getCode() == Code.ARRAY && type.getArrayElementType() == Type.date())
    return Value.dateArray(Arrays.asList(com.google.cloud.Date.fromYearMonthDay(2018, 4, 1),
        com.google.cloud.Date.fromYearMonthDay(2018, 4, 2)));
  if (type.getCode() == Code.ARRAY && type.getArrayElementType() == Type.float64())
    return Value.float64Array(Arrays.asList(123.45D, 543.21D));
  if (type.getCode() == Code.ARRAY && type.getArrayElementType() == Type.int64())
    return Value.int64Array(Arrays.asList(12345L, 54321L));
  if (type.getCode() == Code.ARRAY && type.getArrayElementType() == Type.string())
    return Value.stringArray(Arrays.asList("test value " + row, "test value " + row));
  if (type.getCode() == Code.ARRAY && type.getArrayElementType() == Type.timestamp())
    return Value.timestampArray(
        Arrays.asList(com.google.cloud.Timestamp.now(), com.google.cloud.Timestamp.now()));
  return null;
}
 
Example 14
Source File: CloudSpannerResultSetMetaDataTest.java    From spanner-jdbc with MIT License 5 votes vote down vote up
private String getTypeClassName(Type type) {
  if (type == Type.bool())
    return Boolean.class.getName();
  if (type == Type.bytes())
    return Byte[].class.getName();
  if (type == Type.date())
    return Date.class.getName();
  if (type == Type.float64())
    return Double.class.getName();
  if (type == Type.int64())
    return Long.class.getName();
  if (type == Type.string())
    return String.class.getName();
  if (type == Type.timestamp())
    return Timestamp.class.getName();
  if (type.getCode() == Code.ARRAY) {
    if (type.getArrayElementType() == Type.bool())
      return Boolean[].class.getName();
    if (type.getArrayElementType() == Type.bytes())
      return Byte[][].class.getName();
    if (type.getArrayElementType() == Type.date())
      return Date[].class.getName();
    if (type.getArrayElementType() == Type.float64())
      return Double[].class.getName();
    if (type.getArrayElementType() == Type.int64())
      return Long[].class.getName();
    if (type.getArrayElementType() == Type.string())
      return String[].class.getName();
    if (type.getArrayElementType() == Type.timestamp())
      return Timestamp[].class.getName();
  }
  return null;
}
 
Example 15
Source File: CloudSpannerConversionUtil.java    From spanner-jdbc with MIT License 4 votes vote down vote up
/**
 * Converts the given value from the Google {@link Type} to the Java {@link Class} type.
 * 
 * @param value The value to convert
 * @param type The type in the database
 * @param targetType The java class target type to convert to
 * @return The converted value
 * @throws CloudSpannerSQLException Thrown if the given value cannot be converted to the specified
 *         type
 */
public static Object convert(Object value, Type type, Class<?> targetType)
    throws CloudSpannerSQLException {
  Preconditions.checkNotNull(type, "type may not be null");
  Preconditions.checkNotNull(targetType, "targetType may not be null");
  if (value == null)
    return null;
  if (targetType.equals(String.class))
    return value.toString();

  try {
    if (targetType.equals(Boolean.class) && type.getCode() == Code.BOOL)
      return value;
    if (targetType.equals(Boolean.class) && type.getCode() == Code.INT64)
      return Boolean.valueOf((Long) value != 0);
    if (targetType.equals(Boolean.class) && type.getCode() == Code.FLOAT64)
      return Boolean.valueOf((Double) value != 0d);
    if (targetType.equals(Boolean.class) && type.getCode() == Code.STRING)
      return Boolean.valueOf((String) value);

    if (targetType.equals(BigDecimal.class) && type.getCode() == Code.BOOL)
      return (Boolean) value ? BigDecimal.ONE : BigDecimal.ZERO;
    if (targetType.equals(BigDecimal.class) && type.getCode() == Code.INT64)
      return BigDecimal.valueOf((Long) value);
    if (targetType.equals(BigDecimal.class) && type.getCode() == Code.FLOAT64)
      return BigDecimal.valueOf((Double) value);
    if (targetType.equals(BigDecimal.class) && type.getCode() == Code.STRING)
      return new BigDecimal((String) value);

    if (targetType.equals(Long.class) && type.getCode() == Code.BOOL)
      return (Boolean) value ? 1L : 0L;
    if (targetType.equals(Long.class) && type.getCode() == Code.INT64)
      return value;
    if (targetType.equals(Long.class) && type.getCode() == Code.FLOAT64)
      return ((Double) value).longValue();
    if (targetType.equals(Long.class) && type.getCode() == Code.STRING)
      return Long.valueOf((String) value);

    if (targetType.equals(Integer.class) && type.getCode() == Code.BOOL)
      return (Boolean) value ? 1 : 0;
    if (targetType.equals(Integer.class) && type.getCode() == Code.INT64)
      return ((Long) value).intValue();
    if (targetType.equals(Integer.class) && type.getCode() == Code.FLOAT64)
      return ((Double) value).intValue();
    if (targetType.equals(Integer.class) && type.getCode() == Code.STRING)
      return Integer.valueOf((String) value);

    if (targetType.equals(BigInteger.class) && type.getCode() == Code.BOOL)
      return (Boolean) value ? BigInteger.ONE : BigInteger.ZERO;
    if (targetType.equals(BigInteger.class) && type.getCode() == Code.INT64)
      return BigInteger.valueOf((Long) value);
    if (targetType.equals(BigInteger.class) && type.getCode() == Code.FLOAT64)
      return BigInteger.valueOf(((Double) value).longValue());
    if (targetType.equals(BigInteger.class) && type.getCode() == Code.STRING)
      return new BigInteger((String) value);

    if (targetType.equals(Float.class) && type.getCode() == Code.BOOL)
      return (Boolean) value ? Float.valueOf(1f) : Float.valueOf(0f);
    if (targetType.equals(Float.class) && type.getCode() == Code.INT64)
      return ((Long) value).floatValue();
    if (targetType.equals(Float.class) && type.getCode() == Code.FLOAT64)
      return ((Double) value).floatValue();
    if (targetType.equals(Float.class) && type.getCode() == Code.STRING)
      return Float.valueOf((String) value);

    if (targetType.equals(Double.class) && type.getCode() == Code.BOOL)
      return (Boolean) value ? Double.valueOf(1d) : Double.valueOf(0d);
    if (targetType.equals(Double.class) && type.getCode() == Code.INT64)
      return ((Long) value).doubleValue();
    if (targetType.equals(Double.class) && type.getCode() == Code.FLOAT64)
      return value;
    if (targetType.equals(Double.class) && type.getCode() == Code.STRING)
      return Double.valueOf((String) value);
  } catch (Exception e) {
    throw new CloudSpannerSQLException("Cannot convert " + value + " to " + targetType.getName(),
        com.google.rpc.Code.INVALID_ARGUMENT, e);
  }

  throw new CloudSpannerSQLException(
      "Cannot convert " + type.getCode().name() + " to " + targetType.getName(),
      com.google.rpc.Code.INVALID_ARGUMENT);
}
 
Example 16
Source File: StructAccessor.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
private Class getSingleItemTypeCode(Type colType) {
	Code code = colType.getCode();
	return code.equals(Code.ARRAY)
			? SpannerTypeMapper.getArrayJavaClassFor(colType.getArrayElementType().getCode())
			: SpannerTypeMapper.getSimpleJavaClassFor(code);
}