embulk-output-jdbc 0.2.3 → 0.2.4

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.
@@ -1,54 +1,54 @@
1
- package org.embulk.output.jdbc;
2
-
3
- import java.math.BigDecimal;
4
- import java.io.IOException;
5
- import java.sql.SQLException;
6
- import java.sql.PreparedStatement;
7
- import java.sql.Date;
8
- import java.sql.Time;
9
- import java.sql.Timestamp;
10
-
11
- public interface BatchInsert
12
- {
13
- public void prepare(String loadTable, JdbcSchema insertSchema) throws SQLException;
14
-
15
- public int getBatchWeight();
16
-
17
- public void add() throws IOException, SQLException;
18
-
19
- public void close() throws IOException, SQLException;
20
-
21
- public void flush() throws IOException, SQLException;
22
-
23
- public void finish() throws IOException, SQLException;
24
-
25
- public void setNull(int sqlType) throws IOException, SQLException;
26
-
27
- public void setBoolean(boolean v) throws IOException, SQLException;
28
-
29
- public void setByte(byte v) throws IOException, SQLException;
30
-
31
- public void setShort(short v) throws IOException, SQLException;
32
-
33
- public void setInt(int v) throws IOException, SQLException;
34
-
35
- public void setLong(long v) throws IOException, SQLException;
36
-
37
- public void setFloat(float v) throws IOException, SQLException;
38
-
39
- public void setDouble(double v) throws IOException, SQLException;
40
-
41
- public void setBigDecimal(BigDecimal v) throws IOException, SQLException;
42
-
43
- public void setString(String v) throws IOException, SQLException;
44
-
45
- public void setNString(String v) throws IOException, SQLException;
46
-
47
- public void setBytes(byte[] v) throws IOException, SQLException;
48
-
49
- public void setSqlDate(Date v, int sqlType) throws IOException, SQLException;
50
-
51
- public void setSqlTime(Time v, int sqlType) throws IOException, SQLException;
52
-
53
- public void setSqlTimestamp(Timestamp v, int sqlType) throws IOException, SQLException;
54
- }
1
+ package org.embulk.output.jdbc;
2
+
3
+ import java.math.BigDecimal;
4
+ import java.io.IOException;
5
+ import java.sql.SQLException;
6
+ import java.sql.PreparedStatement;
7
+ import java.sql.Date;
8
+ import java.sql.Time;
9
+ import java.sql.Timestamp;
10
+
11
+ public interface BatchInsert
12
+ {
13
+ public void prepare(String loadTable, JdbcSchema insertSchema) throws SQLException;
14
+
15
+ public int getBatchWeight();
16
+
17
+ public void add() throws IOException, SQLException;
18
+
19
+ public void close() throws IOException, SQLException;
20
+
21
+ public void flush() throws IOException, SQLException;
22
+
23
+ public void finish() throws IOException, SQLException;
24
+
25
+ public void setNull(int sqlType) throws IOException, SQLException;
26
+
27
+ public void setBoolean(boolean v) throws IOException, SQLException;
28
+
29
+ public void setByte(byte v) throws IOException, SQLException;
30
+
31
+ public void setShort(short v) throws IOException, SQLException;
32
+
33
+ public void setInt(int v) throws IOException, SQLException;
34
+
35
+ public void setLong(long v) throws IOException, SQLException;
36
+
37
+ public void setFloat(float v) throws IOException, SQLException;
38
+
39
+ public void setDouble(double v) throws IOException, SQLException;
40
+
41
+ public void setBigDecimal(BigDecimal v) throws IOException, SQLException;
42
+
43
+ public void setString(String v) throws IOException, SQLException;
44
+
45
+ public void setNString(String v) throws IOException, SQLException;
46
+
47
+ public void setBytes(byte[] v) throws IOException, SQLException;
48
+
49
+ public void setSqlDate(Date v, int sqlType) throws IOException, SQLException;
50
+
51
+ public void setSqlTime(Time v, int sqlType) throws IOException, SQLException;
52
+
53
+ public void setSqlTimestamp(Timestamp v, int sqlType) throws IOException, SQLException;
54
+ }
@@ -1,8 +1,8 @@
1
- package org.embulk.output.jdbc;
2
-
3
- import java.sql.SQLException;
4
-
5
- public interface JdbcOutputConnector
6
- {
7
- public JdbcOutputConnection connect(boolean autoCommit) throws SQLException;
8
- }
1
+ package org.embulk.output.jdbc;
2
+
3
+ import java.sql.SQLException;
4
+
5
+ public interface JdbcOutputConnector
6
+ {
7
+ public JdbcOutputConnection connect(boolean autoCommit) throws SQLException;
8
+ }
@@ -1,37 +1,37 @@
1
- package org.embulk.output.jdbc;
2
-
3
- import java.util.List;
4
- import com.fasterxml.jackson.annotation.JsonCreator;
5
- import com.fasterxml.jackson.annotation.JsonValue;
6
-
7
- public class JdbcSchema
8
- {
9
- private List<JdbcColumn> columns;
10
-
11
- @JsonCreator
12
- public JdbcSchema(List<JdbcColumn> columns)
13
- {
14
- this.columns = columns;
15
- }
16
-
17
- @JsonValue
18
- public List<JdbcColumn> getColumns()
19
- {
20
- return columns;
21
- }
22
-
23
- public int getCount()
24
- {
25
- return columns.size();
26
- }
27
-
28
- public JdbcColumn getColumn(int i)
29
- {
30
- return columns.get(i);
31
- }
32
-
33
- public String getColumnName(int i)
34
- {
35
- return columns.get(i).getName();
36
- }
37
- }
1
+ package org.embulk.output.jdbc;
2
+
3
+ import java.util.List;
4
+ import com.fasterxml.jackson.annotation.JsonCreator;
5
+ import com.fasterxml.jackson.annotation.JsonValue;
6
+
7
+ public class JdbcSchema
8
+ {
9
+ private List<JdbcColumn> columns;
10
+
11
+ @JsonCreator
12
+ public JdbcSchema(List<JdbcColumn> columns)
13
+ {
14
+ this.columns = columns;
15
+ }
16
+
17
+ @JsonValue
18
+ public List<JdbcColumn> getColumns()
19
+ {
20
+ return columns;
21
+ }
22
+
23
+ public int getCount()
24
+ {
25
+ return columns.size();
26
+ }
27
+
28
+ public JdbcColumn getColumn(int i)
29
+ {
30
+ return columns.get(i);
31
+ }
32
+
33
+ public String getColumnName(int i)
34
+ {
35
+ return columns.get(i).getName();
36
+ }
37
+ }
@@ -1,155 +1,155 @@
1
- package org.embulk.output.jdbc;
2
-
3
- import java.sql.Connection;
4
- import java.sql.PreparedStatement;
5
- import java.sql.SQLException;
6
- import java.sql.Statement;
7
- import java.util.regex.Matcher;
8
- import java.util.regex.Pattern;
9
- import org.slf4j.Logger;
10
- import org.embulk.spi.Exec;
11
-
12
- public class JdbcUtils
13
- {
14
- public final Logger logger = Exec.getLogger(JdbcUtils.class.getName());
15
-
16
- private static String[] SEARCH_STRING_SPECIAL_CHARS = new String[] { "_", "%" };
17
-
18
- public static String escapeSearchString(String searchString, String escapeString)
19
- {
20
- if (searchString != null && escapeString != null) {
21
- // First of all, escape escapeString '\' itself: '\' -> '\\'
22
- searchString = searchString.replaceAll(Pattern.quote(escapeString),
23
- Matcher.quoteReplacement(escapeString + escapeString));
24
- for (String specialChar : SEARCH_STRING_SPECIAL_CHARS) {
25
- if (specialChar.equals(escapeString)) {
26
- throw new IllegalArgumentException("Special char " + specialChar + " cannot be an escape char");
27
- }
28
- searchString = searchString.replaceAll(Pattern.quote(specialChar),
29
- Matcher.quoteReplacement(escapeString + specialChar));
30
- }
31
- }
32
- return searchString;
33
- }
34
-
35
- private Class<?> connectionClass;
36
-
37
- // Connection.isValid() is available from Java 1.6 + JDBC4.
38
- //private boolean hasIsValid;
39
-
40
- // Connection.setNetworkTimeout() is available from Java 1.7 + JDBC4.
41
- //private boolean hasSetNetworkTimeout;
42
- //private Method setNetworkTimeoutMethod;
43
-
44
- public JdbcUtils(Class<?> connectionClass)
45
- {
46
- this.connectionClass = connectionClass;
47
-
48
- //configureSetNetworkTimeout();
49
- }
50
-
51
- public int executeUpdateWithSqlLogging(Statement stmt, String sql) throws SQLException
52
- {
53
- logger.info("SQL: " + sql);
54
- long startTime = System.currentTimeMillis();
55
- int count = stmt.executeUpdate(sql);
56
- double seconds = (System.currentTimeMillis() - startTime) / 1000.0;
57
- if (count == 0) {
58
- logger.info(String.format("> %.2f seconds", seconds));
59
- } else {
60
- logger.info(String.format("> %.2f seconds (%,d rows)", seconds, count));
61
- }
62
- return count;
63
- }
64
-
65
- //private void configureSetNetworkTimeout() {
66
- // try {
67
- // Method m = connectionClass.getMethod("setNetworkTimeout", Executor.class, int.class);
68
- // if (isCallableMethod(m)) {
69
- // setNetworkTimeoutMethod = m;
70
- // hasSetNetworkTimeout = true;
71
- // }
72
- // } catch (SecurityException ex) {
73
- // } catch (NoSuchMethodException ex) {
74
- // }
75
- //}
76
-
77
- //private boolean isCallableMethod(Method m) {
78
- // int modifiers = m.getModifiers();
79
- // if (Modifier.isAbstract(modifiers)) {
80
- // // Method.invoke throws java.lang.AbstractMethodError if it's an
81
- // // abstract method. Applications can't catch AbstractMethodError.
82
- // return false;
83
- // }
84
- // if (!Modifier.isPublic(modifiers)) {
85
- // // we can only call public methods
86
- // return false;
87
- // }
88
- // return true;
89
- //}
90
-
91
- // PostgreSQL JDBC driver implements isValid() method. But the
92
- // implementation throws following exception:
93
- // "java.io.IOException: Method org.postgresql.jdbc4.Jdbc4Connection.isValid(int) is not yet implemented."
94
- //
95
- // So, checking mechanism doesn't work at all.
96
- // Thus here just runs "SELECT 1" to check connectivity.
97
- //
98
- //public boolean isValidConnection(Connection connection, int timeout) throws SQLException
99
- //{
100
- // Statement stmt = connection.createStatement();
101
- // try {
102
- // stmt.executeQuery("SELECT 1").close();
103
- // return true;
104
- // } catch (SQLException ex) {
105
- // return false;
106
- // } finally {
107
- // stmt.close();
108
- // }
109
- //}
110
-
111
- //public void setNetworkTimeout(Connection connection,
112
- // Executor executor, int milliseconds) throws SQLException {
113
- // Throwable exception = null;
114
- // if (hasSetNetworkTimeout) {
115
- // try {
116
- // setNetworkTimeoutMethod.invoke(connection, executor, milliseconds);
117
- // return;
118
- //
119
- // } catch (IllegalArgumentException ex) {
120
- // // ignore error
121
- // LOG.warn("Connection.setNetworkTimeout failed due to IllegalArgumentException.");
122
- // exception = ex;
123
- //
124
- // } catch (IllegalAccessException ex) {
125
- // // ignore error
126
- // LOG.warn("Connection.setNetworkTimeout failed due to IllegalAccessException.");
127
- // exception = ex;
128
- //
129
- // } catch (InvocationTargetException ex) {
130
- // //Throwable cause = ex.getTargetException();
131
- // //if (cause instanceof SQLException) {
132
- // // throw (SQLException) cause;
133
- // //} else if (cause instanceof RuntimeException) {
134
- // // throw (RuntimeException) cause;
135
- // //} else if (cause instanceof Error) {
136
- // // throw (Error) cause;
137
- // //} else {
138
- // // throw new SQLException(cause);
139
- // //}
140
- // exception = ex.getTargetException();
141
- // // It's safe to ignore exceptions.
142
- // }
143
- //
144
- // hasSetNetworkTimeout = false;
145
- // }
146
- //
147
- // if (exception != null) {
148
- // LOG.warn("Connection.setNetworkTimeout is not available: "+exception);
149
- // } else {
150
- // LOG.warn("Connection.setNetworkTimeout is not available.");
151
- // }
152
- // // TODO any substitute implementations?
153
- //}
154
- }
155
-
1
+ package org.embulk.output.jdbc;
2
+
3
+ import java.sql.Connection;
4
+ import java.sql.PreparedStatement;
5
+ import java.sql.SQLException;
6
+ import java.sql.Statement;
7
+ import java.util.regex.Matcher;
8
+ import java.util.regex.Pattern;
9
+ import org.slf4j.Logger;
10
+ import org.embulk.spi.Exec;
11
+
12
+ public class JdbcUtils
13
+ {
14
+ public final Logger logger = Exec.getLogger(JdbcUtils.class.getName());
15
+
16
+ private static String[] SEARCH_STRING_SPECIAL_CHARS = new String[] { "_", "%" };
17
+
18
+ public static String escapeSearchString(String searchString, String escapeString)
19
+ {
20
+ if (searchString != null && escapeString != null) {
21
+ // First of all, escape escapeString '\' itself: '\' -> '\\'
22
+ searchString = searchString.replaceAll(Pattern.quote(escapeString),
23
+ Matcher.quoteReplacement(escapeString + escapeString));
24
+ for (String specialChar : SEARCH_STRING_SPECIAL_CHARS) {
25
+ if (specialChar.equals(escapeString)) {
26
+ throw new IllegalArgumentException("Special char " + specialChar + " cannot be an escape char");
27
+ }
28
+ searchString = searchString.replaceAll(Pattern.quote(specialChar),
29
+ Matcher.quoteReplacement(escapeString + specialChar));
30
+ }
31
+ }
32
+ return searchString;
33
+ }
34
+
35
+ private Class<?> connectionClass;
36
+
37
+ // Connection.isValid() is available from Java 1.6 + JDBC4.
38
+ //private boolean hasIsValid;
39
+
40
+ // Connection.setNetworkTimeout() is available from Java 1.7 + JDBC4.
41
+ //private boolean hasSetNetworkTimeout;
42
+ //private Method setNetworkTimeoutMethod;
43
+
44
+ public JdbcUtils(Class<?> connectionClass)
45
+ {
46
+ this.connectionClass = connectionClass;
47
+
48
+ //configureSetNetworkTimeout();
49
+ }
50
+
51
+ public int executeUpdateWithSqlLogging(Statement stmt, String sql) throws SQLException
52
+ {
53
+ logger.info("SQL: " + sql);
54
+ long startTime = System.currentTimeMillis();
55
+ int count = stmt.executeUpdate(sql);
56
+ double seconds = (System.currentTimeMillis() - startTime) / 1000.0;
57
+ if (count == 0) {
58
+ logger.info(String.format("> %.2f seconds", seconds));
59
+ } else {
60
+ logger.info(String.format("> %.2f seconds (%,d rows)", seconds, count));
61
+ }
62
+ return count;
63
+ }
64
+
65
+ //private void configureSetNetworkTimeout() {
66
+ // try {
67
+ // Method m = connectionClass.getMethod("setNetworkTimeout", Executor.class, int.class);
68
+ // if (isCallableMethod(m)) {
69
+ // setNetworkTimeoutMethod = m;
70
+ // hasSetNetworkTimeout = true;
71
+ // }
72
+ // } catch (SecurityException ex) {
73
+ // } catch (NoSuchMethodException ex) {
74
+ // }
75
+ //}
76
+
77
+ //private boolean isCallableMethod(Method m) {
78
+ // int modifiers = m.getModifiers();
79
+ // if (Modifier.isAbstract(modifiers)) {
80
+ // // Method.invoke throws java.lang.AbstractMethodError if it's an
81
+ // // abstract method. Applications can't catch AbstractMethodError.
82
+ // return false;
83
+ // }
84
+ // if (!Modifier.isPublic(modifiers)) {
85
+ // // we can only call public methods
86
+ // return false;
87
+ // }
88
+ // return true;
89
+ //}
90
+
91
+ // PostgreSQL JDBC driver implements isValid() method. But the
92
+ // implementation throws following exception:
93
+ // "java.io.IOException: Method org.postgresql.jdbc4.Jdbc4Connection.isValid(int) is not yet implemented."
94
+ //
95
+ // So, checking mechanism doesn't work at all.
96
+ // Thus here just runs "SELECT 1" to check connectivity.
97
+ //
98
+ //public boolean isValidConnection(Connection connection, int timeout) throws SQLException
99
+ //{
100
+ // Statement stmt = connection.createStatement();
101
+ // try {
102
+ // stmt.executeQuery("SELECT 1").close();
103
+ // return true;
104
+ // } catch (SQLException ex) {
105
+ // return false;
106
+ // } finally {
107
+ // stmt.close();
108
+ // }
109
+ //}
110
+
111
+ //public void setNetworkTimeout(Connection connection,
112
+ // Executor executor, int milliseconds) throws SQLException {
113
+ // Throwable exception = null;
114
+ // if (hasSetNetworkTimeout) {
115
+ // try {
116
+ // setNetworkTimeoutMethod.invoke(connection, executor, milliseconds);
117
+ // return;
118
+ //
119
+ // } catch (IllegalArgumentException ex) {
120
+ // // ignore error
121
+ // LOG.warn("Connection.setNetworkTimeout failed due to IllegalArgumentException.");
122
+ // exception = ex;
123
+ //
124
+ // } catch (IllegalAccessException ex) {
125
+ // // ignore error
126
+ // LOG.warn("Connection.setNetworkTimeout failed due to IllegalAccessException.");
127
+ // exception = ex;
128
+ //
129
+ // } catch (InvocationTargetException ex) {
130
+ // //Throwable cause = ex.getTargetException();
131
+ // //if (cause instanceof SQLException) {
132
+ // // throw (SQLException) cause;
133
+ // //} else if (cause instanceof RuntimeException) {
134
+ // // throw (RuntimeException) cause;
135
+ // //} else if (cause instanceof Error) {
136
+ // // throw (Error) cause;
137
+ // //} else {
138
+ // // throw new SQLException(cause);
139
+ // //}
140
+ // exception = ex.getTargetException();
141
+ // // It's safe to ignore exceptions.
142
+ // }
143
+ //
144
+ // hasSetNetworkTimeout = false;
145
+ // }
146
+ //
147
+ // if (exception != null) {
148
+ // LOG.warn("Connection.setNetworkTimeout is not available: "+exception);
149
+ // } else {
150
+ // LOG.warn("Connection.setNetworkTimeout is not available.");
151
+ // }
152
+ // // TODO any substitute implementations?
153
+ //}
154
+ }
155
+