| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 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 | |
|
| 30 | |
|
| 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 | |
} |