embulk-output-jdbc 0.2.0 → 0.2.1

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: 01a92a4a868446c83653516d3028fb5b423f2fdb
4
- data.tar.gz: 1894bda08176a5ec0e9be2008d516689d3066871
3
+ metadata.gz: ad63f756531e91a61059ac0568e2b9d96b47a4fd
4
+ data.tar.gz: 0e2d9ccc4f4f40bd5439ba8639f462583b194f80
5
5
  SHA512:
6
- metadata.gz: 3bc0733eec6851d2f953fe32c703177643b21bf09e34d463af4bf876ef7525b8eeaca4070627c41e0cb595b0f875bfac60ffcc6bea18cfa8c4e4779b2f9c1302
7
- data.tar.gz: 76ba0c713a4dcd324aae9c9d8502556a082df91b36a9708277e69a8b81a91753bd91b7d8143299bd98fb9878f8f6ad09e77634456ad6fcfff0c0066f103be049
6
+ metadata.gz: 629fca902b6c03d39c3a87ff7276f31d6028f1743acde07ee573a911fd8d0dac1050d793e3aaf23de4f0084d6ef4b475d819d3bc5652256f7484cd8d0fcce616
7
+ data.tar.gz: 6d296929dd913354ffc58f7c4cf59b1a67a5bd020f3a5a9ca00ba8c7080673d37a9c6d37df1c730fb24a6244c76556f18229d77285d2e3addd16a5a99e66d0bc
@@ -1,8 +1,5 @@
1
1
  package org.embulk.output;
2
2
 
3
- import java.nio.file.Paths;
4
- import java.util.Set;
5
- import java.util.HashSet;
6
3
  import java.util.Properties;
7
4
  import java.sql.Driver;
8
5
  import java.io.IOException;
@@ -10,8 +7,6 @@ import java.sql.Connection;
10
7
  import java.sql.SQLException;
11
8
  import com.google.common.base.Optional;
12
9
  import com.google.common.base.Throwables;
13
- import org.embulk.spi.Exec;
14
- import org.embulk.spi.PluginClassLoader;
15
10
  import org.embulk.config.Config;
16
11
  import org.embulk.config.ConfigDefault;
17
12
  import org.embulk.output.jdbc.AbstractJdbcOutputPlugin;
@@ -23,8 +18,6 @@ import org.embulk.output.jdbc.JdbcOutputConnection;
23
18
  public class JdbcOutputPlugin
24
19
  extends AbstractJdbcOutputPlugin
25
20
  {
26
- private final static Set<String> loadedJarGlobs = new HashSet<String>();
27
-
28
21
  public interface GenericPluginTask extends PluginTask
29
22
  {
30
23
  @Config("driver_path")
@@ -62,13 +55,7 @@ public class JdbcOutputPlugin
62
55
  GenericPluginTask t = (GenericPluginTask) task;
63
56
 
64
57
  if (t.getDriverPath().isPresent()) {
65
- synchronized (loadedJarGlobs) {
66
- String glob = t.getDriverPath().get();
67
- if (!loadedJarGlobs.contains(glob)) {
68
- loadDriverJar(glob);
69
- loadedJarGlobs.add(glob);
70
- }
71
- }
58
+ loadDriverJar(t.getDriverPath().get());
72
59
  }
73
60
 
74
61
  Properties props = new Properties();
@@ -85,13 +72,6 @@ public class JdbcOutputPlugin
85
72
  t.getSchema().orNull());
86
73
  }
87
74
 
88
- private void loadDriverJar(String glob)
89
- {
90
- // TODO match glob
91
- PluginClassLoader loader = (PluginClassLoader) getClass().getClassLoader();
92
- loader.addPath(Paths.get(glob));
93
- }
94
-
95
75
  private static class GenericOutputConnector
96
76
  implements JdbcOutputConnector
97
77
  {
@@ -1,19 +1,25 @@
1
1
  package org.embulk.output.jdbc;
2
2
 
3
+ import java.util.HashSet;
3
4
  import java.util.List;
4
5
  import java.util.Locale;
5
6
  import java.util.Properties;
7
+ import java.util.Set;
6
8
  import java.util.concurrent.ExecutionException;
7
9
  import java.io.IOException;
10
+ import java.nio.file.Paths;
8
11
  import java.sql.Types;
9
12
  import java.sql.Connection;
10
13
  import java.sql.ResultSet;
11
14
  import java.sql.DatabaseMetaData;
12
15
  import java.sql.SQLException;
16
+
13
17
  import org.slf4j.Logger;
18
+
14
19
  import com.google.common.base.Optional;
15
20
  import com.google.common.base.Throwables;
16
21
  import com.google.common.collect.ImmutableList;
22
+
17
23
  import org.embulk.config.CommitReport;
18
24
  import org.embulk.config.Config;
19
25
  import org.embulk.config.ConfigDefault;
@@ -27,19 +33,24 @@ import org.embulk.spi.Column;
27
33
  import org.embulk.spi.ColumnVisitor;
28
34
  import org.embulk.spi.OutputPlugin;
29
35
  import org.embulk.spi.PageOutput;
36
+ import org.embulk.spi.PluginClassLoader;
30
37
  import org.embulk.spi.Schema;
31
38
  import org.embulk.spi.TransactionalPageOutput;
32
39
  import org.embulk.spi.Page;
33
40
  import org.embulk.spi.PageReader;
34
41
  import org.embulk.spi.time.Timestamp;
42
+ import org.embulk.spi.time.TimestampFormatter;
35
43
  import org.embulk.output.jdbc.setter.ColumnSetter;
36
44
  import org.embulk.output.jdbc.setter.ColumnSetterFactory;
37
45
  import org.embulk.output.jdbc.RetryExecutor.IdempotentOperation;
46
+
38
47
  import static org.embulk.output.jdbc.RetryExecutor.retryExecutor;
39
48
 
40
49
  public abstract class AbstractJdbcOutputPlugin
41
50
  implements OutputPlugin
42
51
  {
52
+ private final static Set<String> loadedJarGlobs = new HashSet<String>();
53
+
43
54
  private final Logger logger = Exec.getLogger(getClass());
44
55
 
45
56
  public interface PluginTask
@@ -73,6 +84,18 @@ public abstract class AbstractJdbcOutputPlugin
73
84
  public void setMultipleLoadTablePrefix(Optional<String> prefix);
74
85
  }
75
86
 
87
+ protected void loadDriverJar(String glob)
88
+ {
89
+ synchronized (loadedJarGlobs) {
90
+ if (!loadedJarGlobs.contains(glob)) {
91
+ // TODO match glob
92
+ PluginClassLoader loader = (PluginClassLoader) getClass().getClassLoader();
93
+ loader.addPath(Paths.get(glob));
94
+ loadedJarGlobs.add(glob);
95
+ }
96
+ }
97
+ }
98
+
76
99
  // for subclasses to add @Config
77
100
  protected Class<? extends PluginTask> getTaskClass()
78
101
  {
@@ -266,7 +289,7 @@ public abstract class AbstractJdbcOutputPlugin
266
289
  // DROP TABLE IF EXISTS xyz__0000000054d92dee1e452158_bulk_load_temp
267
290
  // CREATE TABLE IF NOT EXISTS xyz__0000000054d92dee1e452158_bulk_load_temp
268
291
  // swapTableName = "xyz__0000000054d92dee1e452158_bulk_load_temp"
269
- String swapTableName = task.getTable() + "_" + getTransactionUniqueName() + "_bulk_load_temp";
292
+ String swapTableName = generateSwapTableName(task);
270
293
  con.dropTableIfExists(swapTableName);
271
294
  con.createTableIfNotExists(swapTableName, newJdbcSchemaForNewTable(schema));
272
295
  targetTableSchema = newJdbcSchemaFromExistentTable(con, swapTableName);
@@ -291,6 +314,11 @@ public abstract class AbstractJdbcOutputPlugin
291
314
  task.setLoadSchema(matchSchemaByColumnNames(schema, targetTableSchema));
292
315
  }
293
316
 
317
+ protected String generateSwapTableName(PluginTask task) throws SQLException
318
+ {
319
+ return task.getTable() + "_" + getTransactionUniqueName() + "_bulk_load_temp";
320
+ }
321
+
294
322
  protected void doCommit(JdbcOutputConnection con, PluginTask task, int taskCount)
295
323
  throws SQLException
296
324
  {
@@ -443,7 +471,7 @@ public abstract class AbstractJdbcOutputPlugin
443
471
  }
444
472
  try {
445
473
  PageReader reader = new PageReader(schema);
446
- ColumnSetterFactory factory = new ColumnSetterFactory(batch, reader, null); // TODO TimestampFormatter
474
+ ColumnSetterFactory factory = newColumnSetterFactory(batch, reader, null); // TODO TimestampFormatter
447
475
 
448
476
  JdbcSchema loadSchema = task.getLoadSchema();
449
477
 
@@ -507,6 +535,12 @@ public abstract class AbstractJdbcOutputPlugin
507
535
  }
508
536
  }
509
537
 
538
+ protected ColumnSetterFactory newColumnSetterFactory(BatchInsert batch, PageReader pageReader,
539
+ TimestampFormatter timestampFormatter)
540
+ {
541
+ return new ColumnSetterFactory(batch, pageReader, timestampFormatter);
542
+ }
543
+
510
544
  public static class PluginPageOutput
511
545
  implements TransactionalPageOutput
512
546
  {
@@ -4,8 +4,10 @@ import java.util.List;
4
4
  import java.sql.Connection;
5
5
  import java.sql.DatabaseMetaData;
6
6
  import java.sql.PreparedStatement;
7
+ import java.sql.ResultSet;
7
8
  import java.sql.SQLException;
8
9
  import java.sql.Statement;
10
+
9
11
  import org.slf4j.Logger;
10
12
  import org.embulk.spi.Exec;
11
13
 
@@ -58,12 +60,37 @@ public class JdbcOutputConnection
58
60
  }
59
61
  }
60
62
 
63
+ public boolean tableExists(String tableName) throws SQLException
64
+ {
65
+ try (ResultSet rs = connection.getMetaData().getTables(null, schemaName, tableName, null)) {
66
+ return rs.next();
67
+ }
68
+ }
69
+
61
70
  public void dropTableIfExists(String tableName) throws SQLException
62
71
  {
63
72
  Statement stmt = connection.createStatement();
64
73
  try {
65
- String sql = String.format("DROP TABLE IF EXISTS %s", quoteIdentifierString(tableName));
66
- executeUpdate(stmt, sql);
74
+ dropTableIfExists(stmt, tableName);
75
+ commitIfNecessary(connection);
76
+ } catch (SQLException ex) {
77
+ throw safeRollback(connection, ex);
78
+ } finally {
79
+ stmt.close();
80
+ }
81
+ }
82
+
83
+ protected void dropTableIfExists(Statement stmt, String tableName) throws SQLException
84
+ {
85
+ String sql = String.format("DROP TABLE IF EXISTS %s", quoteIdentifierString(tableName));
86
+ executeUpdate(stmt, sql);
87
+ }
88
+
89
+ public void dropTable(String tableName) throws SQLException
90
+ {
91
+ Statement stmt = connection.createStatement();
92
+ try {
93
+ dropTable(stmt, tableName);
67
94
  commitIfNecessary(connection);
68
95
  } catch (SQLException ex) {
69
96
  throw safeRollback(connection, ex);
@@ -72,6 +99,12 @@ public class JdbcOutputConnection
72
99
  }
73
100
  }
74
101
 
102
+ protected void dropTable(Statement stmt, String tableName) throws SQLException
103
+ {
104
+ String sql = String.format("DROP TABLE %s", quoteIdentifierString(tableName));
105
+ executeUpdate(stmt, sql);
106
+ }
107
+
75
108
  public void createTableIfNotExists(String tableName, JdbcSchema schema) throws SQLException
76
109
  {
77
110
  Statement stmt = connection.createStatement();
@@ -92,6 +125,38 @@ public class JdbcOutputConnection
92
125
 
93
126
  sb.append("CREATE TABLE IF NOT EXISTS ");
94
127
  quoteIdentifierString(sb, name);
128
+ sb.append(buildColumnsOfCreateTableSql(schema));
129
+ return sb.toString();
130
+ }
131
+
132
+ public void createTable(String tableName, JdbcSchema schema) throws SQLException
133
+ {
134
+ Statement stmt = connection.createStatement();
135
+ try {
136
+ String sql = buildCreateTableSql(tableName, schema);
137
+ executeUpdate(stmt, sql);
138
+ commitIfNecessary(connection);
139
+ } catch (SQLException ex) {
140
+ throw safeRollback(connection, ex);
141
+ } finally {
142
+ stmt.close();
143
+ }
144
+ }
145
+
146
+ protected String buildCreateTableSql(String name, JdbcSchema schema)
147
+ {
148
+ StringBuilder sb = new StringBuilder();
149
+
150
+ sb.append("CREATE TABLE ");
151
+ quoteIdentifierString(sb, name);
152
+ sb.append(buildColumnsOfCreateTableSql(schema));
153
+ return sb.toString();
154
+ }
155
+
156
+ private String buildColumnsOfCreateTableSql(JdbcSchema schema)
157
+ {
158
+ StringBuilder sb = new StringBuilder();
159
+
95
160
  sb.append(" (");
96
161
  boolean first = true;
97
162
  for (JdbcColumn c : schema.getColumns()) {
@@ -287,23 +352,15 @@ public class JdbcOutputConnection
287
352
  {
288
353
  Statement stmt = connection.createStatement();
289
354
  try {
290
- {
291
- StringBuilder sb = new StringBuilder();
292
- sb.append("DROP TABLE IF EXISTS ");
293
- quoteIdentifierString(sb, toTable);
294
- String sql = sb.toString();
295
- executeUpdate(stmt, sql);
296
- }
297
-
298
- {
299
- StringBuilder sb = new StringBuilder();
300
- sb.append("ALTER TABLE ");
301
- quoteIdentifierString(sb, fromTable);
302
- sb.append(" RENAME TO ");
303
- quoteIdentifierString(sb, toTable);
304
- String sql = sb.toString();
305
- executeUpdate(stmt, sql);
306
- }
355
+ dropTableIfExists(stmt, toTable);
356
+
357
+ StringBuilder sb = new StringBuilder();
358
+ sb.append("ALTER TABLE ");
359
+ quoteIdentifierString(sb, fromTable);
360
+ sb.append(" RENAME TO ");
361
+ quoteIdentifierString(sb, toTable);
362
+ String sql = sb.toString();
363
+ executeUpdate(stmt, sql);
307
364
 
308
365
  commitIfNecessary(connection);
309
366
  } catch (SQLException ex) {
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.2.0
4
+ version: 0.2.1
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-15 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Inserts or updates records to a table.
14
14
  email:
@@ -39,7 +39,7 @@ files:
39
39
  - src/main/java/org/embulk/output/jdbc/setter/SqlTimestampColumnSetter.java
40
40
  - src/main/java/org/embulk/output/jdbc/setter/StringColumnSetter.java
41
41
  - src/test/java/org/embulk/output/TestJdbcOutputPlugin.java
42
- - classpath/embulk-output-jdbc-0.2.0.jar
42
+ - classpath/embulk-output-jdbc-0.2.1.jar
43
43
  homepage: https://github.com/embulk/embulk-output-jdbc
44
44
  licenses:
45
45
  - Apache 2.0
Binary file