/
PostgreSQLOutputConnection.java
157 lines (145 loc) · 5.22 KB
/
PostgreSQLOutputConnection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package org.embulk.output.postgresql;
import java.util.List;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import org.postgresql.copy.CopyManager;
import org.postgresql.core.BaseConnection;
import org.embulk.spi.Exec;
import org.embulk.output.jdbc.JdbcOutputConnection;
import org.embulk.output.jdbc.JdbcColumn;
import org.embulk.output.jdbc.JdbcSchema;
public class PostgreSQLOutputConnection
extends JdbcOutputConnection
{
public PostgreSQLOutputConnection(Connection connection, String schemaName, boolean autoCommit)
throws SQLException
{
super(connection, schemaName);
connection.setAutoCommit(autoCommit);
}
public String buildCopySql(String toTable, JdbcSchema toTableSchema)
{
StringBuilder sb = new StringBuilder();
sb.append("COPY ");
quoteIdentifierString(sb, toTable);
sb.append(" (");
for (int i=0; i < toTableSchema.getCount(); i++) {
if (i != 0) { sb.append(", "); }
quoteIdentifierString(sb, toTableSchema.getColumnName(i));
}
sb.append(") ");
sb.append("FROM STDIN");
return sb.toString();
}
public CopyManager newCopyManager() throws SQLException
{
return new CopyManager((BaseConnection) connection);
}
@Override
protected String buildCollectMergeSql(List<String> fromTables, JdbcSchema schema, String toTable, List<String> mergeKeys) throws SQLException
{
StringBuilder sb = new StringBuilder();
sb.append("WITH updated AS (");
sb.append("UPDATE ");
quoteIdentifierString(sb, toTable);
sb.append(" SET ");
for (int i=0; i < schema.getCount(); i++) {
if (i != 0) { sb.append(", "); }
quoteIdentifierString(sb, schema.getColumnName(i));
sb.append(" = S.");
quoteIdentifierString(sb, schema.getColumnName(i));
}
sb.append(" FROM (");
for (int i=0; i < fromTables.size(); i++) {
if (i != 0) { sb.append(" UNION ALL "); }
sb.append("SELECT ");
for(int j=0; j < schema.getCount(); j++) {
if (j != 0) { sb.append(", "); }
quoteIdentifierString(sb, schema.getColumnName(j));
}
sb.append(" FROM ");
quoteIdentifierString(sb, fromTables.get(i));
}
sb.append(") S");
sb.append(" WHERE ");
for (int i=0; i < mergeKeys.size(); i++) {
if (i != 0) { sb.append(" AND "); }
quoteIdentifierString(sb, toTable);
sb.append(".");
quoteIdentifierString(sb, mergeKeys.get(i));
sb.append(" = ");
sb.append("S.");
quoteIdentifierString(sb, mergeKeys.get(i));
}
sb.append(" RETURNING ");
for (int i=0; i < mergeKeys.size(); i++) {
if (i != 0) { sb.append(", "); }
sb.append("S.");
quoteIdentifierString(sb, mergeKeys.get(i));
}
sb.append(") ");
sb.append("INSERT INTO ");
quoteIdentifierString(sb, toTable);
sb.append(" (");
for (int i=0; i < schema.getCount(); i++) {
if (i != 0) { sb.append(", "); }
quoteIdentifierString(sb, schema.getColumnName(i));
}
sb.append(") ");
sb.append("SELECT DISTINCT ON (");
for (int i=0; i < mergeKeys.size(); i++) {
if (i != 0) { sb.append(", "); }
quoteIdentifierString(sb, mergeKeys.get(i));
}
sb.append(") * FROM (");
for (int i=0; i < fromTables.size(); i++) {
if (i != 0) { sb.append(" UNION ALL "); }
sb.append("SELECT ");
for(int j=0; j < schema.getCount(); j++) {
if (j != 0) { sb.append(", "); }
quoteIdentifierString(sb, schema.getColumnName(j));
}
sb.append(" FROM ");
quoteIdentifierString(sb, fromTables.get(i));
}
sb.append(") S ");
sb.append("WHERE NOT EXISTS (");
sb.append("SELECT 1 FROM updated WHERE ");
for (int i=0; i < mergeKeys.size(); i++) {
if (i != 0) { sb.append(" AND "); }
sb.append("S.");
quoteIdentifierString(sb, mergeKeys.get(i));
sb.append(" = ");
sb.append("updated.");
quoteIdentifierString(sb, mergeKeys.get(i));
}
sb.append(") ");
return sb.toString();
}
protected void collectReplaceView(List<String> fromTables, JdbcSchema schema, String toTable) throws SQLException
{
Statement stmt = connection.createStatement();
try {
String sql = buildCollectInsertSql(fromTables, schema, toTable);
executeUpdate(stmt, sql);
commitIfNecessary(connection);
} catch (SQLException ex) {
throw safeRollback(connection, ex);
} finally {
stmt.close();
}
}
@Override
protected String buildColumnTypeName(JdbcColumn c)
{
switch(c.getSimpleTypeName()) {
case "CLOB":
return "TEXT";
case "BLOB":
return "BYTEA";
default:
return super.buildColumnTypeName(c);
}
}
}