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