Skip to content

Commit

Permalink
#43: implemented drop schema logic
Browse files Browse the repository at this point in the history
  • Loading branch information
AnastasiiaSergienko committed Apr 5, 2019
1 parent 456ed9b commit 375f356
Show file tree
Hide file tree
Showing 19 changed files with 396 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.exasol</groupId>
<artifactId>sql-statement-builder</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>
<name>Exasol SQL Statement Builder</name>
<description>This module provides a Builder for SQL statements that helps creating the correct structure and validates variable parts of the statements.</description>
<url>https://github.com/exasol/sql-statement-builder</url>
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/exasol/sql/StatementFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.exasol.sql.ddl.create.CreateSchema;
import com.exasol.sql.ddl.create.CreateTable;
import com.exasol.sql.ddl.drop.DropSchema;
import com.exasol.sql.ddl.drop.DropTable;
import com.exasol.sql.dml.Insert;
import com.exasol.sql.dql.Select;
Expand Down Expand Up @@ -76,4 +77,14 @@ public CreateSchema createSchema(String schemaName) {
public DropTable dropTable(final String tableName) {
return new DropTable(tableName);
}

/**
* Create a {@link DropSchema} statement
*
* @param schemaName name of the schema to drop
* @return a new instance of a {@link DropSchema} statement
*/
public DropSchema dropSchema(String schemaName) {
return new DropSchema(schemaName);
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/exasol/sql/ddl/Schema.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.exasol.sql.AbstractFragment;
import com.exasol.sql.Fragment;
import com.exasol.sql.ddl.create.CreateSchemaVisitor;
import com.exasol.sql.ddl.drop.DropSchemaVisitor;

/**
* This class represents a {@link Schema} in an SQL Statement
Expand Down Expand Up @@ -33,4 +34,8 @@ public String getName() {
public void accept(CreateSchemaVisitor visitor) {
visitor.visit(this);
}

public void accept(DropSchemaVisitor visitor) {
visitor.visit(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public CreateSchemaRenderer(final StringRendererConfig config) {
/**
* Create an {@link CreateSchemaRenderer} using the default renderer configuration
*
* @return insert renderer
* @return CREATE SCHEMA renderer
*/
public static CreateSchemaRenderer create() {
return new CreateSchemaRenderer(StringRendererConfig.createDefault());
Expand All @@ -32,7 +32,7 @@ public static CreateSchemaRenderer create() {
* Create an {@link CreateSchemaRenderer}
*
* @param config renderer configuration
* @return create schema renderer
* @return CREATE SCHEMA renderer
*/
public static CreateSchemaRenderer create(final StringRendererConfig config) {
return new CreateSchemaRenderer(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public CreateTableRenderer(final StringRendererConfig config) {
/**
* Create an {@link CreateTableRenderer} using the default renderer configuration
*
* @return insert renderer
* @return CREATE TABLE renderer
*/
public static CreateTableRenderer create() {
return new CreateTableRenderer(StringRendererConfig.createDefault());
Expand All @@ -33,7 +33,7 @@ public static CreateTableRenderer create() {
* Create an {@link CreateTableRenderer}
*
* @param config renderer configuration
* @return create table renderer
* @return CREATE TABLE renderer
*/
public static CreateTableRenderer create(final StringRendererConfig config) {
return new CreateTableRenderer(config);
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/exasol/sql/ddl/drop/Cascade.java
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.exasol.sql.Fragment;

/**
* This class represents "cascade constraints" expression in DROP TABLE SQL statement
* This class represents CASCADE CONSTRAINTS clause in DROP TABLE SQL statement
*/
public final class CascadeConstraints extends AbstractFragment implements DropTableFragment {
/**
Expand Down
104 changes: 104 additions & 0 deletions src/main/java/com/exasol/sql/ddl/drop/DropSchema.java
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 src/main/java/com/exasol/sql/ddl/drop/DropSchemaFragment.java
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 src/main/java/com/exasol/sql/ddl/drop/DropSchemaVisitor.java
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);
}
6 changes: 3 additions & 3 deletions src/main/java/com/exasol/sql/ddl/drop/DropTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void accept(final DropTableVisitor visitor) {
}

/**
* Add "if exists" expression into a DROP TABLE statement
* Add IF EXISTS clause into a DROP TABLE statement
*
* @return <code>this</code> for fluent programming
*/
Expand All @@ -42,7 +42,7 @@ public synchronized DropTable ifExists() {
}

/**
* Add "cascade constraints" expression into a DROP TABLE statement
* Add CASCADE CONSTRAINTS clause into a DROP TABLE statement
*
* @return <code>this</code> for fluent programming
*/
Expand All @@ -52,7 +52,7 @@ public DropTable cascadeConstraints() {
}

/**
* Get true when "if exists" expression presents
* Get true when IF EXISTS clause presents
*
* @return if exists
*/
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/exasol/sql/ddl/drop/Restrict.java
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);
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public DropTableRenderer(final StringRendererConfig config) {
/**
* Create an {@link DropTableRenderer} using the default renderer configuration
*
* @return insert renderer
* @return DROP TABLE renderer
*/
public static DropTableRenderer create() {
return new DropTableRenderer(StringRendererConfig.createDefault());
Expand All @@ -31,7 +31,7 @@ public static DropTableRenderer create() {
* Create an {@link DropTableRenderer}
*
* @param config renderer configuration
* @return create table renderer
* @return DROP TABLE renderer
*/
public static DropTableRenderer create(final StringRendererConfig config) {
return new DropTableRenderer(config);
Expand Down
Loading

0 comments on commit 375f356

Please sign in to comment.