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