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