Skip to content

Commit

Permalink
Feature/43 implement create drop schema (#44)
Browse files Browse the repository at this point in the history
* #43: implemented create schema statement
* #43: implemented drop schema statement
* #43: moved classes into appropriate packages
  • Loading branch information
AnastasiiaSergienko authored Apr 9, 2019
1 parent 3301ba8 commit 9422adc
Show file tree
Hide file tree
Showing 72 changed files with 1,276 additions and 237 deletions.
319 changes: 319 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs

Large diffs are not rendered by default.

12 changes: 1 addition & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,7 @@ Goals:

## Usage

```java
import com.exasol.sql.StatementFactory;
import com.exasol.sql.SqlStatement;
import com.exasol.sql.rendering.SelectRenderer;

SqlStatement statement = StatementFactory.getInstance()
.select().field("firstname", "lastname")
.from().table("person");

String statementText = SqlStatementRenderer.render(statement);
```
Please read our [User Guide](../sql-statement-builder/doc/guide/user_guide.md) to find information about usage of `sql-statement-builder`.

## Development

Expand Down
27 changes: 27 additions & 0 deletions doc/guide/statements/create_schema.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# CREATE SCHEMA

The `CreateSchema` class of the SQL Statement Builder provides an entry
point to defining a CREATE SCHEMA SQL statement.

## Usage

1. Create an instance of the `CreateSchema` class through the `StatementFactory`

```java
CreateSchema createSchema = StatementFactory.getInstance().createSchema("schemaName");
```

2. Render the instance of `CreateSchema` class. Click [here](../rendering.md) for more information on Rendering SQL Statement.

- The complete example code

```java
CreateSchema createSchema = StatementFactory.getInstance().createSchema("schemaName");

//optional step: add config
StringRendererConfig config = StringRendererConfig.builder().lowerCase(true).build();
CreateSchemaRenderer renderer = CreateSchemaRenderer.create(config);
createSchema.accept(renderer);

String renderedString = renderer.render();
```
49 changes: 49 additions & 0 deletions doc/guide/statements/drop_schema.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# DROP SCHEMA

The `DropSchema` class of the SQL Statement Builder provides an entry
point to defining a DROP SCHEMA SQL statement.

## Usage

1. Create an instance of the `DropSchema` class through the `StatementFactory`

```java
DropSchema dropSchema = StatementFactory.getInstance().dropSchema("schemaName");
```

2. Add available options using fluent programming if necessary.

The following options are currently supported:

- `IF EXISTS`
- `CASCADE`
- `RESTRICT`

Example:

```java
dropSchema.ifExists().cascade();
```
Please do not use methods `cascade()` and `restrict()` on the same object.
If both these options are used on the same object, `IllegalArgumentException` will be thrown.

3. Render the instance of `DropSchema` class. Click [here](../rendering.md) for more information on Rendering SQL Statement.

- The complete example code

```java
DropSchema dropSchema = StatementFactory.getInstance().dropSchema("schemaName");

//optional step: add additional clauses
dropSchema.ifExists().cascade();
//or
dropSchema.ifExists().restrict();

//Rendering
//optional step: add config
StringRendererConfig config = StringRendererConfig.builder().lowerCase(true).build();

DropSchemaRenderer renderer = DropSchemaRenderer.create(config);
dropSchema.accept(renderer);
String renderedString = renderer.render();
```
2 changes: 2 additions & 0 deletions doc/guide/user_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ you can, for example, use the SSB to render the statement into an SQL string."
**Currently supported SQL statements:**

DDL:
- [CREATE SCHEMA](../guide/statements/create_schema.md)
- [DROP SCHEMA](../guide/statements/drop_schema.md)
- [CREATE TABLE](../guide/statements/create_table.md)
- [DROP TABLE](../guide/statements/drop_table.md)

Expand Down
20 changes: 18 additions & 2 deletions doc/system_requirements.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,15 @@ Needs: dsn
#### CREATE Statements
`req~create-statements~1`

ESB supports the following create statement:
ESB supports the following create statements.

Create schema:

create-statement = "CREATE SCHEMA" schema-reference

schema-reference = schema

Create table:

create-statement = "CREATE TABLE" table-reference table-element-list

Expand All @@ -215,7 +223,15 @@ Needs: dsn
#### DROP Statements
`req~drop-statements~1`

ESB supports the following drop statement:
ESB supports the following drop statement.

Drop schema:

drop-statement = "DROP SCHEMA" [IF EXISTS] schema-reference [CASCADE / RESTRICT]

schema-reference = schema

Drop table:

drop-statement = "DROP TABLE" [IF EXISTS] table-reference [CASCADE CONSTRAINTS]

Expand Down
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
8 changes: 0 additions & 8 deletions src/main/java/com/exasol/sql/SqlStatementVisitor.java

This file was deleted.

26 changes: 24 additions & 2 deletions src/main/java/com/exasol/sql/StatementFactory.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.exasol.sql;

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;
import com.exasol.sql.dml.insert.Insert;
import com.exasol.sql.dql.select.Select;

/**
* The {@link StatementFactory} implements an factory for SQL statements.
Expand Down Expand Up @@ -56,6 +58,16 @@ public CreateTable createTable(final String tableName) {
return new CreateTable(tableName);
}

/**
* Create a {@link CreateSchema} statement
*
* @param schemaName name of the schema to create
* @return a new instance of a {@link CreateSchema} statement
*/
public CreateSchema createSchema(final String schemaName) {
return new CreateSchema(schemaName);
}

/**
* Create a {@link DropTable} statement
*
Expand All @@ -65,4 +77,14 @@ public CreateTable createTable(final String tableName) {
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(final String schemaName) {
return new DropSchema(schemaName);
}
}
15 changes: 13 additions & 2 deletions src/main/java/com/exasol/sql/Table.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.exasol.sql;

import com.exasol.sql.ddl.create.CreateTableVisitor;
import com.exasol.sql.ddl.drop.DropTableVisitor;

import java.util.Optional;

/**
Expand All @@ -26,7 +29,7 @@ public Table(final Fragment root, final String name) {
*
* @param root SQL statement this table belongs to
* @param name table name
* @param as table alias
* @param as table alias
*/
public Table(final Fragment root, final String name, final String as) {
super(root);
Expand All @@ -52,7 +55,15 @@ public Optional<String> getAs() {
return this.as;
}

public void accept(final SqlStatementVisitor visitor) {
public void accept(final TableValuesVisitor visitor) {
visitor.visit(this);
}

public void accept(final CreateTableVisitor visitor) {
visitor.visit(this);
}

public void accept(final DropTableVisitor visitor) {
visitor.visit(this);
}
}
5 changes: 2 additions & 3 deletions src/main/java/com/exasol/sql/TableValuesVisitor.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.exasol.sql;

import com.exasol.sql.dql.ValueTable;
import com.exasol.sql.dql.ValueTableRow;
public interface TableValuesVisitor {
public void visit(Table table);

public interface TableValuesVisitor extends SqlStatementVisitor {
public void visit(Field field);

public void visit(ValueTable valueTable);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.exasol.sql.dql;
package com.exasol.sql;

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

import com.exasol.sql.*;

/**
* Value tables are pseudo-tables constructed from rows and columns of expressions (e.g. literals)
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.exasol.sql.dql;
package com.exasol.sql;

import java.util.*;

import com.exasol.sql.*;
import com.exasol.sql.expression.*;

import java.util.*;

/**
* This class represents a row in a {@link ValueTable}.
*/
Expand All @@ -15,8 +14,7 @@ public class ValueTableRow extends AbstractFragment {
/**
* Create a value table row from a list of expressions
*
* @param root root node of the SQL statement
*
* @param root root node of the SQL statement
* @param expressions value expressions
*/
public ValueTableRow(final Fragment root, final ValueExpression... expressions) {
Expand All @@ -27,8 +25,7 @@ public ValueTableRow(final Fragment root, final ValueExpression... expressions)
/**
* Create a value table row from a list of string literals
*
* @param root root node of the SQL statement
*
* @param root root node of the SQL statement
* @param values sting literals
*/
public ValueTableRow(final Fragment root, final String... values) {
Expand Down Expand Up @@ -84,7 +81,6 @@ public Builder(final Fragment root) {
* Add one or more string literals to the row
*
* @param values strings to be added
*
* @return <code>this</code> for fluent programming
*/
public Builder add(final String... values) {
Expand All @@ -98,7 +94,6 @@ public Builder add(final String... values) {
* Add one or more integer literals to the row
*
* @param values integers to be added
*
* @return <code>this</code> for fluent programming
*/
public Builder add(final int... values) {
Expand All @@ -122,7 +117,6 @@ public Builder addPlaceholder() {
* Add a list of expressions to the {@link ValueTableRow}
*
* @param expressions expressions to be added
*
* @return <code>this</code> for fluent programming
*/
public Builder add(final List<ValueExpression> expressions) {
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/com/exasol/sql/ddl/Schema.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.exasol.sql.ddl;

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
*/
public class Schema extends AbstractFragment {
private final String name;

/**
* Create a new {@link Schema}
*
* @param root SQL statement this schema belongs to
* @param schemaName schema name
*/
public Schema(final Fragment root, final String schemaName) {
super(root);
this.name = schemaName;
}

/**
* Get the schema name
*
* @return schema name
*/
public String getName() {
return name;
}

public void accept(final CreateSchemaVisitor visitor) {
visitor.visit(this);
}

public void accept(final DropSchemaVisitor visitor) {
visitor.visit(this);
}
}
Loading

0 comments on commit 9422adc

Please sign in to comment.