Coverage Report - net.sourceforge.addam.impexp.ExportDataTask
 
Classes in this File Line Coverage Branch Coverage Complexity
ExportDataTask
0%
0/105
0%
0/10
0
 
 1  
 //
 2  
 // Copyright (c) 2004, International Decision Systems
 3  
 // all rights reserved
 4  
 /*
 5  
  * Copyright (c) 2004 International Decision Systems, Inc.  All Rights Reserved.
 6  
  *
 7  
  * By using this Software, You acknowledge that the Software is a valuable asset
 8  
  * and trade secret of either International Decision Systems, Inc. ("IDSI") or a
 9  
  * third party supplier of IDSI and constitutes confidential and proprietary
 10  
  * information.
 11  
  *
 12  
  * NEITHER IDSI NOR ANY AGENT OR PERSON ACTING FOR OR WITH IDSI HAS MADE OR DOES
 13  
  * MAKE ANY STATEMENTS, AFFIRMATIONS, REPRESENTATIONS OR WARRANTIES WHATSOEVER
 14  
  * TO YOU, WHETHER EXPRESS OR IMPLIED, AS TO THE SOFTWARE, THE QUALITY OR
 15  
  * CONDITION OF THE SOFTWARE, OR THE OPERATING CHARACTERISTICS OR RELIABILITY OF
 16  
  * THE SOFTWARE, OR ITS SUITABILITY FOR ANY GENERAL OR PARTICULAR PURPOSE, OR AS
 17  
  * TO ANY OTHER MATTER WHATSOEVER; ANY AND ALL OTHER WARRANTIES INCLUDING
 18  
  * WITHOUT LIMITATION ANY WARRANTIES IMPLIED BY LAW, SUCH AS THE IMPLIED
 19  
  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND TITLE,
 20  
  * USE AND NON-INFRINGEMENT; ARE HEREBY EXPRESSLY DISCLAIMED AND EXCLUDED.
 21  
 */
 22  
 package net.sourceforge.addam.impexp;
 23  
 
 24  
 
 25  
 import net.sourceforge.addam.impexp.csv.CSVPrinter;
 26  
 import net.sourceforge.addam.util.DatabaseMetaDataUtil;
 27  
 import net.sourceforge.addam.util.TableUtil;
 28  
 
 29  
 import org.apache.tools.ant.BuildException;
 30  
 import org.apache.tools.ant.taskdefs.JDBCTask;
 31  
 
 32  
 import java.io.File;
 33  
 import java.io.FileWriter;
 34  
 import java.io.IOException;
 35  
 import java.io.Writer;
 36  
 import java.sql.*;
 37  
 import java.util.ArrayList;
 38  
 import java.util.Iterator;
 39  
 import java.util.List;
 40  
 
 41  0
 public class ExportDataTask extends JDBCTask {
 42  0
     private final List<Tables> includes = new ArrayList<Tables>();
 43  0
     private final List<Tables> excludes = new ArrayList<Tables>();
 44  
     protected File dir;
 45  
     private String schema;
 46  
     private String catalog;
 47  
 
 48  
     /**
 49  
      * Selects all data from the named tables and extracts to the named files.
 50  
      *
 51  
      * @throws org.apache.tools.ant.BuildException
 52  
      *          if anything goes wrong
 53  
      */
 54  
     public void execute() throws BuildException {
 55  
         // run for each table
 56  0
         for (String tableName : getTableNames()) {
 57  0
             Connection connection = null;
 58  
             try {
 59  0
                 connection = this.getConnection();
 60  0
                 this.exportTable(tableName, connection);
 61  0
             } finally {
 62  0
                 if (connection != null)
 63  
                     try {
 64  0
                         connection.close();
 65  0
                     } catch (SQLException e) {
 66  
                         // ignore
 67  0
                     }
 68  0
             }
 69  0
         }
 70  0
     }
 71  
 
 72  
     /**
 73  
      * Selects all data from the named table and extracts to the named file.
 74  
      *
 75  
      * @throws org.apache.tools.ant.BuildException
 76  
      *          if anything goes wrong
 77  
      */
 78  
     public void exportTable(String tableName, Connection connection) throws BuildException {
 79  0
         File toFile = new File(dir, TableUtil.getFileName(tableName));
 80  0
         log("extracting " + tableName + " to file " + toFile);
 81  0
         int count = 0;
 82  0
         Writer writer = null;
 83  
         try {
 84  0
             writer = new FileWriter(toFile);
 85  0
             CSVPrinter printer = new CSVPrinter(writer);
 86  
 
 87  0
             Statement stmt = connection.createStatement();
 88  0
             ResultSet rs = stmt.executeQuery("SELECT * FROM " + tableName + " ORDER BY 1");
 89  0
             ResultSetMetaData rsmd = rs.getMetaData();
 90  0
             int numCols = rsmd.getColumnCount();
 91  0
             String line[] = new String[numCols];
 92  
 
 93  
             // output column names
 94  0
             for (int i = 1; i <= numCols; i++) {
 95  0
                 line[i - 1] = rsmd.getColumnName(i);
 96  
             }
 97  0
             printer.writeRecord(line);
 98  
 
 99  
             // output data
 100  0
             while (rs.next()) {
 101  0
                 for (int i = 1; i <= numCols; i++) {
 102  0
                     String className = rsmd.getColumnClassName(i);
 103  0
                     DataCodec codec = DataCodec.getCodec(className);
 104  
                     Object value;
 105  0
                     if (className.equals("java.sql.Timestamp")) {
 106  0
                         value = rs.getTimestamp(i);
 107  0
                     } else if (className.equals("java.lang.String")) {
 108  0
                         value = rs.getString(i);
 109  0
                     } else {
 110  0
                         value = rs.getObject(i);
 111  
                     }
 112  
 
 113  0
                     String o = codec.encode(value);
 114  0
                     line[i - 1] = (o == null) ? "" : o;
 115  
                 }
 116  0
                 printer.writeRecord(line);
 117  0
                 count++;
 118  0
             }
 119  0
         } catch (BuildException e) {
 120  0
             throw e; // don't re-wrap BuildExceptions
 121  0
         } catch (Exception e) {
 122  0
             e.printStackTrace();
 123  0
             throw new BuildException(e);
 124  0
         } finally {
 125  0
             try {
 126  0
                 if (writer != null) {
 127  0
                     writer.close();
 128  
                 }
 129  0
             } catch (IOException e) {
 130  0
                 log(e.toString());
 131  0
             }
 132  0
         }
 133  0
         log(" ... " + count + " records");
 134  0
     }
 135  
 
 136  
     /**
 137  
      * this is a shortcut for having an <includeTables> subelement by simply providing a filename as an attribute to this task
 138  
      *
 139  
      * @param tableFile a file containing an ordered list of tables
 140  
      */
 141  
     public void setIncludeFile(File tableFile) {
 142  0
         Tables tables = new Tables();
 143  0
         tables.setFile(tableFile);
 144  0
         includes.add(tables);
 145  0
     }
 146  
 
 147  
     /**
 148  
      * this is a shortcut for having a <excludeTables> subelement by simply providing a filename as an attribute to this task
 149  
      *
 150  
      * @param tableFile a file containing an ordered list of tables
 151  
      */
 152  
     public void setExcludeFile(File tableFile) {
 153  0
         Tables tables = new Tables();
 154  0
         tables.setFile(tableFile);
 155  0
         excludes.add(tables);
 156  0
     }
 157  
 
 158  
     /**
 159  
      * @param pattern a regexp pattern to compare table names to
 160  
      */
 161  
     public void setIncludesPattern(String pattern) {
 162  0
         Tables tables = new Tables();
 163  0
         tables.setPattern(pattern);
 164  0
         includes.add(tables);
 165  0
     }
 166  
 
 167  
     /**
 168  
      * @param pattern a regexp pattern to compare table names to
 169  
      */
 170  
     public void setExcludesPattern(String pattern) {
 171  0
         Tables tables = new Tables();
 172  0
         tables.setPattern(pattern);
 173  0
         excludes.add(tables);
 174  0
     }
 175  
 
 176  
     /**
 177  
      * @param names a comma separated list of table names
 178  
      */
 179  
     public void setIncludes(String names) {
 180  0
         Tables tables = new Tables();
 181  0
         tables.setNames(names);
 182  0
         includes.add(tables);
 183  0
     }
 184  
 
 185  
     /**
 186  
      * @param names a comma separated list of table names
 187  
      */
 188  
     public void setExcludes(String names) {
 189  0
         Tables tables = new Tables();
 190  0
         tables.setNames(names);
 191  0
         excludes.add(tables);
 192  0
     }
 193  
 
 194  
     /**
 195  
      * @param tables a set of tables to include or exclude
 196  
      */
 197  
     public void addIncludeTables(Tables tables) {
 198  0
         includes.add(tables);
 199  0
     }
 200  
 
 201  
     /**
 202  
      * @param tables a set of tables to include or exclude
 203  
      */
 204  
     public void addExcludeTables(Tables tables) {
 205  0
         excludes.add(tables);
 206  0
     }
 207  
 
 208  
     /**
 209  
      * @param tableDir the directory to write the files into
 210  
      */
 211  
     public void setDir(File tableDir) {
 212  0
         dir = tableDir;
 213  0
     }
 214  
 
 215  
     public void setSchema(String schema) {
 216  0
         this.schema = schema;
 217  0
     }
 218  
 
 219  
     public void setCatalog(String catalog) {
 220  0
         this.catalog = catalog;
 221  0
     }
 222  
 
 223  
     /**
 224  
      * returns all Tables included by not excluded
 225  
      *
 226  
      * @return List of Strings each naming a table
 227  
      */
 228  
     protected List<String> getTableNames() {
 229  
         List<String> tableNamesFromDB;
 230  
 
 231  0
         Connection connection = null;
 232  
         try {
 233  0
             connection = this.getConnection();
 234  0
             tableNamesFromDB = DatabaseMetaDataUtil.getTables(catalog,schema,connection);
 235  0
         } catch (SQLException e) {
 236  0
             throw new BuildException(e);
 237  0
         } finally {
 238  0
             if (connection != null)
 239  
                 try {
 240  0
                     connection.close();
 241  0
                 } catch (SQLException e) {
 242  
                     // ignore
 243  0
                 }
 244  0
         }
 245  
 
 246  0
         return Tables.narrow(tableNamesFromDB,includes,excludes);
 247  
     }
 248  
 }