embulk-output-jdbc 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: adf000cfbd28d4fb1c8e33f8c11f9551b34c8c70
4
- data.tar.gz: dc37c2dd32bc03ff6b92d16a57da20614bd0dd25
3
+ metadata.gz: 693efe41999ffde2ffae134407fb1b38aedd65ad
4
+ data.tar.gz: 61862ecaafebf03f884c0c3c1756d6a4381c14e4
5
5
  SHA512:
6
- metadata.gz: b5768c7090977ee2f5d01ca59808ea6eea7698d07cea80daf29d06617f13a2befc4e7354de954019590f2bd1c2813382ddb4f3ba97bfdb7713906d92822334e6
7
- data.tar.gz: 8eea4f3829f024e86e661324edd4b948be3a73725826d94b426c3b08ab760c464496349cad2c4948cda09f5f1abb58e76d03b65bf31fe3f90f0221e5596611c9
6
+ metadata.gz: ba4055e90152b59309eae136375e455f30b099cddc93d0470fb60fe0fcd3681143d61a5ab4424865e9dc3a68af3b8da0afbd0e764c4a64de89d2c0a51c400fce
7
+ data.tar.gz: 6a8a60a53f2dae9f38ea0da0cc5a5b890232be4227a683f77a4374d43255a9e7bad4158b809bdf6a0e8b310dd01c283916aa41b6e90310c2df54fea541a0c81b
@@ -66,7 +66,7 @@ public abstract class AbstractJdbcOutputPlugin
66
66
  {
67
67
  @Config("options")
68
68
  @ConfigDefault("{}")
69
- public Properties getOptions();
69
+ public ToStringMap getOptions();
70
70
 
71
71
  @Config("table")
72
72
  public String getTable();
@@ -393,7 +393,10 @@ public abstract class AbstractJdbcOutputPlugin
393
393
  Mode mode = task.getMode();
394
394
  logger.info("Using {} mode", mode);
395
395
 
396
- Optional<JdbcSchema> initialTargetTableSchema = newJdbcSchemaFromTableIfExists(con, task.getTable());
396
+ Optional<JdbcSchema> initialTargetTableSchema =
397
+ mode.ignoreTargetTableSchema() ?
398
+ Optional.<JdbcSchema>absent() :
399
+ newJdbcSchemaFromTableIfExists(con, task.getTable());
397
400
 
398
401
  // TODO get CREATE TABLE statement from task if set
399
402
  JdbcSchema newTableSchema = applyColumnOptionsToNewTableSchema(
@@ -432,11 +435,11 @@ public abstract class AbstractJdbcOutputPlugin
432
435
 
433
436
  // build JdbcSchema from a table
434
437
  JdbcSchema targetTableSchema;
435
- if (mode.ignoreTargetTableSchema() && taskCount != 0) {
438
+ if (initialTargetTableSchema.isPresent()) {
439
+ targetTableSchema = initialTargetTableSchema.get();
440
+ } else if (task.getIntermediateTables().isPresent() && !task.getIntermediateTables().get().isEmpty()) {
436
441
  String firstItermTable = task.getIntermediateTables().get().get(0);
437
442
  targetTableSchema = newJdbcSchemaFromTableIfExists(con, firstItermTable).get();
438
- } else if (initialTargetTableSchema.isPresent()) {
439
- targetTableSchema = initialTargetTableSchema.get();
440
443
  } else {
441
444
  // also create the target table if not exists
442
445
  // CREATE TABLE IF NOT EXISTS xyz
@@ -664,6 +667,11 @@ public abstract class AbstractJdbcOutputPlugin
664
667
  public Optional<JdbcSchema> newJdbcSchemaFromTableIfExists(JdbcOutputConnection connection,
665
668
  String tableName) throws SQLException
666
669
  {
670
+ if (!connection.tableExists(tableName)) {
671
+ // DatabaseMetaData.getPrimaryKeys fails if table does not exist
672
+ return Optional.absent();
673
+ }
674
+
667
675
  DatabaseMetaData dbm = connection.getMetaData();
668
676
  String escape = dbm.getSearchStringEscape();
669
677
 
@@ -1,12 +1,11 @@
1
1
  package org.embulk.output.jdbc;
2
2
 
3
3
  import java.math.BigDecimal;
4
+ import java.util.Calendar;
4
5
  import java.io.IOException;
5
6
  import java.sql.SQLException;
6
7
  import java.sql.PreparedStatement;
7
- import java.sql.Date;
8
- import java.sql.Time;
9
- import java.sql.Timestamp;
8
+ import org.embulk.spi.time.Timestamp;
10
9
 
11
10
  public interface BatchInsert
12
11
  {
@@ -46,9 +45,9 @@ public interface BatchInsert
46
45
 
47
46
  public void setBytes(byte[] v) throws IOException, SQLException;
48
47
 
49
- public void setSqlDate(Date v, int sqlType) throws IOException, SQLException;
48
+ public void setSqlDate(Timestamp v, Calendar cal) throws IOException, SQLException;
50
49
 
51
- public void setSqlTime(Time v, int sqlType) throws IOException, SQLException;
50
+ public void setSqlTime(Timestamp v, Calendar cal) throws IOException, SQLException;
52
51
 
53
- public void setSqlTimestamp(Timestamp v, int sqlType) throws IOException, SQLException;
52
+ public void setSqlTimestamp(Timestamp v, Calendar cal) throws IOException, SQLException;
54
53
  }
@@ -17,7 +17,7 @@ public interface JdbcColumnOption
17
17
  public Optional<String> getType();
18
18
 
19
19
  @Config("value_type")
20
- @ConfigDefault("\"coalesce\"")
20
+ @ConfigDefault("\"coerce\"")
21
21
  public String getValueType();
22
22
 
23
23
  @Config("timestamp_format")
@@ -1,15 +1,16 @@
1
1
  package org.embulk.output.jdbc;
2
2
 
3
3
  import java.util.List;
4
+ import java.util.Calendar;
4
5
  import java.io.IOException;
5
6
  import java.math.BigDecimal;
6
7
  import java.sql.PreparedStatement;
7
8
  import java.sql.SQLException;
8
9
  import java.sql.Date;
9
10
  import java.sql.Time;
10
- import java.sql.Timestamp;
11
11
  import com.google.common.base.Optional;
12
12
  import org.slf4j.Logger;
13
+ import org.embulk.spi.time.Timestamp;
13
14
  import org.embulk.spi.Exec;
14
15
 
15
16
  public class StandardBatchInsert
@@ -163,21 +164,31 @@ public class StandardBatchInsert
163
164
  nextColumn(v.length + 4);
164
165
  }
165
166
 
166
- public void setSqlDate(Date v, int sqlType) throws IOException, SQLException
167
+ public void setSqlDate(Timestamp v, Calendar cal) throws IOException, SQLException
167
168
  {
168
- batch.setObject(index, v, sqlType);
169
+ // JavaDoc of java.sql.Time says:
170
+ // >> 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.
171
+ cal.setTimeInMillis(v.getEpochSecond() * 1000);
172
+ cal.set(Calendar.SECOND, 0);
173
+ cal.set(Calendar.MINUTE, 0);
174
+ cal.set(Calendar.HOUR_OF_DAY, 0);
175
+ Date normalized = new Date(cal.getTimeInMillis());
176
+ batch.setDate(index, normalized, cal);
169
177
  nextColumn(32);
170
178
  }
171
179
 
172
- public void setSqlTime(Time v, int sqlType) throws IOException, SQLException
180
+ public void setSqlTime(Timestamp v, Calendar cal) throws IOException, SQLException
173
181
  {
174
- batch.setObject(index, v, sqlType);
182
+ Time t = new Time(v.toEpochMilli());
183
+ batch.setTime(index, t, cal);
175
184
  nextColumn(32);
176
185
  }
177
186
 
178
- public void setSqlTimestamp(Timestamp v, int sqlType) throws IOException, SQLException
187
+ public void setSqlTimestamp(Timestamp v, Calendar cal) throws IOException, SQLException
179
188
  {
180
- batch.setObject(index, v, sqlType);
189
+ java.sql.Timestamp t = new java.sql.Timestamp(v.toEpochMilli());
190
+ t.setNanos(v.getNano());
191
+ batch.setTimestamp(index, t, cal);
181
192
  nextColumn(32);
182
193
  }
183
194
 
@@ -0,0 +1,54 @@
1
+ package org.embulk.output.jdbc;
2
+
3
+ import com.google.common.base.Optional;
4
+ import com.fasterxml.jackson.databind.JsonNode;
5
+ import com.fasterxml.jackson.databind.JsonMappingException;
6
+ import com.fasterxml.jackson.databind.node.NullNode;
7
+ import com.fasterxml.jackson.annotation.JsonCreator;
8
+ import com.fasterxml.jackson.annotation.JsonValue;
9
+
10
+ public class ToString
11
+ {
12
+ private final String string;
13
+
14
+ public ToString(String string)
15
+ {
16
+ this.string = string;
17
+ }
18
+
19
+ @JsonCreator
20
+ ToString(Optional<JsonNode> option) throws JsonMappingException
21
+ {
22
+ JsonNode node = option.or(NullNode.getInstance());
23
+ if (node.isTextual()) {
24
+ this.string = node.textValue();
25
+ } else if (node.isValueNode()) {
26
+ this.string = node.toString();
27
+ } else {
28
+ throw new JsonMappingException(String.format("Arrays and objects are invalid: '%s'", node));
29
+ }
30
+ }
31
+
32
+ @Override
33
+ public boolean equals(Object obj)
34
+ {
35
+ if (!(obj instanceof ToString)) {
36
+ return false;
37
+ }
38
+ ToString o = (ToString) obj;
39
+ return string.equals(o.string);
40
+ }
41
+
42
+ @Override
43
+ public int hashCode()
44
+ {
45
+ return string.hashCode();
46
+ }
47
+
48
+ @JsonValue
49
+ @Override
50
+ public String toString()
51
+ {
52
+ return string;
53
+ }
54
+ }
@@ -0,0 +1,35 @@
1
+ package org.embulk.output.jdbc;
2
+
3
+ import java.util.Map;
4
+ import java.util.HashMap;
5
+ import java.util.Properties;
6
+ import com.google.common.base.Function;
7
+ import com.google.common.collect.Maps;
8
+ import com.fasterxml.jackson.annotation.JsonCreator;
9
+ import com.fasterxml.jackson.annotation.JsonValue;
10
+
11
+ public class ToStringMap
12
+ extends HashMap<String, String>
13
+ {
14
+ @JsonCreator
15
+ ToStringMap(Map<String, ToString> map)
16
+ {
17
+ super(Maps.transformValues(map, new Function<ToString, String>() {
18
+ public String apply(ToString value)
19
+ {
20
+ if (value == null) {
21
+ return "null";
22
+ } else {
23
+ return value.toString();
24
+ }
25
+ }
26
+ }));
27
+ }
28
+
29
+ public Properties toProperties()
30
+ {
31
+ Properties props = new Properties();
32
+ props.putAll(this);
33
+ return props;
34
+ }
35
+ }
@@ -1,5 +1,7 @@
1
1
  package org.embulk.output.jdbc.setter;
2
2
 
3
+ import java.util.Calendar;
4
+ import java.util.Locale;
3
5
  import java.sql.Types;
4
6
  import org.joda.time.DateTimeZone;
5
7
  import org.embulk.spi.time.TimestampFormatter;
@@ -32,7 +34,7 @@ public class ColumnSetterFactory
32
34
  public ColumnSetter newColumnSetter(JdbcColumn column, JdbcColumnOption option)
33
35
  {
34
36
  switch (option.getValueType()) {
35
- case "coalesce":
37
+ case "coerce":
36
38
  return newCoalesceColumnSetter(column, option);
37
39
  case "byte":
38
40
  return new ByteColumnSetter(batch, column, newDefaultValueSetter(column, option));
@@ -53,17 +55,17 @@ public class ColumnSetterFactory
53
55
  case "nstring":
54
56
  return new NStringColumnSetter(batch, column, newDefaultValueSetter(column, option), newTimestampFormatter(option));
55
57
  case "date":
56
- return new SqlDateColumnSetter(batch, column, newDefaultValueSetter(column, option), getTimeZone(option));
58
+ return new SqlDateColumnSetter(batch, column, newDefaultValueSetter(column, option), newCalendar(option));
57
59
  case "time":
58
- return new SqlTimeColumnSetter(batch, column, newDefaultValueSetter(column, option));
60
+ return new SqlTimeColumnSetter(batch, column, newDefaultValueSetter(column, option), newCalendar(option));
59
61
  case "timestamp":
60
- return new SqlTimestampColumnSetter(batch, column, newDefaultValueSetter(column, option));
62
+ return new SqlTimestampColumnSetter(batch, column, newDefaultValueSetter(column, option), newCalendar(option));
61
63
  case "decimal":
62
64
  return new BigDecimalColumnSetter(batch, column, newDefaultValueSetter(column, option));
63
65
  case "null":
64
66
  return new NullColumnSetter(batch, column, newDefaultValueSetter(column, option));
65
67
  case "pass":
66
- return new PassThroughColumnSetter(batch, column, newDefaultValueSetter(column, option));
68
+ return new PassThroughColumnSetter(batch, column, newDefaultValueSetter(column, option), newCalendar(option));
67
69
  default:
68
70
  throw new ConfigException(String.format("Unknown value_type '%s' for column '%s'", option.getValueType(), column.getName()));
69
71
  }
@@ -74,7 +76,12 @@ public class ColumnSetterFactory
74
76
  return new TimestampFormatter(
75
77
  option.getJRuby(),
76
78
  option.getTimestampFormat().getFormat(),
77
- option.getTimeZone().or(defaultTimeZone));
79
+ getTimeZone(option));
80
+ }
81
+
82
+ protected Calendar newCalendar(JdbcColumnOption option)
83
+ {
84
+ return Calendar.getInstance(getTimeZone(option).toTimeZone(), Locale.ENGLISH);
78
85
  }
79
86
 
80
87
  protected DateTimeZone getTimeZone(JdbcColumnOption option)
@@ -138,11 +145,11 @@ public class ColumnSetterFactory
138
145
 
139
146
  // Time
140
147
  case Types.DATE:
141
- return new SqlDateColumnSetter(batch, column, newDefaultValueSetter(column, option), getTimeZone(option));
148
+ return new SqlDateColumnSetter(batch, column, newDefaultValueSetter(column, option), newCalendar(option));
142
149
  case Types.TIME:
143
- return new SqlTimeColumnSetter(batch, column, newDefaultValueSetter(column, option));
150
+ return new SqlTimeColumnSetter(batch, column, newDefaultValueSetter(column, option), newCalendar(option));
144
151
  case Types.TIMESTAMP:
145
- return new SqlTimestampColumnSetter(batch, column, newDefaultValueSetter(column, option));
152
+ return new SqlTimestampColumnSetter(batch, column, newDefaultValueSetter(column, option), newCalendar(option));
146
153
 
147
154
  // Null
148
155
  case Types.NULL:
@@ -1,5 +1,6 @@
1
1
  package org.embulk.output.jdbc.setter;
2
2
 
3
+ import java.util.Calendar;
3
4
  import java.io.IOException;
4
5
  import java.sql.SQLException;
5
6
  import org.embulk.spi.time.Timestamp;
@@ -10,10 +11,14 @@ import org.embulk.output.jdbc.BatchInsert;
10
11
  public class PassThroughColumnSetter
11
12
  extends ColumnSetter
12
13
  {
14
+ private final Calendar calendar;
15
+
13
16
  public PassThroughColumnSetter(BatchInsert batch, JdbcColumn column,
14
- DefaultValueSetter defaultValue)
17
+ DefaultValueSetter defaultValue,
18
+ Calendar calendar)
15
19
  {
16
20
  super(batch, column, defaultValue);
21
+ this.calendar = calendar;
17
22
  }
18
23
 
19
24
  @Override
@@ -49,8 +54,6 @@ public class PassThroughColumnSetter
49
54
  @Override
50
55
  public void timestampValue(Timestamp v) throws IOException, SQLException
51
56
  {
52
- java.sql.Timestamp t = new java.sql.Timestamp(v.toEpochMilli());
53
- t.setNanos(v.getNano());
54
- batch.setSqlTimestamp(t, getSqlType());
57
+ batch.setSqlTimestamp(v, calendar);
55
58
  }
56
59
  }
@@ -1,9 +1,9 @@
1
1
  package org.embulk.output.jdbc.setter;
2
2
 
3
+ import java.util.Calendar;
3
4
  import java.io.IOException;
4
5
  import java.sql.SQLException;
5
6
  import java.sql.Date;
6
- import org.joda.time.DateTimeZone;
7
7
  import org.embulk.spi.time.Timestamp;
8
8
  import org.embulk.output.jdbc.JdbcColumn;
9
9
  import org.embulk.output.jdbc.BatchInsert;
@@ -11,14 +11,14 @@ import org.embulk.output.jdbc.BatchInsert;
11
11
  public class SqlDateColumnSetter
12
12
  extends ColumnSetter
13
13
  {
14
- private final DateTimeZone timeZone;
14
+ private final Calendar calendar;
15
15
 
16
16
  public SqlDateColumnSetter(BatchInsert batch, JdbcColumn column,
17
17
  DefaultValueSetter defaultValue,
18
- DateTimeZone timeZone)
18
+ Calendar calendar)
19
19
  {
20
20
  super(batch, column, defaultValue);
21
- this.timeZone = timeZone;
21
+ this.calendar = calendar;
22
22
  }
23
23
 
24
24
  @Override
@@ -54,10 +54,6 @@ public class SqlDateColumnSetter
54
54
  @Override
55
55
  public void timestampValue(Timestamp v) throws IOException, SQLException
56
56
  {
57
- // JavaDoc of java.sql.Time says:
58
- // >> 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.
59
- long normalized = timeZone.convertUTCToLocal(v.toEpochMilli());
60
- Date d = new Date(normalized);
61
- batch.setSqlDate(d, getSqlType());
57
+ batch.setSqlDate(v, calendar);
62
58
  }
63
59
  }
@@ -1,5 +1,6 @@
1
1
  package org.embulk.output.jdbc.setter;
2
2
 
3
+ import java.util.Calendar;
3
4
  import java.io.IOException;
4
5
  import java.sql.SQLException;
5
6
  import java.sql.Time;
@@ -10,10 +11,14 @@ import org.embulk.output.jdbc.BatchInsert;
10
11
  public class SqlTimeColumnSetter
11
12
  extends ColumnSetter
12
13
  {
14
+ private final Calendar calendar;
15
+
13
16
  public SqlTimeColumnSetter(BatchInsert batch, JdbcColumn column,
14
- DefaultValueSetter defaultValue)
17
+ DefaultValueSetter defaultValue,
18
+ Calendar calendar)
15
19
  {
16
20
  super(batch, column, defaultValue);
21
+ this.calendar = calendar;
17
22
  }
18
23
 
19
24
  @Override
@@ -49,7 +54,6 @@ public class SqlTimeColumnSetter
49
54
  @Override
50
55
  public void timestampValue(Timestamp v) throws IOException, SQLException
51
56
  {
52
- Time t = new Time(v.toEpochMilli());
53
- batch.setSqlTime(t, getSqlType());
57
+ batch.setSqlTime(v, calendar);
54
58
  }
55
59
  }
@@ -1,5 +1,6 @@
1
1
  package org.embulk.output.jdbc.setter;
2
2
 
3
+ import java.util.Calendar;
3
4
  import java.io.IOException;
4
5
  import java.sql.SQLException;
5
6
  import org.embulk.spi.time.Timestamp;
@@ -9,10 +10,14 @@ import org.embulk.output.jdbc.BatchInsert;
9
10
  public class SqlTimestampColumnSetter
10
11
  extends ColumnSetter
11
12
  {
13
+ private final Calendar calendar;
14
+
12
15
  public SqlTimestampColumnSetter(BatchInsert batch, JdbcColumn column,
13
- DefaultValueSetter defaultValue)
16
+ DefaultValueSetter defaultValue,
17
+ Calendar calendar)
14
18
  {
15
19
  super(batch, column, defaultValue);
20
+ this.calendar = calendar;
16
21
  }
17
22
 
18
23
  @Override
@@ -48,8 +53,6 @@ public class SqlTimestampColumnSetter
48
53
  @Override
49
54
  public void timestampValue(Timestamp v) throws IOException, SQLException
50
55
  {
51
- java.sql.Timestamp t = new java.sql.Timestamp(v.toEpochMilli());
52
- t.setNanos(v.getNano());
53
- batch.setSqlTimestamp(t, getSqlType());
56
+ batch.setSqlTimestamp(v, calendar);
54
57
  }
55
58
  }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: embulk-output-jdbc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sadayuki Furuhashi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-19 00:00:00.000000000 Z
11
+ date: 2015-06-11 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Inserts or updates records to a table.
14
14
  email:
@@ -29,6 +29,8 @@ files:
29
29
  - src/main/java/org/embulk/output/jdbc/JdbcSchema.java
30
30
  - src/main/java/org/embulk/output/jdbc/JdbcUtils.java
31
31
  - src/main/java/org/embulk/output/jdbc/StandardBatchInsert.java
32
+ - src/main/java/org/embulk/output/jdbc/ToString.java
33
+ - src/main/java/org/embulk/output/jdbc/ToStringMap.java
32
34
  - src/main/java/org/embulk/output/jdbc/setter/BigDecimalColumnSetter.java
33
35
  - src/main/java/org/embulk/output/jdbc/setter/BooleanColumnSetter.java
34
36
  - src/main/java/org/embulk/output/jdbc/setter/ByteColumnSetter.java
@@ -51,7 +53,7 @@ files:
51
53
  - src/main/java/org/embulk/output/jdbc/setter/SqlTimestampColumnSetter.java
52
54
  - src/main/java/org/embulk/output/jdbc/setter/StringColumnSetter.java
53
55
  - src/test/java/org/embulk/output/TestJdbcOutputPlugin.java
54
- - classpath/embulk-output-jdbc-0.3.0.jar
56
+ - classpath/embulk-output-jdbc-0.4.0.jar
55
57
  homepage: https://github.com/embulk/embulk-output-jdbc
56
58
  licenses:
57
59
  - Apache 2.0
Binary file