| 1 |
|
package org.apache.torque.task; |
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
import java.io.BufferedOutputStream; |
| 23 |
|
import java.io.BufferedReader; |
| 24 |
|
import java.io.File; |
| 25 |
|
import java.io.FileInputStream; |
| 26 |
|
import java.io.FileOutputStream; |
| 27 |
|
import java.io.FileReader; |
| 28 |
|
import java.io.IOException; |
| 29 |
|
import java.io.InputStreamReader; |
| 30 |
|
import java.io.PrintStream; |
| 31 |
|
import java.io.Reader; |
| 32 |
|
import java.io.StringReader; |
| 33 |
|
import java.sql.Connection; |
| 34 |
|
import java.sql.DatabaseMetaData; |
| 35 |
|
import java.sql.Driver; |
| 36 |
|
import java.sql.ResultSet; |
| 37 |
|
import java.sql.ResultSetMetaData; |
| 38 |
|
import java.sql.SQLException; |
| 39 |
|
import java.sql.SQLWarning; |
| 40 |
|
import java.sql.Statement; |
| 41 |
|
import java.util.ArrayList; |
| 42 |
|
import java.util.HashMap; |
| 43 |
|
import java.util.Iterator; |
| 44 |
|
import java.util.List; |
| 45 |
|
import java.util.Map; |
| 46 |
|
import java.util.Properties; |
| 47 |
|
|
| 48 |
|
import org.apache.commons.lang.StringUtils; |
| 49 |
|
import org.apache.tools.ant.AntClassLoader; |
| 50 |
|
import org.apache.tools.ant.BuildException; |
| 51 |
|
import org.apache.tools.ant.Project; |
| 52 |
|
import org.apache.tools.ant.PropertyHelper; |
| 53 |
|
import org.apache.tools.ant.Task; |
| 54 |
|
import org.apache.tools.ant.types.EnumeratedAttribute; |
| 55 |
|
import org.apache.tools.ant.types.Path; |
| 56 |
|
import org.apache.tools.ant.types.Reference; |
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
@author |
| 63 |
|
@author |
| 64 |
|
@author |
| 65 |
|
@author |
| 66 |
|
@author |
| 67 |
|
@version |
| 68 |
|
|
|
|
|
| 0% |
Uncovered Elements: 333 (333) |
Complexity: 103 |
Complexity Density: 0.49 |
|
| 69 |
|
public class TorqueSQLExec extends Task { |
| 70 |
|
private int goodSql = 0; |
| 71 |
|
private int totalSql = 0; |
| 72 |
|
private Path classpath; |
| 73 |
|
private AntClassLoader loader; |
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 1 |
|
| 78 |
|
public static class DelimiterType extends EnumeratedAttribute { |
| 79 |
|
public static final String NORMAL = "normal"; |
| 80 |
|
public static final String ROW = "row"; |
| 81 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 82 |
0
|
public String[] getValues() {... |
| 83 |
0
|
return new String[] { NORMAL, ROW }; |
| 84 |
|
} |
| 85 |
|
} |
| 86 |
|
|
| 87 |
|
|
| 88 |
|
private Connection conn = null; |
| 89 |
|
|
| 90 |
|
|
| 91 |
|
private boolean autocommit = false; |
| 92 |
|
|
| 93 |
|
|
| 94 |
|
private Statement statement = null; |
| 95 |
|
|
| 96 |
|
|
| 97 |
|
private String driver = null; |
| 98 |
|
|
| 99 |
|
|
| 100 |
|
private String url = null; |
| 101 |
|
|
| 102 |
|
|
| 103 |
|
private String userId = null; |
| 104 |
|
|
| 105 |
|
|
| 106 |
|
private String password = null; |
| 107 |
|
|
| 108 |
|
|
| 109 |
|
private String delimiter = ";"; |
| 110 |
|
|
| 111 |
|
|
| 112 |
|
|
| 113 |
|
|
| 114 |
|
private String delimiterType = DelimiterType.NORMAL; |
| 115 |
|
|
| 116 |
|
|
| 117 |
|
private boolean print = false; |
| 118 |
|
|
| 119 |
|
|
| 120 |
|
private boolean showheaders = true; |
| 121 |
|
|
| 122 |
|
|
| 123 |
|
private File output = null; |
| 124 |
|
|
| 125 |
|
|
| 126 |
|
private String rdbms = null; |
| 127 |
|
|
| 128 |
|
|
| 129 |
|
private String version = null; |
| 130 |
|
|
| 131 |
|
|
| 132 |
|
private String onError = "abort"; |
| 133 |
|
|
| 134 |
|
|
| 135 |
|
private String encoding = null; |
| 136 |
|
|
| 137 |
|
|
| 138 |
|
private String srcDir; |
| 139 |
|
|
| 140 |
|
|
| 141 |
|
private File sqldbmap; |
| 142 |
|
|
| 143 |
|
|
| 144 |
|
|
| 145 |
|
|
| 146 |
|
@param |
| 147 |
|
|
| 148 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 149 |
0
|
public void setSqlDbMap(String sqldbmap) {... |
| 150 |
0
|
this.sqldbmap = getProject().resolveFile(sqldbmap); |
| 151 |
|
} |
| 152 |
|
|
| 153 |
|
|
| 154 |
|
|
| 155 |
|
|
| 156 |
|
@return |
| 157 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 158 |
0
|
public File getSqlDbMap() {... |
| 159 |
0
|
return sqldbmap; |
| 160 |
|
} |
| 161 |
|
|
| 162 |
|
|
| 163 |
|
|
| 164 |
|
|
| 165 |
|
@param |
| 166 |
|
|
| 167 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 168 |
0
|
public void setSrcDir(String srcDir) {... |
| 169 |
0
|
this.srcDir = getProject().resolveFile(srcDir).toString(); |
| 170 |
|
} |
| 171 |
|
|
| 172 |
|
|
| 173 |
|
|
| 174 |
|
|
| 175 |
|
@return |
| 176 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 177 |
0
|
public String getSrcDir() {... |
| 178 |
0
|
return srcDir; |
| 179 |
|
} |
| 180 |
|
|
| 181 |
|
|
| 182 |
|
|
| 183 |
|
|
| 184 |
|
@param |
| 185 |
|
|
| 186 |
|
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 187 |
0
|
public void setClasspath(Path classpath) {... |
| 188 |
0
|
if (this.classpath == null) { |
| 189 |
0
|
this.classpath = classpath; |
| 190 |
|
} else { |
| 191 |
0
|
this.classpath.append(classpath); |
| 192 |
|
} |
| 193 |
|
} |
| 194 |
|
|
| 195 |
|
|
| 196 |
|
|
| 197 |
|
|
| 198 |
|
@return |
| 199 |
|
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 200 |
0
|
public Path createClasspath() {... |
| 201 |
0
|
if (this.classpath == null) { |
| 202 |
0
|
this.classpath = new Path(getProject()); |
| 203 |
|
} |
| 204 |
0
|
return this.classpath.createPath(); |
| 205 |
|
} |
| 206 |
|
|
| 207 |
|
|
| 208 |
|
|
| 209 |
|
|
| 210 |
|
@param |
| 211 |
|
|
| 212 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 213 |
0
|
public void setClasspathRef(Reference r) {... |
| 214 |
0
|
createClasspath().setRefid(r); |
| 215 |
|
} |
| 216 |
|
|
| 217 |
|
|
| 218 |
|
|
| 219 |
|
|
| 220 |
|
@param |
| 221 |
|
|
| 222 |
|
@deprecated |
| 223 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 224 |
0
|
public void addText(String sql) {... |
| 225 |
|
} |
| 226 |
|
|
| 227 |
|
|
| 228 |
|
|
| 229 |
|
|
| 230 |
|
@param |
| 231 |
|
|
| 232 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 233 |
0
|
public void setDriver(String driver) {... |
| 234 |
0
|
this.driver = driver; |
| 235 |
|
} |
| 236 |
|
|
| 237 |
|
|
| 238 |
|
|
| 239 |
|
|
| 240 |
|
@param |
| 241 |
|
|
| 242 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 243 |
0
|
public void setUrl(String url) {... |
| 244 |
0
|
this.url = url; |
| 245 |
|
} |
| 246 |
|
|
| 247 |
|
|
| 248 |
|
|
| 249 |
|
|
| 250 |
|
@param |
| 251 |
|
|
| 252 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 253 |
0
|
public void setUserid(String userId) {... |
| 254 |
0
|
this.userId = userId; |
| 255 |
|
} |
| 256 |
|
|
| 257 |
|
|
| 258 |
|
|
| 259 |
|
|
| 260 |
|
@param |
| 261 |
|
|
| 262 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 263 |
0
|
public void setEncoding(String encoding) {... |
| 264 |
0
|
this.encoding = encoding; |
| 265 |
|
} |
| 266 |
|
|
| 267 |
|
|
| 268 |
|
|
| 269 |
|
|
| 270 |
|
@param |
| 271 |
|
|
| 272 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 273 |
0
|
public void setPassword(String password) {... |
| 274 |
0
|
this.password = password; |
| 275 |
|
} |
| 276 |
|
|
| 277 |
|
|
| 278 |
|
|
| 279 |
|
|
| 280 |
|
@param |
| 281 |
|
|
| 282 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 283 |
0
|
public void setAutocommit(boolean autocommit) {... |
| 284 |
0
|
this.autocommit = autocommit; |
| 285 |
|
} |
| 286 |
|
|
| 287 |
|
|
| 288 |
|
|
| 289 |
|
|
| 290 |
|
|
| 291 |
|
|
| 292 |
|
|
| 293 |
|
|
| 294 |
|
@param |
| 295 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 296 |
0
|
public void setDelimiter(String delimiter) {... |
| 297 |
0
|
this.delimiter = delimiter; |
| 298 |
|
} |
| 299 |
|
|
| 300 |
|
|
| 301 |
|
|
| 302 |
|
|
| 303 |
|
|
| 304 |
|
|
| 305 |
|
@param |
| 306 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 307 |
0
|
public void setDelimiterType(DelimiterType delimiterType) {... |
| 308 |
0
|
this.delimiterType = delimiterType.getValue(); |
| 309 |
|
} |
| 310 |
|
|
| 311 |
|
|
| 312 |
|
|
| 313 |
|
|
| 314 |
|
@param |
| 315 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 316 |
0
|
public void setPrint(boolean print) {... |
| 317 |
0
|
this.print = print; |
| 318 |
|
} |
| 319 |
|
|
| 320 |
|
|
| 321 |
|
|
| 322 |
|
|
| 323 |
|
@param |
| 324 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 325 |
0
|
public void setShowheaders(boolean showheaders) {... |
| 326 |
0
|
this.showheaders = showheaders; |
| 327 |
|
} |
| 328 |
|
|
| 329 |
|
|
| 330 |
|
|
| 331 |
|
|
| 332 |
|
@param |
| 333 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 334 |
0
|
public void setOutput(File output) {... |
| 335 |
0
|
this.output = output; |
| 336 |
|
} |
| 337 |
|
|
| 338 |
|
|
| 339 |
|
|
| 340 |
|
|
| 341 |
|
@param |
| 342 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 343 |
0
|
public void setRdbms(String vendor) {... |
| 344 |
0
|
this.rdbms = vendor.toLowerCase(); |
| 345 |
|
} |
| 346 |
|
|
| 347 |
|
|
| 348 |
|
|
| 349 |
|
|
| 350 |
|
@param |
| 351 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 352 |
0
|
public void setVersion(String version) {... |
| 353 |
0
|
this.version = version.toLowerCase(); |
| 354 |
|
} |
| 355 |
|
|
| 356 |
|
|
| 357 |
|
|
| 358 |
|
|
| 359 |
|
@param |
| 360 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 361 |
0
|
public void setOnerror(OnError action) {... |
| 362 |
0
|
this.onError = action.getValue(); |
| 363 |
|
} |
| 364 |
|
|
| 365 |
|
|
| 366 |
|
|
| 367 |
|
|
| 368 |
|
@throws |
| 369 |
|
|
|
|
|
| 0% |
Uncovered Elements: 64 (64) |
Complexity: 14 |
Complexity Density: 0.33 |
|
| 370 |
0
|
@SuppressWarnings("unchecked")... |
| 371 |
|
public void execute() throws BuildException { |
| 372 |
0
|
if (sqldbmap == null || getSqlDbMap().exists() == false) { |
| 373 |
0
|
throw new BuildException("You haven't provided an sqldbmap, or " + "the one you specified doesn't exist: " + sqldbmap); |
| 374 |
|
} |
| 375 |
|
|
| 376 |
0
|
if (driver == null) { |
| 377 |
0
|
throw new BuildException("Driver attribute must be set!", getLocation()); |
| 378 |
|
} |
| 379 |
0
|
if (userId == null) { |
| 380 |
0
|
throw new BuildException("User Id attribute must be set!", getLocation()); |
| 381 |
|
} |
| 382 |
0
|
if (password == null) { |
| 383 |
0
|
throw new BuildException("Password attribute must be set!", getLocation()); |
| 384 |
|
} |
| 385 |
0
|
if (url == null) { |
| 386 |
0
|
throw new BuildException("Url attribute must be set!", getLocation()); |
| 387 |
|
} |
| 388 |
|
|
| 389 |
0
|
Properties map = new Properties(); |
| 390 |
|
|
| 391 |
0
|
try { |
| 392 |
0
|
FileInputStream fis = new FileInputStream(getSqlDbMap()); |
| 393 |
0
|
map.load(fis); |
| 394 |
0
|
fis.close(); |
| 395 |
|
} catch (IOException ioe) { |
| 396 |
0
|
throw new BuildException("Cannot open and process the sqldbmap!"); |
| 397 |
|
} |
| 398 |
|
|
| 399 |
0
|
Map<Object, Object> databases = new HashMap<Object, Object>(); |
| 400 |
|
|
| 401 |
0
|
Iterator<?> eachFileName = map.keySet().iterator(); |
| 402 |
0
|
while (eachFileName.hasNext()) { |
| 403 |
0
|
String sqlfile = (String) eachFileName.next(); |
| 404 |
0
|
String database = map.getProperty(sqlfile); |
| 405 |
|
|
| 406 |
0
|
List<Object> files = (List<Object>) databases.get(database); |
| 407 |
|
|
| 408 |
0
|
if (files == null) { |
| 409 |
0
|
files = new ArrayList<Object>(); |
| 410 |
0
|
databases.put(database, files); |
| 411 |
|
} |
| 412 |
|
|
| 413 |
|
|
| 414 |
|
|
| 415 |
0
|
if (sqlfile.indexOf("schema.sql") != -1) { |
| 416 |
0
|
files.add(0, sqlfile); |
| 417 |
|
} else { |
| 418 |
0
|
files.add(sqlfile); |
| 419 |
|
} |
| 420 |
|
} |
| 421 |
|
|
| 422 |
0
|
Iterator<?> eachDatabase = databases.keySet().iterator(); |
| 423 |
0
|
while (eachDatabase.hasNext()) { |
| 424 |
0
|
String db = (String) eachDatabase.next(); |
| 425 |
0
|
List<Object> transactions = new ArrayList<Object>(); |
| 426 |
0
|
eachFileName = ((List<?>) databases.get(db)).iterator(); |
| 427 |
0
|
while (eachFileName.hasNext()) { |
| 428 |
0
|
String fileName = (String) eachFileName.next(); |
| 429 |
0
|
File file = new File(srcDir, fileName); |
| 430 |
|
|
| 431 |
0
|
if (file.exists()) { |
| 432 |
0
|
Transaction transaction = new Transaction(); |
| 433 |
0
|
transaction.setSrc(file); |
| 434 |
0
|
transactions.add(transaction); |
| 435 |
|
} else { |
| 436 |
0
|
System.out.println("File '" + file.getAbsolutePath() + "' in sqldbmap does not exist, so skipping it."); |
| 437 |
|
} |
| 438 |
|
} |
| 439 |
|
|
| 440 |
0
|
insertDatabaseSqlFiles(url, db, transactions); |
| 441 |
|
} |
| 442 |
|
} |
| 443 |
|
|
| 444 |
|
|
| 445 |
|
|
| 446 |
|
|
| 447 |
|
@param |
| 448 |
|
@param |
| 449 |
|
@param |
| 450 |
|
|
|
|
|
| 0% |
Uncovered Elements: 76 (76) |
Complexity: 25 |
Complexity Density: 0.46 |
|
| 451 |
0
|
private void insertDatabaseSqlFiles(String url, String database, List<?> transactions) {... |
| 452 |
0
|
url = StringUtils.replace(url, "@DB@", database); |
| 453 |
0
|
System.out.println("Our new url -> " + url); |
| 454 |
|
|
| 455 |
0
|
Driver driverInstance = null; |
| 456 |
0
|
try { |
| 457 |
0
|
Class<?> dc; |
| 458 |
0
|
if (classpath != null) { |
| 459 |
0
|
log("Loading " + driver + " using AntClassLoader with classpath " + classpath, Project.MSG_VERBOSE); |
| 460 |
|
|
| 461 |
0
|
loader = new AntClassLoader(getProject(), classpath); |
| 462 |
0
|
dc = loader.loadClass(driver); |
| 463 |
|
} else { |
| 464 |
0
|
log("Loading " + driver + " using system loader.", Project.MSG_VERBOSE); |
| 465 |
0
|
dc = Class.forName(driver); |
| 466 |
|
} |
| 467 |
0
|
driverInstance = (Driver) dc.newInstance(); |
| 468 |
|
} catch (ClassNotFoundException e) { |
| 469 |
0
|
throw new BuildException("Class Not Found: JDBC driver " + driver + " could not be loaded", getLocation()); |
| 470 |
|
} catch (IllegalAccessException e) { |
| 471 |
0
|
throw new BuildException("Illegal Access: JDBC driver " + driver + " could not be loaded", getLocation()); |
| 472 |
|
} catch (InstantiationException e) { |
| 473 |
0
|
throw new BuildException("Instantiation Exception: JDBC driver " + driver + " could not be loaded", getLocation()); |
| 474 |
|
} |
| 475 |
|
|
| 476 |
0
|
try { |
| 477 |
0
|
log("connecting to " + url, Project.MSG_VERBOSE); |
| 478 |
0
|
Properties info = new Properties(); |
| 479 |
0
|
info.put("user", userId); |
| 480 |
0
|
info.put("password", password); |
| 481 |
0
|
conn = driverInstance.connect(url, info); |
| 482 |
|
|
| 483 |
0
|
if (conn == null) { |
| 484 |
|
|
| 485 |
0
|
throw new SQLException("No suitable Driver for " + url); |
| 486 |
|
} |
| 487 |
|
|
| 488 |
0
|
if (!isValidRdbms(conn)) { |
| 489 |
0
|
return; |
| 490 |
|
} |
| 491 |
|
|
| 492 |
0
|
conn.setAutoCommit(autocommit); |
| 493 |
0
|
statement = conn.createStatement(); |
| 494 |
0
|
PrintStream out = System.out; |
| 495 |
0
|
try { |
| 496 |
0
|
if (output != null) { |
| 497 |
0
|
log("Opening PrintStream to output file " + output, Project.MSG_VERBOSE); |
| 498 |
0
|
out = new PrintStream(new BufferedOutputStream(new FileOutputStream(output))); |
| 499 |
|
} |
| 500 |
|
|
| 501 |
|
|
| 502 |
0
|
for (Iterator<?> it = transactions.iterator(); it.hasNext();) { |
| 503 |
0
|
Transaction transaction = (Transaction) it.next(); |
| 504 |
0
|
transaction.runTransaction(out); |
| 505 |
0
|
if (!autocommit) { |
| 506 |
0
|
log("Commiting transaction", Project.MSG_VERBOSE); |
| 507 |
0
|
conn.commit(); |
| 508 |
|
} |
| 509 |
|
} |
| 510 |
|
} finally { |
| 511 |
0
|
if (out != null && out != System.out) { |
| 512 |
0
|
out.close(); |
| 513 |
|
} |
| 514 |
|
} |
| 515 |
|
} catch (IOException e) { |
| 516 |
0
|
if (!autocommit && conn != null && onError.equals("abort")) { |
| 517 |
0
|
try { |
| 518 |
0
|
conn.rollback(); |
| 519 |
|
} catch (SQLException ex) { |
| 520 |
|
|
| 521 |
|
} |
| 522 |
|
} |
| 523 |
0
|
throw new BuildException(e, getLocation()); |
| 524 |
|
} catch (SQLException e) { |
| 525 |
0
|
if (!autocommit && conn != null && onError.equals("abort")) { |
| 526 |
0
|
try { |
| 527 |
0
|
conn.rollback(); |
| 528 |
|
} catch (SQLException ex) { |
| 529 |
|
|
| 530 |
|
} |
| 531 |
|
} |
| 532 |
0
|
throw new BuildException(e, getLocation()); |
| 533 |
|
} finally { |
| 534 |
0
|
try { |
| 535 |
0
|
if (statement != null) { |
| 536 |
0
|
statement.close(); |
| 537 |
|
} |
| 538 |
0
|
if (conn != null) { |
| 539 |
0
|
conn.close(); |
| 540 |
|
} |
| 541 |
|
} catch (SQLException e) { |
| 542 |
|
} |
| 543 |
|
} |
| 544 |
|
|
| 545 |
0
|
System.out.println(goodSql + " of " + totalSql + " SQL statements executed successfully"); |
| 546 |
|
} |
| 547 |
|
|
| 548 |
|
|
| 549 |
|
|
| 550 |
|
|
| 551 |
|
@param |
| 552 |
|
@param |
| 553 |
|
@throws |
| 554 |
|
@throws |
| 555 |
|
|
|
|
|
| 0% |
Uncovered Elements: 35 (35) |
Complexity: 13 |
Complexity Density: 0.57 |
|
| 556 |
0
|
protected void runStatements(Reader reader, PrintStream out) throws SQLException, IOException {... |
| 557 |
0
|
String sql = ""; |
| 558 |
0
|
String line = ""; |
| 559 |
|
|
| 560 |
0
|
BufferedReader in = new BufferedReader(reader); |
| 561 |
0
|
PropertyHelper ph = PropertyHelper.getPropertyHelper(getProject()); |
| 562 |
|
|
| 563 |
0
|
try { |
| 564 |
0
|
while ((line = in.readLine()) != null) { |
| 565 |
0
|
line = line.trim(); |
| 566 |
0
|
line = ph.replaceProperties("", line, getProject().getProperties()); |
| 567 |
0
|
if (line.startsWith("//") || line.startsWith("--")) { |
| 568 |
0
|
continue; |
| 569 |
|
} |
| 570 |
0
|
if (line.length() > 4 && line.substring(0, 4).equalsIgnoreCase("REM ")) { |
| 571 |
0
|
continue; |
| 572 |
|
} |
| 573 |
|
|
| 574 |
0
|
sql += " " + line; |
| 575 |
0
|
sql = sql.trim(); |
| 576 |
|
|
| 577 |
|
|
| 578 |
|
|
| 579 |
|
|
| 580 |
0
|
if (line.indexOf("--") >= 0) { |
| 581 |
0
|
sql += "\n"; |
| 582 |
|
} |
| 583 |
|
|
| 584 |
0
|
if (delimiterType.equals(DelimiterType.NORMAL) && sql.endsWith(delimiter) || delimiterType.equals(DelimiterType.ROW) && line.equals(delimiter)) { |
| 585 |
0
|
log("SQL: " + sql, Project.MSG_VERBOSE); |
| 586 |
0
|
execSQL(sql.substring(0, sql.length() - delimiter.length()), out); |
| 587 |
0
|
sql = ""; |
| 588 |
|
} |
| 589 |
|
} |
| 590 |
|
|
| 591 |
|
|
| 592 |
0
|
if (!sql.equals("")) { |
| 593 |
0
|
execSQL(sql, out); |
| 594 |
|
} |
| 595 |
|
} catch (SQLException e) { |
| 596 |
0
|
throw e; |
| 597 |
|
} |
| 598 |
|
} |
| 599 |
|
|
| 600 |
|
|
| 601 |
|
|
| 602 |
|
|
| 603 |
|
@param |
| 604 |
|
|
|
|
|
| 0% |
Uncovered Elements: 29 (29) |
Complexity: 11 |
Complexity Density: 0.58 |
|
| 605 |
0
|
protected boolean isValidRdbms(Connection conn) {... |
| 606 |
0
|
if (rdbms == null && version == null) { |
| 607 |
0
|
return true; |
| 608 |
|
} |
| 609 |
|
|
| 610 |
0
|
try { |
| 611 |
0
|
DatabaseMetaData dmd = conn.getMetaData(); |
| 612 |
|
|
| 613 |
0
|
if (rdbms != null) { |
| 614 |
0
|
String theVendor = dmd.getDatabaseProductName().toLowerCase(); |
| 615 |
|
|
| 616 |
0
|
log("RDBMS = " + theVendor, Project.MSG_VERBOSE); |
| 617 |
0
|
if (theVendor == null || theVendor.indexOf(rdbms) < 0) { |
| 618 |
0
|
log("Not the required RDBMS: " + rdbms, Project.MSG_VERBOSE); |
| 619 |
0
|
return false; |
| 620 |
|
} |
| 621 |
|
} |
| 622 |
|
|
| 623 |
0
|
if (version != null) { |
| 624 |
0
|
String theVersion = dmd.getDatabaseProductVersion().toLowerCase(); |
| 625 |
|
|
| 626 |
0
|
log("Version = " + theVersion, Project.MSG_VERBOSE); |
| 627 |
0
|
if (theVersion == null || !(theVersion.startsWith(version) || theVersion.indexOf(" " + version) >= 0)) { |
| 628 |
0
|
log("Not the required version: \"" + version + "\"", Project.MSG_VERBOSE); |
| 629 |
0
|
return false; |
| 630 |
|
} |
| 631 |
|
} |
| 632 |
|
} catch (SQLException e) { |
| 633 |
|
|
| 634 |
0
|
log("Failed to obtain required RDBMS information", Project.MSG_ERR); |
| 635 |
0
|
return false; |
| 636 |
|
} |
| 637 |
|
|
| 638 |
0
|
return true; |
| 639 |
|
} |
| 640 |
|
|
| 641 |
|
|
| 642 |
|
|
| 643 |
|
|
| 644 |
|
@param |
| 645 |
|
@param |
| 646 |
|
@throws |
| 647 |
|
|
|
|
|
| 0% |
Uncovered Elements: 28 (28) |
Complexity: 7 |
Complexity Density: 0.39 |
|
| 648 |
0
|
protected void execSQL(String sql, PrintStream out) throws SQLException {... |
| 649 |
|
|
| 650 |
0
|
if ("".equals(sql.trim())) { |
| 651 |
0
|
return; |
| 652 |
|
} |
| 653 |
|
|
| 654 |
0
|
try { |
| 655 |
0
|
totalSql++; |
| 656 |
0
|
if (!statement.execute(sql)) { |
| 657 |
0
|
log(statement.getUpdateCount() + " rows affected", Project.MSG_VERBOSE); |
| 658 |
|
} else { |
| 659 |
0
|
if (print) { |
| 660 |
0
|
printResults(out); |
| 661 |
|
} |
| 662 |
|
} |
| 663 |
|
|
| 664 |
0
|
SQLWarning warning = conn.getWarnings(); |
| 665 |
0
|
while (warning != null) { |
| 666 |
0
|
log(warning + " sql warning", Project.MSG_VERBOSE); |
| 667 |
0
|
warning = warning.getNextWarning(); |
| 668 |
|
} |
| 669 |
0
|
conn.clearWarnings(); |
| 670 |
0
|
goodSql++; |
| 671 |
|
} catch (SQLException e) { |
| 672 |
0
|
System.out.println("Failed to execute: " + sql); |
| 673 |
0
|
if (!onError.equals("continue")) { |
| 674 |
0
|
throw e; |
| 675 |
|
} |
| 676 |
0
|
log(e.toString(), Project.MSG_ERR); |
| 677 |
|
} |
| 678 |
|
} |
| 679 |
|
|
| 680 |
|
|
| 681 |
|
|
| 682 |
|
|
| 683 |
|
@param |
| 684 |
|
@throws |
| 685 |
|
|
|
|
|
| 0% |
Uncovered Elements: 44 (44) |
Complexity: 9 |
Complexity Density: 0.32 |
|
| 686 |
0
|
protected void printResults(PrintStream out) throws java.sql.SQLException {... |
| 687 |
0
|
ResultSet rs = null; |
| 688 |
0
|
do { |
| 689 |
0
|
rs = statement.getResultSet(); |
| 690 |
0
|
if (rs != null) { |
| 691 |
0
|
log("Processing new result set.", Project.MSG_VERBOSE); |
| 692 |
0
|
ResultSetMetaData md = rs.getMetaData(); |
| 693 |
0
|
int columnCount = md.getColumnCount(); |
| 694 |
0
|
StringBuffer line = new StringBuffer(); |
| 695 |
0
|
if (showheaders) { |
| 696 |
0
|
for (int col = 1; col < columnCount; col++) { |
| 697 |
0
|
line.append(md.getColumnName(col)); |
| 698 |
0
|
line.append(","); |
| 699 |
|
} |
| 700 |
0
|
line.append(md.getColumnName(columnCount)); |
| 701 |
0
|
out.println(line); |
| 702 |
0
|
line.setLength(0); |
| 703 |
|
} |
| 704 |
0
|
while (rs.next()) { |
| 705 |
0
|
boolean first = true; |
| 706 |
0
|
for (int col = 1; col <= columnCount; col++) { |
| 707 |
0
|
String columnValue = rs.getString(col); |
| 708 |
0
|
if (columnValue != null) { |
| 709 |
0
|
columnValue = columnValue.trim(); |
| 710 |
|
} |
| 711 |
|
|
| 712 |
0
|
if (first) { |
| 713 |
0
|
first = false; |
| 714 |
|
} else { |
| 715 |
0
|
line.append(","); |
| 716 |
|
} |
| 717 |
0
|
line.append(columnValue); |
| 718 |
|
} |
| 719 |
0
|
out.println(line); |
| 720 |
0
|
line.setLength(0); |
| 721 |
|
} |
| 722 |
|
} |
| 723 |
0
|
} while (statement.getMoreResults()); |
| 724 |
0
|
out.println(); |
| 725 |
|
} |
| 726 |
|
|
| 727 |
|
|
| 728 |
|
|
| 729 |
|
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 1 |
|
| 730 |
|
public static class OnError extends EnumeratedAttribute { |
| 731 |
|
public static final String CONTINUE = "continue"; |
| 732 |
|
|
| 733 |
|
public static final String STOP = "stop"; |
| 734 |
|
|
| 735 |
|
public static final String ABORT = "abort"; |
| 736 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 737 |
0
|
public String[] getValues() {... |
| 738 |
0
|
return new String[] { CONTINUE, STOP, ABORT }; |
| 739 |
|
} |
| 740 |
|
} |
| 741 |
|
|
| 742 |
|
|
| 743 |
|
|
| 744 |
|
|
| 745 |
|
|
|
|
|
| 0% |
Uncovered Elements: 19 (19) |
Complexity: 6 |
Complexity Density: 0.6 |
|
| 746 |
|
public class Transaction { |
| 747 |
|
private File tSrcFile = null; |
| 748 |
|
private String tSqlCommand = ""; |
| 749 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 750 |
0
|
public void setSrc(File src) {... |
| 751 |
0
|
this.tSrcFile = src; |
| 752 |
|
} |
| 753 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 754 |
0
|
public void addText(String sql) {... |
| 755 |
0
|
this.tSqlCommand += sql; |
| 756 |
|
} |
| 757 |
|
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 4 |
Complexity Density: 0.5 |
|
| 758 |
0
|
private void runTransaction(PrintStream out) throws IOException, SQLException {... |
| 759 |
0
|
if (tSqlCommand.length() != 0) { |
| 760 |
0
|
log("Executing commands", Project.MSG_INFO); |
| 761 |
0
|
runStatements(new StringReader(tSqlCommand), out); |
| 762 |
|
} |
| 763 |
|
|
| 764 |
0
|
if (tSrcFile != null) { |
| 765 |
0
|
System.out.println("Executing file: " + tSrcFile.getAbsolutePath()); |
| 766 |
0
|
Reader reader = (encoding == null) ? new FileReader(tSrcFile) : new InputStreamReader(new FileInputStream(tSrcFile), encoding); |
| 767 |
0
|
runStatements(reader, out); |
| 768 |
0
|
reader.close(); |
| 769 |
|
} |
| 770 |
|
} |
| 771 |
|
} |
| 772 |
|
} |