Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public <S> T visit(ParenthesedInsert insert, S context) {
@Override
public <S> T visit(Drop drop, S context) {
if (drop.getType().equalsIgnoreCase("table")) {
fromItemVisitor.visitFromItem(drop.getName(), context);
drop.getNames().forEach(name -> fromItemVisitor.visitFromItem(name, context));
}
// @todo: handle schemas

Expand Down
63 changes: 58 additions & 5 deletions src/main/java/net/sf/jsqlparser/statement/drop/Drop.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
Expand All @@ -24,8 +26,13 @@

public class Drop implements Statement {

public enum ObjectType {
DATABASE, EVENT, FUNCTION, INDEX, PROCEDURE, SCHEMA, SEQUENCE, SERVER, TABLE, TABLESPACE, TRIGGER, VIEW, OTHER
}

private String type;
private Table name;
private ObjectType objectType = ObjectType.OTHER;
private final List<Table> names = new ArrayList<>();
private List<String> parameters;
private Map<String, List<String>> typeToParameters = new HashMap<>();
private boolean ifExists = false;
Expand All @@ -46,11 +53,25 @@ public <T, S> T accept(StatementVisitor<T> statementVisitor, S context) {
}

public Table getName() {
return name;
return names.isEmpty() ? null : names.get(0);
}

public void setName(Table name) {
names.clear();
if (name != null) {
names.add(name);
}
}

public List<Table> getNames() {
return Collections.unmodifiableList(names);
}

public void setName(Table string) {
name = string;
public void setNames(Collection<? extends Table> names) {
this.names.clear();
if (names != null) {
this.names.addAll(names);
}
}

public List<String> getParameters() {
Expand All @@ -67,6 +88,22 @@ public String getType() {

public void setType(String string) {
type = string;
try {
objectType = ObjectType.valueOf(string.toUpperCase(Locale.ROOT));
} catch (IllegalArgumentException | NullPointerException ignored) {
objectType = ObjectType.OTHER;
}
}

public ObjectType getObjectType() {
return objectType;
}

public void setObjectType(ObjectType objectType) {
this.objectType = objectType == null ? ObjectType.OTHER : objectType;
if (this.objectType != ObjectType.OTHER) {
this.type = this.objectType.name();
}
}

public boolean isIfExists() {
Expand Down Expand Up @@ -112,7 +149,8 @@ public String toString() {
+ (isUsingTemporary ? "TEMPORARY " : "")
+ (materialized ? "MATERIALIZED " : "")
+ type + " "
+ (ifExists ? "IF EXISTS " : "") + name.toString();
+ (ifExists ? "IF EXISTS " : "") + names.stream().map(Table::toString)
.collect(Collectors.joining(", "));

if (type.equals("FUNCTION")) {
sql += formatFuncParams(getParamsByType("FUNCTION"));
Expand Down Expand Up @@ -149,6 +187,21 @@ public Drop withName(Table name) {
return this;
}

public Drop withNames(Collection<? extends Table> names) {
setNames(names);
return this;
}

public Drop addNames(Table... names) {
Collections.addAll(this.names, names);
return this;
}

public Drop withObjectType(ObjectType objectType) {
setObjectType(objectType);
return this;
}

public Drop withParameters(List<String> parameters) {
this.setParameters(parameters);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1502,7 +1502,7 @@ public void visit(Analyze analyze) {

@Override
public <S> Void visit(Drop drop, S context) {
visit(drop.getName(), context);
drop.getNames().forEach(name -> visit(name, context));
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
package net.sf.jsqlparser.util.deparser;

import java.util.stream.Collectors;
import net.sf.jsqlparser.statement.drop.Drop;
import net.sf.jsqlparser.statement.select.PlainSelect;

Expand All @@ -32,7 +33,8 @@ public void deParse(Drop drop) {
builder.append(" IF EXISTS");
}

builder.append(" ").append(drop.getName());
builder.append(" ").append(drop.getNames().stream().map(Object::toString)
.collect(Collectors.joining(", ")));

if (drop.getType().equals("FUNCTION")) {
builder.append(Drop.formatFuncParams(drop.getParamsByType("FUNCTION")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void validate(Drop drop) {

NamedObject named = NamedObject.forName(type);
if (Arrays.asList(NamedObject.table, NamedObject.view).contains(named)) {
validateName(named, drop.getName().getFullyQualifiedName());
drop.getNames().forEach(name -> validateName(named, name.getFullyQualifiedName()));
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -12400,17 +12400,24 @@ Drop Drop():
|
tk=<K_VIEW>
|
tk=<K_DATABASE>
|
tk=<K_PROCEDURE>
|
tk=<K_SCHEMA>
|
tk=<K_SEQUENCE>
|
tk=<K_TRIGGER>
|
tk=<K_FUNCTION>
)
{ drop.setType(tk.image); }

[ LOOKAHEAD(2) <K_IF> <K_EXISTS> {drop.setIfExists(true);} ]

name = Table() { drop.setName(name); }
( "," name = Table() { drop.addNames(name); } )*
[ LOOKAHEAD(2) funcArgs = FuncArgsList() ]
(
(
Expand Down
45 changes: 45 additions & 0 deletions src/test/java/net/sf/jsqlparser/statement/drop/DropTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
package net.sf.jsqlparser.statement.drop;

import java.io.StringReader;
import java.util.List;
import java.util.stream.Collectors;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
Expand Down Expand Up @@ -169,4 +171,47 @@ public void testDropTableFollowedByLockTableIssue2490() throws JSQLParserExcepti
"DROP TABLE t1; LOCK TABLE t2 IN SHARE MODE;");
assertEquals(2, statements.size());
}

@Test
public void testMySqlDropObjectTypes() throws JSQLParserException {
Drop database = (Drop) assertSqlCanBeParsedAndDeparsed(
"DROP DATABASE IF EXISTS database_1");
assertEquals(Drop.ObjectType.DATABASE, database.getObjectType());

Drop procedure = (Drop) assertSqlCanBeParsedAndDeparsed(
"DROP PROCEDURE IF EXISTS procedure_1");
assertEquals(Drop.ObjectType.PROCEDURE, procedure.getObjectType());

Drop trigger = (Drop) assertSqlCanBeParsedAndDeparsed(
"DROP TRIGGER IF EXISTS schema_1.trigger_1");
assertEquals(Drop.ObjectType.TRIGGER, trigger.getObjectType());
assertEquals("schema_1.trigger_1", trigger.getName().getFullyQualifiedName());
}

@Test
public void testMySqlDropMultipleTables() throws JSQLParserException {
String sql = "DROP TABLE IF EXISTS table_1, table_2, table_3 RESTRICT";
Drop drop = (Drop) assertSqlCanBeParsedAndDeparsed(sql);

assertEquals(List.of("table_1", "table_2", "table_3"), drop.getNames().stream()
.map(Table::getFullyQualifiedName).collect(Collectors.toList()));
assertEquals("table_1", drop.getName().getFullyQualifiedName());
assertEquals(List.of("RESTRICT"), drop.getParameters());

assertDeparse(new Drop().withObjectType(Drop.ObjectType.TABLE).withIfExists(true)
.withNames(List.of(new Table("table_1"), new Table("table_2"),
new Table("table_3")))
.addParameters("RESTRICT"), sql);
}

@Test
public void testMySqlDropMultipleViews() throws JSQLParserException {
Drop drop = (Drop) assertSqlCanBeParsedAndDeparsed(
"DROP VIEW IF EXISTS view_1, view_2 CASCADE");

assertEquals(Drop.ObjectType.VIEW, drop.getObjectType());
assertEquals(List.of("view_1", "view_2"), drop.getNames().stream()
.map(Table::getFullyQualifiedName).collect(Collectors.toList()));
assertEquals(List.of("CASCADE"), drop.getParameters());
}
}
Loading