Skip to content

Commit 941952f

Browse files
committed
fixes #110 - first implementation
1 parent f2d48cf commit 941952f

File tree

8 files changed

+245
-6
lines changed

8 files changed

+245
-6
lines changed

pom.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@
1919
</licenses>
2020

2121
<dependencies>
22-
<dependency>
23-
<groupId>junit</groupId>
24-
<artifactId>junit</artifactId>
25-
<version>4.11</version>
26-
<scope>test</scope>
27-
</dependency>
2822
<dependency>
2923
<groupId>commons-io</groupId>
3024
<artifactId>commons-io</artifactId>
3125
<version>2.4</version>
3226
<scope>test</scope>
3327
</dependency>
28+
<dependency>
29+
<groupId>junit</groupId>
30+
<artifactId>junit</artifactId>
31+
<version>4.12</version>
32+
<scope>test</scope>
33+
</dependency>
3434
</dependencies>
3535

3636
<developers>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2015 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) 2015 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+
import net.sf.jsqlparser.expression.Expression;
43+
44+
/**
45+
*
46+
* @author toben
47+
*/
48+
public class SetStatement implements Statement {
49+
50+
private String name;
51+
private Expression expression;
52+
53+
public SetStatement(String name, Expression expression) {
54+
this.name = name;
55+
this.expression = expression;
56+
}
57+
58+
public String getName() {
59+
return name;
60+
}
61+
62+
public void setName(String name) {
63+
this.name = name;
64+
}
65+
66+
public Expression getExpression() {
67+
return expression;
68+
}
69+
70+
public void setExpression(Expression expression) {
71+
this.expression = expression;
72+
}
73+
74+
@Override
75+
public String toString() {
76+
return "SET " + name + " = " + expression.toString();
77+
}
78+
79+
@Override
80+
public void accept(StatementVisitor statementVisitor) {
81+
statementVisitor.visit(this);
82+
}
83+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,6 @@ public interface StatementVisitor {
6161
void visit(Statements stmts);
6262

6363
void visit(Execute execute);
64+
65+
void visit(SetStatement set);
6466
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,9 @@ public void visit(Statements stmts) {
9999
public void visit(Execute execute) {
100100

101101
}
102+
103+
@Override
104+
public void visit(SetStatement set) {
105+
106+
}
102107
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2015 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.expression.ExpressionVisitor;
25+
import net.sf.jsqlparser.statement.SetStatement;
26+
import net.sf.jsqlparser.statement.select.PlainSelect;
27+
28+
public class SetStatementDeParser {
29+
30+
private StringBuilder buffer;
31+
private ExpressionVisitor expressionVisitor;
32+
33+
/**
34+
* @param expressionVisitor a {@link ExpressionVisitor} to de-parse
35+
* expressions. It has to share the same<br>
36+
* StringBuilder (buffer parameter) as this object in order to work
37+
* @param buffer the buffer that will be filled with the select
38+
*/
39+
public SetStatementDeParser(ExpressionVisitor expressionVisitor, StringBuilder buffer) {
40+
this.buffer = buffer;
41+
this.expressionVisitor = expressionVisitor;
42+
}
43+
44+
public StringBuilder getBuffer() {
45+
return buffer;
46+
}
47+
48+
public void setBuffer(StringBuilder buffer) {
49+
this.buffer = buffer;
50+
}
51+
52+
public void deParse(SetStatement set) {
53+
buffer.append("SET ").append(set.getName());
54+
buffer.append(" = ");
55+
set.getExpression().accept(expressionVisitor);
56+
}
57+
58+
public ExpressionVisitor getExpressionVisitor() {
59+
return expressionVisitor;
60+
}
61+
62+
public void setExpressionVisitor(ExpressionVisitor visitor) {
63+
expressionVisitor = visitor;
64+
}
65+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
package net.sf.jsqlparser.util.deparser;
2323

2424
import java.util.Iterator;
25+
import net.sf.jsqlparser.statement.SetStatement;
2526

2627
import net.sf.jsqlparser.statement.StatementVisitor;
2728
import net.sf.jsqlparser.statement.Statements;
@@ -164,4 +165,14 @@ public void visit(Execute execute) {
164165
selectDeParser.setExpressionVisitor(expressionDeParser);
165166
executeDeParser.deParse(execute);
166167
}
168+
169+
@Override
170+
public void visit(SetStatement set) {
171+
SelectDeParser selectDeParser = new SelectDeParser();
172+
selectDeParser.setBuffer(buffer);
173+
ExpressionDeParser expressionDeParser = new ExpressionDeParser(selectDeParser, buffer);
174+
SetStatementDeParser setStatementDeparser = new SetStatementDeParser(expressionDeParser, buffer);
175+
selectDeParser.setExpressionVisitor(expressionDeParser);
176+
setStatementDeparser.deParse(set);
177+
}
167178
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ Statement SingleStatement() :
288288
stm = Truncate()
289289
|
290290
stm = Execute()
291+
|
292+
stm = Set()
291293
)
292294
{ return stm; }
293295
}
@@ -309,6 +311,17 @@ Statements Statements() :
309311
}
310312
}
311313

314+
SetStatement Set(): {
315+
String name;
316+
Expression value;
317+
}
318+
{
319+
<K_SET> name = RelObjectNameExt() "=" value=SimpleExpression()
320+
{
321+
return new SetStatement(name,value);
322+
}
323+
}
324+
312325
Update Update():
313326
{
314327
Update update = new Update();
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.After;
24+
import org.junit.AfterClass;
25+
import org.junit.Before;
26+
import org.junit.BeforeClass;
27+
import org.junit.Test;
28+
29+
/**
30+
*
31+
* @author toben
32+
*/
33+
public class SetStatementTest {
34+
35+
public SetStatementTest() {
36+
}
37+
38+
@BeforeClass
39+
public static void setUpClass() {
40+
}
41+
42+
@AfterClass
43+
public static void tearDownClass() {
44+
}
45+
46+
@Before
47+
public void setUp() {
48+
}
49+
50+
@After
51+
public void tearDown() {
52+
}
53+
54+
55+
@Test
56+
public void testSimpleSet() throws JSQLParserException {
57+
assertSqlCanBeParsedAndDeparsed("SET statement_timeout = 0");
58+
}
59+
60+
}

0 commit comments

Comments
 (0)