| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| SelectExecTask |
|
| 2.6666666666666665;2.667 |
| 1 | /* | |
| 2 | * Copyright (c) 2004 International Decision Systems, Inc. All Rights Reserved. | |
| 3 | * | |
| 4 | * By using this Software, You acknowledge that the Software is a valuable asset | |
| 5 | * and trade secret of either International Decision Systems, Inc. ("IDSI") or a | |
| 6 | * third party supplier of IDSI and constitutes confidential and proprietary | |
| 7 | * information. | |
| 8 | * | |
| 9 | * NEITHER IDSI NOR ANY AGENT OR PERSON ACTING FOR OR WITH IDSI HAS MADE OR DOES | |
| 10 | * MAKE ANY STATEMENTS, AFFIRMATIONS, REPRESENTATIONS OR WARRANTIES WHATSOEVER | |
| 11 | * TO YOU, WHETHER EXPRESS OR IMPLIED, AS TO THE SOFTWARE, THE QUALITY OR | |
| 12 | * CONDITION OF THE SOFTWARE, OR THE OPERATING CHARACTERISTICS OR RELIABILITY OF | |
| 13 | * THE SOFTWARE, OR ITS SUITABILITY FOR ANY GENERAL OR PARTICULAR PURPOSE, OR AS | |
| 14 | * TO ANY OTHER MATTER WHATSOEVER; ANY AND ALL OTHER WARRANTIES INCLUDING | |
| 15 | * WITHOUT LIMITATION ANY WARRANTIES IMPLIED BY LAW, SUCH AS THE IMPLIED | |
| 16 | * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND TITLE, | |
| 17 | * USE AND NON-INFRINGEMENT; ARE HEREBY EXPRESSLY DISCLAIMED AND EXCLUDED. | |
| 18 | */ | |
| 19 | package net.sourceforge.addam.selectexec; | |
| 20 | ||
| 21 | import org.apache.tools.ant.BuildException; | |
| 22 | import org.apache.tools.ant.taskdefs.JDBCTask; | |
| 23 | ||
| 24 | import java.sql.*; | |
| 25 | import java.util.ArrayList; | |
| 26 | import java.util.Iterator; | |
| 27 | import java.util.List; | |
| 28 | ||
| 29 | /** | |
| 30 | * This is a strange task, but it can be quite useful... it executes the given | |
| 31 | * SQL and treats the results (the first column, specifically) as a new set of | |
| 32 | * SQL which it then executes. | |
| 33 | * | |
| 34 | * @author TIM3 | |
| 35 | * @since Nov 30, 2004 9:07:45 PM | |
| 36 | */ | |
| 37 | 0 | public class SelectExecTask extends JDBCTask { |
| 38 | ||
| 39 | 0 | private String text = ""; |
| 40 | 0 | private int batchSize = 1; |
| 41 | ||
| 42 | /** | |
| 43 | * sets the number of statements to execute simultaneously | |
| 44 | * | |
| 45 | * @param size | |
| 46 | */ | |
| 47 | public void setBatchSize(int size) { | |
| 48 | 0 | this.batchSize = size; |
| 49 | 0 | } |
| 50 | ||
| 51 | /** | |
| 52 | * adds to the SQL statement | |
| 53 | * | |
| 54 | * @param text | |
| 55 | */ | |
| 56 | public void addText(String text) { | |
| 57 | 0 | this.text += text; |
| 58 | 0 | } |
| 59 | ||
| 60 | public void execute() throws BuildException { | |
| 61 | try { | |
| 62 | 0 | Connection conn = this.getConnection(); |
| 63 | 0 | List resultSQL = new ArrayList(); |
| 64 | 0 | log(text); |
| 65 | 0 | PreparedStatement select = conn.prepareStatement(text); |
| 66 | 0 | ResultSet results = select.executeQuery(); |
| 67 | 0 | while (results.next()) { |
| 68 | 0 | resultSQL.add(results.getString(1)); |
| 69 | 0 | } |
| 70 | 0 | Statement exec = conn.createStatement(); |
| 71 | 0 | int i = 0; |
| 72 | 0 | for (Iterator it = resultSQL.iterator(); it.hasNext(); i++) { |
| 73 | 0 | String sql = it.next().toString(); |
| 74 | 0 | if (i % batchSize == 0) { |
| 75 | 0 | exec.executeBatch(); |
| 76 | 0 | exec.clearBatch(); |
| 77 | } | |
| 78 | 0 | log(sql); |
| 79 | 0 | exec.addBatch(sql); |
| 80 | } | |
| 81 | 0 | exec.executeBatch(); |
| 82 | 0 | conn.commit(); |
| 83 | 0 | } catch (SQLException e) { |
| 84 | 0 | e.printStackTrace(); |
| 85 | 0 | throw new BuildException("upgrade failed: " + e, e); |
| 86 | 0 | } |
| 87 | 0 | } |
| 88 | } |