1 19 20 package org.apache.cayenne.dba.ingres; 21 22 import java.util.Iterator ; 23 24 import org.apache.cayenne.access.trans.TrimmingQualifierTranslator; 25 import org.apache.cayenne.access.trans.QueryAssembler; 26 import org.apache.cayenne.access.trans.QualifierTranslator; 27 import org.apache.cayenne.CayenneRuntimeException; 28 import org.apache.cayenne.access.types.ExtendedTypeMap; 29 import org.apache.cayenne.dba.JdbcAdapter; 30 import org.apache.cayenne.dba.PkGenerator; 31 import org.apache.cayenne.dba.TypesMapping; 32 import org.apache.cayenne.map.DbAttribute; 33 import org.apache.cayenne.map.DbEntity; 34 import org.apache.cayenne.map.DerivedDbEntity; 35 36 50 public class IngresAdapter extends JdbcAdapter { 51 public static final String TRIM_FUNCTION = "TRIM"; 52 53 public QualifierTranslator getQualifierTranslator(QueryAssembler queryAssembler) { 54 return new TrimmingQualifierTranslator( 55 queryAssembler, 56 IngresAdapter.TRIM_FUNCTION); 57 } 58 62 public String createTable(DbEntity ent) { 63 if (ent instanceof DerivedDbEntity) { 66 throw new CayenneRuntimeException("Can't create table for derived DbEntity '" 67 + ent.getName() 68 + "'."); 69 } 70 71 StringBuffer buf = new StringBuffer (); 72 buf.append("CREATE TABLE ").append(ent.getFullyQualifiedName()).append(" ("); 73 74 Iterator it = ent.getAttributes().iterator(); 76 boolean first = true; 77 while (it.hasNext()) { 78 if (first) { 79 first = false; 80 } 81 else { 82 buf.append(", "); 83 } 84 85 DbAttribute at = (DbAttribute) it.next(); 86 87 if (at.getType() == TypesMapping.NOT_DEFINED) { 89 throw new CayenneRuntimeException("Undefined type for attribute '" 90 + ent.getFullyQualifiedName() 91 + "." 92 + at.getName() 93 + "'."); 94 } 95 96 String [] types = externalTypesForJdbcType(at.getType()); 97 if (types == null || types.length == 0) { 98 throw new CayenneRuntimeException("Undefined type for attribute '" 99 + ent.getFullyQualifiedName() 100 + "." 101 + at.getName() 102 + "': " 103 + at.getType()); 104 } 105 106 String type = types[0]; 107 buf.append(at.getName()).append(' ').append(type); 108 109 if (TypesMapping.supportsLength(at.getType())) { 111 int len = at.getMaxLength(); 112 int scale = TypesMapping.isDecimal(at.getType()) ? at.getScale() : -1; 113 114 if (scale > len) { 116 scale = -1; 117 } 118 119 if (len > 0) { 120 buf.append('(').append(len); 121 122 if (scale >= 0) { 123 buf.append(", ").append(scale); 124 } 125 126 buf.append(')'); 127 } 128 } 129 130 if (at.isMandatory()) { 132 buf.append(" NOT NULL"); 133 } 134 } 135 136 Iterator pkit = ent.getPrimaryKey().iterator(); 138 if (pkit.hasNext()) { 139 if (first) 140 first = false; 141 else 142 buf.append(", "); 143 144 buf.append("PRIMARY KEY ("); 145 boolean firstPk = true; 146 while (pkit.hasNext()) { 147 if (firstPk) 148 firstPk = false; 149 else 150 buf.append(", "); 151 152 DbAttribute at = (DbAttribute) pkit.next(); 153 buf.append(at.getName()); 154 } 155 buf.append(')'); 156 } 157 buf.append(')'); 158 return buf.toString(); 159 } 160 161 protected void configureExtendedTypes(ExtendedTypeMap map) { 162 super.configureExtendedTypes(map); 163 map.registerType(new IngresCharType()); 164 } 165 166 169 protected PkGenerator createPkGenerator() { 170 return new IngresPkGenerator(); 171 } 172 173 } 174 | Popular Tags |