Search in sources :

Example 46 with Type

use of com.google.api.expr.v1alpha1.Type in project pgadapter by GoogleCloudPlatform.

the class JdbcMockServerTest method testQueryWithParameters.

@Test
public void testQueryWithParameters() throws SQLException {
    String jdbcSql = "select col_bigint, col_bool, col_bytea, col_float8, col_int, col_numeric, col_timestamptz, col_date, col_varchar " + "from all_types " + "where col_bigint=? " + "and col_bool=? " + "and col_bytea=? " + "and col_int=? " + "and col_float8=? " + "and col_numeric=? " + "and col_timestamptz=? " + "and col_date=? " + "and col_varchar=?";
    String pgSql = "select col_bigint, col_bool, col_bytea, col_float8, col_int, col_numeric, col_timestamptz, col_date, col_varchar " + "from all_types " + "where col_bigint=$1 " + "and col_bool=$2 " + "and col_bytea=$3 " + "and col_int=$4 " + "and col_float8=$5 " + "and col_numeric=$6 " + "and col_timestamptz=$7 " + "and col_date=$8 " + "and col_varchar=$9";
    mockSpanner.putStatementResult(StatementResult.query(Statement.of(pgSql), ALL_TYPES_RESULTSET));
    mockSpanner.putStatementResult(StatementResult.query(Statement.newBuilder(pgSql).bind("p1").to(1L).bind("p2").to(true).bind("p3").to(ByteArray.copyFrom("test")).bind("p4").to(100).bind("p5").to(3.14d).bind("p6").to(com.google.cloud.spanner.Value.pgNumeric("6.626")).bind("p7").to(Timestamp.parseTimestamp("2022-02-16T13:18:02.123457000Z")).bind("p8").to(Date.parseDate("2022-03-29")).bind("p9").to("test").build(), ALL_TYPES_RESULTSET));
    OffsetDateTime offsetDateTime = LocalDateTime.of(2022, 2, 16, 13, 18, 2, 123456789).atOffset(ZoneOffset.UTC);
    OffsetDateTime truncatedOffsetDateTime = offsetDateTime.truncatedTo(ChronoUnit.MICROS);
    // (10 points to you if you guessed the last one up front!).
    for (int preparedThreshold : new int[] { 5, 1, 0, -1 }) {
        try (Connection connection = DriverManager.getConnection(createUrl())) {
            try (PreparedStatement preparedStatement = connection.prepareStatement(jdbcSql)) {
                preparedStatement.unwrap(PgStatement.class).setPrepareThreshold(preparedThreshold);
                int index = 0;
                preparedStatement.setLong(++index, 1L);
                preparedStatement.setBoolean(++index, true);
                preparedStatement.setBytes(++index, "test".getBytes(StandardCharsets.UTF_8));
                preparedStatement.setInt(++index, 100);
                preparedStatement.setDouble(++index, 3.14d);
                preparedStatement.setBigDecimal(++index, new BigDecimal("6.626"));
                preparedStatement.setObject(++index, offsetDateTime);
                preparedStatement.setObject(++index, LocalDate.of(2022, 3, 29));
                preparedStatement.setString(++index, "test");
                try (ResultSet resultSet = preparedStatement.executeQuery()) {
                    assertTrue(resultSet.next());
                    index = 0;
                    assertEquals(1L, resultSet.getLong(++index));
                    assertTrue(resultSet.getBoolean(++index));
                    assertArrayEquals("test".getBytes(StandardCharsets.UTF_8), resultSet.getBytes(++index));
                    assertEquals(3.14d, resultSet.getDouble(++index), 0.0d);
                    assertEquals(100, resultSet.getInt(++index));
                    assertEquals(new BigDecimal("6.626"), resultSet.getBigDecimal(++index));
                    if (preparedThreshold < 0) {
                        // The binary format will truncate the timestamp value to microseconds.
                        assertEquals(truncatedOffsetDateTime, resultSet.getObject(++index, OffsetDateTime.class));
                    } else {
                        assertEquals(offsetDateTime, resultSet.getObject(++index, OffsetDateTime.class));
                    }
                    assertEquals(LocalDate.of(2022, 3, 29), resultSet.getObject(++index, LocalDate.class));
                    assertEquals("test", resultSet.getString(++index));
                    assertFalse(resultSet.next());
                }
            }
        }
        List<ExecuteSqlRequest> requests = mockSpanner.getRequestsOfType(ExecuteSqlRequest.class);
        // Prepare threshold less than 0 means use binary transfer + DESCRIBE statement.
        assertEquals(preparedThreshold < 0 ? 2 : 1, requests.size());
        ExecuteSqlRequest executeRequest = requests.get(requests.size() - 1);
        assertEquals(QueryMode.NORMAL, executeRequest.getQueryMode());
        assertEquals(pgSql, executeRequest.getSql());
        Map<String, Value> params = executeRequest.getParams().getFieldsMap();
        Map<String, Type> types = executeRequest.getParamTypesMap();
        assertEquals(TypeCode.INT64, types.get("p1").getCode());
        assertEquals("1", params.get("p1").getStringValue());
        assertEquals(TypeCode.BOOL, types.get("p2").getCode());
        assertTrue(params.get("p2").getBoolValue());
        assertEquals(TypeCode.BYTES, types.get("p3").getCode());
        assertEquals(Base64.getEncoder().encodeToString("test".getBytes(StandardCharsets.UTF_8)), params.get("p3").getStringValue());
        assertEquals(TypeCode.INT64, types.get("p4").getCode());
        assertEquals("100", params.get("p4").getStringValue());
        assertEquals(TypeCode.FLOAT64, types.get("p5").getCode());
        assertEquals(3.14d, params.get("p5").getNumberValue(), 0.0d);
        assertEquals(TypeCode.NUMERIC, types.get("p6").getCode());
        assertEquals(TypeAnnotationCode.PG_NUMERIC, types.get("p6").getTypeAnnotation());
        assertEquals("6.626", params.get("p6").getStringValue());
        assertEquals(TypeCode.TIMESTAMP, types.get("p7").getCode());
        assertEquals("2022-02-16T13:18:02.123457000Z", params.get("p7").getStringValue());
        assertEquals(TypeCode.DATE, types.get("p8").getCode());
        assertEquals("2022-03-29", params.get("p8").getStringValue());
        assertEquals(TypeCode.STRING, types.get("p9").getCode());
        assertEquals("test", params.get("p9").getStringValue());
        mockSpanner.clearRequests();
    }
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) Type(com.google.spanner.v1.Type) PreparedType(com.google.cloud.spanner.pgadapter.wireprotocol.ControlMessage.PreparedType) ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) OffsetDateTime(java.time.OffsetDateTime) PgStatement(org.postgresql.jdbc.PgStatement) ResultSet(java.sql.ResultSet) Value(com.google.protobuf.Value) Test(org.junit.Test)

Example 47 with Type

use of com.google.api.expr.v1alpha1.Type in project tomee by apache.

the class Cmp2Generator method initCmrFields.

/**
 * Initialize the CMR fields associated with a CMR
 * definition.  This initializes two fields per CMR
 * defined field:  1)  The CMR field itself (which might
 * be initialized to an instance of a defined type) and 2)
 * the appropriate CMD accessor that handles the
 * different types of relationship.
 *
 * @param mv       The method context we're initializing in.
 * @param cmrField The CMR field to process.
 */
private void initCmrFields(final MethodVisitor mv, final CmrField cmrField) {
    // this.${cmrField.name} = new ${cmrField.initialValueType}();
    final Type initialValueType = cmrField.getInitialValueType();
    if (initialValueType != null) {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitTypeInsn(NEW, initialValueType.getInternalName());
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, initialValueType.getInternalName(), "<init>", "()V", false);
        mv.visitFieldInsn(PUTFIELD, implClassName, cmrField.getName(), cmrField.getDescriptor());
    }
    // this.${cmrField.name}Cmr = new ${cmrField.accessorType}<${cmrField.type}, ${cmrField.proxyType}>(this,
    // ${cmrField.name},
    // ${cmrField.type},
    // ${cmrField.relatedName});
    mv.visitVarInsn(ALOAD, 0);
    mv.visitTypeInsn(NEW, cmrField.getAccessorInternalName());
    mv.visitInsn(DUP);
    // arg0: EntityBean source = this
    mv.visitVarInsn(ALOAD, 0);
    // arg1: String sourceProperty - "b"
    mv.visitLdcInsn(cmrField.getName());
    // arg2: Class<Bean> relatedType = BBean_BBean
    mv.visitLdcInsn(cmrField.getType());
    // arg3: String relatedProperty
    if (cmrField.getRelatedName() != null) {
        mv.visitLdcInsn(cmrField.getRelatedName());
    } else {
        mv.visitInsn(ACONST_NULL);
    }
    // invoke
    mv.visitMethodInsn(INVOKESPECIAL, cmrField.getAccessorInternalName(), "<init>", "(Ljakarta/ejb/EntityBean;Ljava/lang/String;Ljava/lang/Class;Ljava/lang/String;)V", false);
    // bCmr = result
    mv.visitFieldInsn(PUTFIELD, implClassName, cmrField.getName() + "Cmr", cmrField.getAccessorDescriptor());
}
Also used : Type(org.apache.xbean.asm9.Type)

Example 48 with Type

use of com.google.api.expr.v1alpha1.Type in project tomee by apache.

the class KeysAnnotationVisitor method visit.

@Override
public void visit(final String name, final Object value) {
    if ("value".equals(name)) {
        if (value instanceof Type) {
            final Type type = (Type) value;
            final int sort = type.getSort();
            switch(sort) {
                case Type.OBJECT:
                    if (type.getClassName().equals(ValidationRunner.class.getName())) {
                        classInfos.add(current);
                    }
                    break;
            }
        } else {
            currentMethod.keys.add((String) value);
        }
    }
}
Also used : Type(org.apache.xbean.asm9.Type)

Example 49 with Type

use of com.google.api.expr.v1alpha1.Type in project tomee by apache.

the class JwtValidationGenerator method generateMethods.

protected void generateMethods(final ClassWriter cw) {
    for (final MethodConstraints methodConstraints : constraints) {
        final Method method = methodConstraints.getMethod();
        final String name = method.getName();
        // Declare a method of return type JsonWebToken for use with
        // a call to BeanValidation's ExecutableValidator.validateReturnValue
        final Type returnType = Type.getType(JsonWebToken.class);
        final Type[] parameterTypes = Type.getArgumentTypes(method);
        final String descriptor = Type.getMethodDescriptor(returnType, parameterTypes);
        final int access = method.isVarArgs() ? ACC_PUBLIC + ACC_VARARGS : ACC_PUBLIC;
        final MethodVisitor mv = cw.visitMethod(access, name, descriptor, null, null);
        // Put the method name on the
        final AnnotationVisitor av = mv.visitAnnotation(Type.getDescriptor(Generated.class), true);
        av.visit("value", this.getClass().getName());
        av.visitEnd();
        // track the MethodVisitor
        // We will later copy over the annotations
        generatedMethods.put(method.getName() + Type.getMethodDescriptor(method), new ConstrainedMethodVisitor(mv, methodConstraints));
        // The method will simply return null
        mv.visitCode();
        mv.visitInsn(ACONST_NULL);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(1, 1);
    }
}
Also used : Type(org.apache.xbean.asm9.Type) AnnotationVisitor(org.apache.xbean.asm9.AnnotationVisitor) Method(java.lang.reflect.Method) MethodVisitor(org.apache.xbean.asm9.MethodVisitor)

Example 50 with Type

use of com.google.api.expr.v1alpha1.Type in project tomee by apache.

the class ReturnValidationGenerator method generateMethods.

protected void generateMethods(final ClassWriter cw) {
    for (final MethodConstraints methodConstraints : constraints) {
        final Method method = methodConstraints.getMethod();
        final String name = method.getName();
        // Declare a method of return type JsonWebToken for use with
        // a call to BeanValidation's ExecutableValidator.validateReturnValue
        final Type returnType = Type.getReturnType(method);
        final Type[] parameterTypes = Type.getArgumentTypes(method);
        final String descriptor = Type.getMethodDescriptor(returnType, parameterTypes);
        final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, name, descriptor, null, null);
        // Put the method name on the
        final AnnotationVisitor av = mv.visitAnnotation(Type.getDescriptor(Generated.class), true);
        av.visit("value", this.getClass().getName());
        av.visitEnd();
        // track the MethodVisitor
        // We will later copy over the annotations
        generatedMethods.put(method.getName() + Type.getMethodDescriptor(method), new ConstrainedMethodVisitor(mv, methodConstraints));
        if (method.getReturnType().equals(Void.TYPE)) {
            mv.visitCode();
            mv.visitInsn(RETURN);
            mv.visitMaxs(0, 1);
        } else if (method.getReturnType().equals(Long.TYPE)) {
            mv.visitCode();
            mv.visitInsn(LCONST_0);
            mv.visitInsn(LRETURN);
            mv.visitMaxs(2, 4);
            mv.visitEnd();
        } else if (method.getReturnType().equals(Float.TYPE)) {
            mv.visitCode();
            mv.visitInsn(FCONST_0);
            mv.visitInsn(FRETURN);
            mv.visitMaxs(1, 3);
            mv.visitEnd();
        } else if (method.getReturnType().equals(Double.TYPE)) {
            mv.visitCode();
            mv.visitInsn(DCONST_0);
            mv.visitInsn(DRETURN);
            mv.visitMaxs(2, 4);
            mv.visitEnd();
        } else if (method.getReturnType().isPrimitive()) {
            mv.visitCode();
            mv.visitInsn(ICONST_0);
            mv.visitInsn(IRETURN);
            mv.visitMaxs(1, 3);
            mv.visitEnd();
        } else {
            // The method will simply return null
            mv.visitCode();
            mv.visitInsn(ACONST_NULL);
            mv.visitInsn(ARETURN);
            mv.visitMaxs(1, 1);
        }
    }
}
Also used : Type(org.apache.xbean.asm9.Type) AnnotationVisitor(org.apache.xbean.asm9.AnnotationVisitor) Method(java.lang.reflect.Method) MethodVisitor(org.apache.xbean.asm9.MethodVisitor)

Aggregations

Type (com.google.api.expr.v1alpha1.Type)30 Test (org.junit.Test)16 MapType (com.google.api.expr.v1alpha1.Type.MapType)15 Type (edu.stanford.CVC4.Type)14 Type (com.google.spanner.v1.Type)12 Test (org.junit.jupiter.api.Test)12 CheckedExpr (com.google.api.expr.v1alpha1.CheckedExpr)11 ByteString (com.google.protobuf.ByteString)11 ArrayType (edu.stanford.CVC4.ArrayType)11 BitVectorType (edu.stanford.CVC4.BitVectorType)11 Expr (edu.stanford.CVC4.Expr)11 ArrayList (java.util.ArrayList)11 Type (org.apache.xbean.asm9.Type)10 Expr (com.google.api.expr.v1alpha1.Expr)9 CVC4.vectorExpr (edu.stanford.CVC4.vectorExpr)9 FieldType (org.projectnessie.cel.common.types.ref.FieldType)8 ListValue (com.google.protobuf.ListValue)7 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)7 CheckerEnv.dynElementType (org.projectnessie.cel.checker.CheckerEnv.dynElementType)7 CheckerEnv.getObjectWellKnownType (org.projectnessie.cel.checker.CheckerEnv.getObjectWellKnownType)7