Skip to content

Commit

Permalink
Minimize use of java.util.Calendar
Browse files Browse the repository at this point in the history
  • Loading branch information
dmikurube committed Apr 15, 2020
1 parent 59535bf commit 113a20e
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 81 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.embulk.output.jdbc;

import java.math.BigDecimal;
import java.util.Calendar;
import java.io.IOException;
import java.sql.SQLException;
import java.time.Instant;
import java.time.ZoneId;

public interface BatchInsert
{
Expand Down Expand Up @@ -47,9 +47,9 @@ public interface BatchInsert

public void setBytes(byte[] v) throws IOException, SQLException;

public void setSqlDate(Instant v, Calendar cal) throws IOException, SQLException;
public void setSqlDate(Instant v, ZoneId zone) throws IOException, SQLException;

public void setSqlTime(Instant v, Calendar cal) throws IOException, SQLException;
public void setSqlTime(Instant v, ZoneId zone) throws IOException, SQLException;

public void setSqlTimestamp(Instant v, Calendar cal) throws IOException, SQLException;
public void setSqlTimestamp(Instant v, ZoneId zone) throws IOException, SQLException;
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.embulk.output.jdbc;

import java.util.Calendar;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.BatchUpdateException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Date;
import java.sql.Time;
import java.time.Instant;
import java.time.ZoneId;
import java.util.Locale;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -184,28 +184,31 @@ public void setBytes(byte[] v) throws IOException, SQLException
nextColumn(v.length + 4);
}

public void setSqlDate(final Instant v, final Calendar cal) throws IOException, SQLException
public void setSqlDate(final Instant v, final ZoneId zone) throws IOException, SQLException
{
// JavaDoc of java.sql.Time says:
// >> To conform with the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated.
final java.util.Calendar cal = java.util.Calendar.getInstance(java.util.TimeZone.getTimeZone(zone), Locale.ENGLISH);
cal.setTimeInMillis(v.getEpochSecond() * 1000);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.HOUR_OF_DAY, 0);
Date normalized = new Date(cal.getTimeInMillis());
cal.set(java.util.Calendar.SECOND, 0);
cal.set(java.util.Calendar.MINUTE, 0);
cal.set(java.util.Calendar.HOUR_OF_DAY, 0);
final java.sql.Date normalized = new java.sql.Date(cal.getTimeInMillis());
batch.setDate(index, normalized, cal);
nextColumn(32);
}

public void setSqlTime(final Instant v, final Calendar cal) throws IOException, SQLException
public void setSqlTime(final Instant v, final ZoneId zone) throws IOException, SQLException
{
final java.util.Calendar cal = java.util.Calendar.getInstance(java.util.TimeZone.getTimeZone(zone), Locale.ENGLISH);
Time t = new Time(v.toEpochMilli());
batch.setTime(index, t, cal);
nextColumn(32);
}

public void setSqlTimestamp(final Instant v, final Calendar cal) throws IOException, SQLException
public void setSqlTimestamp(final Instant v, final ZoneId zone) throws IOException, SQLException
{
final java.util.Calendar cal = java.util.Calendar.getInstance(java.util.TimeZone.getTimeZone(zone), Locale.ENGLISH);
java.sql.Timestamp t = new java.sql.Timestamp(v.toEpochMilli());
t.setNanos(v.getNano());
batch.setTimestamp(index, t, cal);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.embulk.output.jdbc.setter;

import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;
import java.time.ZoneId;
import java.util.Optional;
import java.sql.Types;
import org.embulk.config.ConfigSource;
Expand Down Expand Up @@ -59,37 +57,37 @@ public ColumnSetter newColumnSetter(JdbcColumn column, JdbcColumnOption option)
case "nstring":
return new NStringColumnSetter(batch, column, newDefaultValueSetter(column, option), newTimestampFormatter(option));
case "date":
return new SqlDateColumnSetter(batch, column, newDefaultValueSetter(column, option), newCalendar(option));
return new SqlDateColumnSetter(batch, column, newDefaultValueSetter(column, option), newZoneId(option));
case "time":
return new SqlTimeColumnSetter(batch, column, newDefaultValueSetter(column, option), newCalendar(option));
return new SqlTimeColumnSetter(batch, column, newDefaultValueSetter(column, option), newZoneId(option));
case "timestamp":
return new SqlTimestampColumnSetter(batch, column, newDefaultValueSetter(column, option), newCalendar(option));
return new SqlTimestampColumnSetter(batch, column, newDefaultValueSetter(column, option), newZoneId(option));
case "decimal":
return new BigDecimalColumnSetter(batch, column, newDefaultValueSetter(column, option));
case "json":
return new JsonColumnSetter(batch, column, newDefaultValueSetter(column, option));
case "null":
return new NullColumnSetter(batch, column, newDefaultValueSetter(column, option));
case "pass":
return new PassThroughColumnSetter(batch, column, newDefaultValueSetter(column, option), newCalendar(option));
return new PassThroughColumnSetter(batch, column, newDefaultValueSetter(column, option), newZoneId(option));
default:
throw new ConfigException(String.format("Unknown value_type '%s' for column '%s'", option.getValueType(), column.getName()));
}
}

protected TimestampFormatter newTimestampFormatter(JdbcColumnOption option)
protected final TimestampFormatter newTimestampFormatter(JdbcColumnOption option)
{
final String format = option.getTimestampFormat();
final String timezone = option.getTimeZone().orElse(this.defaultTimeZone);
return TimestampFormatter.builder(format, true).setDefaultZoneFromString(timezone).build();
}

protected Calendar newCalendar(JdbcColumnOption option)
protected final ZoneId newZoneId(final JdbcColumnOption option)
{
return Calendar.getInstance(TimeZone.getTimeZone(getTimeZone(option)), Locale.ENGLISH);
return ZoneId.of(this.getTimeZone(option));
}

protected String getTimeZone(JdbcColumnOption option)
protected final String getTimeZone(JdbcColumnOption option)
{
return option.getTimeZone().orElse(defaultTimeZone);
}
Expand Down Expand Up @@ -151,11 +149,11 @@ public ColumnSetter newCoalesceColumnSetter(JdbcColumn column, JdbcColumnOption

// Time
case Types.DATE:
return new SqlDateColumnSetter(batch, column, newDefaultValueSetter(column, option), newCalendar(option));
return new SqlDateColumnSetter(batch, column, newDefaultValueSetter(column, option), newZoneId(option));
case Types.TIME:
return new SqlTimeColumnSetter(batch, column, newDefaultValueSetter(column, option), newCalendar(option));
return new SqlTimeColumnSetter(batch, column, newDefaultValueSetter(column, option), newZoneId(option));
case Types.TIMESTAMP:
return new SqlTimestampColumnSetter(batch, column, newDefaultValueSetter(column, option), newCalendar(option));
return new SqlTimestampColumnSetter(batch, column, newDefaultValueSetter(column, option), newZoneId(option));

// Null
case Types.NULL:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.embulk.output.jdbc.setter;

import java.util.Calendar;
import java.io.IOException;
import java.sql.SQLException;
import java.time.Instant;
import java.time.ZoneId;

import org.embulk.output.jdbc.JdbcColumn;
import org.embulk.output.jdbc.BatchInsert;
Expand All @@ -12,14 +12,14 @@
public class PassThroughColumnSetter
extends ColumnSetter
{
private final Calendar calendar;
private final ZoneId zone;

public PassThroughColumnSetter(BatchInsert batch, JdbcColumn column,
DefaultValueSetter defaultValue,
Calendar calendar)
ZoneId zone)
{
super(batch, column, defaultValue);
this.calendar = calendar;
this.zone = zone;
}

@Override
Expand Down Expand Up @@ -55,7 +55,7 @@ public void stringValue(String v) throws IOException, SQLException
@Override
public void timestampValue(final Instant v) throws IOException, SQLException
{
batch.setSqlTimestamp(v, calendar);
batch.setSqlTimestamp(v, this.zone);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.embulk.output.jdbc.setter;

import java.util.Calendar;
import java.io.IOException;
import java.sql.SQLException;
import java.time.Instant;
import java.time.ZoneId;

import org.embulk.output.jdbc.JdbcColumn;
import org.embulk.output.jdbc.BatchInsert;
Expand All @@ -12,14 +12,14 @@
public class SqlDateColumnSetter
extends ColumnSetter
{
protected final Calendar calendar;
protected final ZoneId zone;

public SqlDateColumnSetter(BatchInsert batch, JdbcColumn column,
DefaultValueSetter defaultValue,
Calendar calendar)
ZoneId zone)
{
super(batch, column, defaultValue);
this.calendar = calendar;
this.zone = zone;
}

@Override
Expand Down Expand Up @@ -55,7 +55,7 @@ public void stringValue(String v) throws IOException, SQLException
@Override
public void timestampValue(final Instant v) throws IOException, SQLException
{
batch.setSqlDate(v, calendar);
batch.setSqlDate(v, this.zone);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.embulk.output.jdbc.setter;

import java.util.Calendar;
import java.io.IOException;
import java.sql.SQLException;
import java.time.Instant;
import java.time.ZoneId;

import org.embulk.output.jdbc.JdbcColumn;
import org.embulk.output.jdbc.BatchInsert;
Expand All @@ -12,14 +12,14 @@
public class SqlTimeColumnSetter
extends ColumnSetter
{
protected final Calendar calendar;
protected final ZoneId zone;

public SqlTimeColumnSetter(BatchInsert batch, JdbcColumn column,
DefaultValueSetter defaultValue,
Calendar calendar)
ZoneId zone)
{
super(batch, column, defaultValue);
this.calendar = calendar;
this.zone = zone;
}

@Override
Expand Down Expand Up @@ -55,7 +55,7 @@ public void stringValue(String v) throws IOException, SQLException
@Override
public void timestampValue(final Instant v) throws IOException, SQLException
{
batch.setSqlTime(v, calendar);
batch.setSqlTime(v, this.zone);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.embulk.output.jdbc.setter;

import java.util.Calendar;
import java.io.IOException;
import java.sql.SQLException;
import java.time.Instant;
import java.time.ZoneId;

import org.embulk.output.jdbc.JdbcColumn;
import org.embulk.output.jdbc.BatchInsert;
Expand All @@ -12,14 +12,14 @@
public class SqlTimestampColumnSetter
extends ColumnSetter
{
protected final Calendar calendar;
protected final ZoneId zone;

public SqlTimestampColumnSetter(BatchInsert batch, JdbcColumn column,
DefaultValueSetter defaultValue,
Calendar calendar)
ZoneId zone)
{
super(batch, column, defaultValue);
this.calendar = calendar;
this.zone = zone;
}

@Override
Expand Down Expand Up @@ -55,7 +55,7 @@ public void stringValue(String v) throws IOException, SQLException
@Override
public void timestampValue(final Instant v) throws IOException, SQLException
{
batch.setSqlTimestamp(v, calendar);
batch.setSqlTimestamp(v, this.zone);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import org.embulk.output.jdbc.BatchInsert;
Expand Down Expand Up @@ -260,24 +260,24 @@ public void setBytes(byte[] v) throws IOException, SQLException
}

@Override
public void setSqlDate(final Instant v, final Calendar calendar) throws IOException, SQLException
public void setSqlDate(final Instant v, final ZoneId zone) throws IOException, SQLException
{
throw new SQLException("Unsupported");
}

@Override
public void setSqlTime(final Instant v, final Calendar calendar) throws IOException, SQLException
public void setSqlTime(final Instant v, final ZoneId zone) throws IOException, SQLException
{
throw new SQLException("Unsupported");
}

@Override
public void setSqlTimestamp(final Instant v, final Calendar calendar) throws IOException, SQLException
public void setSqlTimestamp(final Instant v, final ZoneId zone) throws IOException, SQLException
{
java.sql.Timestamp t = new java.sql.Timestamp(v.toEpochMilli());
t.setNanos(v.getNano());
DateFormat format = formats[buffer.getCurrentColumn()];
format.setTimeZone(calendar.getTimeZone());
format.setTimeZone(java.util.TimeZone.getTimeZone(zone));
buffer.addValue(format.format(t));
}
}
Loading

0 comments on commit 113a20e

Please sign in to comment.