-
Notifications
You must be signed in to change notification settings - Fork 84
/
DB2OutputConnection.java
96 lines (83 loc) · 3.08 KB
/
DB2OutputConnection.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
package org.embulk.output.db2;
import java.sql.Connection;
import java.sql.SQLException;
import org.embulk.output.jdbc.JdbcColumn;
import org.embulk.output.jdbc.JdbcOutputConnection;
import org.embulk.output.jdbc.TableIdentifier;
import static java.util.Locale.ENGLISH;
public class DB2OutputConnection
extends JdbcOutputConnection
{
public DB2OutputConnection(Connection connection, String schemaName)
throws SQLException
{
super(connection, schemaName);
}
@Override
protected String buildRenameTableSql(TableIdentifier fromTable, TableIdentifier toTable)
{
StringBuilder sb = new StringBuilder();
sb.append("RENAME TABLE ");
sb.append(quoteTableIdentifier(fromTable));
sb.append(" TO ");
sb.append(quoteTableIdentifier(toTable));
return sb.toString();
}
@Override
protected void setSearchPath(String schema) throws SQLException
{
// NOP
}
@Override
protected boolean supportsTableIfExistsClause()
{
return false;
}
@Override
protected String buildColumnTypeName(JdbcColumn c)
{
switch(c.getSimpleTypeName()) {
case "BOOLEAN":
return "SMALLINT";
// NCHAR/NVARCHAR/NCLOB are synonyms for CHAR/VARCHAR/CLOB/GRAPHIC/VARGRAPHIC
case "CHAR":
case "VARCHAR":
case "CLOB":
String charUnit;
if (c.getSizeTypeParameter() == c.getDataLength()) {
charUnit = "OCTETS";
} else if (c.getSizeTypeParameter() * 2 == c.getDataLength()) {
charUnit = "CODEUNITS16";
} else if (c.getSizeTypeParameter() * 4 == c.getDataLength()) {
charUnit = "CODEUNITS32";
} else {
throw new IllegalArgumentException(String.format(ENGLISH, "Column %s has unexpected size %d and length %d.",
c.getName(), c.getSizeTypeParameter(), c.getDataLength()));
}
return String.format(ENGLISH, "%s(%d %s)", c.getSimpleTypeName(), c.getSizeTypeParameter(), charUnit);
case "GRAPHIC":
case "VARGRAPHIC":
String graphicUnit;
if (c.getSizeTypeParameter() == c.getDataLength()) {
graphicUnit = "CODEUNITS16";
} else if (c.getSizeTypeParameter() * 2 == c.getDataLength()) {
graphicUnit = "CODEUNITS32";
} else {
throw new IllegalArgumentException(String.format(ENGLISH, "Column %s has unexpected size %d and length %d.",
c.getName(), c.getSizeTypeParameter(), c.getDataLength()));
}
return String.format(ENGLISH, "%s(%d %s)", c.getSimpleTypeName(), c.getSizeTypeParameter(), graphicUnit);
default:
return super.buildColumnTypeName(c);
}
}
@Override
public void close() throws SQLException
{
if (!connection.isClosed()) {
// DB2 JDBC Driver requires explicit commit/rollback before closing connection.
connection.rollback();
}
super.close();
}
}