1 19 20 package org.apache.cayenne.dba.mysql; 21 22 import java.sql.Types ; 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.Comparator ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 29 import org.apache.cayenne.access.DataNode; 30 import org.apache.cayenne.access.types.ByteArrayType; 31 import org.apache.cayenne.access.types.CharType; 32 import org.apache.cayenne.access.types.ExtendedTypeMap; 33 import org.apache.cayenne.dba.JdbcAdapter; 34 import org.apache.cayenne.dba.PkGenerator; 35 import org.apache.cayenne.map.DbAttribute; 36 import org.apache.cayenne.map.DbEntity; 37 import org.apache.cayenne.map.DbRelationship; 38 import org.apache.cayenne.query.Query; 39 import org.apache.cayenne.query.SQLAction; 40 41 63 public class MySQLAdapter extends JdbcAdapter { 64 65 public MySQLAdapter() { 66 this.setSupportsFkConstraints(false); 68 this.setSupportsUniqueConstraints(true); 69 this.setSupportsGeneratedKeys(true); 70 } 71 72 77 public SQLAction getAction(Query query, DataNode node) { 78 return query.createSQLAction(new MySQLActionBuilder(this, node 79 .getEntityResolver())); 80 } 81 82 public String dropTable(DbEntity entity) { 83 return "DROP TABLE IF EXISTS " + entity.getFullyQualifiedName() + " CASCADE"; 84 } 85 86 90 protected void configureExtendedTypes(ExtendedTypeMap map) { 91 super.configureExtendedTypes(map); 92 93 map.registerType(new CharType(false, false)); 98 map.registerType(new ByteArrayType(false, false)); 99 } 100 101 public DbAttribute buildAttribute( 102 String name, 103 String typeName, 104 int type, 105 int size, 106 int precision, 107 boolean allowNulls) { 108 109 if (typeName != null) { 110 typeName = typeName.toLowerCase(); 111 } 112 113 if (type == Types.OTHER) { 116 if ("longblob".equals(typeName)) { 117 type = Types.BLOB; 118 } 119 else if ("mediumblob".equals(typeName)) { 120 type = Types.BLOB; 121 } 122 else if ("blob".equals(typeName)) { 123 type = Types.BLOB; 124 } 125 else if ("tinyblob".equals(typeName)) { 126 type = Types.VARBINARY; 127 } 128 else if ("longtext".equals(typeName)) { 129 type = Types.CLOB; 130 } 131 else if ("mediumtext".equals(typeName)) { 132 type = Types.CLOB; 133 } 134 else if ("text".equals(typeName)) { 135 type = Types.CLOB; 136 } 137 else if ("tinytext".equals(typeName)) { 138 type = Types.VARCHAR; 139 } 140 } 141 else if (typeName != null && typeName.endsWith(" unsigned")) { 143 if (typeName.equals("int unsigned") 146 || typeName.equals("integer unsigned") 147 || typeName.equals("mediumint unsigned")) { 148 type = Types.BIGINT; 149 } 150 } 153 154 return super.buildAttribute(name, typeName, type, size, precision, allowNulls); 155 } 156 157 161 public String tableTypeForView() { 162 return null; 163 } 164 165 169 protected PkGenerator createPkGenerator() { 170 return new MySQLPkGenerator(); 171 } 172 173 177 public String createTable(DbEntity entity) { 178 String ddlSQL = super.createTable(entity); 179 ddlSQL += " ENGINE=InnoDB"; 181 return ddlSQL; 182 } 183 184 191 protected void createTableAppendPKClause(StringBuffer sqlBuffer, DbEntity entity) { 193 194 List pkList = new ArrayList (entity.getPrimaryKey()); 196 Collections.sort(pkList, new PKComparator()); 197 198 Iterator pkit = pkList.iterator(); 199 if (pkit.hasNext()) { 200 201 sqlBuffer.append(", PRIMARY KEY ("); 202 boolean firstPk = true; 203 while (pkit.hasNext()) { 204 if (firstPk) 205 firstPk = false; 206 else 207 sqlBuffer.append(", "); 208 209 DbAttribute at = (DbAttribute) pkit.next(); 210 sqlBuffer.append(at.getName()); 211 } 212 sqlBuffer.append(')'); 213 } 214 215 if (supportsFkConstraints()) { 219 Iterator relationships = entity.getRelationships().iterator(); 220 while (relationships.hasNext()) { 221 DbRelationship relationship = (DbRelationship) relationships.next(); 222 if (relationship.getJoins().size() > 0 223 && relationship.isToPK() 224 && !relationship.isToDependentPK()) { 225 226 sqlBuffer.append(", KEY ("); 227 228 Iterator columns = relationship.getSourceAttributes().iterator(); 229 DbAttribute column = (DbAttribute) columns.next(); 230 sqlBuffer.append(column.getName()); 231 232 while (columns.hasNext()) { 233 column = (DbAttribute) columns.next(); 234 sqlBuffer.append(", ").append(column.getName()); 235 } 236 237 sqlBuffer.append(")"); 238 } 239 } 240 } 241 } 242 243 246 protected void createTableAppendColumn(StringBuffer sqlBuffer, DbAttribute column) { 247 super.createTableAppendColumn(sqlBuffer, column); 248 249 if (column.isGenerated()) { 250 sqlBuffer.append(" AUTO_INCREMENT"); 251 } 252 } 253 254 final class PKComparator implements Comparator { 255 256 public int compare(Object o1, Object o2) { 257 DbAttribute a1 = (DbAttribute) o1; 258 DbAttribute a2 = (DbAttribute) o2; 259 if (a1.isGenerated() != a2.isGenerated()) { 260 return a1.isGenerated() ? -1 : 1; 261 } 262 else { 263 return a1.getName().compareTo(a2.getName()); 264 } 265 } 266 } 267 } 268 | Popular Tags |