-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDb2Service.java
More file actions
179 lines (151 loc) · 4.72 KB
/
Db2Service.java
File metadata and controls
179 lines (151 loc) · 4.72 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package org.db2;
import org.json.JSONArray;
import java.sql.*;
import java.util.regex.*;
public class Db2Service {
final String DB_URL;
final String USER;
final String PASSWORD;
final String DATABASE;
final ResponseMapper mapper;
Connection connection = null;
Statement statement = null;
CallableStatement callableStatement = null;
ResultSet response = null;
public Db2Service(String host, String port, String database, String user, String password, ResponseMapper mapper) {
this.USER = user;
this.PASSWORD = password;
this.DATABASE = database;
this.mapper = mapper;
this.DB_URL = this.getDbUrlFromArguments(host, port, database);
}
public JSONArray executeQuery(String query) throws SQLException {
this.statement = connection.createStatement();
this.response = statement.executeQuery(query);
return mapper.convertToJson(response);
}
public int applyScript(String script) throws SQLException {
String cleanedScript = removeComments(script);
String[] statements = splitStatements(cleanedScript);
int totalUpdateCount = 0;
for (String statement : statements) {
statement = statement.trim();
if (statement.isEmpty()) {
continue;
}
Statement statementInstance = connection.createStatement();
try {
statementInstance.execute(statement);
totalUpdateCount += statementInstance.getUpdateCount();
} catch (SQLException e) {
int reorgPendingErrorCode = -668;
if (e.getErrorCode() == reorgPendingErrorCode) {
String tableName = extractTableNameFromError(e.getMessage());
if (tableName != null) {
reorganizeTable(tableName, statementInstance);
// retry
statementInstance.execute(statement);
totalUpdateCount += statementInstance.getUpdateCount();
} else {
throw e;
}
} else {
throw e;
}
} finally {
statementInstance.close();
}
}
return totalUpdateCount;
}
private String removeComments(String script) {
return script.replaceAll("(?s)(?m)(?<=\\n)(?:/\\*.*?\\*/|--.*?$)(?=\\n)", "");
}
private String[] splitStatements(String query) {
String[] parts = query.trim().split(";\\s+", -1);
java.util.ArrayList<String> statements = new java.util.ArrayList<>();
for (String part : parts) {
part = part.trim();
if (!part.isEmpty()) {
statements.add(part);
}
}
return statements.toArray(new String[0]);
}
private void reorganizeTable(String tableName, Statement stmt) throws SQLException {
// Use ADMIN_CMD to execute REORG TABLE command
// Escape single quotes in table name for the command string
String escapedTableName = tableName.replace("'", "''");
String reorgSql = "CALL SYSPROC.ADMIN_CMD('REORG TABLE " + escapedTableName + "')";
stmt.execute(reorgSql);
if (!connection.getAutoCommit()) {
connection.commit();
}
}
private String extractTableNameFromError(String errorMessage) {
// Extract table name from error message like: SQLERRMC=7;db1.table2
Pattern pattern = Pattern.compile("SQLERRMC=\\d+;([^,;\\s]+)");
Matcher matcher = pattern.matcher(errorMessage);
if (matcher.find()) {
String tableName = matcher.group(1).trim();
// Quote the table name properly for REORG statement
// If it contains a dot, split into schema.table and quote both parts
if (tableName.contains(".")) {
String[] parts = tableName.split("\\.", 2);
if (parts.length == 2) {
return "\"" + parts[0] + "\".\"" + parts[1] + "\"";
}
}
return "\"" + tableName + "\"";
}
return null;
}
public int executeCallableQuery(String query, String inParam) throws SQLException {
this.callableStatement = connection.prepareCall(query);
if (!inParam.isEmpty()) {
int param = Integer.parseInt(inParam);
this.callableStatement.setInt(1, param);
this.callableStatement.execute();
return param;
}
this.callableStatement.registerOutParameter(1, Types.INTEGER);
this.callableStatement.execute();
return this.callableStatement.getInt(1);
}
public void openConnection() throws SQLException {
this.connection = DriverManager.getConnection(this.DB_URL, this.USER, this.PASSWORD);
}
public void closeConnection() {
if (this.response != null) {
try {
this.response.close();
} catch (SQLException _) {
/* Ignored */
}
}
if (this.statement != null) {
try {
this.statement.close();
} catch (SQLException _) {
/* Ignored */
}
}
if (this.callableStatement != null) {
try {
this.callableStatement.close();
} catch (SQLException _) {
/* Ignored */
}
}
if (this.connection != null) {
try {
this.connection.close();
} catch (SQLException _) {
/* Ignored */
}
}
}
private String getDbUrlFromArguments(String host, String port, String database) {
return String.format("jdbc:db2://%s:%s/%s", host, port, database);
}
}