forked from embulk/embulk-output-jdbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySQLOutputConnection.java
138 lines (125 loc) · 4.9 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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;
import org.embulk.output.jdbc.TableIdentifier;
public class MySQLOutputConnection
extends JdbcOutputConnection
{
public MySQLOutputConnection(Connection connection, boolean autoCommit)
throws SQLException
{
super(connection, null);
connection.setAutoCommit(autoCommit);
}
@Override
protected String buildPreparedMergeSql(TableIdentifier toTable, JdbcSchema toTableSchema, MergeConfig mergeConfig) throws SQLException
{
StringBuilder sb = new StringBuilder();
sb.append("INSERT INTO ");
quoteTableIdentifier(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<TableIdentifier> fromTables, JdbcSchema schema, TableIdentifier toTable, MergeConfig mergeConfig) throws SQLException
{
StringBuilder sb = new StringBuilder();
sb.append("INSERT INTO ");
quoteTableIdentifier(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 ");
quoteTableIdentifier(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();
}
//
//
// The MySQL Connector/J 5.1.35 introduce new option `Current MySQL Connect`.
// It has incompatibility behavior current version and 5.1.35.
//
// This method announces users about this change before the update driver version.
//
@Override
public void showDriverVersion() throws SQLException {
super.showDriverVersion();
logger.warn("This plugin will update MySQL Connector/J version in the near future release.");
logger.warn("It has some incompatibility changes.");
logger.warn("For example, the 5.1.35 introduced `noTimezoneConversionForDateType` and `cacheDefaultTimezone` options.");
logger.warn("Please read a document and make sure configuration carefully before updating the plugin.");
}
}