Skip to content

Commit 3e16345

Browse files
KyongSik-Yoonwumpz
authored andcommitted
Implements #509 (#504)
* Supporting MySql hit SQL_CALC_FOUND_ROWS for selecting row count. * Supporting MySql hit SQL_CALC_FOUND_ROWS for selecting row count. - refactoring * Supporting MySql hit SQL_CALC_FOUND_ROWS for selecting row count. - missing copyright.ˆ * Supporting MySql hit SQL_CALC_FOUND_ROWS for selecting row count. - Modify field type to boolean for prevent memory consumption by creating object and try assertSqlCanBeParsedAndDeparsed on unit test.
1 parent 45ac8c8 commit 3e16345

File tree

8 files changed

+347
-4
lines changed

8 files changed

+347
-4
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.expression;
23+
24+
/**
25+
* @author sam
26+
*/
27+
public class MySqlSqlCalcFoundRows {
28+
public static final String DESCRIPTION = "SQL_CALC_FOUND_ROWS";
29+
30+
public String getDescription() {
31+
return DESCRIPTION;
32+
}
33+
}

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

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

24-
import java.util.ArrayList;
25-
import java.util.Collections;
2624
import net.sf.jsqlparser.expression.Expression;
25+
import net.sf.jsqlparser.expression.OracleHierarchicalExpression;
26+
import net.sf.jsqlparser.expression.OracleHint;
2727
import net.sf.jsqlparser.schema.Table;
2828

29+
import java.util.ArrayList;
30+
import java.util.Collections;
2931
import java.util.Iterator;
3032
import java.util.List;
31-
import net.sf.jsqlparser.expression.OracleHierarchicalExpression;
32-
import net.sf.jsqlparser.expression.OracleHint;
3333

3434
/**
3535
* The core of a "SELECT" statement (no UNION, no ORDER BY)
@@ -58,6 +58,7 @@ public class PlainSelect implements SelectBody {
5858
private Table forUpdateTable = null;
5959
private boolean useBrackets = false;
6060
private Wait wait;
61+
private boolean mySqlSqlCalcFoundRows = false;
6162

6263
public boolean isUseBrackets() {
6364
return useBrackets;
@@ -311,6 +312,9 @@ public String toString() {
311312
if (top != null) {
312313
sql.append(top).append(" ");
313314
}
315+
if (mySqlSqlCalcFoundRows) {
316+
sql.append("SQL_CALC_FOUND_ROWS").append(" ");
317+
}
314318
sql.append(getStringList(selectItems));
315319

316320
if (intoTables != null) {
@@ -455,4 +459,12 @@ public static String getStringList(List<?> list, boolean useComma, boolean useBr
455459

456460
return ans.toString();
457461
}
462+
463+
public void setMySqlSqlCalcFoundRows(boolean mySqlCalcFoundRows) {
464+
this.mySqlSqlCalcFoundRows = mySqlCalcFoundRows;
465+
}
466+
467+
public boolean getMySqlSqlCalcFoundRows() {
468+
return this.mySqlSqlCalcFoundRows;
469+
}
458470
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,16 @@ public void visit(PlainSelect plainSelect) {
9191
}
9292

9393
}
94+
9495
Top top = plainSelect.getTop();
9596
if (top != null) {
9697
buffer.append(top).append(" ");
9798
}
9899

100+
if (plainSelect.getMySqlSqlCalcFoundRows()) {
101+
buffer.append("SQL_CALC_FOUND_ROWS").append(" ");
102+
}
103+
99104
for (Iterator<SelectItem> iter = plainSelect.getSelectItems().iterator(); iter.hasNext();) {
100105
SelectItem selectItem = iter.next();
101106
selectItem.accept(this);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
272272
| <K_UPDATE:"UPDATE">
273273
| <K_UPSERT:"UPSERT">
274274
| <K_USE : "USE">
275+
| <K_SQL_CALC_FOUND_ROWS: "SQL_CALC_FOUND_ROWS">
275276
| <K_USING:"USING">
276277
| <K_VALIDATE : "VALIDATE">
277278
| <K_VALUE:"VALUE">
@@ -1059,6 +1060,7 @@ PlainSelect PlainSelect():
10591060
List<Table> intoTables = null;
10601061
Table updateTable = null;
10611062
Wait wait = null;
1063+
boolean mySqlSqlCalcFoundRows = false;
10621064
}
10631065
{
10641066
<K_SELECT>
@@ -1080,6 +1082,10 @@ PlainSelect PlainSelect():
10801082
(
10811083
<K_UNIQUE> { Distinct distinct = new Distinct(true); plainSelect.setDistinct(distinct); }
10821084
)
1085+
|
1086+
(
1087+
<K_SQL_CALC_FOUND_ROWS> { plainSelect.setMySqlSqlCalcFoundRows(true); }
1088+
)
10831089
]
10841090

10851091
[top = Top() { plainSelect.setTop(top); } ]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.expression.mysql;
23+
24+
/**
25+
* @author sam
26+
*/
27+
public class MySqlSqlCalcFoundRowRef {
28+
public boolean sqlCalcFoundRows = false;
29+
30+
public MySqlSqlCalcFoundRowRef(boolean sqlCalcFoundRows) {
31+
this.sqlCalcFoundRows = sqlCalcFoundRows;
32+
}
33+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.expression.mysql;
23+
24+
import net.sf.jsqlparser.JSQLParserException;
25+
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
26+
import net.sf.jsqlparser.statement.Statement;
27+
import org.junit.Test;
28+
29+
import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
30+
import static org.junit.Assert.assertFalse;
31+
import static org.junit.Assert.assertTrue;
32+
33+
/**
34+
* @author sam
35+
*/
36+
public class MySqlSqlCalcFoundRowsTest {
37+
@Test
38+
public void testPossibleParsingWithSqlCalcFoundRowsHint() throws JSQLParserException {
39+
MySqlSqlCalcFoundRowRef ref = new MySqlSqlCalcFoundRowRef(false);
40+
String sqlCalcFoundRowsContainingSql = "SELECT SQL_CALC_FOUND_ROWS * FROM TABLE";
41+
String generalSql = "SELECT * FROM TABLE";
42+
43+
accept(CCJSqlParserUtil.parse(sqlCalcFoundRowsContainingSql), ref);
44+
assertTrue(ref.sqlCalcFoundRows);
45+
46+
accept(CCJSqlParserUtil.parse(generalSql), ref);
47+
assertFalse(ref.sqlCalcFoundRows);
48+
49+
assertSqlCanBeParsedAndDeparsed(sqlCalcFoundRowsContainingSql);
50+
assertSqlCanBeParsedAndDeparsed(generalSql);
51+
}
52+
53+
public void accept(Statement statement, MySqlSqlCalcFoundRowRef ref) {
54+
statement.accept(SqlCalcFoundRowsStatementVisitorFactory.create(ref));
55+
}
56+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.expression.mysql;
23+
24+
import net.sf.jsqlparser.statement.select.PlainSelect;
25+
import net.sf.jsqlparser.statement.select.SelectVisitor;
26+
import net.sf.jsqlparser.statement.select.SetOperationList;
27+
import net.sf.jsqlparser.statement.select.WithItem;
28+
29+
/**
30+
* @author sam
31+
*/
32+
public class SelectVisitorUsingSqlCalcFoundRowsFactory {
33+
public static SelectVisitor create(final MySqlSqlCalcFoundRowRef ref) {
34+
return new SelectVisitor() {
35+
@Override
36+
public void visit(PlainSelect plainSelect) {
37+
ref.sqlCalcFoundRows = plainSelect.getMySqlSqlCalcFoundRows();
38+
}
39+
40+
@Override
41+
public void visit(SetOperationList setOpList) {
42+
43+
}
44+
45+
@Override
46+
public void visit(WithItem withItem) {
47+
48+
}
49+
};
50+
}
51+
}

0 commit comments

Comments
 (0)