Skip to content

Add support for jdbc parameters in ColDataType precision and scale #2191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -6992,6 +6992,7 @@ ColDataType DataType():
Token prefix = null;
Token tk = null;
Token tk2 = null;
JdbcParameter jdbc = null;
String schema;
String type="";
List<String> argumentsStringList = new ArrayList<String>();
Expand Down Expand Up @@ -7026,12 +7027,29 @@ ColDataType DataType():
| tk=<K_CHAR> | tk=<K_JSON> | tk=<K_STRING> ) { type += " " + tk.image; } )+
]
[
LOOKAHEAD(2) "(" ( tk=<S_LONG> { precision = Integer.valueOf(tk.image); } | tk=<K_MAX> { precision = Integer.MAX_VALUE; } )
[ "," tk = <S_LONG> { scale = Integer.valueOf(tk.image); } ]
LOOKAHEAD(2) "("
(
// Handle precision: S_LONG, K_MAX, or JDBC Parameter
tk=<S_LONG> { precision = Integer.valueOf(tk.image); }
| tk=<K_MAX> { precision = Integer.MAX_VALUE; }
| jdbc = JdbcParameter() { precision = -1; argumentsStringList.add("?"); }
)
[
","
(
tk = <S_LONG> { scale = Integer.valueOf(tk.image); }
| jdbc = JdbcParameter() { scale = -1; argumentsStringList.add("?"); }
)
]
")"
]
{
colDataType = new ColDataType(type, precision, scale);
// If JDBC parameters were used, construct with string arguments
if (!argumentsStringList.isEmpty()) {
colDataType = new ColDataType(type + "(" + String.join(", ", argumentsStringList) + ")");
} else {
colDataType = new ColDataType(type, precision, scale);
}
}
)
)
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5343,6 +5343,37 @@ public void testMissingBracketsNestedInIssue() throws JSQLParserException {
true);
}

@Test
public void testJdbcParameterInColDataTypePrecision() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed(
"SELECT price::VARCHAR(?) FROM products WHERE quantity = 50;",
true);
}

@Test
public void testJdbcParameterInColDataTypePrecisionAndScale() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed(
"SELECT price::DECIMAL(?, ?) FROM products WHERE quantity = 50;",
true);
}

@Test
public void testPrecisionJdbcParameterWithWhitespaces() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed(
"SELECT price :: numeric(?) FROM products WHERE quantity = 50;",
true);
}

@Test
public void testCastWithPrecisionJdbcParameter() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT (CAST(? AS VARCHAR(?))) FROM products;");
}

@Test
public void testCastFunctionOutputWithPrecisionJdbcParameter() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT AVG(Measurement)::numeric(?);");
}

@Test
public void testAnyComparisionExpressionValuesList1232() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("select * from foo where id != ALL(VALUES 1,2,3)", true);
Expand Down
Loading