Skip to content

Commit 884dcaf

Browse files
committed
toString for windowing elements corrected
lax equality test implemented included oracle test sqls included @ and # for identifiers
1 parent 2f5d134 commit 884dcaf

File tree

281 files changed

+2775
-29
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

281 files changed

+2775
-29
lines changed

src/main/java/net/sf/jsqlparser/expression/WindowElement.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void setRange(WindowRange range) {
6060
@Override
6161
public String toString() {
6262
StringBuilder buffer = new StringBuilder(type.toString());
63-
63+
6464
if (offset != null) {
6565
buffer.append(offset.toString());
6666
} else if (range != null) {
@@ -69,5 +69,4 @@ public String toString() {
6969

7070
return buffer.toString();
7171
}
72-
7372
}

src/main/java/net/sf/jsqlparser/expression/WindowOffset.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void setType(Type type) {
5454
public String toString() {
5555
StringBuilder buffer = new StringBuilder();
5656
if (expression != null) {
57-
buffer.append(expression);
57+
buffer.append(' ').append(expression);
5858
if (type != null) {
5959
buffer.append(' ');
6060
buffer.append(type);

src/main/java/net/sf/jsqlparser/expression/WindowRange.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public void setStart(WindowOffset start) {
4545
@Override
4646
public String toString() {
4747
StringBuilder buffer = new StringBuilder();
48-
buffer.append(" BETWEEN ");
48+
buffer.append(" BETWEEN");
4949
buffer.append(start);
50-
buffer.append(" AND ");
50+
buffer.append(" AND");
5151
buffer.append(end);
5252
return buffer.toString();
5353
}

src/main/javacc/net/sf/jsqlparser/parser/JSqlParserCC.jj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ TOKEN:
218218
{
219219
< S_IDENTIFIER: ( <LETTER> | <ADDITIONAL_LETTERS> )+ ( <DIGIT> | <LETTER> | <ADDITIONAL_LETTERS> | <SPECIAL_CHARS>)* >
220220
| < #LETTER: ["a"-"z", "A"-"Z", "_"] >
221-
| < #SPECIAL_CHARS: "$" | "_">
221+
| < #SPECIAL_CHARS: "$" | "_" | "#" | "@">
222222
| < S_CHAR_LITERAL: "'" (~["'"])* "'" ("'" (~["'"])* "'")*>
223223
| < S_QUOTED_IDENTIFIER: "\"" (~["\n","\r","\""])* "\"" | ("`" (~["\n","\r","`"])* "`") | ("[" (~["\n","\r","]"])* "]") >
224224

src/test/java/net/sf/jsqlparser/test/TestUtils.java

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,67 @@
2727
import java.io.*;
2828

2929
import static junit.framework.Assert.assertEquals;
30+
import org.junit.Test;
3031

3132
/**
3233
*
3334
* @author toben
3435
*/
3536
public class TestUtils {
36-
public static void assertSqlCanBeParsedAndDeparsed(String statement) throws JSQLParserException {
37-
Statement parsed = CCJSqlParserUtil.parse(new StringReader(statement));
38-
assertStatementCanBeDeparsedAs(parsed, statement);
39-
}
40-
41-
public static void assertStatementCanBeDeparsedAs(Statement parsed, String statement) {
42-
assertEquals(statement, parsed.toString());
43-
44-
StatementDeParser deParser = new StatementDeParser(new StringBuilder());
45-
parsed.accept(deParser);
46-
assertEquals(statement, deParser.getBuffer().toString());
47-
}
48-
49-
public static void assertExpressionCanBeDeparsedAs(final Expression parsed, String expression) {
50-
ExpressionDeParser expressionDeParser = new ExpressionDeParser();
51-
StringBuilder stringBuilder = new StringBuilder();
52-
expressionDeParser.setBuffer(stringBuilder);
53-
SelectDeParser selectDeParser = new SelectDeParser(expressionDeParser, stringBuilder);
54-
expressionDeParser.setSelectVisitor(selectDeParser);
55-
parsed.accept(expressionDeParser);
56-
57-
assertEquals(expression, stringBuilder.toString());
58-
}
37+
38+
public static void assertSqlCanBeParsedAndDeparsed(String statement) throws JSQLParserException {
39+
assertSqlCanBeParsedAndDeparsed(statement, false);
40+
}
41+
42+
/**
43+
* Tries to parse and deparse the given statement.
44+
*
45+
* @param statement
46+
* @param laxDeparsingCheck removes all linefeeds from the original and
47+
* removes all double spaces. The check is caseinsensitive.
48+
* @throws JSQLParserException
49+
*/
50+
public static void assertSqlCanBeParsedAndDeparsed(String statement, boolean laxDeparsingCheck) throws JSQLParserException {
51+
Statement parsed = CCJSqlParserUtil.parse(new StringReader(statement));
52+
assertStatementCanBeDeparsedAs(parsed, statement, laxDeparsingCheck);
53+
}
54+
55+
public static void assertStatementCanBeDeparsedAs(Statement parsed, String statement) {
56+
assertStatementCanBeDeparsedAs(parsed, statement, false);
57+
}
58+
59+
public static void assertStatementCanBeDeparsedAs(Statement parsed, String statement, boolean laxDeparsingCheck) {
60+
assertEquals(buildSqlString(statement, laxDeparsingCheck),
61+
buildSqlString(parsed.toString(), laxDeparsingCheck));
62+
63+
StatementDeParser deParser = new StatementDeParser(new StringBuilder());
64+
parsed.accept(deParser);
65+
assertEquals(buildSqlString(statement, laxDeparsingCheck),
66+
buildSqlString(deParser.getBuffer().toString(), laxDeparsingCheck));
67+
}
68+
69+
public static String buildSqlString(String sql, boolean laxDeparsingCheck) {
70+
if (laxDeparsingCheck) {
71+
return sql.replaceAll("\\s", " ").replaceAll("\\s+", " ").toLowerCase().trim();
72+
} else {
73+
return sql;
74+
}
75+
}
76+
77+
@Test
78+
public void testBuildSqlString() {
79+
assertEquals("select * from test", buildSqlString(" SELECT * FROM \r\n \t TEST \n", true));
80+
assertEquals("select * from test", buildSqlString("select * from test", false));
81+
}
82+
83+
public static void assertExpressionCanBeDeparsedAs(final Expression parsed, String expression) {
84+
ExpressionDeParser expressionDeParser = new ExpressionDeParser();
85+
StringBuilder stringBuilder = new StringBuilder();
86+
expressionDeParser.setBuffer(stringBuilder);
87+
SelectDeParser selectDeParser = new SelectDeParser(expressionDeParser, stringBuilder);
88+
expressionDeParser.setSelectVisitor(selectDeParser);
89+
parsed.accept(expressionDeParser);
90+
91+
assertEquals(expression, stringBuilder.toString());
92+
}
5993
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (C) 2014 JSQLParser.
3+
*
4+
* This library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* This library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this library; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17+
* MA 02110-1301 USA
18+
*/
19+
package net.sf.jsqlparser.test.select;
20+
21+
import java.io.File;
22+
import java.io.IOException;
23+
import java.util.logging.Level;
24+
import java.util.logging.Logger;
25+
import net.sf.jsqlparser.JSQLParserException;
26+
import static net.sf.jsqlparser.test.TestUtils.*;
27+
import org.apache.commons.io.FileUtils;
28+
import org.junit.Test;
29+
30+
/**
31+
* Tries to parse and deparse all statments in net.sf.jsqlparser.test.oracle-tests.
32+
* @author toben
33+
*/
34+
public class SpecialOracleTests {
35+
36+
private static final File SQLS_DIR = new File("target/test-classes/net/sf/jsqlparser/test/oracle-tests");
37+
private static final Logger LOG = Logger.getLogger(SpecialOracleTests.class.getName());
38+
39+
@Test
40+
public void testAllSqls() throws IOException {
41+
File[] sqlTestFiles = SQLS_DIR.listFiles();
42+
43+
for (File file : sqlTestFiles) {
44+
LOG.log(Level.INFO, "testing {0}", file.getName());
45+
String sql = FileUtils.readFileToString(file);
46+
try {
47+
assertSqlCanBeParsedAndDeparsed(sql, true);
48+
49+
LOG.info(" -> SUCCESS");
50+
} catch (JSQLParserException ex) {
51+
LOG.log(Level.SEVERE, null, ex);
52+
}
53+
}
54+
}
55+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
with
2+
codes2codelocales as
3+
(
4+
select t6.cdl_name as cod_name, t7.cdl_name as cod_cod_name, t14.cod_oid
5+
from servicedesk.itsm_codes t14
6+
left outer join servicedesk.itsm_codes_locale t6 on (t6.cdl_cod_oid=t14.cod_oid)
7+
left outer join servicedesk.itsm_codes_locale t7 on (t7.cdl_cod_oid=t14.cod_cod_oid)
8+
)
9+
, incident as
10+
(
11+
select t1.*,
12+
c2cl1.cod_name as "closure code", c2cl1.cod_cod_name as "closure code parent",
13+
c2cl2.cod_name as "reason caused code", c2cl2.cod_cod_name as "reason caused code parent",
14+
t11.cdl_name "severity", t13.cdl_name "business impact", t16.cdl_name "priority",
15+
t2.rct_name "status", t12.rct_name "category", t99.rct_name "folder"
16+
from servicedesk.itsm_incidents t1
17+
join servicedesk.itsm_codes_locale t11 on (t1.inc_sev_oid=t11.cdl_cod_oid)
18+
join servicedesk.itsm_codes_locale t13 on (t1.inc_imp_oid=t13.cdl_cod_oid)
19+
join servicedesk.itsm_codes_locale t16 on (t1.inc_pri_oid=t16.cdl_cod_oid)
20+
join servicedeskrepo.rep_codes_text t2 on (t1.inc_sta_oid=t2.rct_rcd_oid)
21+
join servicedeskrepo.rep_codes_text t12 on (t1.inc_cat_oid=t12.rct_rcd_oid)
22+
join servicedeskrepo.rep_codes_text t99 on (t1.inc_poo_oid=t99.rct_rcd_oid)
23+
left outer join codes2codelocales c2cl1 on (t1.inc_clo_oid=c2cl1.cod_oid)
24+
left outer join codes2codelocales c2cl2 on (t1.inc_cla_oid=c2cl2.cod_oid)
25+
where t1."reg_created" between sysdate-1 and sysdate
26+
)
27+
, workgrouphistory as
28+
(
29+
select i.inc_id
30+
, max(t101.hin_subject) keep (dense_rank first order by (t101.reg_created)) as "first"
31+
, max(t101.hin_subject) keep (dense_rank last order by (t101.reg_created)) as "last"
32+
from
33+
servicedesk.itsm_historylines_incident t101
34+
join incident i on (t101.hin_inc_oid = i.inc_oid)
35+
-- from servicedesk.itsm_incidents i (t101.hin_inc_oid = i.inc_oid)
36+
where t101.hin_subject like 'to workgroup from%'
37+
-- and i."reg_created" between sysdate-1 and sysdate
38+
group by i.inc_id
39+
)
40+
select
41+
incident.inc_id "id"
42+
,incident."status"
43+
,incident.inc_description "description"
44+
,t4.wog_searchcode "workgroup"
45+
,t5.per_searchcode "person"
46+
,incident.inc_solution "solution"
47+
,incident."closure code"
48+
,incident."closure code parent"
49+
,incident."reason caused code"
50+
,incident."reason caused code parent"
51+
,t10.cit_searchcode "ci"
52+
,incident."severity"
53+
,incident."category"
54+
,incident."business impact"
55+
,incident."priority"
56+
,to_char(incident."reg_created", 'dd-mm-yy hh24:mi:ss') "registered"
57+
,to_char(incident."inc_deadline", 'dd-mm-yy hh24:mi:ss') "deadline"
58+
,to_char(incident."inc_actualfinish", 'dd-mm-yy hh24:mi:ss') "finish"
59+
,t3.icf_incshorttext3 "message group"
60+
,t3.icf_incshorttext4 "application"
61+
,t3.icf_incshorttext2 "msg id"
62+
,incident."folder"
63+
,workgrouphistory."first" "first wg"
64+
,workgrouphistory."last" "last wg"
65+
,t102.hin_subject "frirst pri"
66+
from incident
67+
join servicedesk.itsm_inc_custom_fields t3 on (incident.inc_oid=t3.icf_inc_oid)
68+
join servicedesk.itsm_workgroups t4 on (incident.inc_assign_workgroup=t4.wog_oid)
69+
join workgrouphistory on (incident.inc_id = workgrouphistory.inc_id)
70+
left outer join servicedesk.itsm_persons t5 on (incident.inc_assign_person_to=t5.per_oid)
71+
left outer join servicedesk.itsm_configuration_items t10 on (incident.inc_cit_oid=t10.cit_oid)
72+
left outer join servicedesk.itsm_historylines_incident t102 on (incident.inc_oid = t102.hin_inc_oid and t102.hin_subject like 'priority set to%')
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
select time_id, product
2+
, last_value(quantity ignore nulls) over (partition by product order by time_id) quantity
3+
, last_value(quantity respect nulls) over (partition by product order by time_id) quantity
4+
from ( select times.time_id, product, quantity
5+
from inventory partition by (product)
6+
right outer join times on (times.time_id = inventory.time_id)
7+
where times.time_id between to_date('01/04/01', 'dd/mm/yy')
8+
and to_date('06/04/01', 'dd/mm/yy'))
9+
order by 2,1
10+
11+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
select times.time_id, product, quantity from inventory
2+
partition by (product)
3+
right outer join times on (times.time_id = inventory.time_id)
4+
where times.time_id between to_date('01/04/01', 'dd/mm/yy')
5+
and to_date('06/04/01', 'dd/mm/yy')
6+
order by 2,1
7+
8+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
select deptno
2+
, ename
3+
, hiredate
4+
, listagg(ename, ',') within group (order by hiredate) over (partition by deptno) as employees
5+
from emp

0 commit comments

Comments
 (0)