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

The following examples show how to use com.google.cloud.spanner.Type#bool() . 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: RandomDdlGenerator.java    From DataflowTemplates with Apache License 2.0 6 votes vote down vote up
private Type typeOf(Type.Code code) {
  switch (code) {
    case BOOL:
      return Type.bool();
    case FLOAT64:
      return Type.float64();
    case STRING:
      return Type.string();
    case BYTES:
      return Type.bytes();
    case TIMESTAMP:
      return Type.timestamp();
    case DATE:
      return Type.date();
    case INT64:
      return Type.int64();
  }
  throw new IllegalArgumentException("Arrays and Structs are not supported");
}
 
Example 2
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 3
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 4
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 5
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 6
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 7
Source File: CloudSpannerResultSetMetaDataTest.java    From spanner-jdbc with MIT License 5 votes vote down vote up
private int getPrecision(TestColumn col) {
  if (col.type == Type.bool())
    return 1;
  if (col.type == Type.date())
    return 10;
  if (col.type == Type.float64())
    return 14;
  if (col.type == Type.int64())
    return 10;
  if (col.type == Type.timestamp())
    return 24;
  if (col.isTableColumn())
    return col.size;
  return 0;
}
 
Example 8
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 9
Source File: SpannerSchema.java    From beam with Apache License 2.0 5 votes vote down vote up
private static Type parseSpannerType(String spannerType) {
  spannerType = spannerType.toUpperCase();
  if ("BOOL".equals(spannerType)) {
    return Type.bool();
  }
  if ("INT64".equals(spannerType)) {
    return Type.int64();
  }
  if ("FLOAT64".equals(spannerType)) {
    return Type.float64();
  }
  if (spannerType.startsWith("STRING")) {
    return Type.string();
  }
  if (spannerType.startsWith("BYTES")) {
    return Type.bytes();
  }
  if ("TIMESTAMP".equals(spannerType)) {
    return Type.timestamp();
  }
  if ("DATE".equals(spannerType)) {
    return Type.date();
  }

  if (spannerType.startsWith("ARRAY")) {
    // Substring "ARRAY<xxx>"
    String spannerArrayType = spannerType.substring(6, spannerType.length() - 1);
    Type itemType = parseSpannerType(spannerArrayType);
    return Type.array(itemType);
  }
  throw new IllegalArgumentException("Unknown spanner type " + spannerType);
}
 
Example 10
Source File: AvroSchemaToDdlConverter.java    From DataflowTemplates with Apache License 2.0 4 votes vote down vote up
private com.google.cloud.spanner.Type inferType(Schema f, boolean supportArrays) {
  Schema.Type type = f.getType();
  LogicalType logicalType = LogicalTypes.fromSchema(f);

  switch (type) {
    case BOOLEAN:
      return Type.bool();
    case INT:
      return com.google.cloud.spanner.Type.int64();
    case LONG:
      if (LogicalTypes.timestampMillis().equals(logicalType)) {
        return com.google.cloud.spanner.Type.timestamp();
      }
      if (LogicalTypes.timestampMicros().equals(logicalType)) {
        return com.google.cloud.spanner.Type.timestamp();
      }
      return com.google.cloud.spanner.Type.int64();
    case FLOAT:
    case DOUBLE:
      return com.google.cloud.spanner.Type.float64();
    case STRING:
      return com.google.cloud.spanner.Type.string();
    case BYTES:
      return com.google.cloud.spanner.Type.bytes();
    case ARRAY:
      {
        if (supportArrays) {
          Schema element = f.getElementType();
          if (element.getType() == Schema.Type.UNION) {
            Schema unpacked = unpackNullable(element);
            if (unpacked == null) {
              throw new IllegalArgumentException("Cannot infer a type " + f);
            }
            element = unpacked;
          }
          try {
            return com.google.cloud.spanner.Type.array(inferType(element, false));
          } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("Cannot infer array type for field " + f);
          }
        }
        // Throw exception.
        break;
      }
  }
  throw new IllegalArgumentException("Cannot infer a type " + f);
}