1 19 20 package org.apache.cayenne.dba.db2; 21 22 import java.sql.PreparedStatement ; 23 import java.util.Iterator ; 24 25 import org.apache.cayenne.CayenneRuntimeException; 26 import org.apache.cayenne.access.trans.QualifierTranslator; 27 import org.apache.cayenne.access.trans.QueryAssembler; 28 import org.apache.cayenne.access.types.BooleanType; 29 import org.apache.cayenne.access.types.ByteArrayType; 30 import org.apache.cayenne.access.types.CharType; 31 import org.apache.cayenne.access.types.ExtendedTypeMap; 32 import org.apache.cayenne.dba.JdbcAdapter; 33 import org.apache.cayenne.dba.PkGenerator; 34 import org.apache.cayenne.dba.TypesMapping; 35 import org.apache.cayenne.map.DbAttribute; 36 import org.apache.cayenne.map.DbEntity; 37 import org.apache.cayenne.map.DerivedDbEntity; 38 39 57 public class DB2Adapter extends JdbcAdapter { 58 59 62 protected PkGenerator createPkGenerator() { 63 return new DB2PkGenerator(); 64 } 65 66 protected void configureExtendedTypes(ExtendedTypeMap map) { 67 super.configureExtendedTypes(map); 68 69 map.registerType(new CharType(true, true)); 71 72 map.registerType(new DB2BooleanType()); 74 75 map.registerType(new ByteArrayType(false, false)); 76 } 77 78 82 public String createTable(DbEntity ent) { 83 if (ent instanceof DerivedDbEntity) { 86 throw new CayenneRuntimeException("Can't create table for derived DbEntity '" 87 + ent.getName() 88 + "'."); 89 } 90 91 StringBuffer buf = new StringBuffer (); 92 buf.append("CREATE TABLE ").append(ent.getFullyQualifiedName()).append(" ("); 93 94 Iterator it = ent.getAttributes().iterator(); 96 boolean first = true; 97 while (it.hasNext()) { 98 if (first) { 99 first = false; 100 } 101 else { 102 buf.append(", "); 103 } 104 105 DbAttribute at = (DbAttribute) it.next(); 106 107 if (at.getType() == TypesMapping.NOT_DEFINED) { 109 throw new CayenneRuntimeException("Undefined type for attribute '" 110 + ent.getFullyQualifiedName() 111 + "." 112 + at.getName() 113 + "'."); 114 } 115 116 String [] types = externalTypesForJdbcType(at.getType()); 117 if (types == null || types.length == 0) { 118 throw new CayenneRuntimeException("Undefined type for attribute '" 119 + ent.getFullyQualifiedName() 120 + "." 121 + at.getName() 122 + "': " 123 + at.getType()); 124 } 125 126 String type = types[0]; 127 buf.append(at.getName()).append(' ').append(type); 128 129 if (TypesMapping.supportsLength(at.getType())) { 131 int len = at.getMaxLength(); 132 int scale = TypesMapping.isDecimal(at.getType()) ? at.getScale() : -1; 133 134 if (scale > len) { 136 scale = -1; 137 } 138 139 if (len > 0) { 140 buf.append('(').append(len); 141 142 if (scale >= 0) { 143 buf.append(", ").append(scale); 144 } 145 146 buf.append(')'); 147 } 148 } 149 150 if (at.isMandatory()) { 151 buf.append(" NOT NULL"); 152 } 153 } 154 155 Iterator pkit = ent.getPrimaryKey().iterator(); 157 if (pkit.hasNext()) { 158 if (first) 159 first = false; 160 else 161 buf.append(", "); 162 163 buf.append("PRIMARY KEY ("); 164 boolean firstPk = true; 165 while (pkit.hasNext()) { 166 if (firstPk) 167 firstPk = false; 168 else 169 buf.append(", "); 170 171 DbAttribute at = (DbAttribute) pkit.next(); 172 buf.append(at.getName()); 173 } 174 buf.append(')'); 175 } 176 buf.append(')'); 177 return buf.toString(); 178 } 179 180 183 public QualifierTranslator getQualifierTranslator(QueryAssembler queryAssembler) { 184 return new DB2QualifierTranslator(queryAssembler, "RTRIM"); 185 } 186 187 final class DB2BooleanType extends BooleanType { 188 189 public void setJdbcObject( 190 PreparedStatement st, 191 Object val, 192 int pos, 193 int type, 194 int precision) throws Exception { 195 196 if (val != null) { 197 st.setInt(pos, ((Boolean ) val).booleanValue() ? 1 : 0); 198 } 199 else { 200 st.setNull(pos, type); 201 } 202 } 203 } 204 205 } 206 | Popular Tags |