Skip to content

Commit e60fa8c

Browse files
committed
#fixes 480
1 parent 8f4cc66 commit e60fa8c

File tree

8 files changed

+163
-1
lines changed

8 files changed

+163
-1
lines changed

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

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

7676
void visit(Upsert upsert);
7777

78+
void visit(UseStatement use);
79+
7880
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ public void visit(AlterView alterView) {
126126

127127
@Override
128128
public void visit(Upsert upsert) {
129-
129+
}
130+
131+
@Override
132+
public void visit(UseStatement use) {
130133
}
131134
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2017 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+
package net.sf.jsqlparser.statement;
23+
24+
/**
25+
*
26+
* @author toben
27+
*/
28+
public class UseStatement implements Statement {
29+
30+
private String name;
31+
32+
public UseStatement(String name) {
33+
this.name = name;
34+
}
35+
36+
public String getName() {
37+
return name;
38+
}
39+
40+
public void setName(String name) {
41+
this.name = name;
42+
}
43+
44+
@Override
45+
public String toString() {
46+
return "USE " + name;
47+
}
48+
49+
@Override
50+
public void accept(StatementVisitor statementVisitor) {
51+
statementVisitor.visit(this);
52+
}
53+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
import net.sf.jsqlparser.statement.Statement;
9797
import net.sf.jsqlparser.statement.StatementVisitor;
9898
import net.sf.jsqlparser.statement.Statements;
99+
import net.sf.jsqlparser.statement.UseStatement;
99100
import net.sf.jsqlparser.statement.alter.Alter;
100101
import net.sf.jsqlparser.statement.create.index.CreateIndex;
101102
import net.sf.jsqlparser.statement.create.table.CreateTable;
@@ -749,4 +750,8 @@ public void visit(Upsert upsert) {
749750
visit(upsert.getSelect());
750751
}
751752
}
753+
754+
@Override
755+
public void visit(UseStatement use) {
756+
}
752757
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import net.sf.jsqlparser.statement.SetStatement;
2828
import net.sf.jsqlparser.statement.StatementVisitor;
2929
import net.sf.jsqlparser.statement.Statements;
30+
import net.sf.jsqlparser.statement.UseStatement;
3031
import net.sf.jsqlparser.statement.alter.Alter;
3132
import net.sf.jsqlparser.statement.create.index.CreateIndex;
3233
import net.sf.jsqlparser.statement.create.table.CreateTable;
@@ -215,4 +216,9 @@ public void visit(Upsert upsert) {
215216
UpsertDeParser upsertDeParser = new UpsertDeParser(expressionDeParser, selectDeParser, buffer);
216217
upsertDeParser.deParse(upsert);
217218
}
219+
220+
@Override
221+
public void visit(UseStatement use) {
222+
new UseStatementDeParser(buffer).deParse(use);
223+
}
218224
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2017 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+
package net.sf.jsqlparser.util.deparser;
23+
24+
import net.sf.jsqlparser.statement.UseStatement;
25+
26+
public class UseStatementDeParser {
27+
28+
private StringBuilder buffer;
29+
30+
public UseStatementDeParser(StringBuilder buffer) {
31+
this.buffer = buffer;
32+
}
33+
34+
public StringBuilder getBuffer() {
35+
return buffer;
36+
}
37+
38+
public void setBuffer(StringBuilder buffer) {
39+
this.buffer = buffer;
40+
}
41+
42+
public void deParse(UseStatement set) {
43+
buffer.append("USE ").append(set.getName());
44+
}
45+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,8 @@ Statement SingleStatement() :
397397
|
398398
stm = Set()
399399
|
400+
stm = Use()
401+
|
400402
stm = Commit()
401403
)
402404
} catch (ParseException e) {
@@ -456,6 +458,16 @@ SetStatement Set(): {
456458
}
457459
}
458460

461+
UseStatement Use(): {
462+
String name;
463+
}
464+
{
465+
<K_USE> name = RelObjectNameExt()
466+
{
467+
return new UseStatement(name);
468+
}
469+
}
470+
459471
Update Update():
460472
{
461473
Update update = new Update();
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (C) 2015 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 static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
23+
import org.junit.Test;
24+
25+
/**
26+
*
27+
* @author toben
28+
*/
29+
public class UseStatementTest {
30+
31+
32+
@Test
33+
public void testSimpleUse() throws JSQLParserException {
34+
assertSqlCanBeParsedAndDeparsed("USE mydatabase");
35+
}
36+
}

0 commit comments

Comments
 (0)