commit | author | age
|
a59fca
|
1 |
package de.uhilger.baselink; |
U |
2 |
|
|
3 |
import java.sql.Connection; |
|
4 |
import java.sql.PreparedStatement; |
|
5 |
import java.sql.SQLException; |
|
6 |
import java.sql.Statement; |
|
7 |
import java.util.List; |
|
8 |
import java.util.Map; |
|
9 |
import java.util.logging.Level; |
|
10 |
import java.util.logging.Logger; |
|
11 |
|
|
12 |
/** |
|
13 |
* |
|
14 |
* @author Ulrich Hilger |
|
15 |
*/ |
|
16 |
public class Script extends DBActor { |
|
17 |
private static final Logger logger = Logger.getLogger(Script.class.getName()); |
|
18 |
|
|
19 |
public Script(PersistenceManager pm) { |
|
20 |
this.pm = pm; |
|
21 |
} |
|
22 |
/** |
|
23 |
* Execute an SQL statement and return keys generated in the database |
|
24 |
* @param sql the statement to execute |
|
25 |
* @return a list of generated keys |
|
26 |
*/ |
|
27 |
public List<Map<String, Object>> executeWithKeys(String sql) { |
|
28 |
List<Map<String, Object>> keys = null; |
|
29 |
Connection c = null; |
|
30 |
try { |
|
31 |
c = pm.getConnection(); |
|
32 |
keys = executeWithKeys(c, sql); |
|
33 |
c.close(); |
|
34 |
c = null; |
|
35 |
} catch (SQLException ex) { |
|
36 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
37 |
} finally { |
|
38 |
pm.closeConnectionFinally(c); |
|
39 |
} |
|
40 |
return keys; |
|
41 |
} |
|
42 |
|
|
43 |
/** |
|
44 |
* Execute an SQL statement and return keys generated in the database |
|
45 |
* @param c database connection to use |
|
46 |
* @param sql the statement to execute |
|
47 |
* @return a list of generated keys |
|
48 |
*/ |
|
49 |
public List<Map<String, Object>> executeWithKeys(Connection c, String sql) { |
|
50 |
List<Map<String, Object>> keys = null; |
|
51 |
Statement s = null; |
|
52 |
try { |
|
53 |
s = c.createStatement(); |
|
54 |
s.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS); |
|
55 |
keys = pm.toList(s.getGeneratedKeys()); |
|
56 |
s.close(); |
|
57 |
s = null; |
|
58 |
} catch (SQLException ex) { |
|
59 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
60 |
} finally { |
|
61 |
pm.closeStatementFinally(s); |
|
62 |
} |
|
63 |
return keys; |
|
64 |
} |
|
65 |
|
|
66 |
/** |
|
67 |
* Execute an SQL statement |
|
68 |
* @param sql the statement to execute |
|
69 |
* @return the number of records affected by this statement or -1 if none |
|
70 |
*/ |
|
71 |
public int execute(String sql) { |
|
72 |
int numRows = -1; |
|
73 |
Connection c = null; |
|
74 |
try { |
|
75 |
c = pm.getConnection(); |
|
76 |
numRows = execute(c, sql); |
|
77 |
c.close(); |
|
78 |
c = null; |
|
79 |
} catch (SQLException ex) { |
|
80 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
81 |
} finally { |
|
82 |
pm.closeConnectionFinally(c); |
|
83 |
} |
|
84 |
return numRows; |
|
85 |
} |
|
86 |
|
|
87 |
/** |
|
88 |
* Execute an SQL statement |
|
89 |
* @param c database connection to use |
|
90 |
* @param sql the statement to execute |
|
91 |
* @return the number of records affected by this statement or -1 if none |
|
92 |
*/ |
|
93 |
public int execute(Connection c, String sql) { |
|
94 |
int numRows = -1; |
|
95 |
Statement s = null; |
|
96 |
try { |
|
97 |
s = c.createStatement(); |
|
98 |
numRows = s.executeUpdate(sql); |
|
99 |
s.close(); |
|
100 |
s = null; |
|
101 |
} catch (SQLException ex) { |
|
102 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
103 |
} finally { |
|
104 |
pm.closeStatementFinally(s); |
|
105 |
} |
|
106 |
return numRows; |
|
107 |
} |
|
108 |
|
|
109 |
/** |
|
110 |
* Execute an SQL statement |
|
111 |
* @param sql the SQL string with ? at the position of params |
|
112 |
* @param params list of parameters in the order they appear in the SQL string |
|
113 |
* @return the number of records affected by this statement or -1 if none |
|
114 |
*/ |
|
115 |
public int execute(String sql, Object... params) { |
|
116 |
int numRows = -1; |
|
117 |
Connection c = null; |
|
118 |
try { |
|
119 |
c = pm.getConnection(); |
|
120 |
numRows = execute(c, sql, params); |
|
121 |
c.close(); |
|
122 |
c = null; |
|
123 |
} catch (SQLException ex) { |
|
124 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
125 |
} finally { |
|
126 |
pm.closeConnectionFinally(c); |
|
127 |
} |
|
128 |
return numRows; |
|
129 |
} |
|
130 |
|
|
131 |
/** |
|
132 |
* Execute an SQL statement |
|
133 |
* @param c database connection to use |
|
134 |
* @param sql the SQL string with ? at the position of params |
|
135 |
* @param params list of parameters in the order they appear in the SQL string |
|
136 |
* @return the number of records affected by this statement or -1 if none |
|
137 |
*/ |
|
138 |
public int execute(Connection c, String sql, Object... params) { |
|
139 |
int numRows = -1; |
|
140 |
Statement s = null; |
|
141 |
try { |
|
142 |
//s = c.createStatement(); |
|
143 |
s = pm.buildQuery(c, sql, params); |
|
144 |
if(s != null && s instanceof PreparedStatement) { |
|
145 |
try (PreparedStatement ps = (PreparedStatement) s) { |
|
146 |
numRows = ps.executeUpdate(); |
|
147 |
} |
|
148 |
} |
|
149 |
s = null; |
|
150 |
} catch (Exception ex) { |
|
151 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
152 |
} finally { |
|
153 |
pm.closeStatementFinally(s); |
|
154 |
} |
|
155 |
return numRows; |
|
156 |
} |
|
157 |
|
|
158 |
/** |
|
159 |
* Execute an SQL script |
|
160 |
* @param sqlScript the SQL script to execute |
|
161 |
* @return an array of row counts referring to the number of affected rows of |
|
162 |
* each sql statement in the script in the order of SQL commands in the script |
|
163 |
* Statement.EXECUTE_FAILED indicates failure of single steps in the script |
|
164 |
* Statement.SUCCESS_NO_INFO indicates successful execution without information |
|
165 |
* about the number of affected rows |
|
166 |
*/ |
|
167 |
public int[] executeScript(String sqlScript) { |
|
168 |
int[] ergebnisse = null; |
|
169 |
Connection c = null; |
|
170 |
Statement s = null; |
|
171 |
try { |
|
172 |
c = pm.getConnection(); |
|
173 |
s = c.createStatement(); |
|
174 |
String[] sqlKommandos = sqlScript.split(";"); |
|
175 |
for (String sqlKommando : sqlKommandos) { |
|
176 |
s.addBatch(sqlKommando); |
|
177 |
} |
|
178 |
ergebnisse = s.executeBatch(); |
|
179 |
} catch(SQLException ex) { |
|
180 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
181 |
} finally { |
|
182 |
pm.closeStatementFinally(s); |
|
183 |
pm.closeConnectionFinally(c); |
|
184 |
} |
|
185 |
return ergebnisse; |
|
186 |
} |
|
187 |
|
|
188 |
/** |
|
189 |
* Execute an SQL script |
|
190 |
* @param c the Connection object to use |
|
191 |
* @param sqlScript the SQL script to execute |
|
192 |
* @return an array of row counts referring to the number of affected rows of |
|
193 |
* each sql statement in the script in the order of SQL commands in the script |
|
194 |
* Statement.EXECUTE_FAILED indicates failure of single steps in the script |
|
195 |
* Statement.SUCCESS_NO_INFO indicates successful execution without information |
|
196 |
* about the number of affected rows |
|
197 |
*/ |
|
198 |
public int[] executeScript(Connection c, String sqlScript) { |
|
199 |
int[] ergebnisse = null; |
|
200 |
Statement s = null; |
|
201 |
try { |
|
202 |
s = c.createStatement(); |
|
203 |
String[] sqlKommandos = sqlScript.split(";"); |
|
204 |
for (String sqlKommando : sqlKommandos) { |
|
205 |
s.addBatch(sqlKommando); |
|
206 |
} |
|
207 |
ergebnisse = s.executeBatch(); |
|
208 |
} catch(SQLException ex) { |
|
209 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
210 |
} finally { |
|
211 |
pm.closeStatementFinally(s); |
|
212 |
} |
|
213 |
return ergebnisse; |
|
214 |
} |
|
215 |
|
|
216 |
} |