1 19 20 21 package org.apache.cayenne.dba.hsqldb; 22 23 import java.sql.PreparedStatement ; 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 27 import org.apache.cayenne.CayenneRuntimeException; 28 import org.apache.cayenne.access.DataNode; 29 import org.apache.cayenne.access.types.DefaultType; 30 import org.apache.cayenne.access.types.ExtendedTypeMap; 31 import org.apache.cayenne.dba.JdbcAdapter; 32 import org.apache.cayenne.map.DbAttribute; 33 import org.apache.cayenne.map.DbEntity; 34 import org.apache.cayenne.map.DbJoin; 35 import org.apache.cayenne.map.DbRelationship; 36 import org.apache.cayenne.query.Query; 37 import org.apache.cayenne.query.SQLAction; 38 39 57 public class HSQLDBAdapter extends JdbcAdapter { 58 59 protected void configureExtendedTypes(ExtendedTypeMap map) { 60 super.configureExtendedTypes(map); 61 map.registerType(new ShortType()); 62 map.registerType(new ByteType()); 63 } 64 65 70 protected String getTableName(DbEntity entity) { 71 return entity.getFullyQualifiedName(); 72 } 73 74 79 protected String getSchemaName(DbEntity entity) { 80 if (entity.getSchema() != null && entity.getSchema().length() > 0) { 81 return entity.getSchema() + "."; 82 } 83 84 return ""; 85 } 86 87 92 public SQLAction getAction(Query query, DataNode node) { 93 return query 94 .createSQLAction(new HSQLActionBuilder(this, node.getEntityResolver())); 95 } 96 97 102 public String createUniqueConstraint(DbEntity source, Collection columns) { 103 if (columns == null || columns.isEmpty()) { 104 throw new CayenneRuntimeException( 105 "Can't create UNIQUE constraint - no columns specified."); 106 } 107 108 String srcName = getTableName(source); 109 110 StringBuffer buf = new StringBuffer (); 111 112 buf.append("ALTER TABLE ").append(srcName); 113 buf.append(" ADD CONSTRAINT "); 114 115 buf.append(getSchemaName(source)); 116 buf.append("U_"); 117 buf.append(source.getName()); 118 buf.append("_"); 119 buf.append((long) (System.currentTimeMillis() / (Math.random() * 100000))); 120 buf.append(" UNIQUE ("); 121 122 Iterator it = columns.iterator(); 123 DbAttribute first = (DbAttribute) it.next(); 124 buf.append(first.getName()); 125 126 while (it.hasNext()) { 127 DbAttribute next = (DbAttribute) it.next(); 128 buf.append(", "); 129 buf.append(next.getName()); 130 } 131 132 buf.append(")"); 133 134 return buf.toString(); 135 } 136 137 142 public String createFkConstraint(DbRelationship rel) { 143 StringBuffer buf = new StringBuffer (); 144 StringBuffer refBuf = new StringBuffer (); 145 146 String srcName = getTableName((DbEntity) rel.getSourceEntity()); 147 String dstName = getTableName((DbEntity) rel.getTargetEntity()); 148 149 buf.append("ALTER TABLE "); 150 buf.append(srcName); 151 152 buf.append(" ADD CONSTRAINT "); 154 buf.append(getSchemaName((DbEntity) rel.getSourceEntity())); 155 buf.append("C_"); 156 buf.append(rel.getSourceEntity().getName()); 157 buf.append("_"); 158 buf.append((long) (System.currentTimeMillis() / (Math.random() * 100000))); 159 160 buf.append(" FOREIGN KEY ("); 161 162 Iterator jit = rel.getJoins().iterator(); 163 boolean first = true; 164 while (jit.hasNext()) { 165 DbJoin join = (DbJoin) jit.next(); 166 if (!first) { 167 buf.append(", "); 168 refBuf.append(", "); 169 } 170 else 171 first = false; 172 173 buf.append(join.getSourceName()); 174 refBuf.append(join.getTargetName()); 175 } 176 177 buf.append(") REFERENCES "); 178 buf.append(dstName); 179 buf.append(" ("); 180 buf.append(refBuf.toString()); 181 buf.append(')'); 182 183 buf.append(" ON DELETE CASCADE"); 185 186 return buf.toString(); 187 } 188 189 194 public String createTable(DbEntity ent) { 195 197 String sql = super.createTable(ent); 198 199 if (sql != null && sql.toUpperCase().startsWith("CREATE TABLE ")) { 200 sql = "CREATE CACHED TABLE " + sql.substring("CREATE TABLE ".length()); 201 } 202 203 return sql; 204 } 205 206 final class ShortType extends DefaultType { 207 208 ShortType() { 209 super(Short .class.getName()); 210 } 211 212 public void setJdbcObject( 213 PreparedStatement st, 214 Object val, 215 int pos, 216 int type, 217 int precision) throws Exception { 218 219 if (val == null) { 220 super.setJdbcObject(st, val, pos, type, precision); 221 } 222 else { 223 224 short s = ((Number ) val).shortValue(); 225 st.setShort(pos, s); 226 } 227 } 228 } 229 230 final class ByteType extends DefaultType { 231 232 ByteType() { 233 super(Byte .class.getName()); 234 } 235 236 public void setJdbcObject( 237 PreparedStatement st, 238 Object val, 239 int pos, 240 int type, 241 int precision) throws Exception { 242 243 if (val == null) { 244 super.setJdbcObject(st, val, pos, type, precision); 245 } 246 else { 247 248 byte b = ((Number ) val).byteValue(); 249 st.setByte(pos, b); 250 } 251 } 252 } 253 } 254 | Popular Tags |