This repository was archived by the owner on Jul 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJdbcExecutor.java
More file actions
166 lines (153 loc) · 6.71 KB
/
JdbcExecutor.java
File metadata and controls
166 lines (153 loc) · 6.71 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
package com.planner.api.database;
import com.planner.api.files.FileReader;
import com.planner.api.model.QueryResponse;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Logger;
@RequestScoped
public class JdbcExecutor {
private final Logger LOGGER = Logger.getLogger(JdbcExecutor.class.getName());
@Inject
ConnectionBuilder connectionBuilder;
@Inject
FileReader fileReader;
/**
* <ol>
* <li>Connect to default database.</li>
* <li>Execute dynamic select statement.</li>
* </ol>
*
* @param sqlSelectStatement SELECT * FROM table
* @return extracted response
* @throws SQLException if a database access error occurs or this method is called on a closed result set
*/
public QueryResponse executeDynamicQuery(String sqlSelectStatement) throws SQLException {
try (Connection connection = connectionBuilder.createConnection();
Statement statement = connection.createStatement()) {
return executeSelectStatement(statement, sqlSelectStatement);
}
}
/**
* <ol>
* <li>Connect to custom database.</li>
* <li>Execute dynamic select statement.</li>
* </ol>
*
* @param dbUrl pattern: jdbc:mysql://host:port/database-name
* @param username example: root
* @param password example: password
* @param sqlSelectStatement example: SELECT * FROM table
* @return extracted response
* @throws SQLException if a database access error occurs or this method is called on a closed result set
*/
public QueryResponse executeDynamicQuery(String dbUrl, String username, String password, String sqlSelectStatement) throws SQLException {
try (Connection connection = connectionBuilder.createConnection(dbUrl, username, password);
Statement statement = connection.createStatement()) {
return executeSelectStatement(statement, sqlSelectStatement);
}
}
/**
* <ol>
* <li>Reads statement from file resource.</li>
* <li>Connect to default database.</li>
* <li>Execute static sql statement.</li>
* </ol>
*
* @param fileName in the directory: resources/sql-statements/fileName
* @return extracted response
* @throws SQLException if a database access error occurs or this method is called on a closed result set
* @throws IOException if file not found
*/
public QueryResponse executeStaticFile(String fileName) throws SQLException, IOException {
String fileSqlStatement = fileReader.readStaticSqlStatement(fileName);
try (Connection connection = connectionBuilder.createConnection();
Statement statement = connection.createStatement()) {
return executeFileStatement(statement, fileSqlStatement);
}
}
/**
* <ol>
* <li>Reads statement from file resource.</li>
* <li>Connect to default database.</li>
* <li>Execute static sql statement.</li>
* </ol>
*
* @param dbUrl pattern: jdbc:mysql://host:port/database-name
* @param username example: root
* @param password example: password
* @param fileName in the directory: resources/sql-statements/fileName
* @return extracted response
* @throws SQLException if a database access error occurs or this method is called on a closed result set
* @throws IOException if file not found
*/
public QueryResponse executeStaticFile(String dbUrl, String username, String password, String fileName) throws SQLException, IOException {
String fileSqlStatement = fileReader.readStaticSqlStatement(fileName);
try (Connection connection = connectionBuilder.createConnection(dbUrl, username, password);
Statement statement = connection.createStatement()) {
return executeFileStatement(statement, fileSqlStatement);
}
}
/**
* <ul>
* <li>If statements start with SELECT: Executes single SELECT statement.</li>
* <li>Else: Executes all found statements and collects instance count in the response.</li>
* </ul>
*
* @param connectionStatement statement from jdbc connection
* @param fileSqlStatement statement from file resource
* @return extracted response
* @throws SQLException if a database access error occurs or this method is called on a closed result set
*/
private QueryResponse executeFileStatement(Statement connectionStatement, String fileSqlStatement) throws SQLException {
QueryResponse response;
if (fileSqlStatement.toLowerCase().startsWith("select")) {
response = executeSelectStatement(connectionStatement, fileSqlStatement);
} else {
response = new QueryResponse();
for (String sqlStatement : fileSqlStatement.split(";")) {
response.instanceCount += executeModificationStatement(connectionStatement, sqlStatement);
}
}
return response;
}
/**
* Executes single SELECT statement.
*
* @param connectionStatement statement from jdbc connection
* @param sqlQuery SELECT sql statement
* @return extracted response
* @throws SQLException if a database access error occurs or this method is called on a closed result set
*/
private QueryResponse executeSelectStatement(Statement connectionStatement, String sqlQuery) throws SQLException {
LOGGER.info("Execute SQL Query Statement:\n" + sqlQuery);
try (ResultSet resultSet = connectionStatement.executeQuery(sqlQuery)) {
return ResultMapper.resultToResponse(resultSet);
}
}
/**
* Executes single modification statement.
*
* @param connectionStatement statement from jdbc connection
* @param sqlStatement CREATE / INSERT / UPDATE / DELETE sql statement
* @return instance count of result
* @throws SQLException if a database access error occurs or this method is called on a closed result set
*/
private int executeModificationStatement(Statement connectionStatement, String sqlStatement) throws SQLException {
LOGGER.info("Execute SQL Statement:\n" + sqlStatement);
int foundInstances = 0;
boolean resultPresent = connectionStatement.execute(sqlStatement);
if (resultPresent) {
try (ResultSet resultSet = connectionStatement.getResultSet()) {
while (resultSet.next()) {
foundInstances++;
}
}
}
return foundInstances;
}
}