1 56 package org.objectstyle.cayenne.dba.openbase; 57 58 import java.sql.PreparedStatement ; 59 import java.sql.ResultSet ; 60 import java.sql.Types ; 61 import java.util.Iterator ; 62 63 import org.apache.log4j.Logger; 64 import org.objectstyle.cayenne.CayenneRuntimeException; 65 import org.objectstyle.cayenne.access.trans.QualifierTranslator; 66 import org.objectstyle.cayenne.access.trans.QueryAssembler; 67 import org.objectstyle.cayenne.access.types.CharType; 68 import org.objectstyle.cayenne.access.types.DefaultType; 69 import org.objectstyle.cayenne.access.types.ExtendedTypeMap; 70 import org.objectstyle.cayenne.dba.JdbcAdapter; 71 import org.objectstyle.cayenne.dba.PkGenerator; 72 import org.objectstyle.cayenne.dba.TypesMapping; 73 import org.objectstyle.cayenne.map.DbAttribute; 74 import org.objectstyle.cayenne.map.DbEntity; 75 import org.objectstyle.cayenne.map.DbJoin; 76 import org.objectstyle.cayenne.map.DbRelationship; 77 import org.objectstyle.cayenne.map.DerivedDbEntity; 78 79 97 public class OpenBaseAdapter extends JdbcAdapter { 98 private static Logger logObj = Logger.getLogger(OpenBaseAdapter.class); 99 100 public OpenBaseAdapter() { 101 this.setSupportsUniqueConstraints(false); 103 } 104 105 protected void configureExtendedTypes(ExtendedTypeMap map) { 106 super.configureExtendedTypes(map); 107 108 map.registerType(new OpenBaseByteType()); 111 112 map.registerType(new OpenBaseCharType()); 113 } 114 115 public DbAttribute buildAttribute( 116 String name, 117 String typeName, 118 int type, 119 int size, 120 int precision, 121 boolean allowNulls) { 122 123 if (type == Types.CHAR) { 126 type = Types.VARCHAR; 127 } 128 129 return super.buildAttribute(name, typeName, type, size, precision, allowNulls); 130 } 131 132 135 public String getBatchTerminator() { 136 return "go"; 137 } 138 139 142 public String tableTypeForView() { 143 return null; 145 } 146 147 150 public QualifierTranslator getQualifierTranslator(QueryAssembler queryAssembler) { 151 return new OpenBaseQualifierTranslator(queryAssembler); 152 } 153 154 159 protected PkGenerator createPkGenerator() { 160 return new OpenBasePkGenerator(); 161 } 162 163 167 public String createTable(DbEntity ent) { 168 if (ent instanceof DerivedDbEntity) { 171 throw new CayenneRuntimeException( 172 "Can't create table for derived DbEntity '" + ent.getName() + "'."); 173 } 174 175 StringBuffer buf = new StringBuffer (); 176 buf.append("CREATE TABLE ").append(ent.getFullyQualifiedName()).append(" ("); 177 178 Iterator it = ent.getAttributes().iterator(); 180 boolean first = true; 181 while (it.hasNext()) { 182 if (first) { 183 first = false; 184 } 185 else { 186 buf.append(", "); 187 } 188 189 DbAttribute at = (DbAttribute) it.next(); 190 191 if (at.getType() == TypesMapping.NOT_DEFINED) { 193 throw new CayenneRuntimeException( 194 "Undefined type for attribute '" 195 + ent.getFullyQualifiedName() 196 + "." 197 + at.getName() 198 + "'."); 199 } 200 201 String [] types = externalTypesForJdbcType(at.getType()); 202 if (types == null || types.length == 0) { 203 throw new CayenneRuntimeException( 204 "Undefined type for attribute '" 205 + ent.getFullyQualifiedName() 206 + "." 207 + at.getName() 208 + "': " 209 + at.getType()); 210 } 211 212 String type = types[0]; 213 buf.append(at.getName()).append(' ').append(type); 214 215 if (TypesMapping.supportsLength(at.getType())) { 217 int len = at.getMaxLength(); 218 int prec = TypesMapping.isDecimal(at.getType()) ? at.getPrecision() : -1; 219 220 if (prec > len) { 222 prec = -1; 223 } 224 225 if (len > 0) { 226 buf.append('(').append(len); 227 228 if (prec >= 0) { 229 buf.append(", ").append(prec); 230 } 231 232 buf.append(')'); 233 } 234 } 235 236 if (at.isMandatory()) { 237 buf.append(" NOT NULL"); 238 } 239 else { 240 buf.append(" NULL"); 241 } 242 } 243 244 buf.append(')'); 245 return buf.toString(); 246 } 247 248 252 public String createFkConstraint(DbRelationship rel) { 253 StringBuffer buf = new StringBuffer (); 254 255 258 DbEntity sourceEntity = (DbEntity) rel.getSourceEntity(); 259 DbEntity targetEntity = (DbEntity) rel.getTargetEntity(); 260 String toMany = (!rel.isToMany()) ? "'1'" : "'0'"; 261 262 265 int joinsLen = rel.getJoins().size(); 266 if (joinsLen == 0) { 267 throw new CayenneRuntimeException( 268 "Relationship has no joins: " + rel.getName()); 269 } 270 else if (joinsLen > 1) { 271 logObj.warn( 272 "Only a single join relationships are supported by OpenBase. Ignoring extra joins."); 273 } 274 275 DbJoin join = (DbJoin) rel.getJoins().get(0); 276 277 buf 278 .append("INSERT INTO _SYS_RELATIONSHIP (") 279 .append("dest_table, dest_column, source_table, source_column, ") 280 .append("block_delete, cascade_delete, one_to_many, operator, relationshipName") 281 .append(") VALUES ('") 282 .append(sourceEntity.getFullyQualifiedName()) 283 .append("', '") 284 .append(join.getSourceName()) 285 .append("', '") 286 .append(targetEntity.getFullyQualifiedName()) 287 .append("', '") 288 .append(join.getTargetName()) 289 .append("', 0, 0, ") 290 .append(toMany) 291 .append(", '=', '") 292 .append(rel.getName()) 293 .append("')"); 294 295 return buf.toString(); 296 } 297 298 static class OpenBaseByteType extends DefaultType { 301 OpenBaseByteType() { 302 super(Byte .class.getName()); 303 } 304 305 public Object materializeObject(ResultSet rs, int index, int type) 306 throws Exception { 307 308 int val = rs.getInt(index); 310 return (rs.wasNull()) ? null : new Byte ((byte) val); 311 } 312 } 313 314 static class OpenBaseCharType extends CharType { 315 OpenBaseCharType() { 316 super(false, true); 317 } 318 319 public void setJdbcObject( 320 PreparedStatement st, 321 Object val, 322 int pos, 323 int type, 324 int precision) 325 throws Exception { 326 327 if (type == Types.CLOB || type == Types.LONGVARCHAR) { 330 st.setString(pos, (String ) val); 331 } 332 else { 333 super.setJdbcObject(st, val, pos, type, precision); 334 } 335 } 336 } 337 } 338 | Popular Tags |