Skip to content

Commit 01bcbd5

Browse files
committed
fixes #367
1 parent 1a90b16 commit 01bcbd5

File tree

7 files changed

+213
-21
lines changed

7 files changed

+213
-21
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2018 JSQLParser
6+
* %%
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation, either version 2.1 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Lesser Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Lesser Public
18+
* License along with this program. If not, see
19+
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
20+
* #L%
21+
*/
22+
/*
23+
* Copyright (C) 2018 JSQLParser.
24+
*
25+
* This library is free software; you can redistribute it and/or
26+
* modify it under the terms of the GNU Lesser General Public
27+
* License as published by the Free Software Foundation; either
28+
* version 2.1 of the License, or (at your option) any later version.
29+
*
30+
* This library is distributed in the hope that it will be useful,
31+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
32+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33+
* Lesser General Public License for more details.
34+
*
35+
* You should have received a copy of the GNU Lesser General Public
36+
* License along with this library; if not, write to the Free Software
37+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
38+
* MA 02110-1301 USA
39+
*/
40+
package net.sf.jsqlparser.statement;
41+
42+
/**
43+
*
44+
* @author Tobias Warneke ([email protected])
45+
*/
46+
public class Block implements Statement {
47+
48+
private Statements statements;
49+
50+
public Statements getStatements() {
51+
return statements;
52+
}
53+
54+
public void setStatements(Statements statements) {
55+
this.statements = statements;
56+
}
57+
58+
@Override
59+
public void accept(StatementVisitor statementVisitor) {
60+
statementVisitor.visit(this);
61+
}
62+
63+
@Override
64+
public String toString() {
65+
return "BEGIN\n" + (statements != null ? statements.toString() : "") + "END";
66+
}
67+
}

src/main/java/net/sf/jsqlparser/statement/StatementVisitor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,6 @@ public interface StatementVisitor {
7777

7878
void visit(UseStatement use);
7979

80+
void visit(Block block);
81+
8082
}

src/main/java/net/sf/jsqlparser/statement/StatementVisitorAdapter.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@
3838
import net.sf.jsqlparser.statement.upsert.Upsert;
3939

4040
public class StatementVisitorAdapter implements StatementVisitor {
41+
4142
@Override
4243
public void visit(Commit commit) {
43-
44+
4445
}
4546

4647
@Override
@@ -131,4 +132,8 @@ public void visit(Upsert upsert) {
131132
@Override
132133
public void visit(UseStatement use) {
133134
}
135+
136+
@Override
137+
public void visit(Block block) {
138+
}
134139
}

src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
import java.util.ArrayList;
2525
import java.util.List;
26-
2726
import net.sf.jsqlparser.expression.AllComparisonExpression;
2827
import net.sf.jsqlparser.expression.AnalyticExpression;
2928
import net.sf.jsqlparser.expression.AnyComparisonExpression;
@@ -45,7 +44,6 @@
4544
import net.sf.jsqlparser.expression.KeepExpression;
4645
import net.sf.jsqlparser.expression.LongValue;
4746
import net.sf.jsqlparser.expression.MySQLGroupConcat;
48-
import net.sf.jsqlparser.expression.ValueListExpression;
4947
import net.sf.jsqlparser.expression.NotExpression;
5048
import net.sf.jsqlparser.expression.NullValue;
5149
import net.sf.jsqlparser.expression.NumericBind;
@@ -59,6 +57,7 @@
5957
import net.sf.jsqlparser.expression.TimeValue;
6058
import net.sf.jsqlparser.expression.TimestampValue;
6159
import net.sf.jsqlparser.expression.UserVariable;
60+
import net.sf.jsqlparser.expression.ValueListExpression;
6261
import net.sf.jsqlparser.expression.WhenClause;
6362
import net.sf.jsqlparser.expression.operators.arithmetic.Addition;
6463
import net.sf.jsqlparser.expression.operators.arithmetic.BitwiseAnd;
@@ -93,6 +92,7 @@
9392
import net.sf.jsqlparser.expression.operators.relational.RegExpMySQLOperator;
9493
import net.sf.jsqlparser.schema.Column;
9594
import net.sf.jsqlparser.schema.Table;
95+
import net.sf.jsqlparser.statement.Block;
9696
import net.sf.jsqlparser.statement.Commit;
9797
import net.sf.jsqlparser.statement.SetStatement;
9898
import net.sf.jsqlparser.statement.Statement;
@@ -135,15 +135,15 @@
135135

136136
/**
137137
* Find all used tables within an select statement.
138-
*
138+
*
139139
* Override extractTableName method to modify the extracted table names (e.g. without schema).
140140
*/
141141
public class TablesNamesFinder implements SelectVisitor, FromItemVisitor, ExpressionVisitor, ItemsListVisitor, SelectItemVisitor, StatementVisitor {
142142

143143
private static final String NOT_SUPPORTED_YET = "Not supported yet.";
144144
private List<String> tables;
145145
private boolean allowColumnProcessing = false;
146-
146+
147147
/**
148148
* There are special names, that are not table names but are parsed as tables. These names are
149149
* collected here and are not included in the tables - names anymore.
@@ -210,25 +210,26 @@ public void visit(PlainSelect plainSelect) {
210210
if (plainSelect.getWhere() != null) {
211211
plainSelect.getWhere().accept(this);
212212
}
213-
214-
if(plainSelect.getHaving() != null){
213+
214+
if (plainSelect.getHaving() != null) {
215215
plainSelect.getHaving().accept(this);
216216
}
217-
217+
218218
if (plainSelect.getOracleHierarchical() != null) {
219219
plainSelect.getOracleHierarchical().accept(this);
220220
}
221221
}
222222

223223
/**
224224
* Override to adapt the tableName generation (e.g. with / without schema).
225+
*
225226
* @param table
226-
* @return
227+
* @return
227228
*/
228229
protected String extractTableName(Table table) {
229230
return table.getFullyQualifiedName();
230231
}
231-
232+
232233
@Override
233234
public void visit(Table tableName) {
234235
String tableWholeName = extractTableName(tableName);
@@ -471,7 +472,7 @@ public void visit(AnyComparisonExpression anyComparisonExpression) {
471472
@Override
472473
public void visit(SubJoin subjoin) {
473474
subjoin.getLeft().accept(this);
474-
for(Join join : subjoin.getJoinList()) {
475+
for (Join join : subjoin.getJoinList()) {
475476
join.getRightItem().accept(this);
476477
}
477478
}
@@ -543,10 +544,11 @@ public void visit(ValuesList valuesList) {
543544
}
544545

545546
/**
546-
* Initializes table names collector. Important is the usage of Column instances to find
547-
* table names. This is only allowed for expression parsing, where a better place for
548-
* tablenames could not be there. For complete statements only from items are used to avoid
549-
* some alias as tablenames.
547+
* Initializes table names collector. Important is the usage of Column instances to find table
548+
* names. This is only allowed for expression parsing, where a better place for tablenames could
549+
* not be there. For complete statements only from items are used to avoid some alias as
550+
* tablenames.
551+
*
550552
* @param allowColumnProcessing
551553
*/
552554
protected void init(boolean allowColumnProcessing) {
@@ -621,7 +623,7 @@ public void visit(KeepExpression aexpr) {
621623
@Override
622624
public void visit(MySQLGroupConcat groupConcat) {
623625
}
624-
626+
625627
@Override
626628
public void visit(ValueListExpression valueList) {
627629
valueList.getExpressionList().accept(this);
@@ -808,4 +810,11 @@ public void visit(UseStatement use) {
808810
public void visit(ParenthesisFromItem parenthesis) {
809811
parenthesis.getFromItem().accept(this);
810812
}
813+
814+
@Override
815+
public void visit(Block block) {
816+
if (block.getStatements() != null) {
817+
visit(block.getStatements());
818+
}
819+
}
811820
}

src/main/java/net/sf/jsqlparser/util/deparser/StatementDeParser.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
package net.sf.jsqlparser.util.deparser;
2323

2424
import java.util.Iterator;
25-
25+
import net.sf.jsqlparser.statement.Block;
2626
import net.sf.jsqlparser.statement.Commit;
2727
import net.sf.jsqlparser.statement.SetStatement;
28+
import net.sf.jsqlparser.statement.Statement;
2829
import net.sf.jsqlparser.statement.StatementVisitor;
2930
import net.sf.jsqlparser.statement.Statements;
3031
import net.sf.jsqlparser.statement.UseStatement;
@@ -46,6 +47,7 @@
4647
import net.sf.jsqlparser.statement.upsert.Upsert;
4748

4849
public class StatementDeParser implements StatementVisitor {
50+
4951
private ExpressionDeParser expressionDeParser;
5052

5153
private SelectDeParser selectDeParser;
@@ -146,7 +148,7 @@ public void visit(Select select) {
146148
public void visit(Truncate truncate) {
147149
buffer.append("TRUNCATE TABLE ");
148150
buffer.append(truncate.getTable());
149-
if(truncate.getCascade()){
151+
if (truncate.getCascade()) {
150152
buffer.append(" CASCADE");
151153
}
152154
}
@@ -206,7 +208,7 @@ public void visit(Merge merge) {
206208
//TODO implementation of a deparser
207209
buffer.append(merge.toString());
208210
}
209-
211+
210212
@Override
211213
public void visit(Commit commit) {
212214
buffer.append(commit.toString());
@@ -226,4 +228,16 @@ public void visit(Upsert upsert) {
226228
public void visit(UseStatement use) {
227229
new UseStatementDeParser(buffer).deParse(use);
228230
}
231+
232+
@Override
233+
public void visit(Block block) {
234+
buffer.append("BEGIN\n");
235+
if (block.getStatements() != null) {
236+
for (Statement stmt : block.getStatements().getStatements()) {
237+
stmt.accept(this);
238+
buffer.append(";\n");
239+
}
240+
}
241+
buffer.append("END");
242+
}
229243
}

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,15 +428,45 @@ Statement SingleStatement() :
428428
}
429429
}
430430

431+
Block Block() #Block : {
432+
Statements stmts = new Statements();
433+
List<Statement> list = new ArrayList<Statement>();
434+
Statement stm;
435+
Block block = new Block();
436+
}
437+
{
438+
<K_BEGIN>
439+
(<ST_SEMICOLON>)*
440+
try {
441+
(stm = SingleStatement() | stm = Block()) { list.add(stm); } <ST_SEMICOLON>
442+
( (stm = SingleStatement() | stm = Block()) <ST_SEMICOLON> { list.add(stm); } )*
443+
} catch (ParseException e) {
444+
if (errorRecovery) {
445+
parseErrors.add(e);
446+
error_skipto(ST_SEMICOLON);
447+
}
448+
else
449+
throw e;
450+
}
451+
{
452+
stmts.setStatements(list);
453+
block.setStatements(stmts);
454+
}
455+
<K_END>
456+
{
457+
return block;
458+
}
459+
}
460+
431461
Statements Statements() #Statements :
432462
{ Statements stmts = new Statements();
433463
List<Statement> list = new ArrayList<Statement>();
434464
Statement stm; }
435465
{
436466
(<ST_SEMICOLON>)*
437467
try {
438-
stm = SingleStatement() { list.add(stm); }
439-
( <ST_SEMICOLON> [stm = SingleStatement() { list.add(stm); }] )*
468+
(stm = SingleStatement() | stm = Block()) { list.add(stm); }
469+
( <ST_SEMICOLON> [(stm = SingleStatement() | stm = Block()) { list.add(stm); }] )*
440470
<EOF>
441471
} catch (ParseException e) {
442472
if (errorRecovery) {
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (C) 2018 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.statement;
20+
21+
import net.sf.jsqlparser.JSQLParserException;
22+
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
23+
import org.junit.After;
24+
import org.junit.AfterClass;
25+
import static org.junit.Assert.assertEquals;
26+
import org.junit.Before;
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
30+
/**
31+
*
32+
* @author Tobias Warneke ([email protected])
33+
*/
34+
public class BlockTest {
35+
36+
public BlockTest() {
37+
}
38+
39+
@BeforeClass
40+
public static void setUpClass() {
41+
}
42+
43+
@AfterClass
44+
public static void tearDownClass() {
45+
}
46+
47+
@Before
48+
public void setUp() {
49+
}
50+
51+
@After
52+
public void tearDown() {
53+
}
54+
55+
/**
56+
* Test of getStatements method, of class Block.
57+
*/
58+
@Test
59+
public void testGetStatements() throws JSQLParserException {
60+
Statements stmts = CCJSqlParserUtil.parseStatements("begin\nselect * from feature;\nend");
61+
assertEquals("BEGIN\n"
62+
+ "SELECT * FROM feature;\n"
63+
+ "END;\n", stmts.toString());
64+
}
65+
}

0 commit comments

Comments
 (0)