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