1 | |
package org.codehaus.mojo.sql; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
import java.io.BufferedOutputStream; |
17 | |
import java.io.BufferedReader; |
18 | |
import java.io.File; |
19 | |
import java.io.FileInputStream; |
20 | |
import java.io.FileOutputStream; |
21 | |
import java.io.FileReader; |
22 | |
import java.io.IOException; |
23 | |
import java.io.InputStream; |
24 | |
import java.io.InputStreamReader; |
25 | |
import java.io.OutputStream; |
26 | |
import java.io.PrintStream; |
27 | |
import java.io.Reader; |
28 | |
import java.io.StringReader; |
29 | |
import java.sql.Connection; |
30 | |
import java.sql.Driver; |
31 | |
import java.sql.ResultSet; |
32 | |
import java.sql.ResultSetMetaData; |
33 | |
import java.sql.SQLException; |
34 | |
import java.sql.SQLWarning; |
35 | |
import java.sql.Statement; |
36 | |
import java.util.ArrayList; |
37 | |
import java.util.Collections; |
38 | |
import java.util.Enumeration; |
39 | |
import java.util.List; |
40 | |
import java.util.Properties; |
41 | |
import java.util.StringTokenizer; |
42 | |
import java.util.Vector; |
43 | |
|
44 | |
import org.apache.commons.io.IOUtils; |
45 | |
import org.apache.commons.lang.StringEscapeUtils; |
46 | |
import org.apache.maven.execution.MavenSession; |
47 | |
import org.apache.maven.plugin.AbstractMojo; |
48 | |
import org.apache.maven.plugin.MojoExecutionException; |
49 | |
import org.apache.maven.project.MavenProject; |
50 | |
import org.apache.maven.settings.Server; |
51 | |
import org.apache.maven.settings.Settings; |
52 | |
import org.apache.maven.shared.filtering.MavenFileFilter; |
53 | |
import org.apache.maven.shared.filtering.MavenFileFilterRequest; |
54 | |
import org.apache.maven.shared.filtering.MavenFilteringException; |
55 | |
import org.codehaus.plexus.util.FileUtils; |
56 | |
import org.codehaus.plexus.util.IOUtil; |
57 | |
import org.codehaus.plexus.util.StringUtils; |
58 | |
import org.springframework.core.io.DefaultResourceLoader; |
59 | |
import org.springframework.core.io.Resource; |
60 | |
import org.springframework.core.io.ResourceLoader; |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | 0 | public class SqlExecMojo extends AbstractMojo { |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
public static final String ON_ERROR_ABORT = "abort"; |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
public static final String ON_ERROR_ABORT_AFTER = "abortAfter"; |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
public static final String ON_ERROR_CONTINUE = "continue"; |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
private String username; |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
private String password; |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
private boolean enableAnonymousPassword; |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
private String driverProperties; |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
private Settings settings; |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
private String settingsKey; |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
private boolean skipOnConnectionError; |
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
private boolean forceMojoExecution; |
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
protected MavenProject project; |
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
private MavenSession mavenSession; |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | 0 | private String sqlCommand = ""; |
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
private File[] srcFiles; |
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
private String[] resourceLocations; |
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
private Fileset fileset; |
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
private boolean skip; |
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
private String url; |
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
private String driver; |
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
private boolean autocommit; |
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | 0 | private String onError = ON_ERROR_ABORT; |
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | 0 | private String delimiter = ";"; |
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | 0 | private String delimiterType = DelimiterType.NORMAL; |
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
private Order orderFile; |
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | 0 | @Deprecated |
296 | |
private boolean enableBlockMode = false; |
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | 0 | private boolean keepFormat = false; |
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | 0 | private boolean printResultSet = false; |
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | 0 | private boolean showheaders = true; |
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
private File outputFile; |
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
private String outputDelimiter; |
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | 0 | private String encoding = ""; |
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | 0 | private boolean append = false; |
346 | |
|
347 | |
|
348 | |
|
349 | |
|
350 | |
|
351 | |
|
352 | |
|
353 | |
|
354 | 0 | private boolean escapeProcessing = true; |
355 | |
|
356 | |
|
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | 0 | private int successfulStatements = 0; |
363 | |
|
364 | |
|
365 | |
|
366 | |
|
367 | 0 | private int totalStatements = 0; |
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | 0 | private Connection conn = null; |
373 | |
|
374 | |
|
375 | |
|
376 | |
|
377 | 0 | private Statement statement = null; |
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | 0 | private Vector<Transaction> transactions = new Vector<Transaction>(); |
383 | |
|
384 | |
|
385 | |
|
386 | |
|
387 | |
|
388 | |
private MavenFileFilter fileFilter; |
389 | |
|
390 | |
|
391 | |
|
392 | |
|
393 | |
|
394 | |
|
395 | |
|
396 | |
private boolean enableFiltering; |
397 | |
|
398 | |
|
399 | |
|
400 | |
|
401 | |
|
402 | |
|
403 | |
|
404 | |
|
405 | |
private boolean executeTrailingSQL; |
406 | |
|
407 | |
|
408 | |
|
409 | |
|
410 | |
|
411 | |
|
412 | |
|
413 | |
|
414 | |
|
415 | |
|
416 | |
|
417 | |
|
418 | |
public Transaction createTransaction() { |
419 | 0 | Transaction t = new Transaction(); |
420 | 0 | transactions.addElement(t); |
421 | 0 | return t; |
422 | |
} |
423 | |
|
424 | |
|
425 | |
|
426 | |
|
427 | |
|
428 | |
|
429 | |
|
430 | |
public void addText(String sql) { |
431 | 0 | this.sqlCommand += sql; |
432 | 0 | } |
433 | |
|
434 | |
|
435 | |
|
436 | |
|
437 | |
|
438 | |
|
439 | |
|
440 | |
public void setEncoding(String encoding) { |
441 | 0 | this.encoding = encoding; |
442 | 0 | } |
443 | |
|
444 | |
|
445 | |
|
446 | |
|
447 | |
|
448 | |
|
449 | |
|
450 | |
public void setDelimiter(String delimiter) { |
451 | 0 | this.delimiter = delimiter; |
452 | 0 | } |
453 | |
|
454 | |
|
455 | |
|
456 | |
|
457 | |
|
458 | |
|
459 | |
|
460 | |
public void setDelimiterType(String delimiterType) { |
461 | 0 | this.delimiterType = delimiterType; |
462 | 0 | } |
463 | |
|
464 | |
|
465 | |
|
466 | |
|
467 | |
|
468 | |
|
469 | |
|
470 | |
|
471 | |
@Deprecated |
472 | |
public void setPrintResutlSet(boolean print) { |
473 | 0 | setPrintResultSet(print); |
474 | 0 | } |
475 | |
|
476 | |
|
477 | |
|
478 | |
|
479 | |
|
480 | |
|
481 | |
|
482 | |
public void setPrintResultSet(boolean print) { |
483 | 0 | this.printResultSet = print; |
484 | 0 | } |
485 | |
|
486 | |
|
487 | |
|
488 | |
|
489 | |
|
490 | |
|
491 | |
|
492 | |
public void setShowheaders(boolean showheaders) { |
493 | 0 | this.showheaders = showheaders; |
494 | 0 | } |
495 | |
|
496 | |
|
497 | |
|
498 | |
|
499 | |
|
500 | |
|
501 | |
|
502 | |
public void setOutputFile(File output) { |
503 | 0 | this.outputFile = output; |
504 | 0 | } |
505 | |
|
506 | |
|
507 | |
|
508 | |
|
509 | |
|
510 | |
|
511 | |
|
512 | |
public void setAppend(boolean append) { |
513 | 0 | this.append = append; |
514 | 0 | } |
515 | |
|
516 | |
|
517 | |
|
518 | |
|
519 | |
|
520 | |
|
521 | |
|
522 | |
public void setKeepFormat(boolean keepformat) { |
523 | 0 | this.keepFormat = keepformat; |
524 | 0 | } |
525 | |
|
526 | |
|
527 | |
|
528 | |
|
529 | |
|
530 | |
|
531 | |
|
532 | |
public void setEscapeProcessing(boolean enable) { |
533 | 0 | escapeProcessing = enable; |
534 | 0 | } |
535 | |
|
536 | |
|
537 | |
|
538 | |
|
539 | |
|
540 | |
|
541 | |
|
542 | |
|
543 | |
|
544 | |
|
545 | |
|
546 | |
|
547 | |
|
548 | |
|
549 | |
protected boolean skipMojo() { |
550 | 0 | if (skip) { |
551 | 0 | getLog().info("Skip sql execution"); |
552 | 0 | return true; |
553 | |
} |
554 | |
|
555 | 0 | if (!forceMojoExecution && project != null && "pom".equals(project.getPackaging())) { |
556 | 0 | getLog().info("Skipping sql execution for project with packaging type 'pom'"); |
557 | 0 | return true; |
558 | |
} |
559 | |
|
560 | 0 | return false; |
561 | |
} |
562 | |
|
563 | |
|
564 | |
|
565 | |
|
566 | |
|
567 | |
|
568 | |
@Override |
569 | |
public void execute() throws MojoExecutionException { |
570 | |
|
571 | 0 | if (skipMojo()) { |
572 | 0 | return; |
573 | |
} |
574 | |
|
575 | 0 | successfulStatements = 0; |
576 | |
|
577 | 0 | totalStatements = 0; |
578 | |
|
579 | 0 | loadUserInfoFromSettings(); |
580 | |
|
581 | 0 | addCommandToTransactions(); |
582 | |
|
583 | 0 | addFilesToTransactions(); |
584 | |
|
585 | 0 | addFileSetToTransactions(); |
586 | |
|
587 | 0 | addResourcesToTransactions(); |
588 | |
|
589 | 0 | sortTransactions(); |
590 | |
|
591 | |
try { |
592 | 0 | conn = getConnection(); |
593 | 0 | } catch (SQLException e) { |
594 | 0 | if (!this.skipOnConnectionError) { |
595 | 0 | throw new MojoExecutionException(e.getMessage(), e); |
596 | |
} else { |
597 | |
|
598 | 0 | return; |
599 | |
} |
600 | 0 | } |
601 | |
|
602 | |
try { |
603 | 0 | statement = conn.createStatement(); |
604 | 0 | statement.setEscapeProcessing(escapeProcessing); |
605 | |
|
606 | 0 | PrintStream out = System.out; |
607 | |
try { |
608 | 0 | if (outputFile != null) { |
609 | 0 | getLog().debug("Opening PrintStream to output file " + outputFile); |
610 | 0 | out = new PrintStream(new BufferedOutputStream(new FileOutputStream(outputFile.getAbsolutePath(), |
611 | |
append))); |
612 | |
} |
613 | |
|
614 | |
|
615 | 0 | for (Enumeration<Transaction> e = transactions.elements(); e.hasMoreElements();) { |
616 | 0 | Transaction t = e.nextElement(); |
617 | |
|
618 | 0 | t.runTransaction(out); |
619 | |
|
620 | 0 | if (!autocommit) { |
621 | 0 | getLog().debug("Committing transaction"); |
622 | 0 | conn.commit(); |
623 | |
} |
624 | 0 | } |
625 | |
} finally { |
626 | 0 | if (out != null && out != System.out) { |
627 | 0 | out.close(); |
628 | |
} |
629 | |
} |
630 | 0 | } catch (IOException e) { |
631 | 0 | throw new MojoExecutionException(e.getMessage(), e); |
632 | 0 | } catch (SQLException e) { |
633 | 0 | if (!autocommit && conn != null && ON_ERROR_ABORT.equalsIgnoreCase(getOnError())) { |
634 | |
try { |
635 | 0 | conn.rollback(); |
636 | 0 | } catch (SQLException ex) { |
637 | |
|
638 | 0 | } |
639 | |
} |
640 | 0 | throw new MojoExecutionException(e.getMessage(), e); |
641 | |
} finally { |
642 | 0 | try { |
643 | 0 | if (statement != null) { |
644 | 0 | statement.close(); |
645 | |
} |
646 | 0 | if (conn != null) { |
647 | 0 | conn.close(); |
648 | |
} |
649 | 0 | } catch (SQLException ex) { |
650 | |
|
651 | 0 | } |
652 | 0 | } |
653 | |
|
654 | 0 | getLog().info( |
655 | |
getSuccessfulStatements() + " of " + getTotalStatements() + " SQL statements executed successfully"); |
656 | |
|
657 | 0 | if (ON_ERROR_ABORT_AFTER.equalsIgnoreCase(getOnError()) && totalStatements != successfulStatements) { |
658 | 0 | throw new MojoExecutionException("Some SQL statements failed to execute"); |
659 | |
} |
660 | |
|
661 | 0 | } |
662 | |
|
663 | |
|
664 | |
|
665 | |
|
666 | |
|
667 | |
private void addCommandToTransactions() { |
668 | 0 | createTransaction().addText(sqlCommand.trim()); |
669 | 0 | } |
670 | |
|
671 | |
|
672 | |
|
673 | |
|
674 | |
|
675 | |
private void addFileSetToTransactions() { |
676 | |
String[] includedFiles; |
677 | 0 | if (fileset != null) { |
678 | 0 | fileset.scan(); |
679 | 0 | includedFiles = fileset.getIncludedFiles(); |
680 | |
} else { |
681 | 0 | includedFiles = new String[0]; |
682 | |
} |
683 | |
|
684 | 0 | for (int j = 0; j < includedFiles.length; j++) { |
685 | 0 | createTransaction().setSrc(new File(fileset.getBasedir(), includedFiles[j])); |
686 | |
} |
687 | 0 | } |
688 | |
|
689 | |
protected Resource[] getResources(String[] locations) throws MojoExecutionException { |
690 | 0 | if (locations == null || locations.length == 0) { |
691 | 0 | return new Resource[] {}; |
692 | |
} |
693 | 0 | ResourceLoader loader = new DefaultResourceLoader(); |
694 | 0 | List<Resource> resources = new ArrayList<Resource>(); |
695 | 0 | for (int i = 0; i < locations.length; i++) { |
696 | 0 | String location = locations[i]; |
697 | |
|
698 | 0 | if (StringUtils.isEmpty(location)) { |
699 | 0 | continue; |
700 | |
} |
701 | 0 | Resource resource = loader.getResource(location); |
702 | 0 | if (!resource.exists()) { |
703 | |
|
704 | 0 | throw new MojoExecutionException("Resource " + location + " was not found"); |
705 | |
} |
706 | 0 | resources.add(resource); |
707 | |
} |
708 | 0 | return resources.toArray(new Resource[resources.size()]); |
709 | |
} |
710 | |
|
711 | |
protected void copy(Resource resource, File file) throws IOException { |
712 | 0 | InputStream in = resource.getInputStream(); |
713 | 0 | OutputStream out = new FileOutputStream(file); |
714 | 0 | IOUtils.copyLarge(in, out); |
715 | 0 | } |
716 | |
|
717 | |
|
718 | |
|
719 | |
|
720 | |
|
721 | |
|
722 | |
private void addResourcesToTransactions() throws MojoExecutionException { |
723 | 0 | String[] locations = getResourceLocations(); |
724 | 0 | Resource[] resources = getResources(locations); |
725 | |
|
726 | 0 | MavenFileFilterRequest request = new MavenFileFilterRequest(); |
727 | 0 | request.setEncoding(encoding); |
728 | 0 | request.setMavenSession(mavenSession); |
729 | 0 | request.setMavenProject(project); |
730 | 0 | request.setFiltering(enableFiltering); |
731 | 0 | for (int i = 0; i < resources.length; i++) { |
732 | 0 | Resource resource = resources[i]; |
733 | 0 | String filename = resource.getFilename(); |
734 | 0 | String basename = FileUtils.basename(filename); |
735 | 0 | String extension = FileUtils.extension(filename); |
736 | 0 | if (!extension.startsWith(".")) { |
737 | 0 | extension = "." + extension; |
738 | |
} |
739 | 0 | File sourceFile = FileUtils.createTempFile(basename, extension, null); |
740 | 0 | if (!getLog().isDebugEnabled()) { |
741 | 0 | sourceFile.deleteOnExit(); |
742 | |
} |
743 | |
|
744 | |
try { |
745 | 0 | copy(resource, sourceFile); |
746 | 0 | } catch (IOException e) { |
747 | 0 | throw new MojoExecutionException("Error copying resource " + resource + " to a local temporary file", e); |
748 | 0 | } |
749 | |
|
750 | 0 | if (!enableFiltering) { |
751 | 0 | createTransaction().setSrc(sourceFile); |
752 | 0 | continue; |
753 | |
} |
754 | |
|
755 | 0 | File targetFile = FileUtils.createTempFile(basename, extension, null); |
756 | 0 | if (!getLog().isDebugEnabled()) { |
757 | 0 | sourceFile.deleteOnExit(); |
758 | |
} |
759 | |
|
760 | 0 | request.setFrom(sourceFile); |
761 | 0 | request.setTo(targetFile); |
762 | |
|
763 | |
try { |
764 | 0 | fileFilter.copyFile(request); |
765 | 0 | } catch (MavenFilteringException e) { |
766 | 0 | throw new MojoExecutionException(e.getMessage()); |
767 | 0 | } |
768 | |
|
769 | 0 | createTransaction().setSrc(targetFile); |
770 | |
} |
771 | 0 | } |
772 | |
|
773 | |
|
774 | |
|
775 | |
|
776 | |
|
777 | |
|
778 | |
private void addFilesToTransactions() throws MojoExecutionException { |
779 | 0 | File[] files = getSrcFiles(); |
780 | |
|
781 | 0 | MavenFileFilterRequest request = new MavenFileFilterRequest(); |
782 | 0 | request.setEncoding(encoding); |
783 | 0 | request.setMavenSession(mavenSession); |
784 | 0 | request.setMavenProject(project); |
785 | 0 | request.setFiltering(enableFiltering); |
786 | 0 | for (int i = 0; files != null && i < files.length; ++i) { |
787 | 0 | if (files[i] != null && !files[i].exists()) { |
788 | 0 | throw new MojoExecutionException(files[i].getPath() + " not found."); |
789 | |
} |
790 | |
|
791 | 0 | if (!enableFiltering) { |
792 | 0 | createTransaction().setSrc(files[i]); |
793 | 0 | continue; |
794 | |
} |
795 | |
|
796 | 0 | File sourceFile = files[i]; |
797 | 0 | String basename = FileUtils.basename(sourceFile.getName()); |
798 | 0 | String extension = FileUtils.extension(sourceFile.getName()); |
799 | 0 | if (!extension.startsWith(".")) { |
800 | 0 | extension = "." + extension; |
801 | |
} |
802 | 0 | File targetFile = FileUtils.createTempFile(basename, extension, null); |
803 | 0 | if (!getLog().isDebugEnabled()) { |
804 | 0 | targetFile.deleteOnExit(); |
805 | |
} |
806 | |
|
807 | 0 | request.setFrom(sourceFile); |
808 | 0 | request.setTo(targetFile); |
809 | |
|
810 | |
try { |
811 | 0 | fileFilter.copyFile(request); |
812 | 0 | } catch (MavenFilteringException e) { |
813 | 0 | throw new MojoExecutionException(e.getMessage()); |
814 | 0 | } |
815 | |
|
816 | 0 | createTransaction().setSrc(targetFile); |
817 | |
} |
818 | 0 | } |
819 | |
|
820 | |
|
821 | |
|
822 | |
|
823 | |
protected void sortTransactions() { |
824 | 0 | switch (orderFile) { |
825 | |
case ASCENDING: |
826 | 0 | Collections.sort(transactions); |
827 | 0 | break; |
828 | |
case DESCENDING: |
829 | 0 | Collections.sort(transactions, Collections.reverseOrder()); |
830 | 0 | break; |
831 | |
case NONE: |
832 | 0 | break; |
833 | |
default: |
834 | 0 | throw new RuntimeException("Unknown value for orderFile: " + orderFile); |
835 | |
} |
836 | 0 | } |
837 | |
|
838 | |
|
839 | |
|
840 | |
|
841 | |
|
842 | |
|
843 | |
private void loadUserInfoFromSettings() throws MojoExecutionException { |
844 | 0 | if (this.settingsKey == null) { |
845 | 0 | this.settingsKey = getUrl(); |
846 | |
} |
847 | |
|
848 | 0 | if ((getUsername() == null || getPassword() == null) && (settings != null)) { |
849 | 0 | Server server = this.settings.getServer(this.settingsKey); |
850 | |
|
851 | 0 | if (server != null) { |
852 | 0 | if (getUsername() == null) { |
853 | 0 | setUsername(server.getUsername()); |
854 | |
} |
855 | |
|
856 | 0 | if (getPassword() == null) { |
857 | 0 | setPassword(server.getPassword()); |
858 | |
} |
859 | |
} |
860 | |
} |
861 | |
|
862 | 0 | if (getUsername() == null) { |
863 | |
|
864 | 0 | setUsername(""); |
865 | |
} |
866 | |
|
867 | 0 | if (getPassword() == null) { |
868 | |
|
869 | 0 | setPassword(""); |
870 | |
} |
871 | 0 | } |
872 | |
|
873 | |
|
874 | |
|
875 | |
|
876 | |
|
877 | |
|
878 | |
|
879 | |
|
880 | |
|
881 | |
|
882 | |
|
883 | |
|
884 | |
|
885 | |
private Connection getConnection() throws MojoExecutionException, SQLException { |
886 | 0 | getLog().debug("connecting to " + getUrl()); |
887 | 0 | Properties info = new Properties(); |
888 | 0 | info.put("user", getUsername()); |
889 | |
|
890 | 0 | if (!enableAnonymousPassword) { |
891 | 0 | info.put("password", getPassword()); |
892 | |
} |
893 | |
|
894 | 0 | info.putAll(this.getDriverProperties()); |
895 | |
|
896 | 0 | Driver driverInstance = null; |
897 | |
|
898 | |
try { |
899 | 0 | Class<?> dc = Class.forName(getDriver()); |
900 | 0 | driverInstance = (Driver) dc.newInstance(); |
901 | 0 | } catch (ClassNotFoundException e) { |
902 | 0 | throw new MojoExecutionException("Driver class not found: " + getDriver(), e); |
903 | 0 | } catch (Exception e) { |
904 | 0 | throw new MojoExecutionException("Failure loading driver: " + getDriver(), e); |
905 | 0 | } |
906 | |
|
907 | 0 | Connection conn = driverInstance.connect(getUrl(), info); |
908 | |
|
909 | 0 | if (conn == null) { |
910 | |
|
911 | 0 | throw new SQLException("No suitable Driver for " + getUrl()); |
912 | |
} |
913 | |
|
914 | 0 | conn.setAutoCommit(autocommit); |
915 | 0 | return conn; |
916 | |
} |
917 | |
|
918 | |
|
919 | |
|
920 | |
|
921 | |
|
922 | |
|
923 | |
|
924 | |
protected Properties getDriverProperties() throws MojoExecutionException { |
925 | 0 | Properties properties = new Properties(); |
926 | |
|
927 | 0 | if (!StringUtils.isEmpty(this.driverProperties)) { |
928 | 0 | String[] tokens = StringUtils.split(this.driverProperties, ","); |
929 | 0 | for (int i = 0; i < tokens.length; ++i) { |
930 | 0 | String[] keyValueTokens = StringUtils.split(tokens[i].trim(), "="); |
931 | 0 | if (keyValueTokens.length != 2) { |
932 | 0 | throw new MojoExecutionException("Invalid JDBC Driver properties: " + this.driverProperties); |
933 | |
} |
934 | |
|
935 | 0 | properties.setProperty(keyValueTokens[0], keyValueTokens[1]); |
936 | |
|
937 | |
} |
938 | |
} |
939 | |
|
940 | 0 | return properties; |
941 | |
} |
942 | |
|
943 | |
|
944 | |
|
945 | |
|
946 | |
|
947 | |
|
948 | |
|
949 | |
|
950 | |
|
951 | |
|
952 | |
|
953 | |
private void runStatements(Reader reader, PrintStream out) throws SQLException, IOException { |
954 | |
String line; |
955 | |
|
956 | 0 | if (enableBlockMode) { |
957 | |
|
958 | |
|
959 | 0 | line = IOUtil.toString(reader); |
960 | 0 | execSQL(line, out); |
961 | 0 | return; |
962 | |
} |
963 | |
|
964 | 0 | StringBuffer sql = new StringBuffer(); |
965 | |
|
966 | 0 | BufferedReader in = new BufferedReader(reader); |
967 | |
|
968 | 0 | while ((line = in.readLine()) != null) { |
969 | 0 | getLog().debug("line='" + line + "'"); |
970 | 0 | if (!keepFormat) { |
971 | 0 | line = line.trim(); |
972 | |
} |
973 | |
|
974 | 0 | if (!keepFormat) { |
975 | 0 | if (line.startsWith("//")) { |
976 | 0 | continue; |
977 | |
} |
978 | 0 | if (line.startsWith("--")) { |
979 | 0 | continue; |
980 | |
} |
981 | 0 | StringTokenizer st = new StringTokenizer(line); |
982 | 0 | if (st.hasMoreTokens()) { |
983 | 0 | String token = st.nextToken(); |
984 | 0 | if ("REM".equalsIgnoreCase(token)) { |
985 | 0 | continue; |
986 | |
} |
987 | |
} |
988 | |
} |
989 | |
|
990 | 0 | if (!keepFormat) { |
991 | 0 | sql.append(" ").append(line); |
992 | |
} else { |
993 | 0 | sql.append("\n").append(line); |
994 | |
} |
995 | |
|
996 | |
|
997 | |
|
998 | |
|
999 | 0 | if (!keepFormat) { |
1000 | 0 | if (SqlSplitter.containsSqlEnd(line, delimiter) == SqlSplitter.NO_END) { |
1001 | 0 | sql.append("\n"); |
1002 | |
} |
1003 | |
} |
1004 | |
|
1005 | 0 | if (isEnd(line)) { |
1006 | 0 | execSQL(sql.substring(0, sql.length() - delimiter.length()), out); |
1007 | 0 | sql.setLength(0); |
1008 | |
} |
1009 | |
} |
1010 | |
|
1011 | |
|
1012 | 0 | if (executeTrailingSQL && !sql.toString().equals("")) { |
1013 | 0 | execSQL(sql.toString(), out); |
1014 | |
} |
1015 | 0 | } |
1016 | |
|
1017 | |
protected boolean isEnd(String line) { |
1018 | 0 | if (delimiterType.equals(DelimiterType.ROW)) { |
1019 | 0 | return line.trim().equals(delimiter); |
1020 | |
} |
1021 | 0 | int pos = SqlSplitter.containsSqlEnd(line, delimiter); |
1022 | 0 | if (delimiterType.equals(DelimiterType.NORMAL) && pos > 0) { |
1023 | 0 | return true; |
1024 | |
} |
1025 | 0 | return false; |
1026 | |
} |
1027 | |
|
1028 | |
|
1029 | |
|
1030 | |
|
1031 | |
|
1032 | |
|
1033 | |
|
1034 | |
|
1035 | |
|
1036 | |
private void execSQL(String sql, PrintStream out) throws SQLException { |
1037 | |
|
1038 | 0 | if ("".equals(sql.trim())) { |
1039 | 0 | return; |
1040 | |
} |
1041 | |
|
1042 | 0 | ResultSet resultSet = null; |
1043 | |
try { |
1044 | 0 | totalStatements++; |
1045 | 0 | getLog().debug("SQL: " + sql); |
1046 | |
|
1047 | |
boolean ret; |
1048 | 0 | int updateCountTotal = 0; |
1049 | |
|
1050 | 0 | ret = statement.execute(sql); |
1051 | |
do { |
1052 | 0 | if (!ret) { |
1053 | 0 | int updateCount = statement.getUpdateCount(); |
1054 | 0 | if (updateCount != -1) { |
1055 | 0 | updateCountTotal += updateCount; |
1056 | |
} |
1057 | 0 | } else { |
1058 | 0 | resultSet = statement.getResultSet(); |
1059 | 0 | if (printResultSet) { |
1060 | 0 | printResultSet(resultSet, out); |
1061 | |
} |
1062 | |
} |
1063 | 0 | ret = statement.getMoreResults(); |
1064 | 0 | } while (ret); |
1065 | |
|
1066 | 0 | getLog().debug(updateCountTotal + " rows affected"); |
1067 | |
|
1068 | 0 | if (printResultSet) { |
1069 | 0 | StringBuffer line = new StringBuffer(); |
1070 | 0 | line.append(updateCountTotal).append(" rows affected"); |
1071 | 0 | out.println(line); |
1072 | |
} |
1073 | |
|
1074 | 0 | SQLWarning warning = conn.getWarnings(); |
1075 | 0 | while (warning != null) { |
1076 | 0 | getLog().debug(warning + " sql warning"); |
1077 | 0 | warning = warning.getNextWarning(); |
1078 | |
} |
1079 | 0 | conn.clearWarnings(); |
1080 | 0 | successfulStatements++; |
1081 | 0 | } catch (SQLException e) { |
1082 | 0 | getLog().error("Failed to execute: " + sql); |
1083 | 0 | if (ON_ERROR_ABORT.equalsIgnoreCase(getOnError())) { |
1084 | 0 | throw e; |
1085 | |
} |
1086 | 0 | getLog().error(e.toString()); |
1087 | |
} finally { |
1088 | 0 | if (resultSet != null) { |
1089 | 0 | resultSet.close(); |
1090 | |
} |
1091 | |
} |
1092 | 0 | } |
1093 | |
|
1094 | |
|
1095 | |
|
1096 | |
|
1097 | |
|
1098 | |
|
1099 | |
|
1100 | |
|
1101 | |
|
1102 | |
|
1103 | |
|
1104 | |
private void printResultSet(ResultSet rs, PrintStream out) throws SQLException { |
1105 | 0 | if (rs != null) { |
1106 | 0 | getLog().debug("Processing new result set."); |
1107 | 0 | ResultSetMetaData md = rs.getMetaData(); |
1108 | 0 | int columnCount = md.getColumnCount(); |
1109 | 0 | StringBuffer line = new StringBuffer(); |
1110 | 0 | if (showheaders) { |
1111 | 0 | boolean first = true; |
1112 | 0 | for (int col = 1; col <= columnCount; col++) { |
1113 | 0 | String columnValue = md.getColumnName(col); |
1114 | |
|
1115 | 0 | if (columnValue != null) { |
1116 | 0 | columnValue = columnValue.trim(); |
1117 | |
|
1118 | 0 | if (",".equals(outputDelimiter)) { |
1119 | 0 | columnValue = StringEscapeUtils.escapeCsv(columnValue); |
1120 | |
} |
1121 | |
} |
1122 | |
|
1123 | 0 | if (first) { |
1124 | 0 | first = false; |
1125 | |
} else { |
1126 | 0 | line.append(outputDelimiter); |
1127 | |
} |
1128 | 0 | line.append(columnValue); |
1129 | |
} |
1130 | 0 | out.println(line); |
1131 | 0 | line = new StringBuffer(); |
1132 | |
} |
1133 | 0 | while (rs.next()) { |
1134 | 0 | boolean first = true; |
1135 | 0 | for (int col = 1; col <= columnCount; col++) { |
1136 | 0 | String columnValue = rs.getString(col); |
1137 | 0 | if (columnValue != null) { |
1138 | 0 | columnValue = columnValue.trim(); |
1139 | |
|
1140 | 0 | if (",".equals(outputDelimiter)) { |
1141 | 0 | columnValue = StringEscapeUtils.escapeCsv(columnValue); |
1142 | |
} |
1143 | |
} |
1144 | |
|
1145 | 0 | if (first) { |
1146 | 0 | first = false; |
1147 | |
} else { |
1148 | 0 | line.append(outputDelimiter); |
1149 | |
} |
1150 | 0 | line.append(columnValue); |
1151 | |
} |
1152 | 0 | out.println(line); |
1153 | 0 | line = new StringBuffer(); |
1154 | 0 | } |
1155 | |
} |
1156 | 0 | out.println(); |
1157 | 0 | } |
1158 | |
|
1159 | |
|
1160 | |
|
1161 | |
|
1162 | |
|
1163 | 0 | private class Transaction implements Comparable<Transaction> { |
1164 | 0 | private File tSrcFile = null; |
1165 | |
|
1166 | 0 | private String tSqlCommand = ""; |
1167 | |
|
1168 | |
|
1169 | |
|
1170 | |
|
1171 | |
public void setSrc(File src) { |
1172 | 0 | this.tSrcFile = src; |
1173 | 0 | } |
1174 | |
|
1175 | |
|
1176 | |
|
1177 | |
|
1178 | |
public void addText(String sql) { |
1179 | 0 | this.tSqlCommand += sql; |
1180 | 0 | } |
1181 | |
|
1182 | |
|
1183 | |
|
1184 | |
|
1185 | |
private void runTransaction(PrintStream out) throws IOException, SQLException { |
1186 | 0 | if (tSqlCommand.length() != 0) { |
1187 | 0 | getLog().info("Executing commands"); |
1188 | |
|
1189 | 0 | runStatements(new StringReader(tSqlCommand), out); |
1190 | |
} |
1191 | |
|
1192 | 0 | if (tSrcFile != null) { |
1193 | 0 | getLog().info("Executing file: " + tSrcFile.getAbsolutePath()); |
1194 | |
|
1195 | 0 | Reader reader = null; |
1196 | |
|
1197 | 0 | if (StringUtils.isEmpty(encoding)) { |
1198 | 0 | reader = new FileReader(tSrcFile); |
1199 | |
} else { |
1200 | 0 | reader = new InputStreamReader(new FileInputStream(tSrcFile), encoding); |
1201 | |
} |
1202 | |
|
1203 | |
try { |
1204 | 0 | runStatements(reader, out); |
1205 | |
} finally { |
1206 | 0 | reader.close(); |
1207 | 0 | } |
1208 | |
} |
1209 | 0 | } |
1210 | |
|
1211 | |
@Override |
1212 | |
public int compareTo(Transaction transaction) { |
1213 | |
|
1214 | 0 | if (transaction.tSrcFile == null) { |
1215 | 0 | if (this.tSrcFile == null) { |
1216 | 0 | return 0; |
1217 | |
} else { |
1218 | 0 | return Integer.MAX_VALUE; |
1219 | |
} |
1220 | |
} else { |
1221 | 0 | if (this.tSrcFile == null) { |
1222 | 0 | return Integer.MIN_VALUE; |
1223 | |
} else { |
1224 | 0 | return this.tSrcFile.compareTo(transaction.tSrcFile); |
1225 | |
} |
1226 | |
} |
1227 | |
} |
1228 | |
} |
1229 | |
|
1230 | |
|
1231 | |
|
1232 | |
|
1233 | |
|
1234 | |
public String getUsername() { |
1235 | 0 | return this.username; |
1236 | |
} |
1237 | |
|
1238 | |
public void setUsername(String username) { |
1239 | 0 | this.username = username; |
1240 | 0 | } |
1241 | |
|
1242 | |
public String getPassword() { |
1243 | 0 | return this.password; |
1244 | |
} |
1245 | |
|
1246 | |
public void setPassword(String password) { |
1247 | 0 | this.password = password; |
1248 | 0 | } |
1249 | |
|
1250 | |
public String getUrl() { |
1251 | 0 | return this.url; |
1252 | |
} |
1253 | |
|
1254 | |
public void setUrl(String url) { |
1255 | 0 | this.url = url; |
1256 | 0 | } |
1257 | |
|
1258 | |
public String getDriver() { |
1259 | 0 | return this.driver; |
1260 | |
} |
1261 | |
|
1262 | |
public void setDriver(String driver) { |
1263 | 0 | this.driver = driver; |
1264 | 0 | } |
1265 | |
|
1266 | |
void setAutocommit(boolean autocommit) { |
1267 | 0 | this.autocommit = autocommit; |
1268 | 0 | } |
1269 | |
|
1270 | |
void setFileset(Fileset fileset) { |
1271 | 0 | this.fileset = fileset; |
1272 | 0 | } |
1273 | |
|
1274 | |
public File[] getSrcFiles() { |
1275 | 0 | return this.srcFiles; |
1276 | |
} |
1277 | |
|
1278 | |
public void setSrcFiles(File[] files) { |
1279 | 0 | this.srcFiles = files; |
1280 | 0 | } |
1281 | |
|
1282 | |
|
1283 | |
|
1284 | |
|
1285 | |
@Deprecated |
1286 | |
int getGoodSqls() { |
1287 | 0 | return this.getSuccessfulStatements(); |
1288 | |
} |
1289 | |
|
1290 | |
|
1291 | |
|
1292 | |
|
1293 | |
|
1294 | |
|
1295 | |
public int getSuccessfulStatements() { |
1296 | 0 | return successfulStatements; |
1297 | |
} |
1298 | |
|
1299 | |
|
1300 | |
|
1301 | |
|
1302 | |
|
1303 | |
|
1304 | |
public int getTotalStatements() { |
1305 | 0 | return totalStatements; |
1306 | |
} |
1307 | |
|
1308 | |
public String getOnError() { |
1309 | 0 | return this.onError; |
1310 | |
} |
1311 | |
|
1312 | |
public void setOnError(String action) { |
1313 | 0 | if (ON_ERROR_ABORT.equalsIgnoreCase(action)) { |
1314 | 0 | this.onError = ON_ERROR_ABORT; |
1315 | 0 | } else if (ON_ERROR_CONTINUE.equalsIgnoreCase(action)) { |
1316 | 0 | this.onError = ON_ERROR_CONTINUE; |
1317 | 0 | } else if (ON_ERROR_ABORT_AFTER.equalsIgnoreCase(action)) { |
1318 | 0 | this.onError = ON_ERROR_ABORT_AFTER; |
1319 | |
} else { |
1320 | 0 | throw new IllegalArgumentException(action + " is not a valid value for onError, only '" + ON_ERROR_ABORT |
1321 | |
+ "', '" + ON_ERROR_ABORT_AFTER + "', or '" + ON_ERROR_CONTINUE + "'."); |
1322 | |
} |
1323 | 0 | } |
1324 | |
|
1325 | |
void setSettings(Settings settings) { |
1326 | 0 | this.settings = settings; |
1327 | 0 | } |
1328 | |
|
1329 | |
void setSettingsKey(String key) { |
1330 | 0 | this.settingsKey = key; |
1331 | 0 | } |
1332 | |
|
1333 | |
void setSkip(boolean skip) { |
1334 | 0 | this.skip = skip; |
1335 | 0 | } |
1336 | |
|
1337 | |
public void setDriverProperties(String driverProperties) { |
1338 | 0 | this.driverProperties = driverProperties; |
1339 | 0 | } |
1340 | |
|
1341 | |
public boolean isEnableBlockMode() { |
1342 | 0 | return enableBlockMode; |
1343 | |
} |
1344 | |
|
1345 | |
public void setEnableBlockMode(boolean enableBlockMode) { |
1346 | 0 | this.enableBlockMode = enableBlockMode; |
1347 | 0 | } |
1348 | |
|
1349 | |
public String getSqlCommand() { |
1350 | 0 | return sqlCommand; |
1351 | |
} |
1352 | |
|
1353 | |
public void setSqlCommand(String sqlCommand) { |
1354 | 0 | this.sqlCommand = sqlCommand; |
1355 | 0 | } |
1356 | |
|
1357 | |
public Vector<Transaction> getTransactions() { |
1358 | 0 | return transactions; |
1359 | |
} |
1360 | |
|
1361 | |
public void setTransactions(Vector<Transaction> transactions) { |
1362 | 0 | this.transactions = transactions; |
1363 | 0 | } |
1364 | |
|
1365 | |
public void setFileFilter(MavenFileFilter filter) { |
1366 | 0 | this.fileFilter = filter; |
1367 | 0 | } |
1368 | |
|
1369 | |
public String[] getResourceLocations() { |
1370 | 0 | return resourceLocations; |
1371 | |
} |
1372 | |
|
1373 | |
public void setResourceLocations(String[] resourceLocations) { |
1374 | 0 | this.resourceLocations = resourceLocations; |
1375 | 0 | } |
1376 | |
|
1377 | |
public boolean isExecuteTrailingSQL() { |
1378 | 0 | return executeTrailingSQL; |
1379 | |
} |
1380 | |
|
1381 | |
public void setExecuteTrailingSQL(boolean executeTrailingSQL) { |
1382 | 0 | this.executeTrailingSQL = executeTrailingSQL; |
1383 | 0 | } |
1384 | |
|
1385 | |
public Order getOrderFile() { |
1386 | 0 | return orderFile; |
1387 | |
} |
1388 | |
|
1389 | |
public void setOrderFile(String orderFile) { |
1390 | 0 | this.orderFile = Order.valueOf(orderFile.toUpperCase()); |
1391 | 0 | } |
1392 | |
} |