1 56 57 package org.objectstyle.cayenne.dba.db2; 58 59 import java.sql.PreparedStatement ; 60 import java.util.Iterator ; 61 62 import org.objectstyle.cayenne.CayenneRuntimeException; 63 import org.objectstyle.cayenne.access.DataNode; 64 import org.objectstyle.cayenne.access.trans.QualifierTranslator; 65 import org.objectstyle.cayenne.access.trans.QueryAssembler; 66 import org.objectstyle.cayenne.access.types.BooleanType; 67 import org.objectstyle.cayenne.access.types.CharType; 68 import org.objectstyle.cayenne.access.types.ExtendedTypeMap; 69 import org.objectstyle.cayenne.dba.JdbcAdapter; 70 import org.objectstyle.cayenne.dba.PkGenerator; 71 import org.objectstyle.cayenne.dba.TypesMapping; 72 import org.objectstyle.cayenne.map.DbAttribute; 73 import org.objectstyle.cayenne.map.DbEntity; 74 import org.objectstyle.cayenne.map.DerivedDbEntity; 75 import org.objectstyle.cayenne.query.Query; 76 import org.objectstyle.cayenne.query.SQLAction; 77 78 96 public class DB2Adapter extends JdbcAdapter { 97 98 103 public SQLAction getAction(Query query, DataNode node) { 104 return query.createSQLAction(new DB2ActionBuilder(this, node.getEntityResolver())); 105 } 106 107 110 protected PkGenerator createPkGenerator() { 111 return new DB2PkGenerator(); 112 } 113 114 protected void configureExtendedTypes(ExtendedTypeMap map) { 115 super.configureExtendedTypes(map); 116 117 map.registerType(new CharType(true, true)); 119 120 map.registerType(new DB2BooleanType()); 122 } 123 124 128 public String createTable(DbEntity ent) { 129 if (ent instanceof DerivedDbEntity) { 132 throw new CayenneRuntimeException("Can't create table for derived DbEntity '" 133 + ent.getName() 134 + "'."); 135 } 136 137 StringBuffer buf = new StringBuffer (); 138 buf.append("CREATE TABLE ").append(ent.getFullyQualifiedName()).append(" ("); 139 140 Iterator it = ent.getAttributes().iterator(); 142 boolean first = true; 143 while (it.hasNext()) { 144 if (first) { 145 first = false; 146 } 147 else { 148 buf.append(", "); 149 } 150 151 DbAttribute at = (DbAttribute) it.next(); 152 153 if (at.getType() == TypesMapping.NOT_DEFINED) { 155 throw new CayenneRuntimeException("Undefined type for attribute '" 156 + ent.getFullyQualifiedName() 157 + "." 158 + at.getName() 159 + "'."); 160 } 161 162 String [] types = externalTypesForJdbcType(at.getType()); 163 if (types == null || types.length == 0) { 164 throw new CayenneRuntimeException("Undefined type for attribute '" 165 + ent.getFullyQualifiedName() 166 + "." 167 + at.getName() 168 + "': " 169 + at.getType()); 170 } 171 172 String type = types[0]; 173 buf.append(at.getName()).append(' ').append(type); 174 175 if (TypesMapping.supportsLength(at.getType())) { 177 int len = at.getMaxLength(); 178 int prec = TypesMapping.isDecimal(at.getType()) ? at.getPrecision() : -1; 179 180 if (prec > len) { 182 prec = -1; 183 } 184 185 if (len > 0) { 186 buf.append('(').append(len); 187 188 if (prec >= 0) { 189 buf.append(", ").append(prec); 190 } 191 192 buf.append(')'); 193 } 194 } 195 196 if (at.isMandatory()) { 197 buf.append(" NOT NULL"); 198 } 199 } 200 201 Iterator pkit = ent.getPrimaryKey().iterator(); 203 if (pkit.hasNext()) { 204 if (first) 205 first = false; 206 else 207 buf.append(", "); 208 209 buf.append("PRIMARY KEY ("); 210 boolean firstPk = true; 211 while (pkit.hasNext()) { 212 if (firstPk) 213 firstPk = false; 214 else 215 buf.append(", "); 216 217 DbAttribute at = (DbAttribute) pkit.next(); 218 buf.append(at.getName()); 219 } 220 buf.append(')'); 221 } 222 buf.append(')'); 223 return buf.toString(); 224 } 225 226 229 public QualifierTranslator getQualifierTranslator(QueryAssembler queryAssembler) { 230 return new DB2QualifierTranslator(queryAssembler, "RTRIM"); 231 } 232 233 final class DB2BooleanType extends BooleanType { 234 235 public void setJdbcObject( 236 PreparedStatement st, 237 Object val, 238 int pos, 239 int type, 240 int precision) throws Exception { 241 242 if (val != null) { 243 st.setInt(pos, ((Boolean ) val).booleanValue() ? 1 : 0); 244 } 245 else { 246 st.setNull(pos, type); 247 } 248 } 249 } 250 251 } | Popular Tags |