| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package net.sourceforge.addam.ddlgen; |
| 20 | |
|
| 21 | |
import org.apache.tools.ant.BuildException; |
| 22 | |
import org.apache.tools.ant.taskdefs.JDBCTask; |
| 23 | |
|
| 24 | |
import java.io.*; |
| 25 | |
import java.sql.Connection; |
| 26 | |
import java.sql.DatabaseMetaData; |
| 27 | |
import java.sql.ResultSet; |
| 28 | |
import java.sql.SQLException; |
| 29 | |
import java.util.ArrayList; |
| 30 | |
import java.util.List; |
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | 0 | public class GenerateTask extends JDBCTask { |
| 38 | |
|
| 39 | 0 | private String schema = null; |
| 40 | 0 | private String catalog = null; |
| 41 | 0 | private final List<GeneratorSpec> generatorSpecs = new ArrayList<GeneratorSpec>(); |
| 42 | |
|
| 43 | |
public void setSchema(String schema) { |
| 44 | 0 | this.schema = schema; |
| 45 | 0 | } |
| 46 | |
|
| 47 | |
public void setCatalog(String catalog) { |
| 48 | 0 | this.catalog = catalog; |
| 49 | 0 | } |
| 50 | |
|
| 51 | |
public void addGenerator(GeneratorSpec spec) { |
| 52 | 0 | generatorSpecs.add(spec); |
| 53 | 0 | } |
| 54 | |
|
| 55 | |
public void execute() throws BuildException { |
| 56 | 0 | Exception exception = null; |
| 57 | 0 | ResultSet tablesRS = null; |
| 58 | 0 | Connection connection = null; |
| 59 | |
try { |
| 60 | 0 | connection = getConnection(); |
| 61 | 0 | DatabaseMetaData dbmd = connection.getMetaData(); |
| 62 | 0 | if (dbmd.storesUpperCaseIdentifiers()) { |
| 63 | 0 | catalog = (catalog == null) ? null : catalog.toUpperCase(); |
| 64 | 0 | schema = (schema == null) ? null : schema.toUpperCase(); |
| 65 | 0 | } else if (dbmd.storesLowerCaseIdentifiers()) { |
| 66 | 0 | catalog = (catalog == null) ? null : catalog.toLowerCase(); |
| 67 | 0 | schema = (schema == null) ? null : schema.toLowerCase(); |
| 68 | |
} |
| 69 | 0 | for (GeneratorSpec spec : generatorSpecs) { |
| 70 | 0 | Generator generator = spec.getGenerator(); |
| 71 | 0 | List<String> objects = generator.getObjects(catalog,schema,spec,connection,dbmd); |
| 72 | 0 | for (String dbObject: objects) { |
| 73 | 0 | if (spec.includes(dbObject)) { |
| 74 | 0 | String vendor = spec.getVendor(); |
| 75 | 0 | log("generating " + dbObject + " with " + generator.getClass().getName()); |
| 76 | 0 | BufferedWriter writer = null; |
| 77 | 0 | Writer masterScriptWriter = spec.getMasterScriptWriter(); |
| 78 | 0 | BufferedWriter bufferedMSW = null; |
| 79 | 0 | if (masterScriptWriter != null) { |
| 80 | 0 | bufferedMSW = new BufferedWriter(masterScriptWriter); |
| 81 | |
} |
| 82 | |
try { |
| 83 | 0 | writer = new BufferedWriter(spec.getFileWriter(dbObject)); |
| 84 | 0 | generator.generate(catalog, schema, dbObject, vendor, connection, dbmd, writer); |
| 85 | 0 | if (bufferedMSW != null) { |
| 86 | 0 | bufferedMSW.write(spec.getFileName(dbObject)); |
| 87 | 0 | bufferedMSW.newLine(); |
| 88 | 0 | bufferedMSW.flush(); |
| 89 | |
} |
| 90 | 0 | } finally { |
| 91 | 0 | if (writer != null) writer.close(); |
| 92 | 0 | } |
| 93 | |
} |
| 94 | 0 | } |
| 95 | 0 | } |
| 96 | 0 | } catch (Exception e) { |
| 97 | 0 | exception = e; |
| 98 | 0 | } finally { |
| 99 | 0 | try { |
| 100 | 0 | for (GeneratorSpec spec : generatorSpecs) { |
| 101 | 0 | Writer writer = spec.getMasterScriptWriter(); |
| 102 | 0 | if (writer != null) writer.close(); |
| 103 | 0 | } |
| 104 | 0 | if (tablesRS != null) tablesRS.close(); |
| 105 | 0 | if (connection != null) connection.close(); |
| 106 | 0 | } catch (SQLException e) { |
| 107 | 0 | exception = e; |
| 108 | 0 | } catch (IOException e) { |
| 109 | 0 | exception = e; |
| 110 | 0 | } |
| 111 | 0 | } |
| 112 | 0 | if (exception != null) throw new BuildException(exception); |
| 113 | 0 | } |
| 114 | |
|
| 115 | |
} |