Search in sources :

Example 61 with Alias

use of org.h2.expression.Alias in project h2database by h2database.

the class TestFunctions method testSource.

private void testSource() throws SQLException {
    Connection conn = getConnection("functions");
    Statement stat = conn.createStatement();
    ResultSet rs;
    stat.execute("create force alias sayHi as 'String test(String name) {\n" + "return \"Hello \" + name;\n}'");
    rs = stat.executeQuery("SELECT ALIAS_NAME " + "FROM INFORMATION_SCHEMA.FUNCTION_ALIASES");
    rs.next();
    assertEquals("SAY" + "HI", rs.getString(1));
    rs = stat.executeQuery("call sayHi('Joe')");
    rs.next();
    assertEquals("Hello Joe", rs.getString(1));
    if (!config.memory) {
        conn.close();
        conn = getConnection("functions");
        stat = conn.createStatement();
        rs = stat.executeQuery("call sayHi('Joe')");
        rs.next();
        assertEquals("Hello Joe", rs.getString(1));
    }
    stat.execute("drop alias sayHi");
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) CallableStatement(java.sql.CallableStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SimpleResultSet(org.h2.tools.SimpleResultSet)

Example 62 with Alias

use of org.h2.expression.Alias in project h2database by h2database.

the class TestFunctions method testUUID.

private void testUUID() throws SQLException {
    Connection conn = getConnection("functions");
    Statement stat = conn.createStatement();
    ResultSet rs;
    stat.execute("create alias xorUUID for \"" + getClass().getName() + ".xorUUID\"");
    setCount(0);
    rs = stat.executeQuery("call xorUUID(random_uuid(), random_uuid())");
    rs.next();
    Object o = rs.getObject(1);
    assertEquals(UUID.class.toString(), o.getClass().toString());
    stat.execute("drop alias xorUUID");
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) CallableStatement(java.sql.CallableStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SimpleResultSet(org.h2.tools.SimpleResultSet) UUID(java.util.UUID)

Example 63 with Alias

use of org.h2.expression.Alias in project h2database by h2database.

the class TestFunctions method testSchemaSearchPath.

private void testSchemaSearchPath() throws SQLException {
    deleteDb("functions");
    Connection conn = getConnection("functions");
    Statement stat = conn.createStatement();
    ResultSet rs;
    stat.execute("CREATE SCHEMA TEST");
    stat.execute("SET SCHEMA TEST");
    stat.execute("CREATE ALIAS PARSE_INT2 FOR " + "\"java.lang.Integer.parseInt(java.lang.String, int)\";");
    rs = stat.executeQuery("SELECT ALIAS_NAME FROM " + "INFORMATION_SCHEMA.FUNCTION_ALIASES WHERE ALIAS_SCHEMA ='TEST'");
    rs.next();
    assertEquals("PARSE_INT2", rs.getString(1));
    stat.execute("DROP ALIAS PARSE_INT2");
    stat.execute("SET SCHEMA PUBLIC");
    stat.execute("CREATE ALIAS TEST.PARSE_INT2 FOR " + "\"java.lang.Integer.parseInt(java.lang.String, int)\";");
    stat.execute("SET SCHEMA_SEARCH_PATH PUBLIC, TEST");
    rs = stat.executeQuery("CALL PARSE_INT2('-FF', 16)");
    rs.next();
    assertEquals(-255, rs.getInt(1));
    rs = stat.executeQuery("SELECT ALIAS_NAME FROM " + "INFORMATION_SCHEMA.FUNCTION_ALIASES WHERE ALIAS_SCHEMA ='TEST'");
    rs.next();
    assertEquals("PARSE_INT2", rs.getString(1));
    rs = stat.executeQuery("CALL TEST.PARSE_INT2('-2147483648', 10)");
    rs.next();
    assertEquals(-2147483648, rs.getInt(1));
    rs = stat.executeQuery("CALL FUNCTIONS.TEST.PARSE_INT2('-2147483648', 10)");
    rs.next();
    assertEquals(-2147483648, rs.getInt(1));
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) CallableStatement(java.sql.CallableStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SimpleResultSet(org.h2.tools.SimpleResultSet)

Example 64 with Alias

use of org.h2.expression.Alias in project h2database by h2database.

the class TestFunctions method testDeterministic.

private void testDeterministic() throws SQLException {
    Connection conn = getConnection("functions");
    Statement stat = conn.createStatement();
    ResultSet rs;
    stat.execute("create alias getCount for \"" + getClass().getName() + ".getCount\"");
    setCount(0);
    rs = stat.executeQuery("select getCount() from system_range(1, 2)");
    rs.next();
    assertEquals(0, rs.getInt(1));
    rs.next();
    assertEquals(1, rs.getInt(1));
    stat.execute("drop alias getCount");
    stat.execute("create alias getCount deterministic for \"" + getClass().getName() + ".getCount\"");
    setCount(0);
    rs = stat.executeQuery("select getCount() from system_range(1, 2)");
    rs.next();
    assertEquals(0, rs.getInt(1));
    rs.next();
    assertEquals(0, rs.getInt(1));
    stat.execute("drop alias getCount");
    rs = stat.executeQuery("SELECT * FROM " + "INFORMATION_SCHEMA.FUNCTION_ALIASES " + "WHERE UPPER(ALIAS_NAME) = 'GET' || 'COUNT'");
    assertFalse(rs.next());
    stat.execute("create alias reverse deterministic for \"" + getClass().getName() + ".reverse\"");
    rs = stat.executeQuery("select reverse(x) from system_range(700, 700)");
    rs.next();
    assertEquals("007", rs.getString(1));
    stat.execute("drop alias reverse");
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) CallableStatement(java.sql.CallableStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SimpleResultSet(org.h2.tools.SimpleResultSet)

Example 65 with Alias

use of org.h2.expression.Alias in project h2database by h2database.

the class TestFunctions method testFunctionTableVarArgs.

private void testFunctionTableVarArgs() throws SQLException {
    Connection conn = getConnection("functions");
    Statement stat = conn.createStatement();
    stat.execute("create alias varargs_function_table for \"" + TestFunctions.class.getName() + ".varArgsFunctionTable\"");
    ResultSet rs = stat.executeQuery("select * from varargs_function_table(1,2,3,5,8,13)");
    for (int i : new int[] { 1, 2, 3, 5, 8, 13 }) {
        assertTrue(rs.next());
        assertEquals(i, rs.getInt(1));
    }
    assertFalse(rs.next());
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) CallableStatement(java.sql.CallableStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SimpleResultSet(org.h2.tools.SimpleResultSet)

Aggregations

ResultSet (java.sql.ResultSet)38 Statement (java.sql.Statement)38 Connection (java.sql.Connection)31 SimpleResultSet (org.h2.tools.SimpleResultSet)31 PreparedStatement (java.sql.PreparedStatement)28 CallableStatement (java.sql.CallableStatement)18 ValueString (org.h2.value.ValueString)17 SQLException (java.sql.SQLException)11 Column (org.h2.table.Column)10 GridSqlColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn)6 Expression (org.h2.expression.Expression)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 GridSqlAlias (org.apache.ignite.internal.processors.query.h2.sql.GridSqlAlias)5 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)4 GridSqlElement (org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement)4 GridSqlSelect (org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect)4 GridSqlTable (org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable)4 Session (org.h2.engine.Session)4 ValueExpression (org.h2.expression.ValueExpression)4