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.PreparedStatement;
24 import java.sql.ResultSet;
25 import java.sql.SQLException;
26
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.torque.engine.database.model.Domain;
29 import org.apache.torque.engine.database.model.SchemaType;
30
31
32
33
34
35
36
37 public class PlatformMysqlImpl extends PlatformDefaultImpl {
38
39
40
41 public PlatformMysqlImpl() {
42 super();
43 initialize();
44 }
45
46
47
48
49 private void initialize() {
50 setSchemaDomainMapping(new Domain(SchemaType.NUMERIC, "DECIMAL"));
51 setSchemaDomainMapping(new Domain(SchemaType.LONGVARCHAR, "MEDIUMTEXT"));
52 setSchemaDomainMapping(new Domain(SchemaType.DATE, "DATETIME"));
53 setSchemaDomainMapping(new Domain(SchemaType.BINARY, "BLOB"));
54 setSchemaDomainMapping(new Domain(SchemaType.VARBINARY, "MEDIUMBLOB"));
55 setSchemaDomainMapping(new Domain(SchemaType.LONGVARBINARY, "LONGBLOB"));
56 setSchemaDomainMapping(new Domain(SchemaType.BLOB, "LONGBLOB"));
57 setSchemaDomainMapping(new Domain(SchemaType.CLOB, "LONGTEXT"));
58 }
59
60
61
62
63 public String getAutoIncrement() {
64 return "AUTO_INCREMENT";
65 }
66
67
68
69
70 public boolean hasSize(String sqlType) {
71 return !("MEDIUMTEXT".equals(sqlType) || "LONGTEXT".equals(sqlType) || "BLOB".equals(sqlType) || "MEDIUMBLOB".equals(sqlType) || "LONGBLOB".equals(sqlType));
72 }
73
74 protected String getBaseUrl(String url) {
75 if (url == null) {
76 return null;
77 }
78 url = url.trim();
79 if (!url.startsWith("jdbc:mysql://")) {
80 return null;
81 }
82 int count = StringUtils.countMatches(url, "/");
83 if (count != 3) {
84 return null;
85 }
86 int pos = url.lastIndexOf("/");
87 return url.substring(0, pos + 1);
88 }
89
90 protected String getOptions(String url) {
91 int pos = url.indexOf("?");
92 if (pos == -1) {
93 return "";
94 }
95 return url.substring(pos);
96 }
97
98
99
100
101 @Override
102 public String getServerUrl(String url) {
103 String baseUrl = getBaseUrl(url);
104 if (baseUrl == null) {
105 return url;
106 }
107 String options = getOptions(url);
108 return baseUrl + options;
109 }
110
111 @Override
112 public String getSchemaName(String artifactId) {
113 String s = super.getSchemaName(artifactId);
114 return s.toLowerCase();
115 }
116
117 @Override
118 public String filterInvalidDefaultValues(String defaultValue) {
119 if (defaultValue == null) {
120 return null;
121 }
122 defaultValue = defaultValue.replace("SYS_GUID()", "");
123 defaultValue = defaultValue.replace("SYSDATE", "");
124 defaultValue = defaultValue.replace("USERENV(\'SESSIONID\')", "");
125 return defaultValue.trim();
126 }
127
128 @Override
129 public Long getSequenceNextVal(Connection con, String schema, String sequenceName) {
130 try {
131 PreparedStatement ps = con.prepareStatement("SELECT auto_increment FROM information_schema.tables WHERE table_schema = ? AND table_name = ?");
132 Long nextVal = 0L;
133 ps.setString(1, schema);
134 ps.setString(2, sequenceName);
135 ResultSet rs = ps.executeQuery();
136 if (rs.next()) {
137 nextVal = rs.getLong(1);
138 }
139 rs.close();
140 ps.close();
141 System.out.println("Next Val for " + schema + "." + sequenceName + "=" + nextVal);
142 return nextVal;
143 } catch (SQLException ex) {
144 System.err.println("Unable to extract sequence definition: " + schema + "." + sequenceName);
145 ex.printStackTrace();
146 return 0L;
147 }
148 }
149
150 @Override
151 public String getViewDefinition(Connection con, String schema, String viewName) {
152 try {
153 PreparedStatement ps = con.prepareStatement("SELECT view_definition FROM information_schema.views WHERE table_schema = ? AND table_name = ?");
154 String definition = "";
155 ps.setString(1, schema);
156 ps.setString(2, viewName);
157 ResultSet rs = ps.executeQuery();
158 if (rs.next()) {
159 definition = rs.getString(1);
160 }
161 rs.close();
162 ps.close();
163 return definition;
164 } catch (SQLException ex) {
165 System.err.println("Unable to extract view definition: " + schema + "." + viewName);
166 ex.printStackTrace();
167 return "";
168 }
169 }
170
171 }