Coverage Report - net.sourceforge.addam.ddlgen.generic.InsertGenerator
 
Classes in this File Line Coverage Branch Coverage Complexity
InsertGenerator
0%
0/43
0%
0/14
0
 
 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.ddlgen.generic;
 20  
 
 21  
 import org.apache.velocity.VelocityContext;
 22  
 
 23  
 import java.sql.*;
 24  
 import java.util.ArrayList;
 25  
 import java.util.List;
 26  
 import java.io.BufferedWriter;
 27  
 
 28  
 /**
 29  
  * User: mkrishna
 30  
  * Date: Nov 18, 2004
 31  
  */
 32  0
 public class InsertGenerator extends VelocityGenerator {
 33  0
     private List rows = new ArrayList();
 34  
 
 35  
     public void generate(String catalog, String schema, String object,
 36  
                          String vendor, Connection connection, DatabaseMetaData metadata, BufferedWriter writer)
 37  
             throws Exception {
 38  0
         rows.clear();
 39  0
         if (metadata.storesUpperCaseIdentifiers()) {
 40  0
             catalog = (catalog == null) ? null : catalog.toUpperCase();
 41  0
             schema = (schema == null) ? null : schema.toUpperCase();
 42  0
             object = (object == null) ? null : object.toUpperCase();
 43  0
         } else if (metadata.storesLowerCaseIdentifiers()) {
 44  0
             catalog = (catalog == null) ? null : catalog.toLowerCase();
 45  0
             schema = (schema == null) ? null : schema.toLowerCase();
 46  0
             object = (object == null) ? null : object.toLowerCase();
 47  
         }
 48  0
         super.generate(catalog,schema,object,vendor,connection,metadata,writer);
 49  0
     }
 50  
 
 51  
     protected VelocityContext getContext(String catalog, String schema, String object,
 52  
                                          Connection connection, DatabaseMetaData metadata)
 53  
             throws Exception {
 54  0
         VelocityContext context = new VelocityContext();
 55  0
         ResultSet resultSet = null;
 56  0
         Statement statement = null;
 57  
         try {
 58  0
             statement = connection.createStatement();
 59  0
             statement.execute("select * from " + object + " order by 1");
 60  0
             resultSet = statement.getResultSet();
 61  0
             while (resultSet.next()) {
 62  0
                 rows.add(getRow(resultSet));
 63  0
             }
 64  0
             context.put("tableName", object);
 65  0
             context.put("rows", rows);
 66  0
         } catch (Exception e) {
 67  0
             e.printStackTrace();
 68  0
         } finally {
 69  0
             if (resultSet != null) resultSet.close();
 70  0
             if (statement != null) statement.close();
 71  0
         }
 72  0
         return context;
 73  
     }
 74  
 
 75  
     private List<String> getRow(ResultSet resultSet) throws SQLException {
 76  0
         List<String> table = new ArrayList<String>();
 77  0
         ResultSetMetaData metaData = resultSet.getMetaData();
 78  0
         int columnCount = metaData.getColumnCount();
 79  0
         for (int i = 1; i <= columnCount; i++) {
 80  0
             table.add(getFormattedString(resultSet.getString(i), metaData.getColumnType(i)));
 81  
         }
 82  0
         return table;
 83  
     }
 84  
 
 85  
     protected String getFormattedString(String unformattedString, int sqlType) {
 86  0
         if (Types.DATE == sqlType) {
 87  0
             return "to_timestamp ('" + unformattedString + "', 'yyyy-mm-dd HH24:MI:SS.FF')";
 88  0
         } else if (unformattedString == null) {
 89  0
             return "null";
 90  
         }
 91  0
         return "'" + unformattedString.replaceAll("'", "''") + "'";
 92  
     }
 93  
 
 94  
     protected List getRows() {
 95  0
         return rows;
 96  
     }
 97  
 }