Search in sources :

Example 6 with Array

use of java.sql.Array in project databus by linkedin.

the class OracleAvroGenericEventFactory method getJdbcArray.

private static Array getJdbcArray(ResultSet rs, Schema schema) throws EventCreationException {
    // fails if schema isn't for array type
    Schema elementSchema = schema.getElementType();
    String dbFieldName = SchemaHelper.getMetaField(elementSchema, "dbFieldName");
    if (dbFieldName == null) {
        throw new EventCreationException("array field is missing required metadata dbFieldName. " + schema.getName());
    }
    Array array;
    try {
        array = rs.getArray(dbFieldName);
    } catch (SQLException e) {
        throw new EventCreationException("unable to read array field: " + dbFieldName + ": " + e.getMessage(), e);
    }
    return array;
}
Also used : GenericArray(org.apache.avro.generic.GenericArray) Array(java.sql.Array) SQLException(java.sql.SQLException) EventCreationException(com.linkedin.databus2.producers.EventCreationException) Schema(org.apache.avro.Schema)

Example 7 with Array

use of java.sql.Array in project mybatis-3 by mybatis.

the class SPTest method testGetNamesWithArray_a1.

/*
   * This test shows how to use the ARRAY JDBC type with MyBatis.
   * 
   * This test shows using annotations for stored procedures
   * 
   * @throws SQLException
   */
@Test
public void testGetNamesWithArray_a1() throws SQLException {
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        SPMapper spMapper = sqlSession.getMapper(SPMapper.class);
        Array array = sqlSession.getConnection().createArrayOf("int", new Integer[] { 1, 2, 5 });
        Map<String, Object> parms = new HashMap<String, Object>();
        parms.put("ids", array);
        List<Name> names = spMapper.getNamesWithArrayAnnotated(parms);
        Object[] returnedIds = (Object[]) parms.get("returnedIds");
        assertEquals(4, returnedIds.length);
        assertEquals(3, parms.get("requestedRows"));
        assertEquals(2, names.size());
    } finally {
        sqlSession.close();
    }
}
Also used : Array(java.sql.Array) SqlSession(org.apache.ibatis.session.SqlSession) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 8 with Array

use of java.sql.Array in project mybatis-3 by mybatis.

the class SPTest method testGetNamesWithArray_a2.

/*
   * This test shows how to use the ARRAY JDBC type with MyBatis.
   * 
   * This test shows using annotations for stored procedures and using a
   * resultMap in XML
   * 
   * @throws SQLException
   */
@Test
public void testGetNamesWithArray_a2() throws SQLException {
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        SPMapper spMapper = sqlSession.getMapper(SPMapper.class);
        Array array = sqlSession.getConnection().createArrayOf("int", new Integer[] { 1, 2, 5 });
        Map<String, Object> parms = new HashMap<String, Object>();
        parms.put("ids", array);
        List<Name> names = spMapper.getNamesWithArrayAnnotatedWithXMLResultMap(parms);
        Object[] returnedIds = (Object[]) parms.get("returnedIds");
        assertEquals(4, returnedIds.length);
        assertEquals(3, parms.get("requestedRows"));
        assertEquals(2, names.size());
    } finally {
        sqlSession.close();
    }
}
Also used : Array(java.sql.Array) SqlSession(org.apache.ibatis.session.SqlSession) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 9 with Array

use of java.sql.Array in project mybatis-3 by mybatis.

the class SPTest method testGetNamesWithArray.

/*
   * This test shows how to use the ARRAY JDBC type with MyBatis.
   * 
   * @throws SQLException
   */
@Test
public void testGetNamesWithArray() throws SQLException {
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        SPMapper spMapper = sqlSession.getMapper(SPMapper.class);
        Array array = sqlSession.getConnection().createArrayOf("int", new Integer[] { 1, 2, 5 });
        Map<String, Object> parms = new HashMap<String, Object>();
        parms.put("ids", array);
        List<Name> names = spMapper.getNamesWithArray(parms);
        Object[] returnedIds = (Object[]) parms.get("returnedIds");
        assertEquals(4, returnedIds.length);
        assertEquals(3, parms.get("requestedRows"));
        assertEquals(2, names.size());
    } finally {
        sqlSession.close();
    }
}
Also used : Array(java.sql.Array) SqlSession(org.apache.ibatis.session.SqlSession) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 10 with Array

use of java.sql.Array in project presto by prestodb.

the class Validator method convertJdbcResultSet.

private List<List<Object>> convertJdbcResultSet(ResultSet resultSet) throws SQLException, VerifierException {
    int rowCount = 0;
    int columnCount = resultSet.getMetaData().getColumnCount();
    ImmutableList.Builder<List<Object>> rows = ImmutableList.builder();
    while (resultSet.next()) {
        List<Object> row = new ArrayList<>();
        for (int i = 1; i <= columnCount; i++) {
            Object object = resultSet.getObject(i);
            if (object instanceof BigDecimal) {
                if (((BigDecimal) object).scale() <= 0) {
                    object = ((BigDecimal) object).longValueExact();
                } else {
                    object = ((BigDecimal) object).doubleValue();
                }
            }
            if (object instanceof Array) {
                object = ((Array) object).getArray();
            }
            if (object instanceof byte[]) {
                object = new SqlVarbinary((byte[]) object);
            }
            row.add(object);
        }
        rows.add(unmodifiableList(row));
        rowCount++;
        if (rowCount > maxRowCount) {
            throw new VerifierException("More than '" + maxRowCount + "' rows, failing query");
        }
    }
    return rows.build();
}
Also used : Array(java.sql.Array) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) SqlVarbinary(com.facebook.presto.spi.type.SqlVarbinary) Collections.unmodifiableList(java.util.Collections.unmodifiableList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) BigDecimal(java.math.BigDecimal)

Aggregations

Array (java.sql.Array)122 Test (org.junit.Test)84 Connection (java.sql.Connection)74 ResultSet (java.sql.ResultSet)69 PreparedStatement (java.sql.PreparedStatement)46 Properties (java.util.Properties)29 PhoenixArray (org.apache.phoenix.schema.types.PhoenixArray)25 ArrayList (java.util.ArrayList)23 BaseTest (org.apache.phoenix.query.BaseTest)22 SQLException (java.sql.SQLException)19 ScalarType (org.apache.calcite.avatica.ColumnMetaData.ScalarType)16 Timestamp (java.sql.Timestamp)10 Date (java.sql.Date)8 Random (java.util.Random)7 Time (java.sql.Time)6 Statement (java.sql.Statement)5 Struct (java.sql.Struct)5 HashMap (java.util.HashMap)5 List (java.util.List)5 BigDecimal (java.math.BigDecimal)4