diff --git a/src/main/java/com/exasol/datatype/Timestamp.java b/src/main/java/com/exasol/datatype/Timestamp.java new file mode 100644 index 00000000..59cabed9 --- /dev/null +++ b/src/main/java/com/exasol/datatype/Timestamp.java @@ -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); + } +} diff --git a/src/main/java/com/exasol/datatype/TimestampWithLocalTimezone.java b/src/main/java/com/exasol/datatype/TimestampWithLocalTimezone.java new file mode 100644 index 00000000..02971e71 --- /dev/null +++ b/src/main/java/com/exasol/datatype/TimestampWithLocalTimezone.java @@ -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); + } +} + diff --git a/src/main/java/com/exasol/sql/ddl/CreateTable.java b/src/main/java/com/exasol/sql/ddl/CreateTable.java index 90b3312c..1dc80624 100644 --- a/src/main/java/com/exasol/sql/ddl/CreateTable.java +++ b/src/main/java/com/exasol/sql/ddl/CreateTable.java @@ -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; } diff --git a/src/main/java/com/exasol/sql/ddl/CreateTableVisitor.java b/src/main/java/com/exasol/sql/ddl/CreateTableVisitor.java index 153bdaa3..9ed379c4 100644 --- a/src/main/java/com/exasol/sql/ddl/CreateTableVisitor.java +++ b/src/main/java/com/exasol/sql/ddl/CreateTableVisitor.java @@ -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); } diff --git a/src/main/java/com/exasol/sql/ddl/rendering/CreateTableRenderer.java b/src/main/java/com/exasol/sql/ddl/rendering/CreateTableRenderer.java index 87140bf1..51c3542b 100644 --- a/src/main/java/com/exasol/sql/ddl/rendering/CreateTableRenderer.java +++ b/src/main/java/com/exasol/sql/ddl/rendering/CreateTableRenderer.java @@ -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(); diff --git a/src/test/java/com/exasol/sql/ddl/TestCreateTable.java b/src/test/java/com/exasol/sql/ddl/TestCreateTable.java index 6a65b398..3cea9498 100644 --- a/src/test/java/com/exasol/sql/ddl/TestCreateTable.java +++ b/src/test/java/com/exasol/sql/ddl/TestCreateTable.java @@ -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 @@ -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)); + } } \ No newline at end of file diff --git a/src/test/java/com/exasol/sql/ddl/rendering/TestCreateTableRendering.java b/src/test/java/com/exasol/sql/ddl/rendering/TestCreateTableRendering.java index 0ba44f55..cfe25f30 100644 --- a/src/test/java/com/exasol/sql/ddl/rendering/TestCreateTableRendering.java +++ b/src/test/java/com/exasol/sql/ddl/rendering/TestCreateTableRendering.java @@ -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 @@ -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), @@ -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)")); } }