1 56 57 package org.objectstyle.cayenne.dba.hsqldb; 58 59 import java.util.Collection ; 60 import java.util.Iterator ; 61 62 import org.objectstyle.cayenne.CayenneRuntimeException; 63 import org.objectstyle.cayenne.dba.JdbcAdapter; 64 import org.objectstyle.cayenne.map.DbAttribute; 65 import org.objectstyle.cayenne.map.DbEntity; 66 import org.objectstyle.cayenne.map.DbJoin; 67 import org.objectstyle.cayenne.map.DbRelationship; 68 69 87 public class HSQLDBAdapter extends JdbcAdapter { 88 89 94 public String createUniqueConstraint(DbEntity source, Collection columns) { 95 if (columns == null || columns.isEmpty()) { 96 throw new CayenneRuntimeException( 97 "Can't create UNIQUE constraint - no columns specified."); 98 } 99 100 String srcName = source.getName(); 102 103 StringBuffer buf = new StringBuffer (); 104 105 buf.append("ALTER TABLE ").append(srcName); 106 buf.append(" ADD CONSTRAINT "); 107 buf.append("U_"); 108 buf.append(srcName); 109 buf.append("_"); 110 buf.append((long) (System.currentTimeMillis() / (Math.random() * 100000))); 111 buf.append(" UNIQUE ("); 112 113 Iterator it = columns.iterator(); 114 DbAttribute first = (DbAttribute) it.next(); 115 buf.append(first.getName()); 116 117 while (it.hasNext()) { 118 DbAttribute next = (DbAttribute) it.next(); 119 buf.append(", "); 120 buf.append(next.getName()); 121 } 122 123 buf.append(")"); 124 125 return buf.toString(); 126 } 127 128 133 public String createFkConstraint(DbRelationship rel) { 134 StringBuffer buf = new StringBuffer (); 135 StringBuffer refBuf = new StringBuffer (); 136 137 String srcName = ((DbEntity) rel.getSourceEntity()).getName(); 139 String dstName = ((DbEntity) rel.getTargetEntity()).getName(); 140 141 buf.append("ALTER TABLE "); 142 buf.append(srcName); 143 144 buf.append(" ADD CONSTRAINT "); 146 buf.append("C_"); 147 buf.append(srcName); 148 buf.append("_"); 149 buf.append((long) (System.currentTimeMillis() / (Math.random() * 100000))); 150 151 buf.append(" FOREIGN KEY ("); 152 153 Iterator jit = rel.getJoins().iterator(); 154 boolean first = true; 155 while (jit.hasNext()) { 156 DbJoin join = (DbJoin) jit.next(); 157 if (!first) { 158 buf.append(", "); 159 refBuf.append(", "); 160 } 161 else 162 first = false; 163 164 buf.append(join.getSourceName()); 165 refBuf.append(join.getTargetName()); 166 } 167 168 buf.append(") REFERENCES "); 169 buf.append(dstName); 170 buf.append(" ("); 171 buf.append(refBuf.toString()); 172 buf.append(')'); 173 174 buf.append(" ON DELETE CASCADE"); 176 177 return buf.toString(); 178 } 179 180 186 public String dropTable(DbEntity ent) { 187 return "DROP TABLE " + ent.getName(); 189 } 190 191 197 public String createTable(DbEntity ent) { 198 String sql = super.createTable(ent); 199 200 if (sql != null && sql.toUpperCase().startsWith("CREATE TABLE ")) { 201 sql = "CREATE CACHED TABLE " + sql.substring("CREATE TABLE ".length()); 202 } 203 204 String fqnCreate = "CREATE CACHED TABLE " + ent.getFullyQualifiedName() + " ("; 206 if (sql != null && sql.toUpperCase().startsWith(fqnCreate)) { 207 sql = "CREATE CACHED TABLE " + ent.getName() + " (" + sql.substring(fqnCreate.length()); 208 } 209 210 return sql; 211 } 212 } | Popular Tags |