-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSqlScriptRunner.java
More file actions
133 lines (117 loc) · 4.31 KB
/
SqlScriptRunner.java
File metadata and controls
133 lines (117 loc) · 4.31 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
/**
* @author David Kaya
* @version 0.2
*/
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Reader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class SqlScriptRunner {
public static final String DEFAULT_SCRIPT_DELIMETER = ";";
public static final Logger LOGGER = Logger.getLogger(SqlScriptRunner.class.getName());
private final boolean autoCommit, logErrors;
private final Connection connection;
/**
*
* @param connection : Connection to database.
* @param autoCommit : True - it will commit automatically, false - you have to commit manualy.
*/
public SqlScriptRunner(final Connection connection, final boolean autoCommit) {
this(connection, autoCommit, true);
}
/**
*
* @param connection : Connection to database.
* @param autoCommit : True - it will commit automatically, false - you have to commit manualy.
* @param logErrors : True - it will log errors, false - it will not log errors.
*/
public SqlScriptRunner(final Connection connection, final boolean autoCommit, final boolean logErrors) {
if (connection == null) {
throw new RuntimeException("Connection is required");
}
this.connection = connection;
this.autoCommit = autoCommit;
this.logErrors = logErrors;
}
/**
*
* @param reader - file with your script
* @throws SQLException - throws SQLException on error
*/
public void runScript(final Reader reader) throws SQLException {
final boolean originalAutoCommit = this.connection.getAutoCommit();
try {
if (originalAutoCommit != this.autoCommit) {
this.connection.setAutoCommit(autoCommit);
}
this.runScript(this.connection, reader);
} finally {
this.connection.setAutoCommit(originalAutoCommit);
}
}
private void runScript(final Connection connection, final Reader reader) {
for (String script : formatString(reader)) {
PreparedStatement statement = null;
try {
statement = connection.prepareStatement(script);
statement.execute();
//If auto commit is enabled, then commit
if (autoCommit) {
connection.commit();
}
} catch (SQLException ex) {
if (logErrors) {
Logger.getLogger(SqlScriptRunner.class.getName()).log(Level.SEVERE, null, ex);
} else {
ex.fillInStackTrace();
}
} finally {
if (statement != null) {
try {
statement.close();
} catch (SQLException ex) {
if (logErrors) {
LOGGER.log(Level.SEVERE, null, ex);
} else {
ex.fillInStackTrace();
}
}
}
}
}
}
/**
* Parses file into commands delimeted by ';'
* @param reader
* @return string[] - commands from file
*/
private String[] formatString(final Reader reader) {
String result = "";
String line;
final LineNumberReader lineReader = new LineNumberReader(reader);
try {
while ((line = lineReader.readLine()) != null) {
if (line.startsWith("--") || line.startsWith("//") || line.startsWith("#")) {
//DO NOTHING - It is a commented line
} else {
result += line;
}
}
} catch (IOException ex) {
if (logErrors) {
LOGGER.log(Level.SEVERE, null, ex);
} else {
ex.fillInStackTrace();
}
}
if (result == null) {
throw new RuntimeException("Error while parsing or no scripts in file!");
} else {
return result.replaceAll("(?<!"+DEFAULT_SCRIPT_DELIMETER+")(\\r?\\n)+", "").split(DEFAULT_SCRIPT_DELIMETER);
}
}
}