Search in sources :

Example 71 with Array

use of java.sql.Array in project adempiere by adempiere.

the class CalculateReplenishPlan method getSqlArray.

/**
	 * @param data
	 * @param sqlTypeName
	 * @param conn
	 * @return
	 * @throws SQLException
	 */
private Array getSqlArray(Object[] data, String sqlTypeName, Connection conn) throws SQLException {
    Array array;
    if (conn instanceof C3P0ProxyConnection) {
        C3P0ProxyConnection proxy = (C3P0ProxyConnection) conn;
        try {
            Method m = Connection.class.getMethod("createArrayOf", String.class, Object[].class);
            Object[] args = { sqlTypeName, data };
            array = (Array) proxy.rawConnectionOperation(m, C3P0ProxyConnection.RAW_CONNECTION, args);
        } catch (Exception e) {
            throw new SQLException(e);
        }
    } else {
        array = conn.createArrayOf(sqlTypeName, data);
    }
    return array;
}
Also used : Array(java.sql.Array) C3P0ProxyConnection(com.mchange.v2.c3p0.C3P0ProxyConnection) SQLException(java.sql.SQLException) Method(java.lang.reflect.Method) SQLException(java.sql.SQLException) AdempiereException(org.adempiere.exceptions.AdempiereException)

Example 72 with Array

use of java.sql.Array in project phoenix by apache.

the class StatsCollectorIT method testUpdateStats.

@Test
public void testUpdateStats() throws SQLException, IOException, InterruptedException {
    Connection conn;
    PreparedStatement stmt;
    ResultSet rs;
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    conn = getConnection();
    conn.createStatement().execute("CREATE TABLE " + fullTableName + " ( k VARCHAR, a_string_array VARCHAR(100) ARRAY[4], b_string_array VARCHAR(100) ARRAY[4] \n" + " CONSTRAINT pk PRIMARY KEY (k, b_string_array DESC))" + tableDDLOptions);
    String[] s;
    Array array;
    conn = upsertValues(props, fullTableName);
    // CAll the update statistics query here. If already major compaction has run this will not get executed.
    stmt = conn.prepareStatement("UPDATE STATISTICS " + fullTableName);
    stmt.execute();
    stmt = upsertStmt(conn, fullTableName);
    stmt.setString(1, "z");
    s = new String[] { "xyz", "def", "ghi", "jkll", null, null, "xxx" };
    array = conn.createArrayOf("VARCHAR", s);
    stmt.setArray(2, array);
    s = new String[] { "zya", "def", "ghi", "jkll", null, null, null, "xxx" };
    array = conn.createArrayOf("VARCHAR", s);
    stmt.setArray(3, array);
    stmt.execute();
    stmt = conn.prepareStatement("UPDATE STATISTICS " + fullTableName);
    stmt.execute();
    rs = conn.createStatement().executeQuery("SELECT k FROM " + fullTableName);
    assertTrue(rs.next());
    conn.close();
}
Also used : Array(java.sql.Array) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties) Test(org.junit.Test)

Example 73 with Array

use of java.sql.Array in project phoenix by apache.

the class StringToArrayConverterTest method testToArray_SingleElement.

@Test
public void testToArray_SingleElement() throws SQLException {
    Array singleElementArray = converter.toArray("value");
    assertArrayEquals(new Object[] { "value" }, (Object[]) singleElementArray.getArray());
}
Also used : Array(java.sql.Array) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest) Test(org.junit.Test)

Example 74 with Array

use of java.sql.Array in project phoenix by apache.

the class ArrayIT method testPKWithDescArray.

@Test
public void testPKWithDescArray() throws Exception {
    Connection conn;
    PreparedStatement stmt;
    ResultSet rs;
    long ts = nextTimestamp();
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 10));
    conn = DriverManager.getConnection(getUrl(), props);
    conn.createStatement().execute("CREATE TABLE t ( a VARCHAR ARRAY PRIMARY KEY DESC)\n");
    conn.close();
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 30));
    conn = DriverManager.getConnection(getUrl(), props);
    stmt = conn.prepareStatement("UPSERT INTO t VALUES(?)");
    Array a1 = conn.createArrayOf("VARCHAR", new String[] { "a", "ba" });
    stmt.setArray(1, a1);
    stmt.execute();
    Array a2 = conn.createArrayOf("VARCHAR", new String[] { "a", "c" });
    stmt.setArray(1, a2);
    stmt.execute();
    conn.commit();
    conn.close();
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 40));
    conn = DriverManager.getConnection(getUrl(), props);
    rs = conn.createStatement().executeQuery("SELECT a FROM t ORDER BY a DESC");
    assertTrue(rs.next());
    assertEquals(a2, rs.getArray(1));
    assertTrue(rs.next());
    assertEquals(a1, rs.getArray(1));
    assertFalse(rs.next());
    conn.close();
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 50));
    conn = DriverManager.getConnection(getUrl(), props);
    stmt = conn.prepareStatement("UPSERT INTO t VALUES(?)");
    Array a3 = conn.createArrayOf("VARCHAR", new String[] { "a", "b" });
    stmt.setArray(1, a3);
    stmt.execute();
    conn.commit();
    conn.close();
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 60));
    conn = DriverManager.getConnection(getUrl(), props);
    rs = conn.createStatement().executeQuery("SELECT a FROM t ORDER BY a DESC");
    assertTrue(rs.next());
    assertEquals(a2, rs.getArray(1));
    assertTrue(rs.next());
    assertEquals(a1, rs.getArray(1));
    assertTrue(rs.next());
    assertEquals(a3, rs.getArray(1));
    assertFalse(rs.next());
    conn.close();
}
Also used : Array(java.sql.Array) PhoenixArray(org.apache.phoenix.schema.types.PhoenixArray) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties) BaseTest(org.apache.phoenix.query.BaseTest) Test(org.junit.Test)

Example 75 with Array

use of java.sql.Array in project phoenix by apache.

the class ArrayIT method testVarLengthArrComparisonWithNulls.

@Test
public void testVarLengthArrComparisonWithNulls() throws Exception {
    Connection conn;
    PreparedStatement stmt;
    ResultSet rs;
    long ts = nextTimestamp();
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 10));
    conn = DriverManager.getConnection(getUrl(), props);
    conn.createStatement().execute("CREATE TABLE t ( k VARCHAR PRIMARY KEY, a_string_array VARCHAR(100) ARRAY[4], b_string_array VARCHAR(100) ARRAY[4])");
    conn.close();
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 30));
    conn = DriverManager.getConnection(getUrl(), props);
    stmt = conn.prepareStatement("UPSERT INTO t VALUES(?,?,?)");
    stmt.setString(1, "a");
    String[] s = new String[] { "abc", "def", "ghi", "jkll", null, null, "xxx" };
    Array array = conn.createArrayOf("VARCHAR", s);
    stmt.setArray(2, array);
    s = new String[] { "abc", "def", "ghi", "jkll", null, null, null, "xxx" };
    array = conn.createArrayOf("VARCHAR", s);
    stmt.setArray(3, array);
    stmt.execute();
    conn.commit();
    conn.close();
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 40));
    conn = DriverManager.getConnection(getUrl(), props);
    rs = conn.createStatement().executeQuery("SELECT k, a_string_array[2] FROM t where a_string_array>b_string_array");
    assertTrue(rs.next());
    assertEquals("a", rs.getString(1));
    assertEquals("def", rs.getString(2));
    conn.close();
}
Also used : Array(java.sql.Array) PhoenixArray(org.apache.phoenix.schema.types.PhoenixArray) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties) BaseTest(org.apache.phoenix.query.BaseTest) Test(org.junit.Test)

Aggregations

Array (java.sql.Array)124 Test (org.junit.Test)85 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)24 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