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