Search in sources :

Example 41 with Type

use of com.google.spanner.v1.Type in project pgadapter by GoogleCloudPlatform.

the class CopyStatement method queryInformationSchema.

private void queryInformationSchema() {
    Map<String, TypeCode> tableColumns = new LinkedHashMap<>();
    Statement statement = Statement.newBuilder("SELECT " + COLUMN_NAME + ", " + DATA_TYPE + " FROM information_schema.columns WHERE table_name = $1").bind("p1").to(getTableName()).build();
    try (ResultSet result = connection.executeQuery(statement)) {
        while (result.next()) {
            String columnName = result.getString(COLUMN_NAME);
            TypeCode type = parsePostgreSQLDataType(result.getString(DATA_TYPE));
            tableColumns.put(columnName, type);
        }
    }
    if (tableColumns.isEmpty()) {
        throw SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "Table " + getTableName() + " is not found in information_schema");
    }
    this.tableColumns = tableColumns;
    if (options.getColumnNames() != null) {
        verifyCopyColumns();
    }
    this.indexedColumnsCount = queryIndexedColumnsCount(tableColumns.keySet());
}
Also used : TypeCode(com.google.spanner.v1.TypeCode) ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Statement(com.google.cloud.spanner.Statement) ResultSet(com.google.cloud.spanner.ResultSet) LinkedHashMap(java.util.LinkedHashMap)

Example 42 with Type

use of com.google.spanner.v1.Type in project pgadapter by GoogleCloudPlatform.

the class JdbcSimpleModeMockServerTest method testQueryWithParameters.

@Test
public void testQueryWithParameters() throws SQLException {
    // Query parameters are not supported by the PG wire protocol in the simple query mode. The JDBC
    // driver will therefore convert parameters to literals before sending them to PostgreSQL.
    // The bytea data type is not supported for that (by the PG JDBC driver).
    // Also, the JDBC driver always uses the default timezone of the JVM when setting a timestamp.
    // This is a requirement in the JDBC API (and one that causes about a trillion confusions per
    // year). So we need to extract that from the env in order to determine what the timestamp
    // string will be.
    OffsetDateTime zonedDateTime = LocalDateTime.of(2022, 2, 16, 13, 18, 2, 123456789).atOffset(ZoneOffset.UTC);
    String timestampString = new TimestampUtils(false, TimeZone::getDefault).timeToString(java.sql.Timestamp.from(Instant.from(zonedDateTime)), true);
    String pgSql = "select col_bigint, col_bool, col_bytea, col_float8, col_numeric, col_timestamptz, col_varchar " + "from all_types " + "where col_bigint=1 " + "and col_bool='TRUE' " + "and col_float8=3.14 " + "and col_numeric=6.626 " + String.format("and col_timestamptz='%s' ", timestampString) + "and col_varchar='test'";
    String jdbcSql = "select col_bigint, col_bool, col_bytea, col_float8, col_numeric, col_timestamptz, col_varchar " + "from all_types " + "where col_bigint=? " + "and col_bool=? " + "and col_float8=? " + "and col_numeric=? " + "and col_timestamptz=? " + "and col_varchar=?";
    mockSpanner.putStatementResult(StatementResult.query(com.google.cloud.spanner.Statement.of(pgSql), ALL_TYPES_RESULTSET));
    try (Connection connection = DriverManager.getConnection(createUrl())) {
        try (PreparedStatement preparedStatement = connection.prepareStatement(jdbcSql)) {
            int index = 0;
            preparedStatement.setLong(++index, 1L);
            preparedStatement.setBoolean(++index, true);
            preparedStatement.setDouble(++index, 3.14d);
            preparedStatement.setBigDecimal(++index, new BigDecimal("6.626"));
            preparedStatement.setTimestamp(++index, java.sql.Timestamp.from(Instant.from(zonedDateTime)));
            preparedStatement.setString(++index, "test");
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                assertTrue(resultSet.next());
                assertEquals(1L, resultSet.getLong(1));
                assertFalse(resultSet.next());
            }
        }
    }
    // The statement is sent only once to the mock server in simple query mode.
    assertEquals(1, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class));
    ExecuteSqlRequest request = mockSpanner.getRequestsOfType(ExecuteSqlRequest.class).get(0);
    assertEquals(QueryMode.NORMAL, request.getQueryMode());
    assertEquals(pgSql, request.getSql());
    assertTrue(request.getTransaction().hasSingleUse());
    assertTrue(request.getTransaction().getSingleUse().hasReadOnly());
}
Also used : ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) OffsetDateTime(java.time.OffsetDateTime) TimestampUtils(org.postgresql.jdbc.TimestampUtils) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 43 with Type

use of com.google.spanner.v1.Type in project java-spanner-jdbc by googleapis.

the class PgNumericPreparedStatementTest method assertRequestWithScalar.

private void assertRequestWithScalar(String value) {
    final ExecuteSqlRequest request = mockSpanner.getRequestsOfType(ExecuteSqlRequest.class).get(0);
    final String actualSql = request.getSql();
    final Struct actualParams = request.getParams();
    final Map<String, Type> actualParamTypes = request.getParamTypesMap();
    final Value parameterValue = protoValueFromString(value);
    final Struct expectedParams = Struct.newBuilder().putFields("p1", parameterValue).build();
    final ImmutableMap<String, Type> expectedTypes = ImmutableMap.of("p1", Type.newBuilder().setCode(TypeCode.NUMERIC).setTypeAnnotation(TypeAnnotationCode.PG_NUMERIC).build());
    assertEquals(REWRITTEN_QUERY, actualSql);
    assertEquals(expectedParams, actualParams);
    assertEquals(expectedTypes, actualParamTypes);
}
Also used : Type(com.google.spanner.v1.Type) ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) NullValue(com.google.protobuf.NullValue) Value(com.google.protobuf.Value) ListValue(com.google.protobuf.ListValue) Struct(com.google.protobuf.Struct)

Example 44 with Type

use of com.google.spanner.v1.Type in project java-spanner-jdbc by googleapis.

the class PgNumericPreparedStatementTest method assertRequestWithArray.

private void assertRequestWithArray(Iterable<String> value) {
    final ExecuteSqlRequest request = mockSpanner.getRequestsOfType(ExecuteSqlRequest.class).get(0);
    final String actualSql = request.getSql();
    final Struct actualParams = request.getParams();
    final Map<String, Type> actualParamTypes = request.getParamTypesMap();
    Value parameterValue;
    if (value != null) {
        final ListValue.Builder builder = ListValue.newBuilder();
        value.forEach(v -> builder.addValues(protoValueFromString(v)));
        parameterValue = Value.newBuilder().setListValue(builder.build()).build();
    } else {
        parameterValue = Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
    }
    final Struct expectedParams = Struct.newBuilder().putFields("p1", parameterValue).build();
    final ImmutableMap<String, Type> expectedTypes = ImmutableMap.of("p1", Type.newBuilder().setCode(TypeCode.ARRAY).setArrayElementType(Type.newBuilder().setCode(TypeCode.NUMERIC).setTypeAnnotation(TypeAnnotationCode.PG_NUMERIC)).build());
    assertEquals(REWRITTEN_QUERY, actualSql);
    assertEquals(expectedParams, actualParams);
    assertEquals(expectedTypes, actualParamTypes);
}
Also used : Type(com.google.spanner.v1.Type) ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) ListValue(com.google.protobuf.ListValue) NullValue(com.google.protobuf.NullValue) Value(com.google.protobuf.Value) ListValue(com.google.protobuf.ListValue) Struct(com.google.protobuf.Struct)

Example 45 with Type

use of com.google.spanner.v1.Type in project jodd by oblac.

the class BigClassTest method testAllFeatures.

@Test
public void testAllFeatures() throws IOException, IllegalAccessException, InstantiationException {
    StatCounter.counter = 0;
    final MutableBoolean firstTime = new MutableBoolean(true);
    ProxyAspect aspect = new ProxyAspect(StatCounterAdvice.class, new ProxyPointcutSupport() {

        public boolean apply(MethodInfo mi) {
            if (firstTime.value) {
                firstTime.value = false;
                ClassInfo ci = mi.getClassInfo();
                assertEquals("BigFatJoe", ci.getClassname());
                assertEquals(BigFatJoe.class.getPackage().getName(), ci.getPackage());
                assertEquals("jodd/proxetta/data/BigFatJoe", ci.getReference());
                assertEquals("jodd/proxetta/data/SmallSkinnyZoe", ci.getSuperName());
                AnnotationInfo[] anns = ci.getAnnotations();
                assertNotNull(anns);
                assertEquals(3, anns.length);
                AnnotationInfo ai = anns[0];
                assertSame(ai, getAnnotation(ci, MadvocAction.class));
                assertEquals(MadvocAction.class.getName(), ai.getAnnotationClassname());
                assertEquals("L" + MadvocAction.class.getName().replace('.', '/') + ";", ai.getAnnotationSignature());
                assertEquals("madvocAction", ai.getElement("value"));
                ai = anns[1];
                assertSame(ai, getAnnotation(ci, PetiteBean.class));
                assertEquals(PetiteBean.class.getName(), ai.getAnnotationClassname());
                assertEquals("L" + PetiteBean.class.getName().replace('.', '/') + ";", ai.getAnnotationSignature());
                ai = anns[2];
                assertSame(ai, getAnnotation(ci, InterceptedBy.class));
                assertEquals(InterceptedBy.class.getName(), ai.getAnnotationClassname());
                assertEquals("L" + InterceptedBy.class.getName().replace('.', '/') + ";", ai.getAnnotationSignature());
                assertTrue(ai.getElement("value") instanceof Object[]);
                assertFalse(ai.getElement("value") instanceof String[]);
                Object c1 = ((Object[]) ai.getElement("value"))[0];
                assertEquals("Ljodd/proxetta/data/Str;", ((Type) c1).getDescriptor());
            }
            if (mi.getMethodName().equals("publicMethod")) {
                AnnotationInfo[] anns = mi.getAnnotations();
                assertNotNull(anns);
                assertEquals(3, anns.length);
                AnnotationInfo ai = anns[0];
                assertSame(ai, getAnnotation(mi, Action.class));
                assertEquals(Action.class.getName(), ai.getAnnotationClassname());
                assertEquals("value", ai.getElement("value"));
                assertEquals("alias", ai.getElement("alias"));
                ai = anns[1];
                assertSame(ai, getAnnotation(mi, PetiteInject.class));
                assertEquals(PetiteInject.class.getName(), ai.getAnnotationClassname());
                assertEquals(0, ai.getElementNames().size());
                ai = anns[2];
                assertSame(ai, getAnnotation(mi, Transaction.class));
                assertEquals(Transaction.class.getName(), ai.getAnnotationClassname());
                assertEquals(2, ai.getElementNames().size());
                String s = (String) ai.getElement("propagation");
                assertEquals("PROPAGATION_REQUIRES_NEW", s);
            }
            if (mi.getMethodName().equals("superPublicMethod")) {
                AnnotationInfo[] anns = mi.getAnnotations();
                assertNotNull(anns);
                assertEquals(3, anns.length);
                AnnotationInfo ai = anns[0];
                assertSame(ai, getAnnotation(mi, Action.class));
                assertEquals(Action.class.getName(), ai.getAnnotationClassname());
                assertEquals(0, ai.getElementNames().size());
                ai = anns[1];
                assertSame(ai, getAnnotation(mi, PetiteInject.class));
                assertEquals(PetiteInject.class.getName(), ai.getAnnotationClassname());
                assertEquals(0, ai.getElementNames().size());
                ai = anns[2];
                assertSame(ai, getAnnotation(mi, Transaction.class));
                assertEquals(Transaction.class.getName(), ai.getAnnotationClassname());
                assertEquals(0, ai.getElementNames().size());
            }
            //System.out.println(!isRootMethod(mi) + " " + mi.getDeclaredClassName() + '#' + mi.getMethodName());
            return !isRootMethod(mi);
        }
    });
    byte[] classBytes = ProxyProxetta.withAspects(aspect).builder(BigFatJoe.class).create();
    //		URL resource = BigFatJoe.class.getResource("/" + BigFatJoe.class.getName().replace(".", "/") + ".class");
    //		jodd.io.FileUtil.copy(FileUtil.toFile(resource), new java.io.File(SystemUtil.getUserHome(), "jo.class"));
    //		jodd.io.FileUtil.writeBytes(new java.io.File(SystemUtil.getUserHome(), "joe.class"), classBytes);
    Class clazz = ClassLoaderUtil.defineClass(null, classBytes);
    BigFatJoe bigFatJoe = (BigFatJoe) clazz.newInstance();
    assertEquals(BigFatJoe.class.getName() + "$$Proxetta", bigFatJoe.getClass().getName());
    assertEquals(BigFatJoe.class, ProxettaUtil.getTargetClass(bigFatJoe.getClass()));
    // test invocation
    // 2 x static + 1 x instance
    assertEquals(3, StatCounter.counter);
    bigFatJoe.publicMethod();
    assertEquals(4, StatCounter.counter);
    bigFatJoe.callInnerMethods();
    // private method is not overridden
    assertEquals(7, StatCounter.counter);
    bigFatJoe.superPublicMethod();
    assertEquals(8, StatCounter.counter);
    bigFatJoe.callInnerMethods2();
    // only public super methods are overridden
    assertEquals(9, StatCounter.counter);
    // test class annotation
    MadvocAction ma = (MadvocAction) clazz.getAnnotation(MadvocAction.class);
    assertEquals("madvocAction", ma.value());
    InterceptedBy ib = (InterceptedBy) clazz.getAnnotation(InterceptedBy.class);
    assertNotNull(ib.value());
    assertEquals(2, ib.value().length);
    // test method annotation
    ClassDescriptor cd = ClassIntrospector.lookup(clazz);
    Method m = cd.getMethodDescriptor("publicMethod", false).getMethod();
    assertNotNull(m);
    Annotation[] aa = m.getAnnotations();
    assertEquals(3, aa.length);
    Action a = (Action) aa[0];
    assertEquals("alias", a.alias());
    assertEquals("extension", a.extension());
    assertEquals("method", a.method());
    assertEquals("value", a.value());
    PetiteInject pi = (PetiteInject) aa[1];
    assertEquals("", pi.value());
    Transaction tx = (Transaction) aa[2];
    assertTrue(tx.readOnly());
    assertEquals(1000, tx.timeout());
    assertEquals("PROPAGATION_REQUIRES_NEW", tx.propagation());
    bigFatJoe.runInnerClass();
    // proxy + call
    assertEquals(11, StatCounter.counter);
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) MutableBoolean(jodd.mutable.MutableBoolean) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) Type(jodd.asm5.Type) ProxyPointcutSupport(jodd.proxetta.pointcuts.ProxyPointcutSupport) Test(org.junit.Test)

Aggregations

Type (com.google.api.expr.v1alpha1.Type)30 Test (org.junit.Test)22 Type (edu.stanford.CVC4.Type)14 ArrayList (java.util.ArrayList)14 ByteString (com.google.protobuf.ByteString)13 Type (com.google.spanner.v1.Type)12 ArrayType (edu.stanford.CVC4.ArrayType)11 BitVectorType (edu.stanford.CVC4.BitVectorType)11 Expr (edu.stanford.CVC4.Expr)11 MapType (com.google.api.expr.v1alpha1.Type.MapType)10 Type (org.apache.xbean.asm9.Type)10 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)9 CVC4.vectorExpr (edu.stanford.CVC4.vectorExpr)9 CheckedExpr (com.google.api.expr.v1alpha1.CheckedExpr)8 FieldType (org.projectnessie.cel.common.types.ref.FieldType)8 FormulaType (org.sosy_lab.java_smt.api.FormulaType)8 ListValue (com.google.protobuf.ListValue)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