1
|
package eu.dnetlib.data.claims.sql;
|
2
|
|
3
|
/**
|
4
|
* Created by Eri on 18/11/2015.
|
5
|
*/
|
6
|
|
7
|
|
8
|
import org.apache.log4j.Logger;
|
9
|
|
10
|
import java.sql.ResultSet;
|
11
|
import java.sql.SQLException;
|
12
|
import java.util.ArrayList;
|
13
|
|
14
|
/**
|
15
|
* @author eri
|
16
|
*/
|
17
|
|
18
|
public class SqlDAO {
|
19
|
private SqlStore sqlStore;
|
20
|
//private QueryGenerator queryGenerator;
|
21
|
|
22
|
private Logger log = Logger.getLogger(this.getClass());
|
23
|
private long startTime;
|
24
|
private long endtime;
|
25
|
|
26
|
public SqlDAO() throws Exception {
|
27
|
|
28
|
}
|
29
|
|
30
|
|
31
|
|
32
|
/**
|
33
|
* Example of calling a stored procedure
|
34
|
*
|
35
|
* @throws Exception
|
36
|
*
|
37
|
public void callStoredProcedure() throws Exception {
|
38
|
log.info(" Executing Stored Procedure...");
|
39
|
sqlStore.getConnection();
|
40
|
startTime = System.currentTimeMillis();
|
41
|
|
42
|
String q = queryGenerator.callProcedure("create_views");
|
43
|
sqlStore.executeStoredProcedure(q);
|
44
|
sqlStore.closeConnection();
|
45
|
endtime = System.currentTimeMillis();
|
46
|
log.info("Total time for Procedure : " + ((endtime - startTime) / 60000) + " minutes ");
|
47
|
}
|
48
|
*/
|
49
|
|
50
|
/**
|
51
|
* Example of executing a prepared query with a result set
|
52
|
* with Query and parameters given as arguements
|
53
|
*
|
54
|
* @throws Exception
|
55
|
*/
|
56
|
public ResultSet executePreparedQuery(String q, ArrayList<Object> data) throws SQLException, SQLStoreException {
|
57
|
|
58
|
ResultSet rs = sqlStore.executeQuery(q, data);
|
59
|
return rs;
|
60
|
//return sqlStore.getResults(rs);
|
61
|
}
|
62
|
|
63
|
/**
|
64
|
* Example of executing a prepared query with a result set
|
65
|
* with query set from Query Generator class
|
66
|
*
|
67
|
* @throws Exception
|
68
|
*
|
69
|
public String executePreparedQuery(ArrayList<String> data) throws Exception {
|
70
|
try {
|
71
|
ResultSet rs = sqlStore.executeQuery(queryGenerator.generateSelectAllClaimsQuery(), data);
|
72
|
return sqlStore.getResults(rs);
|
73
|
|
74
|
} catch (Exception e)
|
75
|
|
76
|
{
|
77
|
log.error(e.getMessage());
|
78
|
throw new Exception(e);
|
79
|
}
|
80
|
|
81
|
} */
|
82
|
|
83
|
/**
|
84
|
* Example of executing a custom prepared query
|
85
|
*
|
86
|
* @throws Exception
|
87
|
*/
|
88
|
public ResultSet executePreparedQuery(String q) throws SQLStoreException, SQLException {
|
89
|
ResultSet rs = sqlStore.executeQuery(q);
|
90
|
log.debug("Executing query " + q);
|
91
|
return rs;
|
92
|
|
93
|
|
94
|
|
95
|
}
|
96
|
public boolean executeUpdateQuery(String q, ArrayList<Object> params) throws Exception {
|
97
|
try {
|
98
|
log.debug("Executing query " + q);
|
99
|
return sqlStore.executeUpdate(q, params);
|
100
|
|
101
|
} catch (Exception e)
|
102
|
|
103
|
{
|
104
|
log.debug("\n Query is:"+q);
|
105
|
log.error("Fail to execute query:" + q, e);
|
106
|
throw new Exception(e);
|
107
|
}
|
108
|
|
109
|
}
|
110
|
|
111
|
public void executeUpdateQuery(String q) throws Exception {
|
112
|
try {
|
113
|
log.debug("Executing query " + q);
|
114
|
sqlStore.executeUpdate(q);
|
115
|
|
116
|
} catch (Exception e)
|
117
|
|
118
|
{
|
119
|
log.debug("\n Query is:" + q);
|
120
|
log.error("Fail to execute query:" + q, e);
|
121
|
throw new Exception(e);
|
122
|
}
|
123
|
|
124
|
}
|
125
|
|
126
|
public SqlStore getSqlStore() {
|
127
|
return sqlStore;
|
128
|
}
|
129
|
|
130
|
public void setSqlStore(SqlStore sqlStore) {
|
131
|
this.sqlStore = sqlStore;
|
132
|
}
|
133
|
|
134
|
/*public QueryGenerator getQueryGenerator() {
|
135
|
return queryGenerator;
|
136
|
}
|
137
|
|
138
|
public void setQueryGenerator(QueryGenerator queryGenerator) {
|
139
|
this.queryGenerator = queryGenerator;
|
140
|
}*/
|
141
|
}
|