Skip to content

Commit 144ca3a

Browse files
committed
fixes #34
1 parent 7024bc7 commit 144ca3a

File tree

6 files changed

+179
-0
lines changed

6 files changed

+179
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ configuration details below.
2626
JSqlParser parses an SQL statement and translate it into a hierarchy of Java classes.
2727
The generated hierarchy can be navigated using the Visitor Pattern.
2828

29+
## Extensions Version 0.8.9
30+
31+
* Added **CCJSqlParserUtil.parseExpression** to parse a simple expression. Now you can build expressions from a String like "a+b".
32+
33+
```java
34+
Expression expr = CCJSqlParserUtil.parseExpression("a*(5+mycolumn)");
35+
```
36+
37+
* Improved **SelectUtils** to build simple select statements.
38+
39+
```java
40+
Select select = SelectUtils.buildSelectFromTable(new Table("mytable"));
41+
```
42+
2943
## Extensions Version 0.8.8
3044

3145
* Startet a simple utility class **SelectUtils** to collect basic **select** modification tools.

src/main/java/net/sf/jsqlparser/parser/CCJSqlParserUtil.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.io.Reader;
2626
import java.io.StringReader;
2727
import net.sf.jsqlparser.JSQLParserException;
28+
import net.sf.jsqlparser.expression.Expression;
2829
import net.sf.jsqlparser.statement.Statement;
2930

3031
/**
@@ -67,6 +68,21 @@ public static Statement parse(InputStream is, String encoding) throws JSQLParser
6768
throw new JSQLParserException(ex);
6869
}
6970
}
71+
72+
/**
73+
* Parse an expression.
74+
* @param expression
75+
* @return
76+
* @throws JSQLParserException
77+
*/
78+
public static Expression parseExpression(String expression) throws JSQLParserException {
79+
CCJSqlParser parser = new CCJSqlParser(new StringReader(expression));
80+
try {
81+
return parser.SimpleExpression();
82+
} catch (Exception ex) {
83+
throw new JSQLParserException(ex);
84+
}
85+
}
7086

7187
private CCJSqlParserUtil() {
7288
}

src/main/java/net/sf/jsqlparser/statement/select/PlainSelect.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
*/
2222
package net.sf.jsqlparser.statement.select;
2323

24+
import java.util.ArrayList;
25+
import java.util.Collections;
2426
import net.sf.jsqlparser.expression.Expression;
2527
import net.sf.jsqlparser.schema.Table;
2628

@@ -85,6 +87,13 @@ public void setInto(Table table) {
8587
public void setSelectItems(List<SelectItem> list) {
8688
selectItems = list;
8789
}
90+
91+
public void addSelectItems(SelectItem ... items) {
92+
if (selectItems == null) {
93+
selectItems = new ArrayList<SelectItem>();
94+
}
95+
Collections.addAll(selectItems, items);
96+
}
8897

8998
public void setWhere(Expression where) {
9099
this.where = where;

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,19 @@
2222
package net.sf.jsqlparser.util;
2323

2424
import java.util.ArrayList;
25+
import java.util.Arrays;
2526
import java.util.List;
27+
import net.sf.jsqlparser.JSQLParserException;
2628
import net.sf.jsqlparser.expression.Expression;
29+
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
30+
import net.sf.jsqlparser.schema.Column;
2731
import net.sf.jsqlparser.schema.Table;
32+
import net.sf.jsqlparser.statement.select.AllColumns;
2833
import net.sf.jsqlparser.statement.select.Join;
2934
import net.sf.jsqlparser.statement.select.PlainSelect;
3035
import net.sf.jsqlparser.statement.select.Select;
3136
import net.sf.jsqlparser.statement.select.SelectExpressionItem;
37+
import net.sf.jsqlparser.statement.select.SelectItem;
3238
import net.sf.jsqlparser.statement.select.SelectVisitor;
3339
import net.sf.jsqlparser.statement.select.SetOperationList;
3440
import net.sf.jsqlparser.statement.select.WithItem;
@@ -42,6 +48,53 @@ public final class SelectUtils {
4248

4349
private SelectUtils() {
4450
}
51+
52+
/**
53+
* Builds select expr1, expr2 from table.
54+
* @param table
55+
* @param expr
56+
* @return
57+
*/
58+
public static Select buildSelectFromTableAndExpressions(Table table, Expression ... expr) {
59+
SelectItem[] list = new SelectItem[expr.length];
60+
for (int i=0;i<expr.length;i++) {
61+
list[i]=new SelectExpressionItem(expr[i]);
62+
}
63+
return buildSelectFromTableAndSelectItems(table, list);
64+
}
65+
66+
/**
67+
* Builds select expr1, expr2 from table.
68+
* @param table
69+
* @param expr
70+
* @return
71+
* @throws net.sf.jsqlparser.JSQLParserException
72+
*/
73+
public static Select buildSelectFromTableAndExpressions(Table table, String ... expr) throws JSQLParserException {
74+
SelectItem[] list = new SelectItem[expr.length];
75+
for (int i=0;i<expr.length;i++) {
76+
list[i]=new SelectExpressionItem(CCJSqlParserUtil.parseExpression(expr[i]));
77+
}
78+
return buildSelectFromTableAndSelectItems(table, list);
79+
}
80+
81+
public static Select buildSelectFromTableAndSelectItems(Table table, SelectItem ... selectItems) {
82+
Select select = new Select();
83+
PlainSelect body = new PlainSelect();
84+
body.addSelectItems(selectItems);
85+
body.setFromItem(table);
86+
select.setSelectBody(body);
87+
return select;
88+
}
89+
90+
/**
91+
* Builds select * from table.
92+
* @param table
93+
* @return
94+
*/
95+
public static Select buildSelectFromTable(Table table) {
96+
return buildSelectFromTableAndSelectItems(table, new AllColumns());
97+
}
4598

4699
/**
47100
* Adds an expression to select statements. E.g. a simple column is an
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package net.sf.jsqlparser.parser;
2+
3+
import net.sf.jsqlparser.expression.Expression;
4+
import net.sf.jsqlparser.expression.LongValue;
5+
import net.sf.jsqlparser.expression.Parenthesis;
6+
import net.sf.jsqlparser.expression.operators.arithmetic.Addition;
7+
import net.sf.jsqlparser.expression.operators.arithmetic.Multiplication;
8+
import net.sf.jsqlparser.schema.Column;
9+
import org.junit.After;
10+
import org.junit.AfterClass;
11+
import org.junit.Before;
12+
import org.junit.BeforeClass;
13+
import org.junit.Test;
14+
import static org.junit.Assert.*;
15+
16+
/**
17+
*
18+
* @author toben
19+
*/
20+
public class CCJSqlParserUtilTest {
21+
22+
public CCJSqlParserUtilTest() {
23+
}
24+
25+
@BeforeClass
26+
public static void setUpClass() {
27+
}
28+
29+
@AfterClass
30+
public static void tearDownClass() {
31+
}
32+
33+
@Before
34+
public void setUp() {
35+
}
36+
37+
@After
38+
public void tearDown() {
39+
}
40+
41+
/**
42+
* Test of parseExpression method, of class CCJSqlParserUtil.
43+
*/
44+
@Test
45+
public void testParseExpression() throws Exception {
46+
Expression result = CCJSqlParserUtil.parseExpression("a+b");
47+
assertEquals("a + b", result.toString());
48+
assertTrue(result instanceof Addition);
49+
Addition add = (Addition)result;
50+
assertTrue(add.getLeftExpression() instanceof Column);
51+
assertTrue(add.getRightExpression() instanceof Column);
52+
}
53+
54+
@Test
55+
public void testParseExpression2() throws Exception {
56+
Expression result = CCJSqlParserUtil.parseExpression("2*(a+6.0)");
57+
assertEquals("2 * (a + 6.0)", result.toString());
58+
assertTrue(result instanceof Multiplication);
59+
Multiplication mult = (Multiplication)result;
60+
assertTrue(mult.getLeftExpression() instanceof LongValue);
61+
assertTrue(mult.getRightExpression() instanceof Parenthesis);
62+
}
63+
}

src/test/java/net/sf/jsqlparser/util/SelectUtilsTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package net.sf.jsqlparser.util;
22

33
import net.sf.jsqlparser.JSQLParserException;
4+
import net.sf.jsqlparser.expression.Expression;
45
import net.sf.jsqlparser.expression.LongValue;
56
import net.sf.jsqlparser.expression.operators.arithmetic.Addition;
67
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
78
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
89
import net.sf.jsqlparser.schema.Column;
910
import net.sf.jsqlparser.schema.Table;
1011
import net.sf.jsqlparser.statement.select.Join;
12+
import net.sf.jsqlparser.statement.select.PlainSelect;
1113
import net.sf.jsqlparser.statement.select.Select;
14+
import net.sf.jsqlparser.statement.select.SelectExpressionItem;
1215
import org.junit.After;
1316
import org.junit.AfterClass;
1417
import org.junit.Before;
@@ -68,4 +71,25 @@ public void testAddJoin() throws JSQLParserException {
6871
addJoin.setLeft(true);
6972
assertEquals("SELECT a FROM mytable LEFT JOIN mytable2 ON a = b", select.toString());
7073
}
74+
75+
@Test
76+
public void testBuildSelectFromTableAndExpressions() {
77+
Select select = SelectUtils.buildSelectFromTableAndExpressions(new Table("mytable"), new Column("a"), new Column("b"));
78+
assertEquals("SELECT a, b FROM mytable", select.toString());
79+
}
80+
81+
@Test
82+
public void testBuildSelectFromTable() {
83+
Select select = SelectUtils.buildSelectFromTable(new Table("mytable"));
84+
assertEquals("SELECT * FROM mytable", select.toString());
85+
}
86+
87+
@Test
88+
public void testBuildSelectFromTableAndParsedExpression() throws JSQLParserException {
89+
Select select = SelectUtils.buildSelectFromTableAndExpressions(new Table("mytable"), "a+b", "test");
90+
assertEquals("SELECT a + b, test FROM mytable", select.toString());
91+
92+
assertTrue(((SelectExpressionItem)((PlainSelect)select.getSelectBody()).getSelectItems().get(0)).getExpression() instanceof Addition);
93+
}
94+
7195
}

0 commit comments

Comments
 (0)