File tree Expand file tree Collapse file tree 4 files changed +36
-1
lines changed
jjtree/net/sf/jsqlparser/parser
test/java/net/sf/jsqlparser/statement/truncate Expand file tree Collapse file tree 4 files changed +36
-1
lines changed Original file line number Diff line number Diff line change 3131public class Truncate implements Statement {
3232
3333 private Table table ;
34+ boolean cascade ; // to support TRUNCATE TABLE ... CASCADE
3435
3536 @ Override
3637 public void accept (StatementVisitor statementVisitor ) {
@@ -45,8 +46,19 @@ public void setTable(Table table) {
4546 this .table = table ;
4647 }
4748
49+ public boolean getCascade (){
50+ return cascade ;
51+ }
52+
53+ public void setCascade (boolean c ){
54+ cascade =c ;
55+ }
56+
4857 @ Override
4958 public String toString () {
59+ if (cascade ){
60+ return "TRUNCATE TABLE " + table +" CASCADE" ;
61+ }
5062 return "TRUNCATE TABLE " + table ;
5163 }
5264}
Original file line number Diff line number Diff line change @@ -144,6 +144,11 @@ public void visit(Select select) {
144144
145145 @ Override
146146 public void visit (Truncate truncate ) {
147+ buffer .append ("TRUNCATE TABLE " );
148+ buffer .append (truncate .getTable ());
149+ if (truncate .getCascade ()){
150+ buffer .append (" CASCADE" );
151+ }
147152 }
148153
149154 @ Override
Original file line number Diff line number Diff line change @@ -3449,7 +3449,7 @@ Truncate Truncate():
34493449}
34503450{
34513451 <K_TRUNCATE> <K_TABLE>
3452- table=Table() { truncate.setTable(table); }
3452+ table=Table() { truncate.setTable(table); truncate.setCascade(false); } [ <K_CASCADE> {truncate.setCascade(true);} ]
34533453 {
34543454 return truncate;
34553455 }
Original file line number Diff line number Diff line change 22
33import java .io .StringReader ;
44
5+ import static net .sf .jsqlparser .test .TestUtils .*;
6+ import net .sf .jsqlparser .*;
7+
58import net .sf .jsqlparser .parser .CCJSqlParserManager ;
69import static org .junit .Assert .assertEquals ;
710import org .junit .Test ;
@@ -23,5 +26,20 @@ public void testTruncate() throws Exception {
2326 truncate = (Truncate ) parserManager .parse (new StringReader (statement ));
2427 assertEquals ("mytab" , truncate .getTable ().getName ());
2528 assertEquals (toStringStatement .toUpperCase (), truncate .toString ().toUpperCase ());
29+
30+ statement = "TRUNCATE TABLE mytab CASCADE" ;
31+ truncate = (Truncate ) parserManager .parse (new StringReader (statement ));
32+ assertEquals (statement , truncate .toString ());
33+ }
34+
35+ @ Test
36+ public void testTruncateDeparse () throws JSQLParserException {
37+ assertSqlCanBeParsedAndDeparsed ("TRUNCATE TABLE foo" );
2638 }
39+
40+ @ Test
41+ public void testTruncateCascadeDeparse () throws JSQLParserException {
42+ assertSqlCanBeParsedAndDeparsed ("TRUNCATE TABLE foo CASCADE" );
43+ }
44+
2745}
You can’t perform that action at this time.
0 commit comments