Skip to content

Commit

Permalink
#43: implemented create schema logic
Browse files Browse the repository at this point in the history
  • Loading branch information
AnastasiiaSergienko committed Apr 5, 2019
1 parent 97e135b commit 456ed9b
Show file tree
Hide file tree
Showing 17 changed files with 276 additions and 22 deletions.
8 changes: 0 additions & 8 deletions src/main/java/com/exasol/sql/SqlStatementVisitor.java

This file was deleted.

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

import com.exasol.sql.ddl.create.CreateSchema;
import com.exasol.sql.ddl.create.CreateTable;
import com.exasol.sql.ddl.drop.DropTable;
import com.exasol.sql.dml.Insert;
Expand Down Expand Up @@ -56,6 +57,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(String schemaName) {
return new CreateSchema(schemaName);
}

/**
* Create a {@link DropTable} statement
*
Expand Down
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);
}
}
36 changes: 36 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,36 @@
package com.exasol.sql.ddl;

import com.exasol.sql.AbstractFragment;
import com.exasol.sql.Fragment;
import com.exasol.sql.ddl.create.CreateSchemaVisitor;

/**
* This class represents a {@link Schema} in an SQL Statement
*/
public class Schema extends AbstractFragment {
private String name;

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

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

public void accept(CreateSchemaVisitor visitor) {
visitor.visit(this);
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/exasol/sql/ddl/create/CreateSchema.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.exasol.sql.ddl.create;

import com.exasol.sql.AbstractFragment;
import com.exasol.sql.SqlStatement;
import com.exasol.sql.ddl.Schema;

/**
* This class implements an SQL {@link CreateSchema} statement
*/
public class CreateSchema extends AbstractFragment implements SqlStatement, CreateSchemaFragment {
private Schema schema;

/**
* Create a new instance of an {@link CreateSchema} statement
*
* @param schemaName name of the table to create
*/
public CreateSchema(final String schemaName) {
super(null);
this.schema = new Schema(this, schemaName);
}

/**
* Get a schema name
*
* @return schema name
*/
public String getSchemaName() {
return schema.getName();
}

@Override
public void accept(CreateSchemaVisitor visitor) {
visitor.visit(this);
this.schema.accept(visitor);
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/exasol/sql/ddl/create/CreateSchemaFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.exasol.sql.ddl.create;

import com.exasol.sql.Fragment;

/**
* This is the common interface for all fragments of a CREATE SCHEMA statement.
*/
public interface CreateSchemaFragment extends Fragment {
/**
* Accept a visitor (e.g. a renderer or validator)
*
* @param visitor visitor to accept
*/
public void accept(CreateSchemaVisitor visitor);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.exasol.sql.ddl.create;

import com.exasol.sql.ddl.Schema;

public interface CreateSchemaVisitor {
public void visit(CreateSchema createSchema);

public void visit(Schema schema);
}
18 changes: 13 additions & 5 deletions src/main/java/com/exasol/sql/ddl/create/CreateTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import com.exasol.datatype.type.Boolean;
import com.exasol.datatype.type.*;
import com.exasol.sql.AbstractFragment;
import com.exasol.sql.SqlStatement;
import com.exasol.sql.Table;
import com.exasol.sql.*;

/**
* This class implements an SQL {@link CreateTable} statement
Expand Down Expand Up @@ -151,11 +149,21 @@ public synchronized CreateTable intervalYearToMonthColumn(final String columnNam
return this;
}

protected String getTableName() {
/**
* Get a table name
*
* @return table name
*/
public String getTableName() {
return this.table.getName();
}

protected ColumnsDefinition getColumns() {
/**
* Get columns of a table
*
* @return columns
*/
public ColumnsDefinition getColumns() {
return this.columnsDefinition;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import com.exasol.datatype.type.Boolean;
import com.exasol.datatype.type.*;
import com.exasol.sql.SqlStatementVisitor;
import com.exasol.sql.Table;

public interface CreateTableVisitor {
public void visit(final Table table);

public interface CreateTableVisitor extends SqlStatementVisitor {
public void visit(final CreateTable createTable);

public void visit(final Column column);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.exasol.sql.ddl.create.rendering;

import com.exasol.sql.ddl.Schema;
import com.exasol.sql.ddl.create.CreateSchema;
import com.exasol.sql.ddl.create.CreateSchemaVisitor;
import com.exasol.sql.rendering.AbstractFragmentRenderer;
import com.exasol.sql.rendering.StringRendererConfig;

/**
* The {@link CreateSchemaRenderer} turns SQL statement structures in to SQL strings.
*/
public class CreateSchemaRenderer extends AbstractFragmentRenderer implements CreateSchemaVisitor {
/**
* Create a new {@link CreateSchemaRenderer} with custom render settings.
*
* @param config render configuration settings
*/
public CreateSchemaRenderer(final StringRendererConfig config) {
super(config);
}

/**
* Create an {@link CreateSchemaRenderer} using the default renderer configuration
*
* @return insert renderer
*/
public static CreateSchemaRenderer create() {
return new CreateSchemaRenderer(StringRendererConfig.createDefault());
}

/**
* Create an {@link CreateSchemaRenderer}
*
* @param config renderer configuration
* @return create schema renderer
*/
public static CreateSchemaRenderer create(final StringRendererConfig config) {
return new CreateSchemaRenderer(config);
}

@Override
public void visit(CreateSchema createSchema) {
appendKeyWord("CREATE SCHEMA ");
setLastVisited(createSchema);
}

@Override
public void visit(Schema schema) {
appendAutoQuoted(schema.getName());
setLastVisited(schema);
}
}
6 changes: 4 additions & 2 deletions src/main/java/com/exasol/sql/ddl/drop/DropTableVisitor.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.exasol.sql.ddl.drop;

import com.exasol.sql.SqlStatementVisitor;
import com.exasol.sql.Table;

public interface DropTableVisitor {
public void visit(Table table);

public interface DropTableVisitor extends SqlStatementVisitor {
public void visit(DropTable dropTable);

public void visit(CascadeConstraints cascadeConstraints);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.exasol.hamcrest;

import com.exasol.sql.ddl.create.CreateTableFragment;
import com.exasol.sql.ddl.create.*;
import com.exasol.sql.ddl.create.rendering.CreateSchemaRenderer;
import com.exasol.sql.ddl.create.rendering.CreateTableRenderer;
import com.exasol.sql.ddl.drop.DropTableFragment;
import com.exasol.sql.ddl.drop.rendering.DropTableRenderer;
Expand Down Expand Up @@ -54,6 +55,10 @@ public boolean matchesSafely(final Fragment fragment) {
final DropTableRenderer renderer = new DropTableRenderer(this.config);
((DropTableFragment) root).accept(renderer);
this.renderedText = renderer.render();
} else if (root instanceof CreateSchemaFragment) {
final CreateSchemaRenderer renderer = new CreateSchemaRenderer(this.config);
((CreateSchemaFragment) root).accept(renderer);
this.renderedText = renderer.render();
} else {
throw new UnsupportedOperationException(
"Don't know how to render fragment of type\"" + root.getClass().getName() + "\".");
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/com/exasol/sql/ddl/create/TestCreateSchema.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.exasol.sql.ddl.create;

import com.exasol.sql.StatementFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

class TestCreateSchema {
private static final String TEST_SCHEMA_NAME = "test schema name";
private CreateSchema createSchema;

@BeforeEach
void setUp() {
createSchema = StatementFactory.getInstance().createSchema(TEST_SCHEMA_NAME);
}

@Test
void getTableName() {
assertThat(this.createSchema.getSchemaName(), equalTo(TEST_SCHEMA_NAME));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.exasol.sql.ddl.create.rendering;

import com.exasol.sql.StatementFactory;
import com.exasol.sql.ddl.create.CreateSchema;
import com.exasol.sql.rendering.StringRendererConfig;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.startsWith;

class TestCreateSchemaRenderer {
@Test
void testCreateWithDefaultConfig() {
assertThat(CreateSchemaRenderer.create(), instanceOf(CreateSchemaRenderer.class));
}

@Test
void testCreateWithConfig() {
final StringRendererConfig config = StringRendererConfig.builder().lowerCase(true).build();
final CreateSchemaRenderer renderer = CreateSchemaRenderer.create(config);
final CreateSchema createSchema = StatementFactory.getInstance().createSchema("test name");
createSchema.accept(renderer);
assertThat(renderer.render(), startsWith("create schema"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.exasol.sql.ddl.create.rendering;

import com.exasol.sql.StatementFactory;
import com.exasol.sql.ddl.create.CreateSchema;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static com.exasol.hamcrest.SqlFragmentRenderResultMatcher.rendersTo;
import static org.hamcrest.MatcherAssert.assertThat;

class TestCreateSchemaRendering {
private static final String SCHEMA_NAME = "testName";
private CreateSchema createSchema;

@BeforeEach
void beforeEach() {
this.createSchema = StatementFactory.getInstance().createSchema(SCHEMA_NAME);
}


@Test
void testCreateSchema() {
assertThat(this.createSchema, rendersTo("CREATE SCHEMA testName"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ void testCreateWithConfig() {
final CreateTableRenderer renderer = CreateTableRenderer.create(config);
final CreateTable createTable = StatementFactory.getInstance().createTable("test name");
createTable.accept(renderer);
assertThat(renderer.render(), startsWith("create"));
assertThat(renderer.render(), startsWith("create table"));
}
}
Loading

0 comments on commit 456ed9b

Please sign in to comment.