View Javadoc

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  public class InsertGenerator extends VelocityGenerator {
33      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          rows.clear();
39          if (metadata.storesUpperCaseIdentifiers()) {
40              catalog = (catalog == null) ? null : catalog.toUpperCase();
41              schema = (schema == null) ? null : schema.toUpperCase();
42              object = (object == null) ? null : object.toUpperCase();
43          } else if (metadata.storesLowerCaseIdentifiers()) {
44              catalog = (catalog == null) ? null : catalog.toLowerCase();
45              schema = (schema == null) ? null : schema.toLowerCase();
46              object = (object == null) ? null : object.toLowerCase();
47          }
48          super.generate(catalog,schema,object,vendor,connection,metadata,writer);
49      }
50  
51      protected VelocityContext getContext(String catalog, String schema, String object,
52                                           Connection connection, DatabaseMetaData metadata)
53              throws Exception {
54          VelocityContext context = new VelocityContext();
55          ResultSet resultSet = null;
56          Statement statement = null;
57          try {
58              statement = connection.createStatement();
59              statement.execute("select * from " + object + " order by 1");
60              resultSet = statement.getResultSet();
61              while (resultSet.next()) {
62                  rows.add(getRow(resultSet));
63              }
64              context.put("tableName", object);
65              context.put("rows", rows);
66          } catch (Exception e) {
67              e.printStackTrace();
68          } finally {
69              if (resultSet != null) resultSet.close();
70              if (statement != null) statement.close();
71          }
72          return context;
73      }
74  
75      private List<String> getRow(ResultSet resultSet) throws SQLException {
76          List<String> table = new ArrayList<String>();
77          ResultSetMetaData metaData = resultSet.getMetaData();
78          int columnCount = metaData.getColumnCount();
79          for (int i = 1; i <= columnCount; i++) {
80              table.add(getFormattedString(resultSet.getString(i), metaData.getColumnType(i)));
81          }
82          return table;
83      }
84  
85      protected String getFormattedString(String unformattedString, int sqlType) {
86          if (Types.DATE == sqlType) {
87              return "to_timestamp ('" + unformattedString + "', 'yyyy-mm-dd HH24:MI:SS.FF')";
88          } else if (unformattedString == null) {
89              return "null";
90          }
91          return "'" + unformattedString.replaceAll("'", "''") + "'";
92      }
93  
94      protected List getRows() {
95          return rows;
96      }
97  }