commit | author | age
|
3bf522
|
1 |
/* |
U |
2 |
* BaseLink - Generic object relational mapping |
|
3 |
* Copyright (C) 2024 Ulrich Hilger, http://uhilger.de |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation, either version 3 of the License, or |
|
8 |
* (at your option) any later version. |
|
9 |
* |
|
10 |
* This program is distributed in the hope that it will be useful, |
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
* GNU General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License |
|
16 |
* along with this program. If not, see http://www.gnu.org/licenses/ |
|
17 |
*/ |
a59fca
|
18 |
package de.uhilger.baselink; |
U |
19 |
|
|
20 |
import java.sql.Connection; |
|
21 |
import java.sql.PreparedStatement; |
|
22 |
import java.sql.SQLException; |
|
23 |
import java.sql.Statement; |
|
24 |
import java.util.List; |
|
25 |
import java.util.Map; |
|
26 |
import java.util.logging.Level; |
|
27 |
import java.util.logging.Logger; |
|
28 |
|
|
29 |
/** |
3bf522
|
30 |
* Methoden zum Ausfuehren von SQL-Skripten |
U |
31 |
* |
|
32 |
* @author Copyright (c) Ulrich Hilger, <a href="http://uhilger.de">http://uhilger.de</a> |
|
33 |
* @author Published under the terms and conditions of |
|
34 |
* the <a href="http://www.gnu.org/licenses/" target="_blank">GNU General Public License</a> |
|
35 |
* @version 1, January 22, 2024 |
a59fca
|
36 |
*/ |
U |
37 |
public class Script extends DBActor { |
|
38 |
private static final Logger logger = Logger.getLogger(Script.class.getName()); |
|
39 |
|
|
40 |
public Script(PersistenceManager pm) { |
|
41 |
this.pm = pm; |
|
42 |
} |
|
43 |
/** |
|
44 |
* Execute an SQL statement and return keys generated in the database |
|
45 |
* @param sql the statement to execute |
|
46 |
* @return a list of generated keys |
|
47 |
*/ |
|
48 |
public List<Map<String, Object>> executeWithKeys(String sql) { |
|
49 |
List<Map<String, Object>> keys = null; |
|
50 |
Connection c = null; |
|
51 |
try { |
|
52 |
c = pm.getConnection(); |
|
53 |
keys = executeWithKeys(c, sql); |
|
54 |
c.close(); |
|
55 |
c = null; |
|
56 |
} catch (SQLException ex) { |
|
57 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
58 |
} finally { |
|
59 |
pm.closeConnectionFinally(c); |
|
60 |
} |
|
61 |
return keys; |
|
62 |
} |
|
63 |
|
|
64 |
/** |
|
65 |
* Execute an SQL statement and return keys generated in the database |
|
66 |
* @param c database connection to use |
|
67 |
* @param sql the statement to execute |
|
68 |
* @return a list of generated keys |
|
69 |
*/ |
|
70 |
public List<Map<String, Object>> executeWithKeys(Connection c, String sql) { |
|
71 |
List<Map<String, Object>> keys = null; |
|
72 |
Statement s = null; |
|
73 |
try { |
|
74 |
s = c.createStatement(); |
|
75 |
s.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS); |
|
76 |
keys = pm.toList(s.getGeneratedKeys()); |
|
77 |
s.close(); |
|
78 |
s = null; |
|
79 |
} catch (SQLException ex) { |
|
80 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
81 |
} finally { |
|
82 |
pm.closeStatementFinally(s); |
|
83 |
} |
|
84 |
return keys; |
|
85 |
} |
|
86 |
|
|
87 |
/** |
|
88 |
* Execute an SQL statement |
|
89 |
* @param sql the statement to execute |
|
90 |
* @return the number of records affected by this statement or -1 if none |
|
91 |
*/ |
|
92 |
public int execute(String sql) { |
|
93 |
int numRows = -1; |
|
94 |
Connection c = null; |
|
95 |
try { |
|
96 |
c = pm.getConnection(); |
|
97 |
numRows = execute(c, sql); |
|
98 |
c.close(); |
|
99 |
c = null; |
|
100 |
} catch (SQLException ex) { |
|
101 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
102 |
} finally { |
|
103 |
pm.closeConnectionFinally(c); |
|
104 |
} |
|
105 |
return numRows; |
|
106 |
} |
|
107 |
|
|
108 |
/** |
|
109 |
* Execute an SQL statement |
|
110 |
* @param c database connection to use |
|
111 |
* @param sql the statement to execute |
|
112 |
* @return the number of records affected by this statement or -1 if none |
|
113 |
*/ |
|
114 |
public int execute(Connection c, String sql) { |
|
115 |
int numRows = -1; |
|
116 |
Statement s = null; |
|
117 |
try { |
|
118 |
s = c.createStatement(); |
|
119 |
numRows = s.executeUpdate(sql); |
|
120 |
s.close(); |
|
121 |
s = null; |
|
122 |
} catch (SQLException ex) { |
|
123 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
124 |
} finally { |
|
125 |
pm.closeStatementFinally(s); |
|
126 |
} |
|
127 |
return numRows; |
|
128 |
} |
|
129 |
|
|
130 |
/** |
|
131 |
* Execute an SQL statement |
|
132 |
* @param sql the SQL string with ? at the position of params |
|
133 |
* @param params list of parameters in the order they appear in the SQL string |
|
134 |
* @return the number of records affected by this statement or -1 if none |
|
135 |
*/ |
|
136 |
public int execute(String sql, Object... params) { |
|
137 |
int numRows = -1; |
|
138 |
Connection c = null; |
|
139 |
try { |
|
140 |
c = pm.getConnection(); |
|
141 |
numRows = execute(c, sql, params); |
|
142 |
c.close(); |
|
143 |
c = null; |
|
144 |
} catch (SQLException ex) { |
|
145 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
146 |
} finally { |
|
147 |
pm.closeConnectionFinally(c); |
|
148 |
} |
|
149 |
return numRows; |
|
150 |
} |
|
151 |
|
|
152 |
/** |
|
153 |
* Execute an SQL statement |
|
154 |
* @param c database connection to use |
|
155 |
* @param sql the SQL string with ? at the position of params |
|
156 |
* @param params list of parameters in the order they appear in the SQL string |
|
157 |
* @return the number of records affected by this statement or -1 if none |
|
158 |
*/ |
|
159 |
public int execute(Connection c, String sql, Object... params) { |
|
160 |
int numRows = -1; |
|
161 |
Statement s = null; |
|
162 |
try { |
|
163 |
//s = c.createStatement(); |
|
164 |
s = pm.buildQuery(c, sql, params); |
|
165 |
if(s != null && s instanceof PreparedStatement) { |
|
166 |
try (PreparedStatement ps = (PreparedStatement) s) { |
|
167 |
numRows = ps.executeUpdate(); |
|
168 |
} |
|
169 |
} |
|
170 |
s = null; |
|
171 |
} catch (Exception ex) { |
|
172 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
173 |
} finally { |
|
174 |
pm.closeStatementFinally(s); |
|
175 |
} |
|
176 |
return numRows; |
|
177 |
} |
|
178 |
|
|
179 |
/** |
|
180 |
* Execute an SQL script |
|
181 |
* @param sqlScript the SQL script to execute |
|
182 |
* @return an array of row counts referring to the number of affected rows of |
|
183 |
* each sql statement in the script in the order of SQL commands in the script |
|
184 |
* Statement.EXECUTE_FAILED indicates failure of single steps in the script |
|
185 |
* Statement.SUCCESS_NO_INFO indicates successful execution without information |
|
186 |
* about the number of affected rows |
|
187 |
*/ |
|
188 |
public int[] executeScript(String sqlScript) { |
|
189 |
int[] ergebnisse = null; |
|
190 |
Connection c = null; |
|
191 |
Statement s = null; |
|
192 |
try { |
|
193 |
c = pm.getConnection(); |
|
194 |
s = c.createStatement(); |
|
195 |
String[] sqlKommandos = sqlScript.split(";"); |
|
196 |
for (String sqlKommando : sqlKommandos) { |
|
197 |
s.addBatch(sqlKommando); |
|
198 |
} |
|
199 |
ergebnisse = s.executeBatch(); |
|
200 |
} catch(SQLException ex) { |
|
201 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
202 |
} finally { |
|
203 |
pm.closeStatementFinally(s); |
|
204 |
pm.closeConnectionFinally(c); |
|
205 |
} |
|
206 |
return ergebnisse; |
|
207 |
} |
|
208 |
|
|
209 |
/** |
|
210 |
* Execute an SQL script |
|
211 |
* @param c the Connection object to use |
|
212 |
* @param sqlScript the SQL script to execute |
|
213 |
* @return an array of row counts referring to the number of affected rows of |
|
214 |
* each sql statement in the script in the order of SQL commands in the script |
|
215 |
* Statement.EXECUTE_FAILED indicates failure of single steps in the script |
|
216 |
* Statement.SUCCESS_NO_INFO indicates successful execution without information |
|
217 |
* about the number of affected rows |
|
218 |
*/ |
|
219 |
public int[] executeScript(Connection c, String sqlScript) { |
|
220 |
int[] ergebnisse = null; |
|
221 |
Statement s = null; |
|
222 |
try { |
|
223 |
s = c.createStatement(); |
|
224 |
String[] sqlKommandos = sqlScript.split(";"); |
|
225 |
for (String sqlKommando : sqlKommandos) { |
|
226 |
s.addBatch(sqlKommando); |
|
227 |
} |
|
228 |
ergebnisse = s.executeBatch(); |
|
229 |
} catch(SQLException ex) { |
|
230 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
231 |
} finally { |
|
232 |
pm.closeStatementFinally(s); |
|
233 |
} |
|
234 |
return ergebnisse; |
|
235 |
} |
|
236 |
|
|
237 |
} |