embulk-input-jdbc 0.6.4 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d64ee420864ebfe1de416471638a0df1348f9338
4
- data.tar.gz: 79cfb08db0a917123d6964e2670b7df9d17b060a
3
+ metadata.gz: 5f5b564e877d141a046fdb04fc281bc9730af498
4
+ data.tar.gz: aab75d9db9406b59446617ce03240b615db1f090
5
5
  SHA512:
6
- metadata.gz: 625c73db1ce05cd04aad35bf9a2da954f42a4042aab27009ae85029b84d845480413e6b6fad547474ac39bd495654d3e90ffb57ddda12ec00fe38144272fa008
7
- data.tar.gz: dff6a93e278ad1a3803c142135164260d451c3c77de796f901a22d26a39b552f3f0247a42e1aa8193c01177e0c832ff2c58542da94e5b955a946de054c0c1039
6
+ metadata.gz: 3924ef7b5698e9e9aaea6ed11763d3ff38acdb6bd963e352adca85912c6efc8ca12d0d159bf92d2b8e340e93e5585bfee3f668ef895bf7995c7eaf3d29155596
7
+ data.tar.gz: cadb17a65f0b6a9e658f4d4e830841e7d44662bc668c4964a9c0f67918d836c8f88b9260bfe21ca4991c59a96cc002ba7b51260335a8d28dcd9f9478a0a43f59
@@ -120,6 +120,14 @@ public abstract class AbstractJdbcInputPlugin
120
120
  @ConfigDefault("\"UTC\"")
121
121
  public DateTimeZone getDefaultTimeZone();
122
122
 
123
+ @Config("default_column_options")
124
+ @ConfigDefault("{}")
125
+ public Map<String, JdbcColumnOption> getDefaultColumnOptions();
126
+
127
+ @Config("after_select")
128
+ @ConfigDefault("null")
129
+ public Optional<String> getAfterSelect();
130
+
123
131
  public JdbcSchema getQuerySchema();
124
132
  public void setQuerySchema(JdbcSchema schema);
125
133
 
@@ -158,7 +166,7 @@ public abstract class AbstractJdbcInputPlugin
158
166
  private Schema setupTask(JdbcInputConnection con, PluginTask task) throws SQLException
159
167
  {
160
168
  // build SELECT query and gets schema of its result
161
- String query = getQuery(task, con);
169
+ String query = getQuery(task, con);
162
170
  logger.info("SQL: " + query);
163
171
  JdbcSchema querySchema = con.getSchemaOfQuery(query);
164
172
  task.setQuerySchema(querySchema);
@@ -166,11 +174,11 @@ public abstract class AbstractJdbcInputPlugin
166
174
  // validate column_options
167
175
  newColumnGetters(task, querySchema, null);
168
176
 
169
- ColumnGetterFactory factory = new ColumnGetterFactory(null, task.getDefaultTimeZone());
177
+ ColumnGetterFactory factory = newColumnGetterFactory(null, task.getDefaultTimeZone());
170
178
  ImmutableList.Builder<Column> columns = ImmutableList.builder();
171
179
  for (int i = 0; i < querySchema.getCount(); i++) {
172
180
  JdbcColumn column = querySchema.getColumn(i);
173
- JdbcColumnOption columnOption = columnOptionOf(task.getColumnOptions(), column);
181
+ JdbcColumnOption columnOption = columnOptionOf(task.getColumnOptions(), task.getDefaultColumnOptions(), column, factory.getJdbcType(column.getSqlType()));
174
182
  columns.add(new Column(i,
175
183
  column.getName(),
176
184
  factory.newColumnGetter(column, columnOption).getToType()));
@@ -258,6 +266,11 @@ public abstract class AbstractJdbcInputPlugin
258
266
  }
259
267
  }
260
268
  }
269
+
270
+ if (task.getAfterSelect().isPresent()) {
271
+ con.executeUpdate(task.getAfterSelect().get());
272
+ con.connection.commit();
273
+ }
261
274
  }
262
275
 
263
276
  } catch (SQLException ex) {
@@ -273,21 +286,29 @@ public abstract class AbstractJdbcInputPlugin
273
286
  return report;
274
287
  }
275
288
 
289
+ protected ColumnGetterFactory newColumnGetterFactory(PageBuilder pageBuilder, DateTimeZone dateTimeZone)
290
+ {
291
+ return new ColumnGetterFactory(pageBuilder, dateTimeZone);
292
+ }
293
+
276
294
  private List<ColumnGetter> newColumnGetters(PluginTask task, JdbcSchema querySchema, PageBuilder pageBuilder)
277
295
  throws SQLException
278
296
  {
279
- ColumnGetterFactory factory = new ColumnGetterFactory(pageBuilder, task.getDefaultTimeZone());
297
+ ColumnGetterFactory factory = newColumnGetterFactory(pageBuilder, task.getDefaultTimeZone());
280
298
  ImmutableList.Builder<ColumnGetter> getters = ImmutableList.builder();
281
299
  for (JdbcColumn c : querySchema.getColumns()) {
282
- JdbcColumnOption columnOption = columnOptionOf(task.getColumnOptions(), c);
300
+ JdbcColumnOption columnOption = columnOptionOf(task.getColumnOptions(), task.getDefaultColumnOptions(), c, factory.getJdbcType(c.getSqlType()));
283
301
  getters.add(factory.newColumnGetter(c, columnOption));
284
302
  }
285
303
  return getters.build();
286
304
  }
287
305
 
288
- private static JdbcColumnOption columnOptionOf(Map<String, JdbcColumnOption> columnOptions, JdbcColumn targetColumn)
306
+ private static JdbcColumnOption columnOptionOf(Map<String, JdbcColumnOption> columnOptions, Map<String, JdbcColumnOption> defaultColumnOptions, JdbcColumn targetColumn, String targetColumnSQLType)
289
307
  {
290
- return Optional.fromNullable(columnOptions.get(targetColumn.getName())).or(
308
+ return Optional
309
+ .fromNullable(columnOptions.get(targetColumn.getName()))
310
+ .or(Optional.fromNullable(defaultColumnOptions.get(targetColumnSQLType)))
311
+ .or(
291
312
  // default column option
292
313
  new Supplier<JdbcColumnOption>()
293
314
  {
@@ -94,7 +94,7 @@ public class JdbcInputConnection
94
94
  private final PreparedStatement fetchStatement;
95
95
  private boolean fetched = false;
96
96
 
97
- public SingleSelect(PreparedStatement fetchStatement) throws SQLException
97
+ public SingleSelect(PreparedStatement fetchStatement)
98
98
  {
99
99
  this.fetchStatement = fetchStatement;
100
100
  }
@@ -57,9 +57,7 @@ public abstract class AbstractColumnGetter implements ColumnGetter, ColumnVisito
57
57
  }
58
58
 
59
59
  @Override
60
- public void jsonColumn(Column column) {
61
- throw new UnsupportedOperationException("This plugin doesn't support json type. Please try to upgrade version of the plugin using 'embulk gem update' command. If the latest version still doesn't support json type, please contact plugin developers, or change configuration of input plugin not to use json type.");
62
- }
60
+ public void jsonColumn(Column column) { to.setNull(column); }
63
61
 
64
62
  @Override
65
63
  public void timestampColumn(Column column)
@@ -1,6 +1,9 @@
1
1
  package org.embulk.input.jdbc.getter;
2
2
 
3
+ import java.lang.reflect.Field;
3
4
  import java.sql.Types;
5
+ import java.util.HashMap;
6
+ import java.util.Map;
4
7
 
5
8
  import org.embulk.config.ConfigException;
6
9
  import org.embulk.input.jdbc.JdbcColumn;
@@ -13,8 +16,9 @@ import org.joda.time.DateTimeZone;
13
16
 
14
17
  public class ColumnGetterFactory
15
18
  {
16
- private final PageBuilder to;
19
+ protected final PageBuilder to;
17
20
  private final DateTimeZone defaultTimeZone;
21
+ private final Map<Integer, String> jdbcTypes = getAllJDBCTypes();
18
22
 
19
23
  public ColumnGetterFactory(PageBuilder to, DateTimeZone defaultTimeZone)
20
24
  {
@@ -43,6 +47,8 @@ public class ColumnGetterFactory
43
47
  return new BooleanColumnGetter(to, toType);
44
48
  case "string":
45
49
  return new StringColumnGetter(to, toType);
50
+ case "json":
51
+ return new JsonColumnGetter(to, toType);
46
52
  case "date":
47
53
  return new DateColumnGetter(to, toType, newTimestampFormatter(option, DateColumnGetter.DEFAULT_FORMAT));
48
54
  case "time":
@@ -56,7 +62,28 @@ public class ColumnGetterFactory
56
62
  }
57
63
  }
58
64
 
59
- private String sqlTypeToValueType(JdbcColumn column, int sqlType)
65
+ protected Map<Integer,String> getAllJDBCTypes() {
66
+ Map<Integer,String> map = new HashMap<Integer, String>();
67
+ for(Field f: Types.class.getFields()){
68
+ try {
69
+ map.put((Integer) f.get(null), f.getName());
70
+ } catch(IllegalAccessException iea){
71
+ }
72
+ }
73
+ return map;
74
+ }
75
+
76
+ public String getJdbcType(int sqlType)
77
+ {
78
+ String sqlTypeName = jdbcTypes.get(sqlType);
79
+ if(sqlTypeName == null){
80
+ throw new UnsupportedOperationException(String.format("Unknown SQL type value '%d'", sqlType));
81
+ }
82
+ return sqlTypeName;
83
+ }
84
+
85
+
86
+ protected String sqlTypeToValueType(JdbcColumn column, int sqlType)
60
87
  {
61
88
  switch(sqlType) {
62
89
  // getLong
@@ -135,7 +162,7 @@ public class ColumnGetterFactory
135
162
  }
136
163
  }
137
164
 
138
- private Type getToType(JdbcColumnOption option)
165
+ protected Type getToType(JdbcColumnOption option)
139
166
  {
140
167
  if (!option.getType().isPresent()) {
141
168
  return null;
@@ -0,0 +1,57 @@
1
+ package org.embulk.input.jdbc.getter;
2
+
3
+ import java.sql.ResultSet;
4
+ import java.sql.SQLException;
5
+
6
+ import org.embulk.input.jdbc.getter.AbstractColumnGetter;
7
+ import org.embulk.spi.Column;
8
+ import org.embulk.spi.PageBuilder;
9
+ import org.embulk.spi.json.JsonParseException;
10
+ import org.embulk.spi.json.JsonParser;
11
+ import org.embulk.spi.type.Type;
12
+ import org.embulk.spi.type.Types;
13
+ import org.msgpack.value.Value;
14
+
15
+ public class JsonColumnGetter
16
+ extends AbstractColumnGetter
17
+ {
18
+ final JsonParser jsonParser = new JsonParser();
19
+
20
+ private String value;
21
+
22
+ public JsonColumnGetter(PageBuilder to, Type toType)
23
+ {
24
+ super(to, toType);
25
+ }
26
+
27
+ @Override
28
+ protected void fetch(ResultSet from, int fromIndex) throws SQLException
29
+ {
30
+ value = from.getString(fromIndex);
31
+ }
32
+
33
+ @Override
34
+ protected Type getDefaultToType()
35
+ {
36
+ return Types.JSON;
37
+ }
38
+
39
+ @Override
40
+ public void jsonColumn(Column column)
41
+ {
42
+ Value v;
43
+ try {
44
+ v = jsonParser.parse(value);
45
+ } catch (JsonParseException e) {
46
+ super.jsonColumn(column);
47
+ return;
48
+ }
49
+ to.setJson(column, v);
50
+ }
51
+
52
+ @Override
53
+ public void stringColumn(Column column)
54
+ {
55
+ to.setString(column, value);
56
+ }
57
+ }
@@ -4,12 +4,17 @@ import java.sql.ResultSet;
4
4
  import java.sql.SQLException;
5
5
  import org.embulk.spi.Column;
6
6
  import org.embulk.spi.PageBuilder;
7
+ import org.embulk.spi.json.JsonParseException;
8
+ import org.embulk.spi.json.JsonParser;
7
9
  import org.embulk.spi.type.Type;
8
10
  import org.embulk.spi.type.Types;
11
+ import org.msgpack.value.Value;
9
12
 
10
13
  public class StringColumnGetter
11
14
  extends AbstractColumnGetter
12
15
  {
16
+ final JsonParser jsonParser = new JsonParser();
17
+
13
18
  private String value;
14
19
 
15
20
  public StringColumnGetter(PageBuilder to, Type toType)
@@ -55,6 +60,19 @@ public class StringColumnGetter
55
60
  to.setDouble(column, d);
56
61
  }
57
62
 
63
+ @Override
64
+ public void jsonColumn(Column column)
65
+ {
66
+ Value v;
67
+ try {
68
+ v = jsonParser.parse(value);
69
+ } catch (JsonParseException e) {
70
+ super.jsonColumn(column);
71
+ return;
72
+ }
73
+ to.setJson(column, v);
74
+ }
75
+
58
76
  @Override
59
77
  public void stringColumn(Column column)
60
78
  {
@@ -0,0 +1,79 @@
1
+ package org.embulk.input;
2
+
3
+ import java.io.File;
4
+ import java.util.ArrayList;
5
+ import java.util.List;
6
+
7
+ import org.embulk.EmbulkEmbed;
8
+ import org.embulk.EmbulkEmbed.Bootstrap;
9
+ import org.embulk.config.ConfigSource;
10
+ import org.embulk.plugin.InjectedPluginSource;
11
+
12
+ import com.google.inject.Binder;
13
+ import com.google.inject.Module;
14
+
15
+ public class EmbulkPluginTester
16
+ {
17
+ private static class PluginDefinition
18
+ {
19
+ public final Class<?> iface;
20
+ public final String name;
21
+ public final Class<?> impl;
22
+
23
+
24
+ public PluginDefinition(Class<?> iface, String name, Class<?> impl)
25
+ {
26
+ this.iface = iface;
27
+ this.name = name;
28
+ this.impl = impl;
29
+ }
30
+
31
+ }
32
+
33
+ private final List<PluginDefinition> plugins = new ArrayList<PluginDefinition>();
34
+
35
+ private EmbulkEmbed embulk;
36
+
37
+ public EmbulkPluginTester()
38
+ {
39
+ }
40
+
41
+ public EmbulkPluginTester(Class<?> iface, String name, Class<?> impl)
42
+ {
43
+ addPlugin(iface, name, impl);
44
+ }
45
+
46
+ public void addPlugin(Class<?> iface, String name, Class<?> impl)
47
+ {
48
+ plugins.add(new PluginDefinition(iface, name, impl));
49
+ }
50
+
51
+ public void run(String ymlPath) throws Exception
52
+ {
53
+ if (embulk == null) {
54
+ Bootstrap bootstrap = new EmbulkEmbed.Bootstrap();
55
+ bootstrap.addModules(new Module()
56
+ {
57
+ @Override
58
+ public void configure(Binder binder)
59
+ {
60
+ for (PluginDefinition plugin : plugins) {
61
+ InjectedPluginSource.registerPluginTo(binder, plugin.iface, plugin.name, plugin.impl);
62
+ }
63
+ }
64
+ });
65
+ embulk = bootstrap.initializeCloseable();
66
+ }
67
+
68
+ ConfigSource config = embulk.newConfigLoader().fromYamlFile(new File(ymlPath));
69
+ embulk.run(config);
70
+ }
71
+
72
+ public void destroy() {
73
+ if (embulk != null) {
74
+ embulk.destroy();
75
+ embulk = null;
76
+ }
77
+ }
78
+
79
+ }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: embulk-input-jdbc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.4
4
+ version: 0.7.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: 2016-01-15 00:00:00.000000000 Z
11
+ date: 2016-03-29 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Selects records from a table.
14
14
  email:
@@ -36,11 +36,13 @@ files:
36
36
  - src/main/java/org/embulk/input/jdbc/getter/DateColumnGetter.java
37
37
  - src/main/java/org/embulk/input/jdbc/getter/DoubleColumnGetter.java
38
38
  - src/main/java/org/embulk/input/jdbc/getter/FloatColumnGetter.java
39
+ - src/main/java/org/embulk/input/jdbc/getter/JsonColumnGetter.java
39
40
  - src/main/java/org/embulk/input/jdbc/getter/LongColumnGetter.java
40
41
  - src/main/java/org/embulk/input/jdbc/getter/StringColumnGetter.java
41
42
  - src/main/java/org/embulk/input/jdbc/getter/TimeColumnGetter.java
42
43
  - src/main/java/org/embulk/input/jdbc/getter/TimestampColumnGetter.java
43
- - classpath/embulk-input-jdbc-0.6.4.jar
44
+ - src/test/java/org/embulk/input/EmbulkPluginTester.java
45
+ - classpath/embulk-input-jdbc-0.7.0.jar
44
46
  homepage: https://github.com/embulk/embulk-input-jdbc
45
47
  licenses:
46
48
  - Apache 2.0