1 package org.kuali.db;
2
3 import java.io.IOException;
4 import java.io.InputStreamReader;
5 import java.io.Reader;
6 import java.util.Properties;
7
8 import org.apache.commons.io.IOUtils;
9 import org.springframework.core.io.DefaultResourceLoader;
10 import org.springframework.core.io.Resource;
11 import org.springframework.util.PropertyPlaceholderHelper;
12
13
14
15
16
17 public class SQLGenerator {
18 PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}");
19 JDBCUtils jdbcUtils = new JDBCUtils();
20 DefaultResourceLoader loader = new DefaultResourceLoader();
21 String encoding;
22 Properties properties;
23 String url;
24 DatabaseCommand command;
25 String prefix = "classpath:org/kuali/db/";
26
27 public SQLGenerator() {
28 this(null, null, null);
29 }
30
31 public SQLGenerator(Properties properties, String url, DatabaseCommand command) {
32 super();
33 this.properties = properties;
34 this.url = url;
35 this.command = command;
36 }
37
38 protected String getSQLLocation() {
39 return getLocation("/" + getPackageFriendlyName(command.toString()) + ".sql");
40 }
41
42 public String getSQL() throws IOException {
43 String location = getSQLLocation();
44 Resource resource = loader.getResource(location);
45 String sql = IOUtils.toString(resource.getInputStream(), getEncoding());
46 Properties customProperties = getCustomProperties();
47 if (properties != null) {
48 customProperties.putAll(properties);
49 }
50 sql = helper.replacePlaceholders(sql, customProperties);
51 return sql;
52 }
53
54 protected String getPackageFriendlyName(String s) {
55 return s.toLowerCase().replace("_", "");
56 }
57
58 protected String getLocation(String suffix) {
59 JDBCConfiguration jdbcConfiguration = jdbcUtils.getDatabaseConfiguration(url);
60 DatabaseType type = jdbcConfiguration.getType();
61 String packageFriendlyName = getPackageFriendlyName(type.toString());
62 String location = prefix + packageFriendlyName + suffix;
63 return location;
64 }
65
66 protected String getCustomPropertiesLocation() {
67 return getLocation("/custom.properties");
68 }
69
70 protected Properties getCustomProperties() throws IOException {
71 String location = getCustomPropertiesLocation();
72 Reader input = null;
73 try {
74 Resource resource = loader.getResource(location);
75 input = new InputStreamReader(resource.getInputStream(), getEncoding());
76 Properties p = new Properties();
77 p.load(input);
78 return p;
79 } finally {
80 IOUtils.closeQuietly(input);
81 }
82 }
83
84 public String getEncoding() {
85 return encoding;
86 }
87
88 public void setEncoding(String encoding) {
89 this.encoding = encoding;
90 }
91
92 public Properties getProperties() {
93 return properties;
94 }
95
96 public void setProperties(Properties properties) {
97 this.properties = properties;
98 }
99
100 public String getUrl() {
101 return url;
102 }
103
104 public void setUrl(String url) {
105 this.url = url;
106 }
107
108 public DatabaseCommand getCommand() {
109 return command;
110 }
111
112 public void setCommand(DatabaseCommand command) {
113 this.command = command;
114 }
115
116 public PropertyPlaceholderHelper getHelper() {
117 return helper;
118 }
119
120 public void setHelper(PropertyPlaceholderHelper helper) {
121 this.helper = helper;
122 }
123
124 public JDBCUtils getJdbcUtils() {
125 return jdbcUtils;
126 }
127
128 public void setJdbcUtils(JDBCUtils jdbcUtils) {
129 this.jdbcUtils = jdbcUtils;
130 }
131
132 public DefaultResourceLoader getLoader() {
133 return loader;
134 }
135
136 public void setLoader(DefaultResourceLoader loader) {
137 this.loader = loader;
138 }
139
140 public String getPrefix() {
141 return prefix;
142 }
143
144 public void setPrefix(String prefix) {
145 this.prefix = prefix;
146 }
147
148 }