embulk-output-jdbc 0.2.4 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/build.gradle +2 -2
  3. data/classpath/embulk-output-jdbc-0.3.0.jar +0 -0
  4. data/lib/embulk/output/jdbc.rb +3 -3
  5. data/src/main/java/org/embulk/output/JdbcOutputPlugin.java +138 -120
  6. data/src/main/java/org/embulk/output/jdbc/AbstractJdbcOutputPlugin.java +964 -755
  7. data/src/main/java/org/embulk/output/jdbc/BatchInsert.java +54 -54
  8. data/src/main/java/org/embulk/output/jdbc/JdbcColumn.java +59 -23
  9. data/src/main/java/org/embulk/output/jdbc/JdbcColumnOption.java +34 -0
  10. data/src/main/java/org/embulk/output/jdbc/JdbcOutputConnection.java +95 -114
  11. data/src/main/java/org/embulk/output/jdbc/JdbcOutputConnector.java +8 -8
  12. data/src/main/java/org/embulk/output/jdbc/JdbcSchema.java +60 -37
  13. data/src/main/java/org/embulk/output/jdbc/JdbcUtils.java +155 -155
  14. data/src/main/java/org/embulk/output/jdbc/StandardBatchInsert.java +8 -5
  15. data/src/main/java/org/embulk/output/jdbc/setter/BigDecimalColumnSetter.java +70 -0
  16. data/src/main/java/org/embulk/output/jdbc/setter/BooleanColumnSetter.java +54 -52
  17. data/src/main/java/org/embulk/output/jdbc/setter/ByteColumnSetter.java +76 -0
  18. data/src/main/java/org/embulk/output/jdbc/setter/ColumnSetter.java +10 -91
  19. data/src/main/java/org/embulk/output/jdbc/setter/ColumnSetterFactory.java +189 -137
  20. data/src/main/java/org/embulk/output/jdbc/setter/ColumnSetterVisitor.java +96 -0
  21. data/src/main/java/org/embulk/output/jdbc/setter/DefaultValueSetter.java +48 -0
  22. data/src/main/java/org/embulk/output/jdbc/setter/DoubleColumnSetter.java +61 -51
  23. data/src/main/java/org/embulk/output/jdbc/setter/FloatColumnSetter.java +61 -0
  24. data/src/main/java/org/embulk/output/jdbc/setter/IntColumnSetter.java +76 -0
  25. data/src/main/java/org/embulk/output/jdbc/setter/LongColumnSetter.java +72 -62
  26. data/src/main/java/org/embulk/output/jdbc/setter/NStringColumnSetter.java +59 -0
  27. data/src/main/java/org/embulk/output/jdbc/setter/NullColumnSetter.java +53 -43
  28. data/src/main/java/org/embulk/output/jdbc/setter/NullDefaultValueSetter.java +105 -0
  29. data/src/main/java/org/embulk/output/jdbc/setter/PassThroughColumnSetter.java +56 -0
  30. data/src/main/java/org/embulk/output/jdbc/setter/ShortColumnSetter.java +76 -0
  31. data/src/main/java/org/embulk/output/jdbc/setter/SkipColumnSetter.java +43 -38
  32. data/src/main/java/org/embulk/output/jdbc/setter/SqlDateColumnSetter.java +63 -0
  33. data/src/main/java/org/embulk/output/jdbc/setter/SqlTimeColumnSetter.java +55 -0
  34. data/src/main/java/org/embulk/output/jdbc/setter/SqlTimestampColumnSetter.java +55 -48
  35. data/src/main/java/org/embulk/output/jdbc/setter/StringColumnSetter.java +59 -48
  36. data/src/test/java/org/embulk/output/TestJdbcOutputPlugin.java +5 -5
  37. metadata +16 -4
  38. data/classpath/embulk-output-jdbc-0.2.4.jar +0 -0
  39. data/src/main/java/org/embulk/output/jdbc/RetryExecutor.java +0 -105
@@ -0,0 +1,70 @@
1
+ package org.embulk.output.jdbc.setter;
2
+
3
+ import java.math.BigDecimal;
4
+ import java.io.IOException;
5
+ import java.sql.SQLException;
6
+ import org.embulk.spi.ColumnVisitor;
7
+ import org.embulk.spi.time.Timestamp;
8
+ import org.embulk.spi.time.TimestampFormatter;
9
+ import org.embulk.output.jdbc.JdbcColumn;
10
+ import org.embulk.output.jdbc.BatchInsert;
11
+
12
+ public class BigDecimalColumnSetter
13
+ extends ColumnSetter
14
+ {
15
+ private static final BigDecimal ZERO = BigDecimal.valueOf(0L);
16
+ private static final BigDecimal ONE = BigDecimal.valueOf(1L);
17
+
18
+ public BigDecimalColumnSetter(BatchInsert batch, JdbcColumn column,
19
+ DefaultValueSetter defaultValue)
20
+ {
21
+ super(batch, column, defaultValue);
22
+ }
23
+
24
+ @Override
25
+ public void nullValue() throws IOException, SQLException
26
+ {
27
+ defaultValue.setBigDecimal();
28
+ }
29
+
30
+ @Override
31
+ public void booleanValue(boolean v) throws IOException, SQLException
32
+ {
33
+ batch.setBigDecimal(v ? ONE : ZERO);
34
+ }
35
+
36
+ @Override
37
+ public void longValue(long v) throws IOException, SQLException
38
+ {
39
+ batch.setBigDecimal(BigDecimal.valueOf(v));
40
+ }
41
+
42
+ @Override
43
+ public void doubleValue(double v) throws IOException, SQLException
44
+ {
45
+ if (Double.isNaN(v) || Double.isInfinite(v)) {
46
+ defaultValue.setBigDecimal();
47
+ } else {
48
+ batch.setBigDecimal(BigDecimal.valueOf(v));
49
+ }
50
+ }
51
+
52
+ @Override
53
+ public void stringValue(String v) throws IOException, SQLException
54
+ {
55
+ BigDecimal dv;
56
+ try {
57
+ dv = new BigDecimal(v);
58
+ } catch (NumberFormatException ex) {
59
+ defaultValue.setBigDecimal();
60
+ return;
61
+ }
62
+ batch.setBigDecimal(dv);
63
+ }
64
+
65
+ @Override
66
+ public void timestampValue(Timestamp v) throws IOException, SQLException
67
+ {
68
+ defaultValue.setBigDecimal();
69
+ }
70
+ }
@@ -1,52 +1,54 @@
1
- package org.embulk.output.jdbc.setter;
2
-
3
- import java.io.IOException;
4
- import java.sql.SQLException;
5
- import com.google.common.collect.ImmutableSet;
6
- import org.embulk.spi.PageReader;
7
- import org.embulk.spi.time.Timestamp;
8
- import org.embulk.output.jdbc.JdbcColumn;
9
- import org.embulk.output.jdbc.BatchInsert;
10
-
11
- public class BooleanColumnSetter
12
- extends ColumnSetter
13
- {
14
- private static final ImmutableSet<String> trueStrings =
15
- ImmutableSet.<String>of(
16
- "true", "True", "TRUE",
17
- "yes", "Yes", "YES",
18
- "y", "Y",
19
- "on", "On", "ON",
20
- "1");
21
-
22
- public BooleanColumnSetter(BatchInsert batch, PageReader pageReader,
23
- JdbcColumn column)
24
- {
25
- super(batch, pageReader, column);
26
- }
27
-
28
- protected void booleanValue(boolean v) throws IOException, SQLException
29
- {
30
- batch.setBoolean(v);
31
- }
32
-
33
- protected void longValue(long v) throws IOException, SQLException
34
- {
35
- batch.setBoolean(v > 0);
36
- }
37
-
38
- protected void doubleValue(double v) throws IOException, SQLException
39
- {
40
- batch.setBoolean(v > 0.0);
41
- }
42
-
43
- protected void stringValue(String v) throws IOException, SQLException
44
- {
45
- batch.setBoolean(trueStrings.contains(v));
46
- }
47
-
48
- protected void timestampValue(Timestamp v) throws IOException, SQLException
49
- {
50
- nullValue();
51
- }
52
- }
1
+ package org.embulk.output.jdbc.setter;
2
+
3
+ import java.io.IOException;
4
+ import java.sql.SQLException;
5
+ import com.google.common.collect.ImmutableSet;
6
+ import org.embulk.spi.time.Timestamp;
7
+ import org.embulk.output.jdbc.JdbcColumn;
8
+ import org.embulk.output.jdbc.BatchInsert;
9
+
10
+ public class BooleanColumnSetter
11
+ extends ColumnSetter
12
+ {
13
+ public BooleanColumnSetter(BatchInsert batch, JdbcColumn column,
14
+ DefaultValueSetter defaultValue)
15
+ {
16
+ super(batch, column, defaultValue);
17
+ }
18
+
19
+ @Override
20
+ public void nullValue() throws IOException, SQLException
21
+ {
22
+ defaultValue.setBoolean();
23
+ }
24
+
25
+ @Override
26
+ public void booleanValue(boolean v) throws IOException, SQLException
27
+ {
28
+ batch.setBoolean(v);
29
+ }
30
+
31
+ @Override
32
+ public void longValue(long v) throws IOException, SQLException
33
+ {
34
+ batch.setBoolean(v > 0);
35
+ }
36
+
37
+ @Override
38
+ public void doubleValue(double v) throws IOException, SQLException
39
+ {
40
+ batch.setBoolean(v > 0.0);
41
+ }
42
+
43
+ @Override
44
+ public void stringValue(String v) throws IOException, SQLException
45
+ {
46
+ defaultValue.setBoolean();
47
+ }
48
+
49
+ @Override
50
+ public void timestampValue(Timestamp v) throws IOException, SQLException
51
+ {
52
+ defaultValue.setBoolean();
53
+ }
54
+ }
@@ -0,0 +1,76 @@
1
+ package org.embulk.output.jdbc.setter;
2
+
3
+ import java.io.IOException;
4
+ import java.sql.SQLException;
5
+ import java.math.RoundingMode;
6
+ import com.google.common.math.DoubleMath;
7
+ import org.embulk.spi.ColumnVisitor;
8
+ import org.embulk.spi.time.Timestamp;
9
+ import org.embulk.output.jdbc.JdbcColumn;
10
+ import org.embulk.output.jdbc.BatchInsert;
11
+
12
+ public class ByteColumnSetter
13
+ extends ColumnSetter
14
+ {
15
+ public ByteColumnSetter(BatchInsert batch, JdbcColumn column,
16
+ DefaultValueSetter defaultValue)
17
+ {
18
+ super(batch, column, defaultValue);
19
+ }
20
+
21
+ @Override
22
+ public void nullValue() throws IOException, SQLException
23
+ {
24
+ defaultValue.setByte();
25
+ }
26
+
27
+ @Override
28
+ public void booleanValue(boolean v) throws IOException, SQLException
29
+ {
30
+ batch.setByte(v ? (byte) 1 : (byte) 0);
31
+ }
32
+
33
+ @Override
34
+ public void longValue(long v) throws IOException, SQLException
35
+ {
36
+ if (v > Byte.MAX_VALUE || v < Byte.MIN_VALUE) {
37
+ defaultValue.setByte();
38
+ } else {
39
+ batch.setByte((byte) v);
40
+ }
41
+ }
42
+
43
+ @Override
44
+ public void doubleValue(double v) throws IOException, SQLException
45
+ {
46
+ long lv;
47
+ try {
48
+ // TODO configurable rounding mode
49
+ lv = DoubleMath.roundToLong(v, RoundingMode.HALF_UP);
50
+ } catch (ArithmeticException ex) {
51
+ // NaN / Infinite / -Infinite
52
+ defaultValue.setByte();
53
+ return;
54
+ }
55
+ longValue(lv);
56
+ }
57
+
58
+ @Override
59
+ public void stringValue(String v) throws IOException, SQLException
60
+ {
61
+ byte sv;
62
+ try {
63
+ sv = Byte.parseByte(v);
64
+ } catch (NumberFormatException e) {
65
+ defaultValue.setByte();
66
+ return;
67
+ }
68
+ batch.setByte(sv);
69
+ }
70
+
71
+ @Override
72
+ public void timestampValue(Timestamp v) throws IOException, SQLException
73
+ {
74
+ defaultValue.setByte();
75
+ }
76
+ }
@@ -3,25 +3,22 @@ package org.embulk.output.jdbc.setter;
3
3
  import java.io.IOException;
4
4
  import java.sql.SQLException;
5
5
  import org.embulk.spi.Column;
6
- import org.embulk.spi.ColumnVisitor;
7
- import org.embulk.spi.PageReader;
8
6
  import org.embulk.spi.time.Timestamp;
9
7
  import org.embulk.output.jdbc.JdbcColumn;
10
8
  import org.embulk.output.jdbc.BatchInsert;
11
9
 
12
10
  public abstract class ColumnSetter
13
- implements ColumnVisitor
14
11
  {
15
12
  protected final BatchInsert batch;
16
- protected final PageReader pageReader;
17
13
  protected final JdbcColumn column;
14
+ protected final DefaultValueSetter defaultValue;
18
15
 
19
- public ColumnSetter(BatchInsert batch, PageReader pageReader,
20
- JdbcColumn column)
16
+ public ColumnSetter(BatchInsert batch, JdbcColumn column,
17
+ DefaultValueSetter defaultValue)
21
18
  {
22
19
  this.batch = batch;
23
- this.pageReader = pageReader;
24
20
  this.column = column;
21
+ this.defaultValue = defaultValue;
25
22
  }
26
23
 
27
24
  public JdbcColumn getColumn()
@@ -34,93 +31,15 @@ public abstract class ColumnSetter
34
31
  return column.getSqlType();
35
32
  }
36
33
 
37
- public void booleanColumn(Column column)
38
- {
39
- try {
40
- if (pageReader.isNull(column)) {
41
- nullValue();
42
- } else {
43
- booleanValue(pageReader.getBoolean(column));
44
- }
45
- } catch (IOException | SQLException ex) {
46
- // TODO exception class
47
- throw new RuntimeException(ex);
48
- }
49
- }
50
-
51
- public void longColumn(Column column)
52
- {
53
- try {
54
- if (pageReader.isNull(column)) {
55
- nullValue();
56
- } else {
57
- longValue(pageReader.getLong(column));
58
- }
59
- } catch (IOException | SQLException ex) {
60
- // TODO exception class
61
- throw new RuntimeException(ex);
62
- }
63
- }
64
-
65
- public void doubleColumn(Column column)
66
- {
67
- try {
68
- if (pageReader.isNull(column)) {
69
- nullValue();
70
- } else {
71
- doubleValue(pageReader.getDouble(column));
72
- }
73
- } catch (IOException | SQLException ex) {
74
- // TODO exception class
75
- throw new RuntimeException(ex);
76
- }
77
- }
78
-
79
- public void stringColumn(Column column)
80
- {
81
- try {
82
- if (pageReader.isNull(column)) {
83
- nullValue();
84
- } else {
85
- stringValue(pageReader.getString(column));
86
- }
87
- } catch (IOException | SQLException ex) {
88
- // TODO exception class
89
- throw new RuntimeException(ex);
90
- }
91
- }
92
-
93
- public void timestampColumn(Column column)
94
- {
95
- try {
96
- if (pageReader.isNull(column)) {
97
- nullValue();
98
- } else {
99
- timestampValue(pageReader.getTimestamp(column));
100
- }
101
- } catch (IOException | SQLException ex) {
102
- // TODO exception class
103
- throw new RuntimeException(ex);
104
- }
105
- }
106
-
107
- protected void nullValue() throws IOException, SQLException
108
- {
109
- try {
110
- batch.setNull(getSqlType());
111
- } catch (IOException | SQLException ex) {
112
- // TODO exception class
113
- throw new RuntimeException(ex);
114
- }
115
- }
34
+ public abstract void nullValue() throws IOException, SQLException;
116
35
 
117
- protected abstract void booleanValue(boolean v) throws IOException, SQLException;
36
+ public abstract void booleanValue(boolean v) throws IOException, SQLException;
118
37
 
119
- protected abstract void longValue(long v) throws IOException, SQLException;
38
+ public abstract void longValue(long v) throws IOException, SQLException;
120
39
 
121
- protected abstract void doubleValue(double v) throws IOException, SQLException;
40
+ public abstract void doubleValue(double v) throws IOException, SQLException;
122
41
 
123
- protected abstract void stringValue(String v) throws IOException, SQLException;
42
+ public abstract void stringValue(String v) throws IOException, SQLException;
124
43
 
125
- protected abstract void timestampValue(Timestamp v) throws IOException, SQLException;
44
+ public abstract void timestampValue(Timestamp v) throws IOException, SQLException;
126
45
  }
@@ -1,137 +1,189 @@
1
- package org.embulk.output.jdbc.setter;
2
-
3
- import java.sql.Types;
4
- import org.embulk.spi.time.TimestampFormatter;
5
- import org.embulk.spi.PageReader;
6
- import org.embulk.output.jdbc.BatchInsert;
7
- import org.embulk.output.jdbc.JdbcColumn;
8
-
9
- public class ColumnSetterFactory
10
- {
11
- protected final BatchInsert batch;
12
- protected final PageReader pageReader;
13
- protected final TimestampFormatter timestampFormatter;
14
-
15
- public ColumnSetterFactory(BatchInsert batch, PageReader pageReader,
16
- TimestampFormatter timestampFormatter)
17
- {
18
- this.batch = batch;
19
- this.pageReader = pageReader;
20
- this.timestampFormatter = timestampFormatter;
21
- }
22
-
23
- public SkipColumnSetter newSkipColumnSetter()
24
- {
25
- return new SkipColumnSetter(batch, pageReader);
26
- }
27
-
28
- public ColumnSetter newColumnSetter(JdbcColumn column)
29
- {
30
- switch(column.getSqlType()) {
31
- //// TODO
32
- // setByte
33
- //case Types.TINYINT:
34
- // return new ByteColumnSetter(batch, pageReader, column);
35
-
36
- //// TODO
37
- //// setShort
38
- //case Types.SMALLINT:
39
- // return new ShortColumnSetter(batch, pageReader, column);
40
-
41
- //// TODO
42
- //// setInt
43
- //case Types.INTEGER:
44
- // return new IntColumnSetter(batch, pageReader, column);
45
-
46
- // setLong
47
- case Types.BIGINT:
48
- return new LongColumnSetter(batch, pageReader, column);
49
-
50
- // setDouble
51
- case Types.DOUBLE:
52
- case Types.FLOAT:
53
- return new DoubleColumnSetter(batch, pageReader, column);
54
-
55
- // TODO
56
- //// setFloat
57
- //case Types.REAL:
58
- // return new FloatColumnSetter(batch, pageReader, column);
59
-
60
- // setBool
61
- case Types.BOOLEAN:
62
- case Types.BIT: // JDBC BIT is boolean, unlike SQL-92
63
- return new BooleanColumnSetter(batch, pageReader, column);
64
-
65
- // setString, Clob
66
- case Types.CHAR:
67
- case Types.VARCHAR:
68
- case Types.LONGVARCHAR:
69
- case Types.CLOB:
70
- return new StringColumnSetter(batch, pageReader, column, timestampFormatter);
71
-
72
- // TODO
73
- //// setNString, NClob
74
- //case Types.NCHAR:
75
- //case Types.NVARCHAR:
76
- //case Types.LONGNVARCHAR:
77
- // return new NStringColumnSetter(batch, pageReader, column);
78
-
79
- // TODO
80
- //// setBytes Blob
81
- //case Types.BINARY:
82
- //case Types.VARBINARY:
83
- //case Types.LONGVARBINARY:
84
- //case Types.BLOB:
85
- // return new BytesColumnSetter(batch, pageReader, column);
86
-
87
- // Time
88
- //case Types.DATE:
89
- // return new SqlDateColumnSetter(batch, pageReader, column); // TODO
90
- //case Types.TIME:
91
- // return new SqlTimeColumnSetter(batch, pageReader, column); // TODO
92
- case Types.TIMESTAMP:
93
- return new SqlTimestampColumnSetter(batch, pageReader, column);
94
-
95
- // Null
96
- case Types.NULL:
97
- return new NullColumnSetter(batch, pageReader, column);
98
-
99
- // TODO
100
- //// BigDecimal
101
- //case Types.NUMERIC:
102
- //case Types.DECIMAL:
103
- // return new BigDecimalColumnSetter(batch, pageReader, column);
104
-
105
- // others
106
- case Types.ARRAY: // array
107
- case Types.STRUCT: // map
108
- case Types.REF:
109
- case Types.DATALINK:
110
- case Types.SQLXML: // XML
111
- case Types.ROWID:
112
- case Types.DISTINCT:
113
- case Types.JAVA_OBJECT:
114
- case Types.OTHER:
115
- default:
116
- throw unsupportedOperationException(column);
117
- }
118
- }
119
-
120
- //private static String[] UNSUPPORTED = new String[] {
121
- // "ARRAY",
122
- // "STRUCT",
123
- // "REF",
124
- // "DATALINK",
125
- // "SQLXML",
126
- // "ROWID",
127
- // "DISTINCT",
128
- // "OTHER",
129
- //};
130
-
131
- private static UnsupportedOperationException unsupportedOperationException(JdbcColumn column)
132
- {
133
- throw new UnsupportedOperationException(
134
- String.format("Unsupported type %s (sqlType=%d, size=%d, scale=%d)",
135
- column.getTypeName(), column.getSqlType(), column.getSizeTypeParameter(), column.getScaleTypeParameter()));
136
- }
137
- }
1
+ package org.embulk.output.jdbc.setter;
2
+
3
+ import java.sql.Types;
4
+ import org.joda.time.DateTimeZone;
5
+ import org.embulk.spi.time.TimestampFormatter;
6
+ import org.embulk.output.jdbc.BatchInsert;
7
+ import org.embulk.output.jdbc.JdbcColumn;
8
+ import org.embulk.output.jdbc.JdbcColumnOption;
9
+ import org.embulk.config.ConfigException;
10
+
11
+ public class ColumnSetterFactory
12
+ {
13
+ protected final BatchInsert batch;
14
+ protected final DateTimeZone defaultTimeZone;
15
+
16
+ public ColumnSetterFactory(BatchInsert batch, DateTimeZone defaultTimeZone)
17
+ {
18
+ this.batch = batch;
19
+ this.defaultTimeZone = defaultTimeZone;
20
+ }
21
+
22
+ public SkipColumnSetter newSkipColumnSetter()
23
+ {
24
+ return new SkipColumnSetter(batch);
25
+ }
26
+
27
+ public DefaultValueSetter newDefaultValueSetter(JdbcColumn column, JdbcColumnOption option)
28
+ {
29
+ return new NullDefaultValueSetter(batch, column);
30
+ }
31
+
32
+ public ColumnSetter newColumnSetter(JdbcColumn column, JdbcColumnOption option)
33
+ {
34
+ switch (option.getValueType()) {
35
+ case "coalesce":
36
+ return newCoalesceColumnSetter(column, option);
37
+ case "byte":
38
+ return new ByteColumnSetter(batch, column, newDefaultValueSetter(column, option));
39
+ case "short":
40
+ return new ShortColumnSetter(batch, column, newDefaultValueSetter(column, option));
41
+ case "int":
42
+ return new IntColumnSetter(batch, column, newDefaultValueSetter(column, option));
43
+ case "long":
44
+ return new LongColumnSetter(batch, column, newDefaultValueSetter(column, option));
45
+ case "double":
46
+ return new DoubleColumnSetter(batch, column, newDefaultValueSetter(column, option));
47
+ case "float":
48
+ return new FloatColumnSetter(batch, column, newDefaultValueSetter(column, option));
49
+ case "boolean":
50
+ return new BooleanColumnSetter(batch, column, newDefaultValueSetter(column, option));
51
+ case "string":
52
+ return new StringColumnSetter(batch, column, newDefaultValueSetter(column, option), newTimestampFormatter(option));
53
+ case "nstring":
54
+ return new NStringColumnSetter(batch, column, newDefaultValueSetter(column, option), newTimestampFormatter(option));
55
+ case "date":
56
+ return new SqlDateColumnSetter(batch, column, newDefaultValueSetter(column, option), getTimeZone(option));
57
+ case "time":
58
+ return new SqlTimeColumnSetter(batch, column, newDefaultValueSetter(column, option));
59
+ case "timestamp":
60
+ return new SqlTimestampColumnSetter(batch, column, newDefaultValueSetter(column, option));
61
+ case "decimal":
62
+ return new BigDecimalColumnSetter(batch, column, newDefaultValueSetter(column, option));
63
+ case "null":
64
+ return new NullColumnSetter(batch, column, newDefaultValueSetter(column, option));
65
+ case "pass":
66
+ return new PassThroughColumnSetter(batch, column, newDefaultValueSetter(column, option));
67
+ default:
68
+ throw new ConfigException(String.format("Unknown value_type '%s' for column '%s'", option.getValueType(), column.getName()));
69
+ }
70
+ }
71
+
72
+ protected TimestampFormatter newTimestampFormatter(JdbcColumnOption option)
73
+ {
74
+ return new TimestampFormatter(
75
+ option.getJRuby(),
76
+ option.getTimestampFormat().getFormat(),
77
+ option.getTimeZone().or(defaultTimeZone));
78
+ }
79
+
80
+ protected DateTimeZone getTimeZone(JdbcColumnOption option)
81
+ {
82
+ return option.getTimeZone().or(defaultTimeZone);
83
+ }
84
+
85
+ public ColumnSetter newCoalesceColumnSetter(JdbcColumn column, JdbcColumnOption option)
86
+ {
87
+ switch(column.getSqlType()) {
88
+ // setByte
89
+ case Types.TINYINT:
90
+ return new ByteColumnSetter(batch, column, newDefaultValueSetter(column, option));
91
+
92
+ // setShort
93
+ case Types.SMALLINT:
94
+ return new ShortColumnSetter(batch, column, newDefaultValueSetter(column, option));
95
+
96
+ // setInt
97
+ case Types.INTEGER:
98
+ return new IntColumnSetter(batch, column, newDefaultValueSetter(column, option));
99
+
100
+ // setLong
101
+ case Types.BIGINT:
102
+ return new LongColumnSetter(batch, column, newDefaultValueSetter(column, option));
103
+
104
+ // setDouble
105
+ case Types.DOUBLE:
106
+ case Types.FLOAT:
107
+ return new DoubleColumnSetter(batch, column, newDefaultValueSetter(column, option));
108
+
109
+ // setFloat
110
+ case Types.REAL:
111
+ return new FloatColumnSetter(batch, column, newDefaultValueSetter(column, option));
112
+
113
+ // setBool
114
+ case Types.BOOLEAN:
115
+ case Types.BIT: // JDBC BIT is boolean, unlike SQL-92
116
+ return new BooleanColumnSetter(batch, column, newDefaultValueSetter(column, option));
117
+
118
+ // setString, Clob
119
+ case Types.CHAR:
120
+ case Types.VARCHAR:
121
+ case Types.LONGVARCHAR:
122
+ case Types.CLOB:
123
+ return new StringColumnSetter(batch, column, newDefaultValueSetter(column, option), newTimestampFormatter(option));
124
+
125
+ // setNString, NClob
126
+ case Types.NCHAR:
127
+ case Types.NVARCHAR:
128
+ case Types.LONGNVARCHAR:
129
+ return new NStringColumnSetter(batch, column, newDefaultValueSetter(column, option), newTimestampFormatter(option));
130
+
131
+ // TODO
132
+ //// setBytes Blob
133
+ //case Types.BINARY:
134
+ //case Types.VARBINARY:
135
+ //case Types.LONGVARBINARY:
136
+ //case Types.BLOB:
137
+ // return new BytesColumnSetter(batch, column, newDefaultValueSetter(column, option));
138
+
139
+ // Time
140
+ case Types.DATE:
141
+ return new SqlDateColumnSetter(batch, column, newDefaultValueSetter(column, option), getTimeZone(option));
142
+ case Types.TIME:
143
+ return new SqlTimeColumnSetter(batch, column, newDefaultValueSetter(column, option));
144
+ case Types.TIMESTAMP:
145
+ return new SqlTimestampColumnSetter(batch, column, newDefaultValueSetter(column, option));
146
+
147
+ // Null
148
+ case Types.NULL:
149
+ return new NullColumnSetter(batch, column, newDefaultValueSetter(column, option));
150
+
151
+ // BigDecimal
152
+ case Types.NUMERIC:
153
+ case Types.DECIMAL:
154
+ return new BigDecimalColumnSetter(batch, column, newDefaultValueSetter(column, option));
155
+
156
+ // others
157
+ case Types.ARRAY: // array
158
+ case Types.STRUCT: // map
159
+ case Types.REF:
160
+ case Types.DATALINK:
161
+ case Types.SQLXML: // XML
162
+ case Types.ROWID:
163
+ case Types.DISTINCT:
164
+ case Types.JAVA_OBJECT:
165
+ case Types.OTHER:
166
+ default:
167
+ throw unsupportedOperationException(column);
168
+ }
169
+ }
170
+
171
+ //private static String[] UNSUPPORTED = new String[] {
172
+ // "ARRAY",
173
+ // "STRUCT",
174
+ // "REF",
175
+ // "DATALINK",
176
+ // "SQLXML",
177
+ // "ROWID",
178
+ // "DISTINCT",
179
+ // "OTHER",
180
+ //};
181
+
182
+ private static UnsupportedOperationException unsupportedOperationException(JdbcColumn column)
183
+ {
184
+ throw new UnsupportedOperationException(
185
+ String.format("Unsupported type %s (sqlType=%d, size=%d, scale=%d)",
186
+ column.getDeclaredType().or(column.getSimpleTypeName()),
187
+ column.getSqlType(), column.getSizeTypeParameter(), column.getScaleTypeParameter()));
188
+ }
189
+ }