Permalink
Browse files

fix: function calls without parameters do not require parentheses

According to the JDBC documentation, the parentheses surrounding
the parameters, of a function call, are optional if there are no
parameters. However, Parser required the parentheses to be present at
all times. The new behavior is that the Parser checks whether there are
parentheses, and if not simply adds them. Also, tests have been added
for both cases, function calls to functions with no parameters with and
without parentheses.

This allows to execute function calls like this:

    {?= call somefunction }

Which is line with the JDBC documentation.

The tracking bug is #767.
  • Loading branch information...
RobertZenz authored and vlsi committed Mar 23, 2017
1 parent 4ab5ccb commit 2958cc22a6a00609bfbb6eb17d4e401dfe433123
@@ -921,14 +921,16 @@ public static JdbcCallParseInfo modifyJdbcCall(String jdbcSql, boolean stdString
}
String s = jdbcSql.substring(startIndex, endIndex);
StringBuilder sb = new StringBuilder(s);
if (outParmBeforeFunc) {
int opening = s.indexOf('(') + 1;
if (opening == 0) {
sb.append(outParmBeforeFunc ? "(?)" : "()");
} else if (outParmBeforeFunc) {
// move the single out parameter into the function call
// so that it can be treated like all other parameters
boolean needComma = false;
// have to use String.indexOf for java 2
int opening = s.indexOf('(') + 1;
int closing = s.indexOf(')');
int closing = s.indexOf(')', opening);
for (int j = opening; j < closing; j++) {
if (!Character.isWhitespace(sb.charAt(j))) {
needComma = true;
@@ -10,6 +10,7 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.postgresql.test.TestUtil;
import org.postgresql.test.jdbc2.BaseTest4;
import org.postgresql.util.PSQLState;
@@ -45,6 +46,10 @@ public void setUp() throws Exception {
"CREATE OR REPLACE FUNCTION myiofunc(a INOUT int, b OUT int) AS 'BEGIN b := a; a := 1; END;' LANGUAGE plpgsql");
stmt.execute(
"CREATE OR REPLACE FUNCTION myif(a INOUT int, b IN int) AS 'BEGIN a := b; END;' LANGUAGE plpgsql");
stmt.execute(
"CREATE OR REPLACE FUNCTION mynoparams() returns int AS 'BEGIN return 733; END;' LANGUAGE plpgsql");
stmt.execute(
"CREATE OR REPLACE FUNCTION mynoparamsproc() returns void AS 'BEGIN NULL; END;' LANGUAGE plpgsql");
stmt.execute("create or replace function "
+ "Numeric_Proc( OUT IMAX NUMERIC(30,15), OUT IMIN NUMERIC(30,15), OUT INUL NUMERIC(30,15)) as "
@@ -100,6 +105,8 @@ public void tearDown() throws SQLException {
stmt.execute("drop function mysum(a int, b int)");
stmt.execute("drop function myiofunc(a INOUT int, b OUT int) ");
stmt.execute("drop function myif(a INOUT int, b IN int)");
stmt.execute("drop function mynoparams()");
stmt.execute("drop function mynoparamsproc()");
stmt.close();
super.tearDown();
}
@@ -1008,4 +1015,40 @@ public void testSum() throws SQLException {
assertEquals("2+3 should be 5 when executed via {?= call mysum(?, ?)}", 5, cs.getInt(1));
}
@Test
public void testFunctionNoParametersWithParentheses() throws SQLException {
assumeCallableStatementsSupported();
CallableStatement cs = con.prepareCall("{?= call mynoparams()}");
cs.registerOutParameter(1, Types.INTEGER);
cs.execute();
assertEquals("{?= call mynoparam()} should return 733, but did not.", 733, cs.getInt(1));
TestUtil.closeQuietly(cs);
}
@Test
public void testFunctionNoParametersWithoutParentheses() throws SQLException {
assumeCallableStatementsSupported();
CallableStatement cs = con.prepareCall("{?= call mynoparams}");
cs.registerOutParameter(1, Types.INTEGER);
cs.execute();
assertEquals("{?= call mynoparam()} should return 733, but did not.", 733, cs.getInt(1));
TestUtil.closeQuietly(cs);
}
@Test
public void testProcedureNoParametersWithParentheses() throws SQLException {
assumeCallableStatementsSupported();
CallableStatement cs = con.prepareCall("{ call mynoparamsproc()}");
cs.execute();
TestUtil.closeQuietly(cs);
}
@Test
public void testProcedureNoParametersWithoutParentheses() throws SQLException {
assumeCallableStatementsSupported();
CallableStatement cs = con.prepareCall("{ call mynoparamsproc}");
cs.execute();
TestUtil.closeQuietly(cs);
}
}

0 comments on commit 2958cc2

Please sign in to comment.