Skip to content

Commit

Permalink
#29: added timestamp data type
Browse files Browse the repository at this point in the history
  • Loading branch information
AnastasiiaSergienko committed Feb 26, 2019
1 parent b1d5a13 commit 1a5bec3
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 4 deletions.
23 changes: 23 additions & 0 deletions src/main/java/com/exasol/datatype/Timestamp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.exasol.datatype;

import com.exasol.sql.AbstractFragment;
import com.exasol.sql.Fragment;
import com.exasol.sql.ddl.CreateTableVisitor;

public class Timestamp extends AbstractFragment implements DataType {
private static final String NAME = "TIMESTAMP";

public Timestamp(final Fragment root) {
super(root);
}

@Override
public String getName() {
return NAME;
}

@Override
public void accept(final CreateTableVisitor visitor) {
visitor.visit(this);
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/exasol/datatype/TimestampWithLocalTimezone.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.exasol.datatype;

import com.exasol.sql.AbstractFragment;
import com.exasol.sql.Fragment;
import com.exasol.sql.ddl.CreateTableVisitor;

public class TimestampWithLocalTimezone extends AbstractFragment implements DataType {
private static final String NAME = "TIMESTAMP WITH LOCAL TIME ZONE";

public TimestampWithLocalTimezone(final Fragment root) {
super(root);
}

@Override
public String getName() {
return NAME;
}

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

12 changes: 12 additions & 0 deletions src/main/java/com/exasol/sql/ddl/CreateTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ public CreateTable doublePrecisionColumn(final String columnName) {
return this;
}

public CreateTable timestampColumn(final String columnName) {
checkIfCreateTableColumnsExists();
this.columnsDefinition.add(columnName, new Timestamp(this));
return this;
}

public CreateTable timestampWithLocalTimeZoneColumn(final String columnName) {
checkIfCreateTableColumnsExists();
this.columnsDefinition.add(columnName, new TimestampWithLocalTimezone(this));
return this;
}

public ColumnsDefinition getColumns() {
return this.columnsDefinition;
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/exasol/sql/ddl/CreateTableVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ public interface CreateTableVisitor extends FragmentVisitor {
public void visit(final Decimal decimalColumn);

public void visit(DoublePrecision doublePrecision);

public void visit(Timestamp timestamp);

public void visit(TimestampWithLocalTimezone timestampWithLocalTimezone);
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ public void visit(final DoublePrecision doublePrecision) {
appendDataTypeWithoutParameters(doublePrecision);
}

@Override
public void visit(final Timestamp timestamp) {
appendDataTypeWithoutParameters(timestamp);
}

@Override
public void visit(final TimestampWithLocalTimezone timestampWithLocalTimezone) {
appendDataTypeWithoutParameters(timestampWithLocalTimezone);
}

@Override
public void visit(final Field field) {
throw new UnsupportedOperationException();
Expand Down
23 changes: 22 additions & 1 deletion src/test/java/com/exasol/sql/ddl/TestCreateTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ class TestCreateTable {
private static final String DECIMAL_COLUMN_NAME = "decimalColumn";
private static final int PRECISION = 18;
private static final int SCALE = 0;
private static final String DOUBLE_PRECISION_COLUMN_NAME = "doublePresicionColumn";
private static final String DOUBLE_PRECISION_COLUMN_NAME = "doublePrecisionColumn";
private static final String TIMESTAMP_COLUMN_NAME = "timestampColumn";
private static final String TIMESTAMP_WITH_LOCAL_TIMEZONE_COLUMN_NAME =
"timestampWithLocalTimeZoneColumn";
private CreateTable createTable;

@BeforeEach
Expand Down Expand Up @@ -85,4 +88,22 @@ void testCreateTableWithDoublePrecisionColumn() {
assertThat(column.getColumnName(), equalTo(DOUBLE_PRECISION_COLUMN_NAME));
assertThat(column.getDataType(), instanceOf(DoublePrecision.class));
}

@Test
void testCreateTableWithTimestampColumn() {
final Column column =
this.createTable.timestampColumn(TIMESTAMP_COLUMN_NAME).getColumns().getColumns()
.get(0);
assertThat(column.getColumnName(), equalTo(TIMESTAMP_COLUMN_NAME));
assertThat(column.getDataType(), instanceOf(Timestamp.class));
}

@Test
void testCreateTableWithTimestampWithLocalTimeZoneColumn() {
final Column column =
this.createTable.timestampWithLocalTimeZoneColumn(TIMESTAMP_WITH_LOCAL_TIMEZONE_COLUMN_NAME)
.getColumns().getColumns().get(0);
assertThat(column.getColumnName(), equalTo(TIMESTAMP_WITH_LOCAL_TIMEZONE_COLUMN_NAME));
assertThat(column.getDataType(), instanceOf(TimestampWithLocalTimezone.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ void testCreateTableWithBooleanColumn() {

@Test
void testCreateTableWithDateColumn() {
assertThat(this.createTable.dateColumn("a"),
rendersTo("CREATE TABLE testName (a DATE)"));
assertThat(this.createTable.dateColumn("a"), rendersTo("CREATE TABLE testName (a DATE)"));
}

@Test
Expand All @@ -51,6 +50,18 @@ void testCreateTableWithDoublePrecisionColumn() {
rendersTo("CREATE TABLE testName (a DOUBLE PRECISION)"));
}

@Test
void testCreateTableWithTimestampColumn() {
assertThat(this.createTable.timestampColumn("a"),
rendersTo("CREATE TABLE testName (a TIMESTAMP)"));
}

@Test
void testCreateTableWithTimestampWithLocalTimeZoneColumn() {
assertThat(this.createTable.timestampWithLocalTimeZoneColumn("a"),
rendersTo("CREATE TABLE testName (a TIMESTAMP WITH LOCAL TIME ZONE)"));
}

@Test
void testCreateTableWithCharColumn() {
assertThat(this.createTable.charColumn("a", 10),
Expand Down Expand Up @@ -101,6 +112,7 @@ void testCreateTableWithInvalidDecimalScaleColumn() {
@Test
void testCreateTableWithCharAFewColumns() {
assertThat(this.createTable.decimalColumn("dec_col", 9, 0).charColumn("char_col", 10)
.booleanColumn("bool_col"), rendersTo("CREATE TABLE testName (dec_col DECIMAL(9,0), char_col CHAR(10), bool_col BOOLEAN)"));
.booleanColumn("bool_col"), rendersTo(
"CREATE TABLE testName (dec_col DECIMAL(9,0), char_col CHAR(10), bool_col BOOLEAN)"));
}
}

0 comments on commit 1a5bec3

Please sign in to comment.