use of com.google.spanner.v1.Type in project google-cloud-java by GoogleCloudPlatform.
the class Type method fromProto.
static Type fromProto(com.google.spanner.v1.Type proto) {
Code type = Code.fromProtoCode(proto.getCode());
switch(type) {
case BOOL:
return bool();
case INT64:
return int64();
case FLOAT64:
return float64();
case STRING:
return string();
case BYTES:
return bytes();
case TIMESTAMP:
return timestamp();
case DATE:
return date();
case ARRAY:
checkArgument(proto.hasArrayElementType(), "Missing expected 'array_element_type' field in 'Type' message: %s", proto);
Type elementType;
try {
elementType = fromProto(proto.getArrayElementType());
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Could not parse 'array_element_type' attribute in 'Type' message: " + proto, e);
}
return array(elementType);
case STRUCT:
checkArgument(proto.hasStructType(), "Missing expected 'struct_type' field in 'Type' message: %s", proto);
List<StructField> fields = new ArrayList<>(proto.getStructType().getFieldsCount());
for (com.google.spanner.v1.StructType.Field field : proto.getStructType().getFieldsList()) {
checkArgument(field.hasType(), "Missing expected 'type' attribute in 'Field': %s", proto);
// Names may be empty; for example, the name of the column returned by "SELECT 1".
String name = Strings.nullToEmpty(field.getName());
fields.add(StructField.of(name, fromProto(field.getType())));
}
return struct(fields);
default:
throw new AssertionError("Unimplemented case: " + type);
}
}
use of com.google.spanner.v1.Type in project tomee by apache.
the class CmrField method createSignature.
private static String createSignature(final Type type, final Type... genericTypes) {
final StringBuilder builder = new StringBuilder();
builder.append("L").append(type.getInternalName());
if (genericTypes.length > 0) {
builder.append("<");
for (final Type genericType : genericTypes) {
builder.append(genericType.getDescriptor());
}
builder.append(">");
}
builder.append(";");
return builder.toString();
}
use of com.google.spanner.v1.Type in project tomee by apache.
the class DependencyVisitor method addMethodDesc.
private void addMethodDesc(final String desc) {
addType(Type.getReturnType(desc));
final Type[] types = Type.getArgumentTypes(desc);
for (Type type : types) {
addType(type);
}
}
use of com.google.spanner.v1.Type in project tomee by apache.
the class DynamicSubclass method visitConstructor.
private static MethodVisitor visitConstructor(final ClassWriter cw, final String proxyClassFileName, final String classFileName, final Constructor<?> constructor) {
final String descriptor = Type.getConstructorDescriptor(constructor);
final String[] exceptions = new String[constructor.getExceptionTypes().length];
for (int i = 0; i < exceptions.length; i++) {
exceptions[i] = Type.getInternalName(constructor.getExceptionTypes()[i]);
}
final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", descriptor, null, exceptions);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
int index = 1;
for (final Type type : Type.getArgumentTypes(descriptor)) {
mv.visitVarInsn(type.getOpcode(ILOAD), index);
index += size(type);
}
mv.visitMethodInsn(INVOKESPECIAL, classFileName, "<init>", descriptor, false);
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(PUTFIELD, proxyClassFileName, "this$handler", "Ljava/lang/reflect/InvocationHandler;");
mv.visitInsn(RETURN);
mv.visitMaxs(2, 1);
return mv;
}
use of com.google.spanner.v1.Type in project java-docs-samples by GoogleCloudPlatform.
the class LoadCsvExample method writeToSpanner.
/**
* Write CSV file data to Spanner using JDBC Mutation API *
*/
static void writeToSpanner(Iterable<CSVRecord> records, String tableName) throws SQLException {
System.out.println("Writing data into table...");
List<Mutation> mutations = new ArrayList<>();
for (CSVRecord record : records) {
int index = 0;
WriteBuilder builder = Mutation.newInsertOrUpdateBuilder(tableName);
for (String columnName : tableColumns.keySet()) {
// Iterates through columns in order. Assumes in order columns when no headers provided.
TypeCode columnType = tableColumns.get(columnName);
String recordValue = null;
if (validHeaderField(record, columnName)) {
recordValue = record.get(columnName).trim();
} else if (validNonHeaderField(record, index)) {
recordValue = record.get(index).trim();
index++;
}
if (recordValue != null) {
switch(columnType) {
case STRING:
builder.set(columnName).to(recordValue);
break;
case BYTES:
builder.set(columnName).to(Byte.parseByte(recordValue));
break;
case INT64:
builder.set(columnName).to(Integer.parseInt(recordValue));
break;
case FLOAT64:
builder.set(columnName).to(Float.parseFloat(recordValue));
break;
case BOOL:
builder.set(columnName).to(Boolean.parseBoolean(recordValue));
break;
case NUMERIC:
builder.set(columnName).to(Value.numeric(BigDecimal.valueOf(Double.parseDouble(recordValue))));
break;
case DATE:
builder.set(columnName).to(com.google.cloud.Date.parseDate(recordValue));
break;
case TIMESTAMP:
builder.set(columnName).to(com.google.cloud.Timestamp.parseTimestamp(recordValue));
break;
default:
System.out.print("Invalid Type. This type is not supported.");
}
}
}
mutations.add(builder.build());
}
CloudSpannerJdbcConnection spannerConnection = connection.unwrap(CloudSpannerJdbcConnection.class);
spannerConnection.write(mutations);
spannerConnection.close();
System.out.println("Data successfully written into table.");
}
Aggregations