Skip to content

Commit 516ea8a

Browse files
committed
fixes #842
1 parent c481ce0 commit 516ea8a

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3017,13 +3017,22 @@ JsonExpression JsonExpression() : {
30173017

30183018
IntervalExpression IntervalExpression() : {
30193019
IntervalExpression interval;
3020-
Token token;
3020+
Token token = null;
3021+
Expression expr = null;
30213022
boolean signed = false;
30223023
}
30233024
{
3024-
{ interval = new IntervalExpression(); }
3025-
<K_INTERVAL> ["-" {signed=true;}] (token=<S_LONG> | token=<S_DOUBLE> | token=<S_CHAR_LITERAL> )
3026-
{ interval.setParameter((signed?"-":"") + token.image); }
3025+
3026+
{ interval = new IntervalExpression(); }
3027+
<K_INTERVAL> ["-" {signed=true;}] (token=<S_LONG> | token=<S_DOUBLE> | token=<S_CHAR_LITERAL> | expr = Column())
3028+
{
3029+
if (expr != null) {
3030+
if (signed) expr = new SignedExpression('-', expr);
3031+
interval.setExpression(expr);
3032+
} else {
3033+
interval.setParameter((signed?"-":"") + token.image);
3034+
}
3035+
}
30273036
[ LOOKAHEAD(2) (token = <S_IDENTIFIER> | token = <K_DATE_LITERAL>) { interval.setIntervalType(token.image); } ]
30283037
{
30293038
return interval;

src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3769,9 +3769,27 @@ public void testArrayIssue489() throws JSQLParserException {
37693769
public void testArrayIssue377() throws JSQLParserException {
37703770
assertSqlCanBeParsedAndDeparsed("select 'yelp'::name as pktable_cat, n2.nspname as pktable_schem, c2.relname as pktable_name, a2.attname as pkcolumn_name, 'yelp'::name as fktable_cat, n1.nspname as fktable_schem, c1.relname as fktable_name, a1.attname as fkcolumn_name, i::int2 as key_seq, case ref.confupdtype when 'c' then 0::int2 when 'n' then 2::int2 when 'd' then 4::int2 when 'r' then 1::int2 else 3::int2 end as update_rule, case ref.confdeltype when 'c' then 0::int2 when 'n' then 2::int2 when 'd' then 4::int2 when 'r' then 1::int2 else 3::int2 end as delete_rule, ref.conname as fk_name, cn.conname as pk_name, case when ref.condeferrable then case when ref.condeferred then 5::int2 else 6::int2 end else 7::int2 end as deferrablity from ((((((( (select cn.oid, conrelid, conkey, confrelid, confkey, generate_series(array_lower(conkey, 1), array_upper(conkey, 1)) as i, confupdtype, confdeltype, conname, condeferrable, condeferred from pg_catalog.pg_constraint cn, pg_catalog.pg_class c, pg_catalog.pg_namespace n where contype = 'f' and conrelid = c.oid and relname = 'business' and n.oid = c.relnamespace and n.nspname = 'public' ) ref inner join pg_catalog.pg_class c1 on c1.oid = ref.conrelid) inner join pg_catalog.pg_namespace n1 on n1.oid = c1.relnamespace) inner join pg_catalog.pg_attribute a1 on a1.attrelid = c1.oid and a1.attnum = conkey[i]) inner join pg_catalog.pg_class c2 on c2.oid = ref.confrelid) inner join pg_catalog.pg_namespace n2 on n2.oid = c2.relnamespace) inner join pg_catalog.pg_attribute a2 on a2.attrelid = c2.oid and a2.attnum = confkey[i]) left outer join pg_catalog.pg_constraint cn on cn.conrelid = ref.confrelid and cn.contype = 'p') order by ref.oid, ref.i", true);
37713771
}
3772-
3772+
37733773
@Test
37743774
public void testArrayIssue378() throws JSQLParserException {
37753775
assertSqlCanBeParsedAndDeparsed("select ta.attname, ia.attnum, ic.relname, n.nspname, tc.relname from pg_catalog.pg_attribute ta, pg_catalog.pg_attribute ia, pg_catalog.pg_class tc, pg_catalog.pg_index i, pg_catalog.pg_namespace n, pg_catalog.pg_class ic where tc.relname = 'business' and n.nspname = 'public' and tc.oid = i.indrelid and n.oid = tc.relnamespace and i.indisprimary = 't' and ia.attrelid = i.indexrelid and ta.attrelid = i.indrelid and ta.attnum = i.indkey[ia.attnum-1] and (not ta.attisdropped) and (not ia.attisdropped) and ic.oid = i.indexrelid order by ia.attnum", true);
3776-
}
3776+
}
3777+
3778+
@Test
3779+
public void testIssue842() throws JSQLParserException {
3780+
assertSqlCanBeParsedAndDeparsed("SELECT a.id lendId, "
3781+
+ "a.lend_code lendCode, "
3782+
+ "a.amount, "
3783+
+ "a.remaining_principal remainingPrincipal, "
3784+
+ "a.interest_rate interestRate, "
3785+
+ "date_add(a.lend_time, INTERVAL a.repayment_period DAY) lendEndTime, "
3786+
+ "a.lend_time lendTime "
3787+
+ "FROM risk_lend a "
3788+
+ "WHERE a.loan_id = 1", true);
3789+
}
3790+
3791+
@Test
3792+
public void testIssue842_2() throws JSQLParserException {
3793+
assertSqlCanBeParsedAndDeparsed("SELECT INTERVAL a.repayment_period DAY");
3794+
}
37773795
}

0 commit comments

Comments
 (0)