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();
}
}
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());
}
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);
}
}
}
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);
}
}
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);
}
}
}
Aggregations