embulk-input-jdbc 0.3.0 → 0.4.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: 8f90e12c2fee1a30c130f033663488322ea010a9
4
- data.tar.gz: 9dbae6a7d01b3bda36606b6f269f1f7d2fdcddc9
3
+ metadata.gz: 1c8e7bcef7a17ff17d0765398c37a01e30069d1e
4
+ data.tar.gz: e8da2a078d57ea323a94e4e12228ec38aa9ebff8
5
5
  SHA512:
6
- metadata.gz: 3dbed8686fd882876041fe10914297a63d7cce394ef3e686abc12bba4104f0ac88e3e59b8ce100da98fffa8bbf31bc2f4f040ad77b2c310312e7da441113958a
7
- data.tar.gz: e53a6be622d0ec449c946990cdaedb8a21b3fa83d3a593894dee39675adc3e793bb5c5d1718ae5eda5da8c28895a494abe5ffa0bd5b0a96e82f1b0c1582b5f82
6
+ metadata.gz: 31f6a9698536e5bb62c795a43a0a7215e3f6c654c3fdacf3155702903699146187953dee40c60da4cb9358fe6e6b420f53ea94511ef4e099d4bd71defee6a305
7
+ data.tar.gz: 6e95e49f74ac0d529486b610713d75f8372d619fd4fcc47847a2ea908263c0a1aa593a7fbaf7ddc691d4854d0506405c3d8dc83395d377f99c4e1a071e8debb6
@@ -10,6 +10,7 @@ import com.google.common.base.Throwables;
10
10
  import com.google.common.collect.ImmutableList;
11
11
  import org.embulk.config.CommitReport;
12
12
  import org.embulk.config.Config;
13
+ import org.embulk.config.ConfigException;
13
14
  import org.embulk.config.ConfigDefault;
14
15
  import org.embulk.config.ConfigDiff;
15
16
  import org.embulk.config.ConfigInject;
@@ -39,7 +40,12 @@ public abstract class AbstractJdbcInputPlugin
39
40
  public Properties getOptions();
40
41
 
41
42
  @Config("table")
42
- public String getTable();
43
+ @ConfigDefault("null")
44
+ public Optional<String> getTable();
45
+
46
+ @Config("query")
47
+ @ConfigDefault("null")
48
+ public Optional<String> getQuery();
43
49
 
44
50
  @Config("select")
45
51
  @ConfigDefault("null")
@@ -134,7 +140,7 @@ public abstract class AbstractJdbcInputPlugin
134
140
  private Schema setupTask(JdbcInputConnection con, PluginTask task) throws SQLException
135
141
  {
136
142
  // build SELECT query and gets schema of its result
137
- JdbcSchema querySchema = con.getSchemaOfQuery(task.getTable(), task.getSelect(), task.getWhere(), task.getOrderBy());
143
+ JdbcSchema querySchema = con.getSchemaOfQuery(getQuery(task, con));
138
144
  task.setQuerySchema(querySchema);
139
145
 
140
146
  ColumnGetterFactory factory = newColumnGetterFactory(task);
@@ -147,6 +153,22 @@ public abstract class AbstractJdbcInputPlugin
147
153
  return new Schema(columns.build());
148
154
  }
149
155
 
156
+ private String getQuery(PluginTask task, JdbcInputConnection con)
157
+ {
158
+ if (task.getQuery().isPresent()) {
159
+ if (task.getTable().isPresent() || task.getSelect().isPresent() ||
160
+ task.getWhere().isPresent() || task.getOrderBy().isPresent()) {
161
+ throw new ConfigException("'table', 'select', 'where' and 'order_by' parameters are unnecessary if 'query' parameter is set.");
162
+ }
163
+ return task.getQuery().get();
164
+ } else if (task.getTable().isPresent()) {
165
+ return con.buildSelectQuery(task.getTable().get(), task.getSelect(),
166
+ task.getWhere(), task.getOrderBy());
167
+ } else {
168
+ throw new ConfigException("'table' parameter is required (if 'query' parameter is not set)");
169
+ }
170
+ }
171
+
150
172
  @Override
151
173
  public ConfigDiff resume(TaskSource taskSource,
152
174
  Schema schema, int taskCount,
@@ -201,9 +223,7 @@ public abstract class AbstractJdbcInputPlugin
201
223
  List<ColumnGetter> getters = newColumnGetters(task, querySchema);
202
224
 
203
225
  try (JdbcInputConnection con = newConnection(task)) {
204
- try (BatchSelect cursor = con.newSelectCursor(
205
- task.getTable(), task.getSelect(), task.getWhere(),
206
- task.getOrderBy(), task.getFetchRows())) {
226
+ try (BatchSelect cursor = con.newSelectCursor(getQuery(task, con), task.getFetchRows())) {
207
227
  while (true) {
208
228
  // TODO run fetch() in another thread asynchronously
209
229
  // TODO retry fetch() if it failed (maybe order_by is required and unique_column(s) option is also required)
@@ -41,31 +41,8 @@ public class JdbcInputConnection
41
41
  executeUpdate(sql);
42
42
  }
43
43
 
44
- protected String buildSelectQuery(String tableName,
45
- Optional<String> selectColumnList, Optional<String> whereCondition,
46
- Optional<String> orderByColumn)
47
- {
48
- StringBuilder sb = new StringBuilder();
49
-
50
- sb.append("SELECT ");
51
- sb.append(selectColumnList.or("*"));
52
- sb.append(" FROM ").append(quoteIdentifierString(tableName));
53
- if (whereCondition.isPresent()) {
54
- sb.append(" WHERE ").append(whereCondition.get());
55
- }
56
- if (orderByColumn.isPresent()) {
57
- sb.append("ORDER BY ").append(quoteIdentifierString(orderByColumn.get())).append(" ASC");
58
- }
59
-
60
- return sb.toString();
61
- }
62
-
63
- public JdbcSchema getSchemaOfQuery(String tableName,
64
- Optional<String> selectColumnList, Optional<String> whereCondition,
65
- Optional<String> orderByColumn) throws SQLException
44
+ public JdbcSchema getSchemaOfQuery(String query) throws SQLException
66
45
  {
67
- String query = buildSelectQuery(tableName, selectColumnList, whereCondition,
68
- orderByColumn);
69
46
  PreparedStatement stmt = connection.prepareStatement(query);
70
47
  try {
71
48
  return getSchemaOfResultMetadata(stmt.getMetaData());
@@ -89,18 +66,15 @@ public class JdbcInputConnection
89
66
  return new JdbcSchema(columns.build());
90
67
  }
91
68
 
92
- public BatchSelect newSelectCursor(String tableName,
93
- Optional<String> selectColumnList, Optional<String> whereCondition,
94
- Optional<String> orderByColumn, int fetchRows) throws SQLException
69
+ public BatchSelect newSelectCursor(String query, int fetchRows) throws SQLException
95
70
  {
96
- String select = buildSelectQuery(tableName, selectColumnList, whereCondition, orderByColumn);
97
- return newBatchSelect(select, fetchRows);
71
+ return newBatchSelect(query, fetchRows);
98
72
  }
99
73
 
100
- protected BatchSelect newBatchSelect(String select, int fetchRows) throws SQLException
74
+ protected BatchSelect newBatchSelect(String query, int fetchRows) throws SQLException
101
75
  {
102
- logger.info("SQL: " + select);
103
- PreparedStatement stmt = connection.prepareStatement(select);
76
+ logger.info("SQL: " + query);
77
+ PreparedStatement stmt = connection.prepareStatement(query);
104
78
  stmt.setFetchSize(fetchRows);
105
79
  return new SingleSelect(stmt);
106
80
  }
@@ -169,4 +143,23 @@ public class JdbcInputConnection
169
143
  {
170
144
  return identifierQuoteString + str + identifierQuoteString;
171
145
  }
146
+
147
+ public String buildSelectQuery(String tableName,
148
+ Optional<String> selectColumnList, Optional<String> whereCondition,
149
+ Optional<String> orderByColumn)
150
+ {
151
+ StringBuilder sb = new StringBuilder();
152
+
153
+ sb.append("SELECT ");
154
+ sb.append(selectColumnList.or("*"));
155
+ sb.append(" FROM ").append(quoteIdentifierString(tableName));
156
+ if (whereCondition.isPresent()) {
157
+ sb.append(" WHERE ").append(whereCondition.get());
158
+ }
159
+ if (orderByColumn.isPresent()) {
160
+ sb.append("ORDER BY ").append(quoteIdentifierString(orderByColumn.get())).append(" ASC");
161
+ }
162
+
163
+ return sb.toString();
164
+ }
172
165
  }
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.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-02-28 00:00:00.000000000 Z
11
+ date: 2015-03-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Selects records from a table.
14
14
  email:
@@ -27,7 +27,7 @@ files:
27
27
  - src/main/java/org/embulk/input/jdbc/getter/ColumnGetter.java
28
28
  - src/main/java/org/embulk/input/jdbc/getter/ColumnGetterFactory.java
29
29
  - src/main/java/org/embulk/input/jdbc/getter/ColumnGetters.java
30
- - classpath/embulk-input-jdbc-0.3.0.jar
30
+ - classpath/embulk-input-jdbc-0.4.0.jar
31
31
  homepage: https://github.com/embulk/embulk-input-jdbc
32
32
  licenses:
33
33
  - Apache 2.0