Search in sources :

Example 41 with DataType

use of org.h2.mvstore.type.DataType in project h2database by h2database.

the class TestFunctions method testNvl2.

private void testNvl2() throws SQLException {
    Connection conn = getConnection("functions");
    Statement stat = conn.createStatement();
    String createSQL = "CREATE TABLE testNvl2(id BIGINT, txt1 " + "varchar, txt2 varchar, num number(9, 0));";
    stat.execute(createSQL);
    stat.execute("insert into testNvl2(id, txt1, txt2, num) " + "values(1, 'test1', 'test2', null)");
    stat.execute("insert into testNvl2(id, txt1, txt2, num) " + "values(2, null, 'test4', null)");
    stat.execute("insert into testNvl2(id, txt1, txt2, num) " + "values(3, 'test5', null, null)");
    stat.execute("insert into testNvl2(id, txt1, txt2, num) " + "values(4, null, null, null)");
    stat.execute("insert into testNvl2(id, txt1, txt2, num) " + "values(5, '2', null, 1)");
    stat.execute("insert into testNvl2(id, txt1, txt2, num) " + "values(6, '2', null, null)");
    stat.execute("insert into testNvl2(id, txt1, txt2, num) " + "values(7, 'test2', null, null)");
    String query = "SELECT NVL2(txt1, txt1, txt2), txt1 " + "FROM testNvl2 order by id asc";
    ResultSet rs = stat.executeQuery(query);
    rs.next();
    String actual = rs.getString(1);
    assertEquals("test1", actual);
    rs.next();
    actual = rs.getString(1);
    assertEquals("test4", actual);
    rs.next();
    actual = rs.getString(1);
    assertEquals("test5", actual);
    rs.next();
    actual = rs.getString(1);
    assertEquals(null, actual);
    assertEquals(rs.getMetaData().getColumnType(2), rs.getMetaData().getColumnType(1));
    rs.close();
    rs = stat.executeQuery("SELECT NVL2(num, num, txt1), num " + "FROM testNvl2 where id in(5, 6) order by id asc");
    rs.next();
    assertEquals(rs.getMetaData().getColumnType(2), rs.getMetaData().getColumnType(1));
    assertThrows(ErrorCode.DATA_CONVERSION_ERROR_1, stat).executeQuery("SELECT NVL2(num, num, txt1), num " + "FROM testNvl2 where id = 7 order by id asc");
    // nvl2 should return expr2's datatype, if expr2 is character data.
    rs = stat.executeQuery("SELECT NVL2(1, 'test', 123), 'test' FROM dual");
    rs.next();
    actual = rs.getString(1);
    assertEquals("test", actual);
    assertEquals(rs.getMetaData().getColumnType(2), rs.getMetaData().getColumnType(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)

Aggregations

Value (org.h2.value.Value)16 ValueString (org.h2.value.ValueString)13 DataType (org.h2.value.DataType)12 Expression (org.h2.expression.Expression)6 Column (org.h2.table.Column)6 Constraint (org.h2.constraint.Constraint)5 UserDataType (org.h2.engine.UserDataType)5 UserAggregate (org.h2.engine.UserAggregate)4 Index (org.h2.index.Index)4 Table (org.h2.table.Table)4 ArrayList (java.util.ArrayList)3 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)3 Mode (org.h2.engine.Mode)3 ValueExpression (org.h2.expression.ValueExpression)3 SearchRow (org.h2.result.SearchRow)3 Schema (org.h2.schema.Schema)3 SimpleResultSet (org.h2.tools.SimpleResultSet)3 StatementBuilder (org.h2.util.StatementBuilder)3 CompareMode (org.h2.value.CompareMode)3 IOException (java.io.IOException)2