embulk-output-postgresql 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 45b9b9f967c210d54dc95836f1bedba07e37f879
4
- data.tar.gz: 4228760b850eb3a3b9bca1af22d223f96967b628
3
+ metadata.gz: a9e12ceb372cd836d2bda55df13f1bd78ca1400a
4
+ data.tar.gz: 95cfefc30482d521bd3bcef520e494b7e5d82d39
5
5
  SHA512:
6
- metadata.gz: ce4a7b37a25702f02dd927c58350d06282c3e13a0442093bbdf2b96bf024da9f6deea57fef9a35b1d35a9d27aa9e2867ae78c939a5370652db97bb67e9a63c06
7
- data.tar.gz: 72fa53a7fd9e18954ba5da5e3d01fb3f266e395da8df0f274a7a9a49c8ff29ebc5b51631666b59be39f12a632a57856c2afa4bc566080a996324f5f58617aba9
6
+ metadata.gz: 29dde2105f5caf3e90cf6b423513403c82f87d031cf93df01320ed4ca0d4bec04edbcdbf6441bf9b95e9f5e274d1b8b3d32bd393f05aca02622d04a8b92222ef
7
+ data.tar.gz: 0a6ef290f497fe919486dac90dfc87e0eac002914deca3dd02f0f58ec10a7d10ff7e998d81f2d09794c06363abd100137f28e2be2202b6c5fffb4408be049340
@@ -1,8 +1,12 @@
1
1
  package org.embulk.output;
2
2
 
3
+ import java.util.List;
3
4
  import java.util.Properties;
4
5
  import java.io.IOException;
5
6
  import java.sql.SQLException;
7
+
8
+ import org.embulk.output.jdbc.setter.ColumnSetter;
9
+ import org.embulk.output.postgresql.PostgresqlBatchUpsert;
6
10
  import org.embulk.spi.Exec;
7
11
  import org.embulk.config.Config;
8
12
  import org.embulk.config.ConfigDefault;
@@ -10,6 +14,7 @@ import org.embulk.output.jdbc.AbstractJdbcOutputPlugin;
10
14
  import org.embulk.output.jdbc.BatchInsert;
11
15
  import org.embulk.output.postgresql.PostgreSQLOutputConnector;
12
16
  import org.embulk.output.postgresql.PostgreSQLCopyBatchInsert;
17
+ import org.embulk.spi.PageReader;
13
18
 
14
19
  public class PostgreSQLOutputPlugin
15
20
  extends AbstractJdbcOutputPlugin
@@ -85,9 +90,49 @@ public class PostgreSQLOutputPlugin
85
90
  return new PostgreSQLOutputConnector(url, props, t.getSchema());
86
91
  }
87
92
 
93
+ @Override
94
+ protected PluginPageOutput newPluginPageOutput(PageReader reader,
95
+ BatchInsert batch, List<ColumnSetter> columnSetters,
96
+ int batchSize)
97
+ {
98
+ return new PostgresPluginPageOutput(reader, batch, columnSetters, batchSize);
99
+ }
100
+
101
+ public static class PostgresPluginPageOutput extends PluginPageOutput
102
+ {
103
+
104
+ public PostgresPluginPageOutput(PageReader pageReader, BatchInsert batch, List<ColumnSetter> columnSetters, int batchSize) {
105
+ super(pageReader, batch, columnSetters, batchSize);
106
+ }
107
+
108
+ @Override
109
+ protected void handleColumnsSetters()
110
+ {
111
+ int size = columnSetters.size();
112
+ for (int i=0; i < size; i++) {
113
+ ColumnSetter columnSetter = columnSetters.get(i);
114
+ if (!columnSetter.getColumn().isPrimaryKey()) {
115
+ columns.get(i).visit(columnSetter);
116
+ }
117
+ }
118
+ for (int i=0; i < size; i++) {
119
+ ColumnSetter columnSetter = columnSetters.get(i);
120
+ if (columnSetter.getColumn().isPrimaryKey()) {
121
+ columns.get(i).visit(columnSetter);
122
+ }
123
+ }
124
+ for (int i=0; i < size; i++) {
125
+ columns.get(i).visit(columnSetters.get(i));
126
+ }
127
+ }
128
+
129
+ }
130
+
88
131
  @Override
89
132
  protected BatchInsert newBatchInsert(PluginTask task) throws IOException, SQLException
90
133
  {
91
- return new PostgreSQLCopyBatchInsert(getConnector(task, true));
134
+ PostgreSQLOutputConnector connector = getConnector(task, true);
135
+ return task.getMode().isMerge() ? new PostgresqlBatchUpsert(connector) :
136
+ new PostgreSQLCopyBatchInsert(getConnector(task, true));
92
137
  }
93
138
  }
@@ -54,4 +54,55 @@ public class PostgreSQLOutputConnection
54
54
  return typeName;
55
55
  }
56
56
  }
57
+
58
+ @Override
59
+ protected String buildPrepareUpsertSql(String toTable, JdbcSchema toTableSchema) throws SQLException
60
+ {
61
+ StringBuilder sb = new StringBuilder();
62
+ int size = toTableSchema.getCount();
63
+ String table = quoteIdentifierString(toTable);
64
+ int idx = 0;
65
+
66
+ sb.append("WITH upsert AS (UPDATE ").append(table).append(" SET ");
67
+
68
+ for (int i=0; i < size; i++) {
69
+ JdbcColumn c = toTableSchema.getColumn(i);
70
+ if (!c.isPrimaryKey()) {
71
+ if(idx != 0) { sb.append(", "); }
72
+ idx++;
73
+ quoteIdentifierString(sb, toTableSchema.getColumnName(i));
74
+ sb.append("=?");
75
+ }
76
+ }
77
+
78
+ sb.append(" WHERE ");
79
+ idx = 0;
80
+ for(int i=0; i < size; i++) {
81
+ JdbcColumn c = toTableSchema.getColumn(i);
82
+ if (c.isPrimaryKey()) {
83
+ if(idx != 0) { sb.append(" AND "); }
84
+ idx++;
85
+ quoteIdentifierString(sb, toTableSchema.getColumnName(i));
86
+ sb.append("=?");
87
+ }
88
+ }
89
+ sb.append(" RETURNING true as result)");
90
+
91
+ sb.append(" INSERT INTO ").append(table).append(" (");
92
+ for (int i=0; i < size; i++) {
93
+ if(i != 0) { sb.append(", "); }
94
+ quoteIdentifierString(sb, toTableSchema.getColumnName(i));
95
+ }
96
+ sb.append(")");
97
+
98
+ sb.append(" SELECT ");
99
+ for (int i=0; i < size; i++) {
100
+ if(i != 0) { sb.append(", "); }
101
+ sb.append("?");
102
+ }
103
+ sb.append(" WHERE (SELECT result FROM upsert) is null");
104
+
105
+ return sb.toString();
106
+ }
107
+
57
108
  }
@@ -0,0 +1,24 @@
1
+ package org.embulk.output.postgresql;
2
+
3
+ import org.embulk.output.jdbc.JdbcOutputConnection;
4
+ import org.embulk.output.jdbc.JdbcOutputConnector;
5
+ import org.embulk.output.jdbc.JdbcSchema;
6
+ import org.embulk.output.jdbc.StandardBatchInsert;
7
+
8
+ import java.io.IOException;
9
+ import java.sql.PreparedStatement;
10
+ import java.sql.SQLException;
11
+
12
+ public class PostgresqlBatchUpsert extends StandardBatchInsert {
13
+
14
+ public PostgresqlBatchUpsert(JdbcOutputConnector connector) throws IOException, SQLException {
15
+ super(connector);
16
+ }
17
+
18
+ protected PreparedStatement newPreparedStatement(JdbcOutputConnection connection,
19
+ String loadTable, JdbcSchema insertSchema) throws SQLException
20
+ {
21
+ return connection.prepareUpsertSql(loadTable, insertSchema);
22
+ }
23
+
24
+ }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: embulk-output-postgresql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
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-04-07 00:00:00.000000000 Z
11
+ date: 2015-04-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Inserts or updates records to a table.
14
14
  email:
@@ -25,12 +25,13 @@ files:
25
25
  - src/main/java/org/embulk/output/postgresql/PostgreSQLCopyBatchInsert.java
26
26
  - src/main/java/org/embulk/output/postgresql/PostgreSQLOutputConnection.java
27
27
  - src/main/java/org/embulk/output/postgresql/PostgreSQLOutputConnector.java
28
- - classpath/embulk-output-jdbc-0.2.2.jar
29
- - classpath/embulk-output-postgresql-0.2.2.jar
30
- - classpath/jna-4.1.0.jar
31
- - classpath/jna-platform-4.1.0.jar
28
+ - src/main/java/org/embulk/output/postgresql/PostgresqlBatchUpsert.java
29
+ - classpath/embulk-output-jdbc-0.2.3.jar
32
30
  - classpath/postgresql-9.4-1200-jdbc41.jar
31
+ - classpath/embulk-output-postgresql-0.2.3.jar
33
32
  - classpath/slf4j-simple-1.7.7.jar
33
+ - classpath/jna-platform-4.1.0.jar
34
+ - classpath/jna-4.1.0.jar
34
35
  - classpath/waffle-jna-1.7.jar
35
36
  homepage: https://github.com/embulk/embulk-output-jdbc
36
37
  licenses: