Skip to content

Closes #1583:: Implement Postgresql optional TABLE in TRUNCATE #1585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions src/main/java/net/sf/jsqlparser/statement/truncate/Truncate.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public class Truncate implements Statement {
private Table table;
boolean cascade; // to support TRUNCATE TABLE ... CASCADE

boolean tableToken; // to support TRUNCATE without TABLE
boolean only; // to support TRUNCATE with ONLY

@Override
public void accept(StatementVisitor statementVisitor) {
statementVisitor.visit(this);
Expand All @@ -41,10 +44,42 @@ public void setCascade(boolean c) {

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("TRUNCATE");
if (tableToken) {
sb.append(" TABLE");
}
if (only) {
sb.append(" ONLY");
}
sb.append(" ");
sb.append(table);

if (cascade) {
return "TRUNCATE TABLE " + table + " CASCADE";
sb.append( " CASCADE");
}
return "TRUNCATE TABLE " + table;
return sb.toString();
}

public boolean isTableToken() {
return tableToken;
}

public void setTableToken(boolean hasTable) {
this.tableToken = hasTable;
}

public boolean isOnly() {
return only;
}

public void setOnly(boolean only) {
this.only = only;
}

public Truncate withTableToken(boolean hasTableToken){
this.setTableToken(hasTableToken);
return this;
}

public Truncate withTable(Table table) {
Expand All @@ -56,4 +91,9 @@ public Truncate withCascade(boolean cascade) {
this.setCascade(cascade);
return this;
}
public Truncate withOnly(boolean only) {
this.setOnly(only);
return this;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,20 @@ public void visit(Select select) {

@Override
public void visit(Truncate truncate) {
buffer.append("TRUNCATE TABLE ");
buffer.append("TRUNCATE");
if (truncate.isTableToken()) {
buffer.append(" TABLE");
}
if (truncate.isOnly()) {
buffer.append(" ONLY");
}
buffer.append(" ");
buffer.append(truncate.getTable());

if (truncate.getCascade()) {
buffer.append(" CASCADE");
buffer.append( " CASCADE");
}

}

@Override
Expand Down
10 changes: 9 additions & 1 deletion src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -5451,7 +5451,15 @@ Truncate Truncate():
Table table;
}
{
<K_TRUNCATE> <K_TABLE>
/**
* TRUNCATE can be followed directly by the table name in Postgresql
* See: https://www.postgresql.org/docs/current/sql-truncate.html
*
* TRUNCATE [ TABLE ] [ ONLY ] name [ * ] [, ... ]
* [ RESTART IDENTITY | CONTINUE IDENTITY ] [ CASCADE | RESTRICT ]
*
*/
<K_TRUNCATE> [<K_TABLE> {truncate.setTableToken(true);}] [<K_ONLY> {truncate.setOnly(true);}]
table=Table() { truncate.setTable(table); truncate.setCascade(false); } [ <K_CASCADE> {truncate.setCascade(true);} ]
{
return truncate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
package net.sf.jsqlparser.statement.truncate;

import java.io.StringReader;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.schema.Table;

import static net.sf.jsqlparser.test.TestUtils.assertDeparse;
import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class TruncateTest {
Expand All @@ -39,20 +42,57 @@ public void testTruncate() throws Exception {
statement = "TRUNCATE TABLE mytab CASCADE";
truncate = (Truncate) parserManager.parse(new StringReader(statement));
assertEquals(statement, truncate.toString());

statement = "TRUNCATE TABLE ONLY mytab CASCADE";
truncate = (Truncate) parserManager.parse(new StringReader(statement));
assertEquals(statement, truncate.toString());
}

@Test
public void testTruncatePostgresqlWithoutTableName() throws Exception {
String statement = "TRUncATE myschema.mytab";
Truncate truncate = (Truncate) parserManager.parse(new StringReader(statement));
assertEquals("myschema", truncate.getTable().getSchemaName());
assertEquals("myschema.mytab", truncate.getTable().getFullyQualifiedName());
assertEquals("TRUNCATE MYSCHEMA.MYTAB", truncate.toString().toUpperCase());

statement = "TRUncATE mytab";
truncate = (Truncate) parserManager.parse(new StringReader(statement));
assertEquals("mytab", truncate.getTable().getName());
assertEquals("TRUNCATE MYTAB", truncate.toString().toUpperCase());

statement = "TRUNCATE mytab CASCADE";
truncate = (Truncate) parserManager.parse(new StringReader(statement));
assertEquals("TRUNCATE MYTAB CASCADE", truncate.toString().toUpperCase());
}

@Test
public void testTruncateDeparse() throws JSQLParserException {
String statement = "TRUNCATE TABLE foo";
assertSqlCanBeParsedAndDeparsed(statement);
assertDeparse(new Truncate().withTable(new Table("foo")), statement);
assertDeparse(new Truncate()
.withTable(new Table("foo"))
.withTableToken(true), statement);
}

@Test
public void testTruncateCascadeDeparse() throws JSQLParserException {
String statement = "TRUNCATE TABLE foo CASCADE";
assertSqlCanBeParsedAndDeparsed(statement);
assertDeparse(new Truncate().withTable(new Table("foo")).withCascade(true), statement);
assertDeparse(new Truncate()
.withTable(new Table("foo"))
.withTableToken(true)
.withCascade(true), statement);
}

@Test
public void testTruncateOnlyDeparse() throws JSQLParserException {
String statement = "TRUNCATE TABLE ONLY foo CASCADE";
assertSqlCanBeParsedAndDeparsed(statement);
assertDeparse(new Truncate()
.withTable(new Table("foo"))
.withCascade(true)
.withTableToken(true)
.withOnly(true), statement);
}
}