-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
456ed9b
commit 375f356
Showing
19 changed files
with
396 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.exasol.sql.ddl.drop; | ||
|
||
import com.exasol.sql.AbstractFragment; | ||
import com.exasol.sql.Fragment; | ||
|
||
/** | ||
* This class represents CASCADE clause in DROP SCHEMA SQL statement | ||
*/ | ||
public final class Cascade extends AbstractFragment implements DropSchemaFragment { | ||
/** | ||
* Create an instance of {@link Cascade} class | ||
* | ||
* @param root root SQL statement this fragment belongs to | ||
*/ | ||
protected Cascade(final Fragment root) { | ||
super(root); | ||
} | ||
|
||
@Override | ||
public void accept(final DropSchemaVisitor visitor) { | ||
visitor.visit(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package com.exasol.sql.ddl.drop; | ||
|
||
import com.exasol.sql.AbstractFragment; | ||
import com.exasol.sql.SqlStatement; | ||
import com.exasol.sql.ddl.Schema; | ||
|
||
/** | ||
* This class implements an SQL {@link DropSchema} statement | ||
*/ | ||
public class DropSchema extends AbstractFragment implements SqlStatement, DropSchemaFragment { | ||
private Schema schema; | ||
private Cascade cascade; | ||
private Restrict restrict; | ||
private boolean ifExists = false; | ||
|
||
/** | ||
* Create a new instance of an {@link DropSchema} statement | ||
* | ||
* @param schemaName name of the table to drop | ||
*/ | ||
public DropSchema(final String schemaName) { | ||
super(null); | ||
this.schema = new Schema(this, schemaName); | ||
} | ||
|
||
/** | ||
* Add IF EXISTS clause into a DROP SCHEMA statement | ||
* | ||
* @return <code>this</code> for fluent programming | ||
*/ | ||
public synchronized DropSchema ifExists() { | ||
if (!this.ifExists) { | ||
this.ifExists = true; | ||
} | ||
return this; | ||
} | ||
|
||
/** | ||
* Add CASCADE clause into a DROP SCHEMA statement | ||
* | ||
* @return <code>this</code> for fluent programming | ||
*/ | ||
public synchronized DropSchema cascade() { | ||
cascade = new Cascade(this); | ||
return this; | ||
} | ||
|
||
/** | ||
* Add RESTRICT clause into a DROP SCHEMA statement | ||
* | ||
* @return <code>this</code> for fluent programming | ||
*/ | ||
public synchronized DropSchema restrict() { | ||
restrict = new Restrict(this); | ||
return this; | ||
} | ||
|
||
/** | ||
* Get a schema name | ||
* | ||
* @return schema name | ||
*/ | ||
public String getSchemaName() { | ||
return schema.getName(); | ||
} | ||
|
||
public Cascade getCascade() { | ||
return cascade; | ||
} | ||
|
||
public Restrict getRestrict() { | ||
return restrict; | ||
} | ||
|
||
@Override | ||
public void accept(DropSchemaVisitor visitor) { | ||
validateCascadeAndRestrict(); | ||
visitor.visit(this); | ||
this.schema.accept(visitor); | ||
if (cascade != null) { | ||
cascade.accept(visitor); | ||
} | ||
if (restrict != null) { | ||
restrict.accept(visitor); | ||
} | ||
} | ||
|
||
/** | ||
* Get true when IF EXISTS clause presents | ||
* | ||
* @return if exists | ||
*/ | ||
public boolean getIfExists() { | ||
return ifExists; | ||
} | ||
|
||
private void validateCascadeAndRestrict() { | ||
if (cascade != null && restrict != null) { | ||
throw new IllegalArgumentException( | ||
"DROP SCHEMA expression must not contain CASCADE and RESTRICT clauses at the came time. " | ||
+ "Use only one of them."); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/exasol/sql/ddl/drop/DropSchemaFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.exasol.sql.ddl.drop; | ||
|
||
import com.exasol.sql.Fragment; | ||
|
||
/** | ||
* This is the common interface for all fragments of a DROP SCHEMA statement. | ||
*/ | ||
public interface DropSchemaFragment extends Fragment { | ||
/** | ||
* Accept a visitor (e.g. a renderer or validator) | ||
* | ||
* @param visitor visitor to accept | ||
*/ | ||
public void accept(DropSchemaVisitor visitor); | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/exasol/sql/ddl/drop/DropSchemaVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.exasol.sql.ddl.drop; | ||
|
||
import com.exasol.sql.ddl.Schema; | ||
|
||
public interface DropSchemaVisitor { | ||
public void visit(DropSchema dropSchema); | ||
|
||
public void visit(Schema schema); | ||
|
||
public void visit(Cascade cascade); | ||
|
||
public void visit(Restrict restrict); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.exasol.sql.ddl.drop; | ||
|
||
import com.exasol.sql.AbstractFragment; | ||
import com.exasol.sql.Fragment; | ||
|
||
/** | ||
* This class represents RESTRICT clause in DROP SCHEMA SQL statement | ||
*/ | ||
public final class Restrict extends AbstractFragment implements DropSchemaFragment { | ||
/** | ||
* Create an instance of {@link Restrict} class | ||
* | ||
* @param root root SQL statement this fragment belongs to | ||
*/ | ||
protected Restrict(final Fragment root) { | ||
super(root); | ||
} | ||
|
||
@Override | ||
public void accept(final DropSchemaVisitor visitor) { | ||
visitor.visit(this); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/com/exasol/sql/ddl/drop/rendering/DropSchemaRenderer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.exasol.sql.ddl.drop.rendering; | ||
|
||
import com.exasol.sql.ddl.Schema; | ||
import com.exasol.sql.ddl.drop.*; | ||
import com.exasol.sql.rendering.AbstractFragmentRenderer; | ||
import com.exasol.sql.rendering.StringRendererConfig; | ||
|
||
/** | ||
* The {@link DropSchemaRenderer} turns SQL statement structures in to SQL strings. | ||
*/ | ||
public class DropSchemaRenderer extends AbstractFragmentRenderer implements DropSchemaVisitor { | ||
/** | ||
* Create a new {@link DropSchemaRenderer} with custom render settings. | ||
* | ||
* @param config render configuration settings | ||
*/ | ||
public DropSchemaRenderer(final StringRendererConfig config) { | ||
super(config); | ||
} | ||
|
||
/** | ||
* Create an {@link DropSchemaRenderer} using the default renderer configuration | ||
* | ||
* @return DROP SCHEMA renderer | ||
*/ | ||
public static DropSchemaRenderer create() { | ||
return new DropSchemaRenderer(StringRendererConfig.createDefault()); | ||
} | ||
|
||
/** | ||
* Create an {@link DropSchemaRenderer} | ||
* | ||
* @param config renderer configuration | ||
* @return DROP SCHEMA renderer | ||
*/ | ||
public static DropSchemaRenderer create(final StringRendererConfig config) { | ||
return new DropSchemaRenderer(config); | ||
} | ||
|
||
@Override | ||
public void visit(DropSchema dropSchema) { | ||
appendKeyWord("DROP SCHEMA "); | ||
if (dropSchema.getIfExists()) { | ||
appendKeyWord("IF EXISTS "); | ||
} | ||
setLastVisited(dropSchema); | ||
} | ||
|
||
@Override | ||
public void visit(Schema schema) { | ||
appendAutoQuoted(schema.getName()); | ||
setLastVisited(schema); | ||
} | ||
|
||
@Override | ||
public void visit(Cascade cascade) { | ||
appendKeyWord(" CASCADE"); | ||
setLastVisited(cascade); | ||
} | ||
|
||
@Override | ||
public void visit(Restrict restrict) { | ||
appendKeyWord(" RESTRICT"); | ||
setLastVisited(restrict); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.