Skip to content

Commit

Permalink
fixes #367
Browse files Browse the repository at this point in the history
  • Loading branch information
wumpz committed Aug 25, 2018
1 parent 1a90b16 commit 01bcbd5
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 21 deletions.
67 changes: 67 additions & 0 deletions src/main/java/net/sf/jsqlparser/statement/Block.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2018 JSQLParser
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
/*
* Copyright (C) 2018 JSQLParser.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package net.sf.jsqlparser.statement;

/**
*
* @author Tobias Warneke (t.warneke@gmx.net)
*/
public class Block implements Statement {

private Statements statements;

public Statements getStatements() {
return statements;
}

public void setStatements(Statements statements) {
this.statements = statements;
}

@Override
public void accept(StatementVisitor statementVisitor) {
statementVisitor.visit(this);
}

@Override
public String toString() {
return "BEGIN\n" + (statements != null ? statements.toString() : "") + "END";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,6 @@ public interface StatementVisitor {

void visit(UseStatement use);

void visit(Block block);

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@
import net.sf.jsqlparser.statement.upsert.Upsert;

public class StatementVisitorAdapter implements StatementVisitor {

@Override
public void visit(Commit commit) {

}

@Override
Expand Down Expand Up @@ -131,4 +132,8 @@ public void visit(Upsert upsert) {
@Override
public void visit(UseStatement use) {
}

@Override
public void visit(Block block) {
}
}
39 changes: 24 additions & 15 deletions src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import java.util.ArrayList;
import java.util.List;

import net.sf.jsqlparser.expression.AllComparisonExpression;
import net.sf.jsqlparser.expression.AnalyticExpression;
import net.sf.jsqlparser.expression.AnyComparisonExpression;
Expand All @@ -45,7 +44,6 @@
import net.sf.jsqlparser.expression.KeepExpression;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.MySQLGroupConcat;
import net.sf.jsqlparser.expression.ValueListExpression;
import net.sf.jsqlparser.expression.NotExpression;
import net.sf.jsqlparser.expression.NullValue;
import net.sf.jsqlparser.expression.NumericBind;
Expand All @@ -59,6 +57,7 @@
import net.sf.jsqlparser.expression.TimeValue;
import net.sf.jsqlparser.expression.TimestampValue;
import net.sf.jsqlparser.expression.UserVariable;
import net.sf.jsqlparser.expression.ValueListExpression;
import net.sf.jsqlparser.expression.WhenClause;
import net.sf.jsqlparser.expression.operators.arithmetic.Addition;
import net.sf.jsqlparser.expression.operators.arithmetic.BitwiseAnd;
Expand Down Expand Up @@ -93,6 +92,7 @@
import net.sf.jsqlparser.expression.operators.relational.RegExpMySQLOperator;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Block;
import net.sf.jsqlparser.statement.Commit;
import net.sf.jsqlparser.statement.SetStatement;
import net.sf.jsqlparser.statement.Statement;
Expand Down Expand Up @@ -135,15 +135,15 @@

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

private static final String NOT_SUPPORTED_YET = "Not supported yet.";
private List<String> tables;
private boolean allowColumnProcessing = false;

/**
* There are special names, that are not table names but are parsed as tables. These names are
* collected here and are not included in the tables - names anymore.
Expand Down Expand Up @@ -210,25 +210,26 @@ public void visit(PlainSelect plainSelect) {
if (plainSelect.getWhere() != null) {
plainSelect.getWhere().accept(this);
}
if(plainSelect.getHaving() != null){

if (plainSelect.getHaving() != null) {
plainSelect.getHaving().accept(this);
}

if (plainSelect.getOracleHierarchical() != null) {
plainSelect.getOracleHierarchical().accept(this);
}
}

/**
* Override to adapt the tableName generation (e.g. with / without schema).
*
* @param table
* @return
* @return
*/
protected String extractTableName(Table table) {
return table.getFullyQualifiedName();
}

@Override
public void visit(Table tableName) {
String tableWholeName = extractTableName(tableName);
Expand Down Expand Up @@ -471,7 +472,7 @@ public void visit(AnyComparisonExpression anyComparisonExpression) {
@Override
public void visit(SubJoin subjoin) {
subjoin.getLeft().accept(this);
for(Join join : subjoin.getJoinList()) {
for (Join join : subjoin.getJoinList()) {
join.getRightItem().accept(this);
}
}
Expand Down Expand Up @@ -543,10 +544,11 @@ public void visit(ValuesList valuesList) {
}

/**
* Initializes table names collector. Important is the usage of Column instances to find
* table names. This is only allowed for expression parsing, where a better place for
* tablenames could not be there. For complete statements only from items are used to avoid
* some alias as tablenames.
* Initializes table names collector. Important is the usage of Column instances to find table
* names. This is only allowed for expression parsing, where a better place for tablenames could
* not be there. For complete statements only from items are used to avoid some alias as
* tablenames.
*
* @param allowColumnProcessing
*/
protected void init(boolean allowColumnProcessing) {
Expand Down Expand Up @@ -621,7 +623,7 @@ public void visit(KeepExpression aexpr) {
@Override
public void visit(MySQLGroupConcat groupConcat) {
}

@Override
public void visit(ValueListExpression valueList) {
valueList.getExpressionList().accept(this);
Expand Down Expand Up @@ -808,4 +810,11 @@ public void visit(UseStatement use) {
public void visit(ParenthesisFromItem parenthesis) {
parenthesis.getFromItem().accept(this);
}

@Override
public void visit(Block block) {
if (block.getStatements() != null) {
visit(block.getStatements());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
package net.sf.jsqlparser.util.deparser;

import java.util.Iterator;

import net.sf.jsqlparser.statement.Block;
import net.sf.jsqlparser.statement.Commit;
import net.sf.jsqlparser.statement.SetStatement;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.StatementVisitor;
import net.sf.jsqlparser.statement.Statements;
import net.sf.jsqlparser.statement.UseStatement;
Expand All @@ -46,6 +47,7 @@
import net.sf.jsqlparser.statement.upsert.Upsert;

public class StatementDeParser implements StatementVisitor {

private ExpressionDeParser expressionDeParser;

private SelectDeParser selectDeParser;
Expand Down Expand Up @@ -146,7 +148,7 @@ public void visit(Select select) {
public void visit(Truncate truncate) {
buffer.append("TRUNCATE TABLE ");
buffer.append(truncate.getTable());
if(truncate.getCascade()){
if (truncate.getCascade()) {
buffer.append(" CASCADE");
}
}
Expand Down Expand Up @@ -206,7 +208,7 @@ public void visit(Merge merge) {
//TODO implementation of a deparser
buffer.append(merge.toString());
}

@Override
public void visit(Commit commit) {
buffer.append(commit.toString());
Expand All @@ -226,4 +228,16 @@ public void visit(Upsert upsert) {
public void visit(UseStatement use) {
new UseStatementDeParser(buffer).deParse(use);
}

@Override
public void visit(Block block) {
buffer.append("BEGIN\n");
if (block.getStatements() != null) {
for (Statement stmt : block.getStatements().getStatements()) {
stmt.accept(this);
buffer.append(";\n");
}
}
buffer.append("END");
}
}
34 changes: 32 additions & 2 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -428,15 +428,45 @@ Statement SingleStatement() :
}
}

Block Block() #Block : {
Statements stmts = new Statements();
List<Statement> list = new ArrayList<Statement>();
Statement stm;
Block block = new Block();
}
{
<K_BEGIN>
(<ST_SEMICOLON>)*
try {
(stm = SingleStatement() | stm = Block()) { list.add(stm); } <ST_SEMICOLON>
( (stm = SingleStatement() | stm = Block()) <ST_SEMICOLON> { list.add(stm); } )*
} catch (ParseException e) {
if (errorRecovery) {
parseErrors.add(e);
error_skipto(ST_SEMICOLON);
}
else
throw e;
}
{
stmts.setStatements(list);
block.setStatements(stmts);
}
<K_END>
{
return block;
}
}

Statements Statements() #Statements :
{ Statements stmts = new Statements();
List<Statement> list = new ArrayList<Statement>();
Statement stm; }
{
(<ST_SEMICOLON>)*
try {
stm = SingleStatement() { list.add(stm); }
( <ST_SEMICOLON> [stm = SingleStatement() { list.add(stm); }] )*
(stm = SingleStatement() | stm = Block()) { list.add(stm); }
( <ST_SEMICOLON> [(stm = SingleStatement() | stm = Block()) { list.add(stm); }] )*
<EOF>
} catch (ParseException e) {
if (errorRecovery) {
Expand Down
65 changes: 65 additions & 0 deletions src/test/java/net/sf/jsqlparser/statement/BlockTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (C) 2018 JSQLParser.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package net.sf.jsqlparser.statement;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import org.junit.After;
import org.junit.AfterClass;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
*
* @author Tobias Warneke (t.warneke@gmx.net)
*/
public class BlockTest {

public BlockTest() {
}

@BeforeClass
public static void setUpClass() {
}

@AfterClass
public static void tearDownClass() {
}

@Before
public void setUp() {
}

@After
public void tearDown() {
}

/**
* Test of getStatements method, of class Block.
*/
@Test
public void testGetStatements() throws JSQLParserException {
Statements stmts = CCJSqlParserUtil.parseStatements("begin\nselect * from feature;\nend");
assertEquals("BEGIN\n"
+ "SELECT * FROM feature;\n"
+ "END;\n", stmts.toString());
}
}

0 comments on commit 01bcbd5

Please sign in to comment.