Skip to content

Commit

Permalink
Implements Oracle RENAME oldTable TO newTable Statement (#1286)
Browse files Browse the repository at this point in the history
* Implements Oracle RENAME oldTable TO newTable Statement
Fixes #1253

* Implement MariaDB specific syntax

* Remove redundant License Headers

* Use LinkedHashMap to preserve the order of the Entries

* Increase Test Coverage

Co-authored-by: Tobias <t.warneke@gmx.net>
  • Loading branch information
manticore-projects and wumpz authored Jul 26, 2021
1 parent f86bc2e commit f7f7bcd
Show file tree
Hide file tree
Showing 8 changed files with 329 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import net.sf.jsqlparser.statement.alter.Alter;
import net.sf.jsqlparser.statement.alter.AlterSession;
import net.sf.jsqlparser.statement.alter.RenameTableStatement;
import net.sf.jsqlparser.statement.alter.sequence.AlterSequence;
import net.sf.jsqlparser.statement.comment.Comment;
import net.sf.jsqlparser.statement.create.index.CreateIndex;
Expand Down Expand Up @@ -112,5 +113,7 @@ public interface StatementVisitor {

void visit(AlterSession alterSession);

void visit(RenameTableStatement renameTableStatement);

void visit(PurgeStatement purgeStatement);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import net.sf.jsqlparser.statement.alter.Alter;
import net.sf.jsqlparser.statement.alter.AlterSession;
import net.sf.jsqlparser.statement.alter.RenameTableStatement;
import net.sf.jsqlparser.statement.alter.sequence.AlterSequence;
import net.sf.jsqlparser.statement.comment.Comment;
import net.sf.jsqlparser.statement.create.index.CreateIndex;
Expand Down Expand Up @@ -212,7 +213,9 @@ public void visit(AlterSession alterSession) {
}

@Override
public void visit(RenameTableStatement renameTableStatement) {
}

public void visit(PurgeStatement purgeStatement) {
//@todo: do something usefull here
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2021 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/

package net.sf.jsqlparser.statement.alter;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.StatementVisitor;

/**
*
* @author are
* @see <a href="https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_9019.htm">Rename</a>
*/
public class RenameTableStatement implements Statement {
private final LinkedHashMap<Table, Table> tableNames = new LinkedHashMap<>();

private boolean usingTableKeyword = false;
private boolean usingIfExistsKeyword = false;

private String waitDirective = "";

public RenameTableStatement(Table oldName, Table newName) {
tableNames.put(
Objects.requireNonNull(oldName, "The OLD NAME of the Rename Statement must not be null.")
, Objects.requireNonNull(newName, "The NEW NAME of the Rename Statement must not be null.")
);
}

public RenameTableStatement(Table oldName, Table newName, boolean usingTableKeyword, boolean usingIfExistsKeyword, String waitDirective) {
tableNames.put(
Objects.requireNonNull(oldName, "The OLD NAME of the Rename Statement must not be null.")
, Objects.requireNonNull(newName, "The NEW NAME of the Rename Statement must not be null.")
);

this.usingTableKeyword = usingTableKeyword;
this.usingIfExistsKeyword = usingIfExistsKeyword;
this.waitDirective = waitDirective;
}

public void addTableNames(Table oldName, Table newName) {
tableNames.put(
Objects.requireNonNull(oldName, "The OLD NAME of the Rename Statement must not be null.")
, Objects.requireNonNull(newName, "The NEW NAME of the Rename Statement must not be null.")
);
}


public boolean isUsingTableKeyword() {
return usingTableKeyword;
}

public void setUsingTableKeyword(boolean usingTableKeyword) {
this.usingTableKeyword = usingTableKeyword;
}

public RenameTableStatement withUsingTableKeyword(boolean usingTableKeyword) {
this.usingTableKeyword = usingTableKeyword;
return this;
}

public boolean isUsingIfExistsKeyword() {
return usingIfExistsKeyword;
}

public void setUsingIfExistsKeyword(boolean usingIfExistsKeyword) {
this.usingIfExistsKeyword = usingIfExistsKeyword;
}

public RenameTableStatement withUsingIfExistsKeyword(boolean usingIfExistsKeyword) {
this.usingIfExistsKeyword = usingIfExistsKeyword;
return this;
}

public String getWaitDirective() {
return waitDirective;
}

public void setWaitDirective(String waitDirective) {
this.waitDirective = waitDirective;
}

public RenameTableStatement withWaitDirective(String waitDirective) {
this.waitDirective = waitDirective;
return this;
}

public int getTableNamesSize() {
return tableNames.size();
}

public boolean isTableNamesEmpty() {
return tableNames.isEmpty();
}

public Set<Map.Entry<Table, Table>> getTableNames() {
return tableNames.entrySet();
}

@Override
public void accept(StatementVisitor statementVisitor) {
statementVisitor.visit(this);
}

public StringBuilder appendTo(StringBuilder builder) {
int i=0;
for (Entry<Table, Table> e : tableNames.entrySet()) {
if (i==0) {
builder
.append("RENAME")
.append(usingTableKeyword ? " TABLE " : " ")
.append(usingIfExistsKeyword ? " IF EXISTS " : " ")
.append(e.getKey())
.append(waitDirective!=null && waitDirective.length()>0 ? " " + waitDirective : "")
.append(" TO ")
.append(e.getValue());
} else {
builder
.append(", ")
.append(e.getKey())
.append(" TO ")
.append(e.getValue());
}

i++;
}
return builder;
}

@Override
public String toString() {
return appendTo(new StringBuilder()).toString();
}
}
10 changes: 10 additions & 0 deletions src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.jsqlparser.expression.AnalyticExpression;
import net.sf.jsqlparser.expression.AnyComparisonExpression;
Expand Down Expand Up @@ -68,6 +69,7 @@
import net.sf.jsqlparser.statement.*;
import net.sf.jsqlparser.statement.alter.Alter;
import net.sf.jsqlparser.statement.alter.AlterSession;
import net.sf.jsqlparser.statement.alter.RenameTableStatement;
import net.sf.jsqlparser.statement.alter.sequence.AlterSequence;
import net.sf.jsqlparser.statement.comment.Comment;
import net.sf.jsqlparser.statement.create.index.CreateIndex;
Expand Down Expand Up @@ -1025,6 +1027,14 @@ public void visit(JsonFunction expression) {
}
}

@Override
public void visit(RenameTableStatement renameTableStatement) {
for (Map.Entry<Table, Table> e : renameTableStatement.getTableNames()) {
e.getKey().accept(this);
e.getValue().accept(this);
}
}

@Override
public void visit(PurgeStatement purgeStatement) {
if (purgeStatement.getPurgeObjectType()== PurgeObjectType.TABLE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import net.sf.jsqlparser.statement.UseStatement;
import net.sf.jsqlparser.statement.alter.Alter;
import net.sf.jsqlparser.statement.alter.AlterSession;
import net.sf.jsqlparser.statement.alter.RenameTableStatement;
import net.sf.jsqlparser.statement.alter.sequence.AlterSequence;
import net.sf.jsqlparser.statement.comment.Comment;
import net.sf.jsqlparser.statement.create.index.CreateIndex;
Expand Down Expand Up @@ -351,6 +352,11 @@ public void visit(AlterSession alterSession) {
new AlterSessionDeParser(buffer).deParse(alterSession);
}

@Override
public void visit(RenameTableStatement renameTableStatement) {
renameTableStatement.appendTo(buffer);
}

@Override
public void visit(PurgeStatement purgeStatement) {
purgeStatement.appendTo(buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import net.sf.jsqlparser.statement.UseStatement;
import net.sf.jsqlparser.statement.alter.Alter;
import net.sf.jsqlparser.statement.alter.AlterSession;
import net.sf.jsqlparser.statement.alter.RenameTableStatement;
import net.sf.jsqlparser.statement.alter.sequence.AlterSequence;
import net.sf.jsqlparser.statement.comment.Comment;
import net.sf.jsqlparser.statement.create.function.CreateFunction;
Expand Down Expand Up @@ -266,21 +267,21 @@ public void visit(CreateSynonym createSynonym) {

@Override
public void visit(SavepointStatement savepointStatement) {
//@todo: write something usefull here
}

@Override
public void visit(RollbackStatement rollbackStatement) {
//@todo: write something usefull here
}

@Override
public void visit(AlterSession alterSession) {
//@todo: write something usefull here
}

@Override
public void visit(PurgeStatement purgeStatement) {
//@todo: write something usefull here
public void visit(RenameTableStatement renameTableStatement) {
}

@Override
public void visit(PurgeStatement purgeStatement) {
}
}
43 changes: 43 additions & 0 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,8 @@ Statement SingleStatement() :
|
stm = Set()
|
stm = RenameTableStatement()
|
stm = Reset()
|
LOOKAHEAD(ShowColumns())
Expand Down Expand Up @@ -755,6 +757,47 @@ ResetStatement Reset(): {
{ return reset; }
}

RenameTableStatement RenameTableStatement(): {
RenameTableStatement renameTableStatement;
Table oldName;
Table newName;
boolean usingTableKeyword=false;
boolean usesIfExistsKeyword=false;
String waitDirective = "";
Token token;
}
{
<K_RENAME>
[ LOOKAHEAD(2) <K_TABLE> { usingTableKeyword = true; } ]
[ LOOKAHEAD(2) <K_IF> <K_EXISTS> { usesIfExistsKeyword = true; } ]
oldName = Table()
[ (
<K_WAIT> token=<S_LONG> { waitDirective = "WAIT " + token.image; }
|
<K_NOWAIT> { waitDirective = "NOWAIT"; }
) ]
<K_TO>
newName = Table()

{
renameTableStatement = new RenameTableStatement(oldName, newName, usingTableKeyword, usesIfExistsKeyword, waitDirective);
}

(
","
oldName = Table()
<K_TO>
newName = Table()
{
renameTableStatement.addTableNames(oldName, newName);
}
)*

{
return renameTableStatement;
}
}

PurgeStatement PurgeStatement(): {
PurgeStatement purgeStatement = null;
Table table;
Expand Down
Loading

0 comments on commit f7f7bcd

Please sign in to comment.