1 package org.apache.torque.engine.platform;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.sql.Connection;
23 import java.sql.DatabaseMetaData;
24 import java.sql.PreparedStatement;
25 import java.sql.ResultSet;
26 import java.sql.SQLException;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import org.apache.commons.lang.StringUtils;
31 import org.apache.torque.engine.database.model.Domain;
32 import org.apache.torque.engine.database.model.SchemaType;
33 import org.kuali.common.impex.service.MySqlProducer;
34 import org.kuali.common.impex.service.SqlProducer;
35
36
37
38
39
40
41
42 public class PlatformMysqlImpl extends PlatformDefaultImpl {
43
44
45
46 public PlatformMysqlImpl() {
47 super();
48 initialize();
49 }
50
51
52
53
54 private void initialize() {
55 setSchemaDomainMapping(new Domain(SchemaType.NUMERIC, "DECIMAL"));
56 setSchemaDomainMapping(new Domain(SchemaType.LONGVARCHAR, "MEDIUMTEXT"));
57 setSchemaDomainMapping(new Domain(SchemaType.DATE, "DATETIME"));
58 setSchemaDomainMapping(new Domain(SchemaType.BINARY, "BLOB"));
59 setSchemaDomainMapping(new Domain(SchemaType.VARBINARY, "MEDIUMBLOB"));
60 setSchemaDomainMapping(new Domain(SchemaType.LONGVARBINARY, "LONGBLOB"));
61 setSchemaDomainMapping(new Domain(SchemaType.BLOB, "LONGBLOB"));
62 setSchemaDomainMapping(new Domain(SchemaType.CLOB, "LONGTEXT"));
63 setSchemaDomainMapping(new Domain(SchemaType.TIMESTAMP, "DATETIME"));
64 }
65
66
67
68
69 @Override
70 public String getAutoIncrement() {
71 return "AUTO_INCREMENT";
72 }
73
74
75
76
77 @Override
78 public boolean hasSize(String sqlType) {
79 return !("MEDIUMTEXT".equals(sqlType) || "LONGTEXT".equals(sqlType) || "BLOB".equals(sqlType) || "MEDIUMBLOB".equals(sqlType) || "LONGBLOB".equals(sqlType));
80 }
81
82 protected String getBaseUrl(String url) {
83 if (url == null) {
84 return null;
85 }
86 url = url.trim();
87 if (!url.startsWith("jdbc:mysql://")) {
88 return null;
89 }
90 int count = StringUtils.countMatches(url, "/");
91 if (count != 3) {
92 return null;
93 }
94 int pos = url.lastIndexOf("/");
95 return url.substring(0, pos + 1);
96 }
97
98 protected String getOptions(String url) {
99 int pos = url.indexOf("?");
100 if (pos == -1) {
101 return "";
102 }
103 return url.substring(pos);
104 }
105
106
107
108
109 @Override
110 public String getServerUrl(String url) {
111 String baseUrl = getBaseUrl(url);
112 if (baseUrl == null) {
113 return url;
114 }
115 String options = getOptions(url);
116 return baseUrl + options;
117 }
118
119 @Override
120 public String getSchemaName(String artifactId) {
121 String s = super.getSchemaName(artifactId);
122 return s.toLowerCase();
123 }
124
125 @Override
126 public String filterInvalidDefaultValues(String defaultValue) {
127 if (defaultValue == null) {
128 return null;
129 }
130 defaultValue = defaultValue.replace("SYS_GUID()", "");
131 defaultValue = defaultValue.replace("SYSDATE", "");
132 defaultValue = defaultValue.replace("USERENV(\'SESSIONID\')", "");
133 return defaultValue.trim();
134 }
135
136 @Override
137 public Long getSequenceNextVal(Connection con, String schema, String sequenceName) {
138 try {
139 PreparedStatement ps = con.prepareStatement("SELECT auto_increment FROM information_schema.tables WHERE table_schema = ? AND table_name = ?");
140 Long nextVal = 0L;
141 ps.setString(1, schema);
142 ps.setString(2, sequenceName);
143 ResultSet rs = ps.executeQuery();
144 if (rs.next()) {
145 nextVal = rs.getLong(1);
146 }
147 rs.close();
148 ps.close();
149 System.out.println("Next Val for " + schema + "." + sequenceName + "=" + nextVal);
150 return nextVal;
151 } catch (SQLException ex) {
152 System.err.println("Unable to extract sequence definition: " + schema + "." + sequenceName);
153 ex.printStackTrace();
154 return 0L;
155 }
156 }
157
158 @Override
159 public String getViewDefinition(Connection con, String schema, String viewName) {
160 try {
161 PreparedStatement ps = con.prepareStatement("SELECT view_definition FROM information_schema.views WHERE table_schema = ? AND table_name = ?");
162 String definition = "";
163 ps.setString(1, schema);
164 ps.setString(2, viewName);
165 ResultSet rs = ps.executeQuery();
166 if (rs.next()) {
167 definition = rs.getString(1);
168 }
169 rs.close();
170 ps.close();
171 return definition;
172 } catch (SQLException ex) {
173 System.err.println("Unable to extract view definition: " + schema + "." + viewName);
174 ex.printStackTrace();
175 return "";
176 }
177 }
178
179 private boolean isSequence(String sequenceName) {
180 return sequenceName.toUpperCase().startsWith("SEQ_") || sequenceName.toUpperCase().startsWith("SEQUENCE_") || sequenceName.toUpperCase().endsWith("_SEQ")
181 || sequenceName.toUpperCase().endsWith("_SEQUENCE") || sequenceName.toUpperCase().endsWith("_ID") || sequenceName.toUpperCase().endsWith("_S");
182 }
183
184 @Override
185 public List<String> getSequenceNames(DatabaseMetaData dbMetaData, String databaseSchema) throws SQLException {
186
187
188 List<String> sequenceList = new ArrayList<String>();
189
190 List<String> tableList = getTableNames(dbMetaData, databaseSchema);
191
192 for (String tableName : tableList) {
193
194 if (isSequence(tableName))
195 sequenceList.add(tableName);
196
197 }
198 return sequenceList;
199 }
200
201 @Override
202 public SqlProducer getSqlProducer() {
203 return new MySqlProducer();
204 }
205
206 }