embulk-input-jdbc 0.7.1 → 0.7.2

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: fdf58a6bf2822d6fc08367c222f2cd6eab203d56
4
- data.tar.gz: f68be4ab3068b33d4c35b824c4fed9068bd0c4a4
3
+ metadata.gz: 9a672095825353519696e26638bf7b750406a720
4
+ data.tar.gz: f90651ce7041f7435cc07da9435dafe593b027cc
5
5
  SHA512:
6
- metadata.gz: 7c52c12c901c33c8e8b1e4dfeb4a7ae68246f9257fcb7a9bf933f63dcb258e7e83e51167709c7ea21bbce92cf595095cb2efbe123cacd498b56359f75c9429f8
7
- data.tar.gz: 237ab455d727b5f4831119534860448f06fb1f8ca90286c6f65d6efa232db7c975b3976f40bc8e660d294c9390636549d8d63117139f7b34906e26bc317fccdf
6
+ metadata.gz: 5dbd93a8b413d1ff2b3d53d61c04706a1696cc8a544805c094d9fdef05a8bd11072a98b39edf602b08af9ab679684220bd6699df2d0963ded92d514c7d6e7b2e
7
+ data.tar.gz: 9886411e1b6d7358669827291f53a4994b1a116123d724253c0f39ce9c5041413e8207b8b84ab02b4686c8775be6594f6e70a0e0d74a79190d013f06d0514b23
@@ -186,7 +186,7 @@ public abstract class AbstractJdbcInputPlugin
186
186
  return new Schema(columns.build());
187
187
  }
188
188
 
189
- private String getQuery(PluginTask task, JdbcInputConnection con)
189
+ private String getQuery(PluginTask task, JdbcInputConnection con) throws SQLException
190
190
  {
191
191
  if (task.getQuery().isPresent()) {
192
192
  if (task.getTable().isPresent() || task.getSelect().isPresent() ||
@@ -310,8 +310,23 @@ public abstract class AbstractJdbcInputPlugin
310
310
 
311
311
  private static JdbcColumnOption columnOptionOf(Map<String, JdbcColumnOption> columnOptions, Map<String, JdbcColumnOption> defaultColumnOptions, JdbcColumn targetColumn, String targetColumnSQLType)
312
312
  {
313
+ JdbcColumnOption columnOption = columnOptions.get(targetColumn.getName());
314
+ if (columnOption == null) {
315
+ String foundName = null;
316
+ for (Map.Entry<String, JdbcColumnOption> entry : columnOptions.entrySet()) {
317
+ if (entry.getKey().equalsIgnoreCase(targetColumn.getName())) {
318
+ if (columnOption != null) {
319
+ throw new ConfigException(String.format("Cannot specify column '%s' because both '%s' and '%s' exist in column_options.",
320
+ targetColumn.getName(), foundName, entry.getKey()));
321
+ }
322
+ foundName = entry.getKey();
323
+ columnOption = entry.getValue();
324
+ }
325
+ }
326
+ }
327
+
313
328
  return Optional
314
- .fromNullable(columnOptions.get(targetColumn.getName()))
329
+ .fromNullable(columnOption)
315
330
  .or(Optional.fromNullable(defaultColumnOptions.get(targetColumnSQLType)))
316
331
  .or(
317
332
  // default column option
@@ -5,12 +5,18 @@ import java.sql.DatabaseMetaData;
5
5
  import java.sql.PreparedStatement;
6
6
  import java.sql.ResultSet;
7
7
  import java.sql.ResultSetMetaData;
8
- import java.sql.Statement;
9
8
  import java.sql.SQLException;
9
+ import java.sql.Statement;
10
+ import java.util.Set;
11
+
12
+ import org.embulk.config.ConfigException;
13
+ import org.embulk.spi.Exec;
14
+ import org.slf4j.Logger;
15
+
10
16
  import com.google.common.base.Optional;
11
17
  import com.google.common.collect.ImmutableList;
12
- import org.slf4j.Logger;
13
- import org.embulk.spi.Exec;
18
+ import com.google.common.collect.ImmutableSet;
19
+ import com.google.common.collect.ImmutableSet.Builder;
14
20
 
15
21
  public class JdbcInputConnection
16
22
  implements AutoCloseable
@@ -146,25 +152,90 @@ public class JdbcInputConnection
146
152
 
147
153
  protected String buildTableName(String tableName)
148
154
  {
149
- return quoteIdentifierString(tableName);
155
+ return quoteIdentifierString(tableName);
150
156
  }
151
157
 
152
158
  public String buildSelectQuery(String tableName,
153
159
  Optional<String> selectColumnList, Optional<String> whereCondition,
154
- Optional<String> orderByColumn)
160
+ Optional<String> orderByColumn) throws SQLException
155
161
  {
162
+ String actualTableName;
163
+ if (tableExists(tableName)) {
164
+ actualTableName = tableName;
165
+ } else {
166
+ String upperTableName = tableName.toUpperCase();
167
+ String lowerTableName = tableName.toLowerCase();
168
+ if (tableExists(upperTableName)) {
169
+ if (tableExists(lowerTableName)) {
170
+ throw new ConfigException(String.format("Cannot specify table '%s' because both '%s' and '%s' exist.",
171
+ tableName, upperTableName, lowerTableName));
172
+ } else {
173
+ actualTableName = upperTableName;
174
+ }
175
+ } else {
176
+ if (tableExists(lowerTableName)) {
177
+ actualTableName = lowerTableName;
178
+ } else {
179
+ actualTableName = tableName;
180
+ }
181
+ }
182
+ }
183
+
156
184
  StringBuilder sb = new StringBuilder();
157
185
 
158
186
  sb.append("SELECT ");
159
187
  sb.append(selectColumnList.or("*"));
160
- sb.append(" FROM ").append(buildTableName(tableName));
188
+ sb.append(" FROM ").append(buildTableName(actualTableName));
189
+
161
190
  if (whereCondition.isPresent()) {
162
191
  sb.append(" WHERE ").append(whereCondition.get());
163
192
  }
193
+
164
194
  if (orderByColumn.isPresent()) {
165
- sb.append("ORDER BY ").append(quoteIdentifierString(orderByColumn.get())).append(" ASC");
195
+ String actualOrderByColumn;
196
+ Set<String> columnNames = getColumnNames(actualTableName);
197
+ if (columnNames.contains(orderByColumn.get())) {
198
+ actualOrderByColumn = orderByColumn.get();
199
+ } else {
200
+ String upperOrderByColumn = orderByColumn.get().toUpperCase();
201
+ String lowerOrderByColumn = orderByColumn.get().toLowerCase();
202
+ if (columnNames.contains(upperOrderByColumn)) {
203
+ if (columnNames.contains(lowerOrderByColumn)) {
204
+ throw new ConfigException(String.format("Cannot specify order-by colum '%s' because both '%s' and '%s' exist.",
205
+ orderByColumn.get(), upperOrderByColumn, lowerOrderByColumn));
206
+ } else {
207
+ actualOrderByColumn = upperOrderByColumn;
208
+ }
209
+ } else {
210
+ if (columnNames.contains(lowerOrderByColumn)) {
211
+ actualOrderByColumn = lowerOrderByColumn;
212
+ } else {
213
+ actualOrderByColumn = orderByColumn.get();
214
+ }
215
+ }
216
+ }
217
+
218
+ sb.append("ORDER BY ").append(quoteIdentifierString(actualOrderByColumn)).append(" ASC");
166
219
  }
167
220
 
168
221
  return sb.toString();
169
222
  }
223
+
224
+ private boolean tableExists(String tableName) throws SQLException
225
+ {
226
+ try (ResultSet rs = connection.getMetaData().getTables(null, schemaName, tableName, null)) {
227
+ return rs.next();
228
+ }
229
+ }
230
+
231
+ private Set<String> getColumnNames(String tableName) throws SQLException
232
+ {
233
+ Builder<String> columnNamesBuilder = ImmutableSet.builder();
234
+ try (ResultSet rs = connection.getMetaData().getColumns(null, schemaName, tableName, null)) {
235
+ while (rs.next()) {
236
+ columnNamesBuilder.add(rs.getString("COLUMN_NAME"));
237
+ }
238
+ return columnNamesBuilder.build();
239
+ }
240
+ }
170
241
  }
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.7.1
4
+ version: 0.7.2
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-05-06 00:00:00.000000000 Z
11
+ date: 2016-06-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Selects records from a table.
14
14
  email:
@@ -42,7 +42,7 @@ files:
42
42
  - src/main/java/org/embulk/input/jdbc/getter/TimeColumnGetter.java
43
43
  - src/main/java/org/embulk/input/jdbc/getter/TimestampColumnGetter.java
44
44
  - src/test/java/org/embulk/input/EmbulkPluginTester.java
45
- - classpath/embulk-input-jdbc-0.7.1.jar
45
+ - classpath/embulk-input-jdbc-0.7.2.jar
46
46
  homepage: https://github.com/embulk/embulk-input-jdbc
47
47
  licenses:
48
48
  - Apache 2.0