Search in sources :

Example 1 with Type

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);
    }
}
Also used : ArrayList(java.util.ArrayList) TypeCode(com.google.spanner.v1.TypeCode)

Example 2 with 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();
}
Also used : Type(org.apache.xbean.asm9.Type)

Example 3 with Type

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);
    }
}
Also used : Type(org.apache.xbean.asm9.Type)

Example 4 with 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;
}
Also used : Type(org.apache.xbean.asm9.Type) MethodVisitor(org.apache.xbean.asm9.MethodVisitor)

Example 5 with Type

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.");
}
Also used : TypeCode(com.google.spanner.v1.TypeCode) WriteBuilder(com.google.cloud.spanner.Mutation.WriteBuilder) ArrayList(java.util.ArrayList) CloudSpannerJdbcConnection(com.google.cloud.spanner.jdbc.CloudSpannerJdbcConnection) CSVRecord(org.apache.commons.csv.CSVRecord) Mutation(com.google.cloud.spanner.Mutation)

Aggregations

Type (com.google.api.expr.v1alpha1.Type)30 Test (org.junit.Test)16 ByteString (com.google.protobuf.ByteString)13 ArrayList (java.util.ArrayList)13 MapType (com.google.api.expr.v1alpha1.Type.MapType)12 Type (com.google.spanner.v1.Type)12 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)9 CheckedExpr (com.google.api.expr.v1alpha1.CheckedExpr)8 ListValue (com.google.protobuf.ListValue)8 FieldType (org.projectnessie.cel.common.types.ref.FieldType)8 Expr (com.google.api.expr.v1alpha1.Expr)7 PartialResultSet (com.google.spanner.v1.PartialResultSet)7 Type (org.apache.xbean.asm9.Type)7 CheckerEnv.dynElementType (org.projectnessie.cel.checker.CheckerEnv.dynElementType)7 CheckerEnv.getObjectWellKnownType (org.projectnessie.cel.checker.CheckerEnv.getObjectWellKnownType)7 CheckerEnv.isObjectWellKnownType (org.projectnessie.cel.checker.CheckerEnv.isObjectWellKnownType)7 PrimitiveType (com.google.api.expr.v1alpha1.Type.PrimitiveType)6 Value (com.google.protobuf.Value)6 StatusRuntimeException (io.grpc.StatusRuntimeException)6 Test (org.junit.jupiter.api.Test)6