forked from embulk/embulk-output-jdbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySQLOutputConnection.java
121 lines (109 loc) · 4.07 KB
/
MySQLOutputConnection.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
package org.embulk.output.mysql;
import java.util.List;
import java.sql.Connection;
import java.sql.SQLException;
import org.embulk.output.MySQLTimeZoneComparison;
import org.embulk.output.jdbc.JdbcColumn;
import org.embulk.output.jdbc.JdbcSchema;
import org.embulk.output.jdbc.JdbcOutputConnection;
import org.embulk.output.jdbc.MergeConfig;
public class MySQLOutputConnection
extends JdbcOutputConnection
{
public MySQLOutputConnection(Connection connection, boolean autoCommit)
throws SQLException
{
super(connection, null);
connection.setAutoCommit(autoCommit);
}
@Override
protected String buildPreparedMergeSql(String toTable, JdbcSchema toTableSchema, MergeConfig mergeConfig) throws SQLException
{
StringBuilder sb = new StringBuilder();
sb.append("INSERT INTO ");
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(") VALUES (");
for(int i = 0; i < toTableSchema.getCount(); i++) {
if(i != 0) { sb.append(", "); }
sb.append("?");
}
sb.append(")");
sb.append(" ON DUPLICATE KEY UPDATE ");
if (mergeConfig.getMergeRule().isPresent()) {
List<String> rule = mergeConfig.getMergeRule().get();
for (int i = 0; i < rule.size(); i++) {
if (i != 0) {
sb.append(", ");
}
sb.append(rule.get(i));
}
} else {
for (int i = 0; i < toTableSchema.getCount(); i++) {
if(i != 0) { sb.append(", "); }
String columnName = quoteIdentifierString(toTableSchema.getColumnName(i));
sb.append(columnName).append(" = VALUES(").append(columnName).append(")");
}
}
return sb.toString();
}
@Override
protected String buildCollectMergeSql(List<String> fromTables, JdbcSchema schema, String toTable, MergeConfig mergeConfig) throws SQLException
{
StringBuilder sb = new StringBuilder();
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(") ");
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(" ON DUPLICATE KEY UPDATE ");
if (mergeConfig.getMergeRule().isPresent()) {
List<String> rule = mergeConfig.getMergeRule().get();
for (int i = 0; i < rule.size(); i++) {
if (i != 0) {
sb.append(", ");
}
sb.append(rule.get(i));
}
} else {
for (int i = 0; i < schema.getCount(); i++) {
if(i != 0) { sb.append(", "); }
String columnName = quoteIdentifierString(schema.getColumnName(i));
sb.append(columnName).append(" = VALUES(").append(columnName).append(")");
}
}
return sb.toString();
}
@Override
protected String buildColumnTypeName(JdbcColumn c)
{
switch(c.getSimpleTypeName()) {
case "CLOB":
return "TEXT";
default:
return super.buildColumnTypeName(c);
}
}
public void compareTimeZone() throws SQLException
{
MySQLTimeZoneComparison timeZoneComparison = new MySQLTimeZoneComparison(connection);
timeZoneComparison.compareTimeZone();
}
}