1 19 20 package org.apache.cayenne.dba.openbase; 21 22 import java.sql.PreparedStatement ; 23 import java.sql.ResultSet ; 24 import java.sql.Types ; 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.trans.QualifierTranslator; 30 import org.apache.cayenne.access.trans.QueryAssembler; 31 import org.apache.cayenne.access.types.CharType; 32 import org.apache.cayenne.access.types.DefaultType; 33 import org.apache.cayenne.access.types.ExtendedTypeMap; 34 import org.apache.cayenne.dba.JdbcAdapter; 35 import org.apache.cayenne.dba.PkGenerator; 36 import org.apache.cayenne.dba.TypesMapping; 37 import org.apache.cayenne.map.DbAttribute; 38 import org.apache.cayenne.map.DbEntity; 39 import org.apache.cayenne.map.DbJoin; 40 import org.apache.cayenne.map.DbRelationship; 41 import org.apache.cayenne.map.DerivedDbEntity; 42 import org.apache.cayenne.query.Query; 43 import org.apache.cayenne.query.SQLAction; 44 45 63 public class OpenBaseAdapter extends JdbcAdapter { 64 65 public OpenBaseAdapter() { 66 this.setSupportsUniqueConstraints(false); 68 } 69 70 75 public SQLAction getAction(Query query, DataNode node) { 76 return query.createSQLAction(new OpenBaseActionBuilder(this, node 77 .getEntityResolver())); 78 } 79 80 protected void configureExtendedTypes(ExtendedTypeMap map) { 81 super.configureExtendedTypes(map); 82 83 map.registerType(new OpenBaseByteType()); 86 87 map.registerType(new OpenBaseCharType()); 88 } 89 90 public DbAttribute buildAttribute( 91 String name, 92 String typeName, 93 int type, 94 int size, 95 int scale, 96 boolean allowNulls) { 97 98 if (type == Types.CHAR) { 101 type = Types.VARCHAR; 102 } 103 104 return super.buildAttribute(name, typeName, type, size, scale, allowNulls); 105 } 106 107 110 public String getBatchTerminator() { 111 return "go"; 112 } 113 114 117 public String tableTypeForView() { 118 return null; 120 } 121 122 125 public QualifierTranslator getQualifierTranslator(QueryAssembler queryAssembler) { 126 return new OpenBaseQualifierTranslator(queryAssembler); 127 } 128 129 134 protected PkGenerator createPkGenerator() { 135 return new OpenBasePkGenerator(); 136 } 137 138 142 public String createTable(DbEntity ent) { 143 if (ent instanceof DerivedDbEntity) { 146 throw new CayenneRuntimeException( 147 "Can't create table for derived DbEntity '" + ent.getName() + "'."); 148 } 149 150 StringBuffer buf = new StringBuffer (); 151 buf.append("CREATE TABLE ").append(ent.getFullyQualifiedName()).append(" ("); 152 153 Iterator it = ent.getAttributes().iterator(); 155 boolean first = true; 156 while (it.hasNext()) { 157 if (first) { 158 first = false; 159 } 160 else { 161 buf.append(", "); 162 } 163 164 DbAttribute at = (DbAttribute) it.next(); 165 166 if (at.getType() == TypesMapping.NOT_DEFINED) { 168 throw new CayenneRuntimeException( 169 "Undefined type for attribute '" 170 + ent.getFullyQualifiedName() 171 + "." 172 + at.getName() 173 + "'."); 174 } 175 176 String [] types = externalTypesForJdbcType(at.getType()); 177 if (types == null || types.length == 0) { 178 throw new CayenneRuntimeException( 179 "Undefined type for attribute '" 180 + ent.getFullyQualifiedName() 181 + "." 182 + at.getName() 183 + "': " 184 + at.getType()); 185 } 186 187 String type = types[0]; 188 buf.append(at.getName()).append(' ').append(type); 189 190 if (TypesMapping.supportsLength(at.getType())) { 192 int len = at.getMaxLength(); 193 int scale = TypesMapping.isDecimal(at.getType()) ? at.getScale() : -1; 194 195 if (scale > len) { 197 scale = -1; 198 } 199 200 if (len > 0) { 201 buf.append('(').append(len); 202 203 if (scale >= 0) { 204 buf.append(", ").append(scale); 205 } 206 207 buf.append(')'); 208 } 209 } 210 211 if (at.isMandatory()) { 212 buf.append(" NOT NULL"); 213 } 214 else { 215 buf.append(" NULL"); 216 } 217 } 218 219 buf.append(')'); 220 return buf.toString(); 221 } 222 223 227 public String createFkConstraint(DbRelationship rel) { 228 StringBuffer buf = new StringBuffer (); 229 230 233 DbEntity sourceEntity = (DbEntity) rel.getSourceEntity(); 234 DbEntity targetEntity = (DbEntity) rel.getTargetEntity(); 235 String toMany = (!rel.isToMany()) ? "'1'" : "'0'"; 236 237 240 int joinsLen = rel.getJoins().size(); 241 if (joinsLen == 0) { 242 throw new CayenneRuntimeException( 243 "Relationship has no joins: " + rel.getName()); 244 } 245 else if (joinsLen > 1) { 246 } 248 249 DbJoin join = (DbJoin) rel.getJoins().get(0); 250 251 buf 252 .append("INSERT INTO _SYS_RELATIONSHIP (") 253 .append("dest_table, dest_column, source_table, source_column, ") 254 .append("block_delete, cascade_delete, one_to_many, operator, relationshipName") 255 .append(") VALUES ('") 256 .append(sourceEntity.getFullyQualifiedName()) 257 .append("', '") 258 .append(join.getSourceName()) 259 .append("', '") 260 .append(targetEntity.getFullyQualifiedName()) 261 .append("', '") 262 .append(join.getTargetName()) 263 .append("', 0, 0, ") 264 .append(toMany) 265 .append(", '=', '") 266 .append(rel.getName()) 267 .append("')"); 268 269 return buf.toString(); 270 } 271 272 static class OpenBaseByteType extends DefaultType { 275 OpenBaseByteType() { 276 super(Byte .class.getName()); 277 } 278 279 public Object materializeObject(ResultSet rs, int index, int type) 280 throws Exception { 281 282 int val = rs.getInt(index); 284 return (rs.wasNull()) ? null : new Byte ((byte) val); 285 } 286 } 287 288 static class OpenBaseCharType extends CharType { 289 OpenBaseCharType() { 290 super(false, true); 291 } 292 293 public void setJdbcObject( 294 PreparedStatement st, 295 Object val, 296 int pos, 297 int type, 298 int precision) 299 throws Exception { 300 301 if (type == Types.CLOB || type == Types.LONGVARCHAR) { 304 st.setString(pos, (String ) val); 305 } 306 else { 307 super.setJdbcObject(st, val, pos, type, precision); 308 } 309 } 310 } 311 } 312 | Popular Tags |