1
|
package eu.dnetlib.data.claimsDemo;
|
2
|
|
3
|
import org.apache.log4j.Logger;
|
4
|
|
5
|
import java.sql.*;
|
6
|
import java.util.ArrayList;
|
7
|
import java.util.List;
|
8
|
|
9
|
/**
|
10
|
* @author eri Sql Store class to connect to the DB backend
|
11
|
*/
|
12
|
|
13
|
public class SqlStore {
|
14
|
|
15
|
private Logger log = Logger.getLogger(this.getClass());
|
16
|
|
17
|
private String dbUrl;
|
18
|
private String dbUser;
|
19
|
private String dbPassword;
|
20
|
private Connection connection;
|
21
|
|
22
|
private String dbDriver;
|
23
|
|
24
|
public SqlStore(String dbUrl, String dbUser, String dbPassword, String dbDriver) {
|
25
|
this.dbDriver = dbDriver;
|
26
|
this.dbUser = dbUser;
|
27
|
this.dbUrl = dbUrl;
|
28
|
this.dbPassword = dbPassword;
|
29
|
|
30
|
}
|
31
|
|
32
|
public SqlStore() throws Exception {
|
33
|
|
34
|
}
|
35
|
|
36
|
|
37
|
public void init() throws ClassNotFoundException, SQLException {
|
38
|
Class.forName(dbDriver);
|
39
|
this.getConnection();
|
40
|
|
41
|
|
42
|
}
|
43
|
|
44
|
public void destroy() throws SQLException {
|
45
|
this.closeConnection();
|
46
|
|
47
|
}
|
48
|
|
49
|
public void close() throws SQLException {
|
50
|
closeConnection();
|
51
|
|
52
|
}
|
53
|
|
54
|
/**
|
55
|
* Establishes a JDBC connection to the database server without a given DB.
|
56
|
* Used in order to create or drop databases.
|
57
|
*
|
58
|
* @throws SQLException
|
59
|
*/
|
60
|
public void getConnectionNoDatabase() throws SQLException {
|
61
|
String parsedUrl = this.parseUrl(dbUrl);
|
62
|
log.info("Connecting to " + parsedUrl + dbUser + dbPassword);
|
63
|
connection = (Connection) DriverManager.getConnection(parsedUrl, dbUser, dbPassword);
|
64
|
log.info("Connected to " + parsedUrl);
|
65
|
|
66
|
}
|
67
|
|
68
|
/**
|
69
|
* Establishes a connection to a given DB.
|
70
|
*
|
71
|
* @throws SQLException
|
72
|
*/
|
73
|
public void getConnection() throws SQLException {
|
74
|
|
75
|
log.info("Attempting connection to : " + this.dbUrl + " for " + this.dbUser + " and " + this.dbPassword);
|
76
|
connection = (Connection) DriverManager.getConnection(this.dbUrl, dbUser, dbPassword);
|
77
|
log.info("Connected to " + this.dbUrl);
|
78
|
|
79
|
}
|
80
|
|
81
|
/**
|
82
|
* Closes Database connections.
|
83
|
*
|
84
|
* @throws SQLException
|
85
|
*/
|
86
|
public void closeConnection() throws SQLException {
|
87
|
|
88
|
if (connection != null) {
|
89
|
connection.close();
|
90
|
}
|
91
|
|
92
|
}
|
93
|
|
94
|
/**
|
95
|
* Executes the @param command stored procedure in the Database. Stored
|
96
|
* procedure parameters are specified in values.
|
97
|
*
|
98
|
* @param command
|
99
|
* @param values
|
100
|
* @return
|
101
|
* @throws SQLException
|
102
|
*/
|
103
|
public ResultSet executeStoredProcedure(String command, ArrayList<String> values) throws SQLException {
|
104
|
java.sql.CallableStatement callableStatement = null;
|
105
|
log.debug(" Executing Query ..." + command);
|
106
|
|
107
|
callableStatement = this.connection.prepareCall(command);
|
108
|
int pos = 1;
|
109
|
|
110
|
for (String v : values) {
|
111
|
|
112
|
callableStatement.setObject(pos, v);
|
113
|
|
114
|
pos++;
|
115
|
}
|
116
|
|
117
|
callableStatement.execute();
|
118
|
return callableStatement.getResultSet();
|
119
|
|
120
|
|
121
|
|
122
|
}
|
123
|
|
124
|
/**
|
125
|
* Executes the stored query procedure specified by @param command.
|
126
|
*
|
127
|
* @param command
|
128
|
* @return
|
129
|
* @throws SQLException
|
130
|
*/
|
131
|
public ResultSet executeStoredProcedure(String command) throws SQLException {
|
132
|
log.debug(" Executing Query ..." + command);
|
133
|
java.sql.CallableStatement callableStatement = null;
|
134
|
|
135
|
callableStatement = this.connection.prepareCall(command);
|
136
|
|
137
|
callableStatement.execute();
|
138
|
return callableStatement.getResultSet();
|
139
|
|
140
|
|
141
|
|
142
|
}
|
143
|
|
144
|
/**
|
145
|
* Executes the @param query
|
146
|
*
|
147
|
* @param command
|
148
|
* @return
|
149
|
* @throws SQLException
|
150
|
*/
|
151
|
public ResultSet executeQuery(String command) throws SQLException, SQLStoreException {
|
152
|
log.debug(" Executing Query ... " + command);
|
153
|
Statement st = connection.createStatement();
|
154
|
|
155
|
if (st.execute(command)) {
|
156
|
|
157
|
ResultSet rs = st.getResultSet();
|
158
|
//st.close();
|
159
|
return rs;
|
160
|
} else {
|
161
|
log.error("Fail to execute command " + st.getWarnings());
|
162
|
throw new SQLStoreException("Fail to execute command " + st.getWarnings());
|
163
|
|
164
|
}
|
165
|
|
166
|
|
167
|
|
168
|
}
|
169
|
|
170
|
/**
|
171
|
* Executes the @param prepared query with the @values as parameters
|
172
|
* Returns a result set.
|
173
|
*
|
174
|
* @param command
|
175
|
* @param values
|
176
|
* @return
|
177
|
* @throws SQLException
|
178
|
*/
|
179
|
public ResultSet executeQuery(String command, ArrayList<Object> values) throws SQLException, SQLStoreException {
|
180
|
log.debug(" Executing Query ..." + command);
|
181
|
|
182
|
PreparedStatement st = connection.prepareStatement(command);
|
183
|
|
184
|
int pos = 1;
|
185
|
|
186
|
for (Object v : values) {
|
187
|
|
188
|
st.setObject(pos, v);
|
189
|
|
190
|
pos++;
|
191
|
}
|
192
|
|
193
|
if (st.execute()) {
|
194
|
ResultSet rs = st.getResultSet();
|
195
|
// st.close();
|
196
|
return rs;
|
197
|
} else {
|
198
|
log.error("Fail to execute command " + st.getWarnings());
|
199
|
throw new SQLStoreException("Fail to execute command " + st.getWarnings());
|
200
|
}
|
201
|
|
202
|
|
203
|
|
204
|
}
|
205
|
|
206
|
/**
|
207
|
* Executes the @param command Update query
|
208
|
*
|
209
|
* @param command
|
210
|
* @return
|
211
|
* @throws SQLException
|
212
|
*/
|
213
|
public void executeUpdate(String command) throws SQLException {
|
214
|
log.debug(" Executing Query ..." + command);
|
215
|
Statement st = connection.createStatement();
|
216
|
st.executeUpdate(command);
|
217
|
//st.close();
|
218
|
}
|
219
|
public void executeUpdateWithRollback(List<String> commandS) throws SQLException{
|
220
|
log.debug(" Executing Query ..." + commandS);
|
221
|
Statement st = connection.createStatement();
|
222
|
connection.setAutoCommit(false);
|
223
|
for(String command : commandS) {
|
224
|
if(command.equals("EXCEPTION")){
|
225
|
throw new SQLException("Force rollback ");
|
226
|
}
|
227
|
st.executeUpdate(command);
|
228
|
}
|
229
|
//st.close();
|
230
|
connection.commit();
|
231
|
connection.setAutoCommit(true);
|
232
|
|
233
|
}
|
234
|
/**
|
235
|
* Executes the @param command Prepared Update query with the
|
236
|
*
|
237
|
* @param command
|
238
|
* @param values
|
239
|
* @return
|
240
|
* @throws Exception
|
241
|
* @values as parameters
|
242
|
*/
|
243
|
public boolean executeUpdate(String command, ArrayList<Object> values) throws SQLException {
|
244
|
log.debug(" Executing Query ..." + command);
|
245
|
PreparedStatement st = connection.prepareStatement(command);
|
246
|
|
247
|
int pos = 1;
|
248
|
|
249
|
for (Object v : values) {
|
250
|
|
251
|
st.setObject(pos, v);
|
252
|
|
253
|
pos++;
|
254
|
}
|
255
|
|
256
|
boolean executed = st.execute();
|
257
|
|
258
|
if (executed) {
|
259
|
log.error("Fail to execute update command " + st.getWarnings());
|
260
|
throw new SQLException("Fail to execute update command " + st.getWarnings());
|
261
|
}
|
262
|
return !executed;
|
263
|
}
|
264
|
|
265
|
|
266
|
/**
|
267
|
* Turns autocommit on and off.
|
268
|
*
|
269
|
* @param on
|
270
|
* @throws Exception
|
271
|
*/
|
272
|
public void setautoCommit(boolean on) throws SQLException {
|
273
|
this.connection.setAutoCommit(on);
|
274
|
}
|
275
|
|
276
|
|
277
|
/**
|
278
|
* Creates a serialized string from the ResultSet returned from a query
|
279
|
*
|
280
|
* @throws Exception
|
281
|
*/
|
282
|
public String getResults(ResultSet rs) throws SQLException {
|
283
|
String data = new String();
|
284
|
try {
|
285
|
|
286
|
|
287
|
ResultSetMetaData rsmd = rs.getMetaData();
|
288
|
while (rs.next()) {
|
289
|
for (int i = 1; i < rsmd.getColumnCount() - 1; i++)
|
290
|
|
291
|
{
|
292
|
|
293
|
log.debug(rsmd.getColumnName(i) + "," + rs.getString(i));
|
294
|
data += rsmd.getColumnName(i) + "," + rs.getString(i) + "\n";
|
295
|
}
|
296
|
|
297
|
}
|
298
|
log.debug(" Generated Data :" + data);
|
299
|
rs.close();
|
300
|
return data;
|
301
|
|
302
|
|
303
|
} finally {
|
304
|
this.closeConnection();
|
305
|
}
|
306
|
|
307
|
}
|
308
|
|
309
|
private String parseUrl(String url) {
|
310
|
|
311
|
if (url.endsWith("/")) {
|
312
|
url = url.substring(0, url.lastIndexOf('/'));
|
313
|
|
314
|
}
|
315
|
url = url.substring(0, url.lastIndexOf('/') + 1);
|
316
|
log.debug(" db url parsed" + url);
|
317
|
return url;
|
318
|
}
|
319
|
|
320
|
public String getDbDriver() {
|
321
|
return dbDriver;
|
322
|
}
|
323
|
|
324
|
public void setDbDriver(String dbDriver) {
|
325
|
this.dbDriver = dbDriver;
|
326
|
}
|
327
|
|
328
|
public String getDbUrl() {
|
329
|
return dbUrl;
|
330
|
}
|
331
|
|
332
|
public void setDbUrl(String dbUrl) {
|
333
|
this.dbUrl = dbUrl;
|
334
|
}
|
335
|
|
336
|
public String getDbUser() {
|
337
|
return dbUser;
|
338
|
}
|
339
|
|
340
|
public void setDbUser(String dbUser) {
|
341
|
this.dbUser = dbUser;
|
342
|
}
|
343
|
|
344
|
public String getDbPassword() {
|
345
|
return dbPassword;
|
346
|
}
|
347
|
|
348
|
public void setDbPassword(String dbPassword) {
|
349
|
this.dbPassword = dbPassword;
|
350
|
}
|
351
|
|
352
|
public void setConnection(Connection connection) {
|
353
|
this.connection = connection;
|
354
|
}
|
355
|
}
|