| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
package net.sourceforge.addam.ddlgen.generic; |
| 6 | |
|
| 7 | |
import net.sourceforge.addam.ddlgen.Filter; |
| 8 | |
import net.sourceforge.addam.ddlgen.Generator; |
| 9 | |
|
| 10 | |
import org.apache.velocity.Template; |
| 11 | |
import org.apache.velocity.VelocityContext; |
| 12 | |
import org.apache.velocity.app.Velocity; |
| 13 | |
import org.apache.velocity.exception.ResourceNotFoundException; |
| 14 | |
|
| 15 | |
import java.io.BufferedWriter; |
| 16 | |
import java.sql.Connection; |
| 17 | |
import java.sql.DatabaseMetaData; |
| 18 | |
import java.sql.ResultSet; |
| 19 | |
import java.util.ArrayList; |
| 20 | |
import java.util.List; |
| 21 | |
import java.util.Properties; |
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | 20 | public abstract class VelocityGenerator implements Generator { |
| 30 | |
|
| 31 | |
public List<String> getObjects(String catalog, String schema, Filter filter, |
| 32 | |
Connection connection, DatabaseMetaData metadata) throws Exception { |
| 33 | 0 | if (metadata.storesUpperCaseIdentifiers()) { |
| 34 | 0 | catalog = (catalog == null) ? null : catalog.toUpperCase(); |
| 35 | 0 | schema = (schema == null) ? null : schema.toUpperCase(); |
| 36 | 0 | } else if (metadata.storesLowerCaseIdentifiers()) { |
| 37 | 0 | catalog = (catalog == null) ? null : catalog.toLowerCase(); |
| 38 | 0 | schema = (schema == null) ? null : schema.toLowerCase(); |
| 39 | |
} |
| 40 | 0 | ResultSet tablesRS = metadata.getTables(catalog, schema, "%", new String[]{"TABLE"}); |
| 41 | 0 | List<String> objects = new ArrayList<String>(); |
| 42 | 0 | while (tablesRS.next()) { |
| 43 | 0 | String dbObject = tablesRS.getString(3); |
| 44 | 0 | if (filter.includes(dbObject)) { |
| 45 | 0 | objects.add(dbObject); |
| 46 | |
} |
| 47 | 0 | } |
| 48 | 0 | return objects; |
| 49 | |
} |
| 50 | |
|
| 51 | |
public void generate(String catalog, String schema, String object, |
| 52 | |
String vendor, Connection connection, DatabaseMetaData metadata, BufferedWriter writer) |
| 53 | |
throws Exception { |
| 54 | 0 | if (metadata.storesUpperCaseIdentifiers()) { |
| 55 | 0 | catalog = (catalog == null) ? null : catalog.toUpperCase(); |
| 56 | 0 | schema = (schema == null) ? null : schema.toUpperCase(); |
| 57 | 0 | } else if (metadata.storesLowerCaseIdentifiers()) { |
| 58 | 0 | catalog = (catalog == null) ? null : catalog.toLowerCase(); |
| 59 | 0 | schema = (schema == null) ? null : schema.toLowerCase(); |
| 60 | |
} |
| 61 | 0 | if (vendor == null) vendor = metadata.getDatabaseProductName(); |
| 62 | 0 | Template template = getTemplate(metadata, vendor); |
| 63 | 0 | VelocityContext context = getContext(catalog, schema, object, connection, metadata); |
| 64 | 0 | template.merge(context, writer); |
| 65 | 0 | } |
| 66 | |
|
| 67 | |
protected abstract VelocityContext getContext(String catalog, String schema, String object, |
| 68 | |
Connection connection, DatabaseMetaData metadata) throws Exception; |
| 69 | |
|
| 70 | |
protected Template getTemplate(DatabaseMetaData metadata, String vendor) throws Exception { |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | 0 | Properties props = new Properties(); |
| 75 | 0 | props.put("resource.loader", "class"); |
| 76 | 0 | props.put("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); |
| 77 | 0 | Velocity.init(props); |
| 78 | |
|
| 79 | |
Template template; |
| 80 | |
try { |
| 81 | 0 | String templateName = this.getTemplateName(vendor); |
| 82 | 0 | template = Velocity.getTemplate(templateName); |
| 83 | 0 | } catch (ResourceNotFoundException e) { |
| 84 | 0 | String templateName = this.getTemplateName(""); |
| 85 | 0 | template = Velocity.getTemplate(templateName); |
| 86 | 0 | } |
| 87 | 0 | return template; |
| 88 | |
} |
| 89 | |
|
| 90 | |
protected String getTemplateName(String vendor) { |
| 91 | 0 | String name = getClass().getName().replace('.', '/'); |
| 92 | 0 | int lastIndex = name.lastIndexOf("/"); |
| 93 | 0 | String dir = name.substring(0, lastIndex); |
| 94 | 0 | String file = name.substring(lastIndex + 1); |
| 95 | 0 | String suffix = (vendor == null || vendor.length()==0) ? "" : "-" + vendor; |
| 96 | 0 | String templateName = dir + "/" + file + suffix + ".vm"; |
| 97 | 0 | templateName = templateName.replace("Generator",""); |
| 98 | 0 | return templateName; |
| 99 | |
} |
| 100 | |
|
| 101 | |
} |