1 19 20 package org.apache.cayenne.dba.postgres; 21 22 import java.sql.Types ; 23 import java.util.Iterator ; 24 25 import org.apache.cayenne.CayenneRuntimeException; 26 import org.apache.cayenne.access.DataNode; 27 import org.apache.cayenne.access.trans.QualifierTranslator; 28 import org.apache.cayenne.access.trans.QueryAssembler; 29 import org.apache.cayenne.access.types.CharType; 30 import org.apache.cayenne.access.types.ExtendedTypeMap; 31 import org.apache.cayenne.dba.JdbcAdapter; 32 import org.apache.cayenne.dba.PkGenerator; 33 import org.apache.cayenne.dba.TypesMapping; 34 import org.apache.cayenne.map.DbAttribute; 35 import org.apache.cayenne.map.DbEntity; 36 import org.apache.cayenne.map.DerivedDbEntity; 37 import org.apache.cayenne.query.Query; 38 import org.apache.cayenne.query.SQLAction; 39 40 60 public class PostgresAdapter extends JdbcAdapter { 61 62 public PostgresAdapter() { 63 setSupportsBatchUpdates(true); 64 } 65 66 71 public SQLAction getAction(Query query, DataNode node) { 72 return query.createSQLAction(new PostgresActionBuilder(this, node 73 .getEntityResolver())); 74 } 75 76 80 protected void configureExtendedTypes(ExtendedTypeMap map) { 81 82 super.configureExtendedTypes(map); 83 84 map.registerType(new CharType(true, false)); 85 map.registerType(new PostgresByteArrayType(true, true)); 86 } 87 88 public DbAttribute buildAttribute( 89 String name, 90 String typeName, 91 int type, 92 int size, 93 int scale, 94 boolean allowNulls) { 95 96 if ("bytea".equalsIgnoreCase(typeName)) { 100 type = Types.LONGVARBINARY; 101 } 102 else if ("oid".equals(typeName)) { 104 type = Types.BLOB; 105 } 106 else if ("text".equalsIgnoreCase(typeName)) { 108 type = Types.CLOB; 109 } 110 111 return super.buildAttribute(name, typeName, type, size, scale, allowNulls); 112 } 113 114 121 public String createTable(DbEntity ent) { 122 123 if (ent instanceof DerivedDbEntity) { 126 throw new CayenneRuntimeException("Can't create table for derived DbEntity '" 127 + ent.getName() 128 + "'."); 129 } 130 131 StringBuffer buf = new StringBuffer (); 132 buf.append("CREATE TABLE ").append(ent.getFullyQualifiedName()).append(" ("); 133 134 Iterator it = ent.getAttributes().iterator(); 136 boolean first = true; 137 while (it.hasNext()) { 138 if (first) { 139 first = false; 140 } 141 else { 142 buf.append(", "); 143 } 144 145 DbAttribute at = (DbAttribute) it.next(); 146 147 if (at.getType() == TypesMapping.NOT_DEFINED) { 149 throw new CayenneRuntimeException("Undefined type for attribute '" 150 + ent.getFullyQualifiedName() 151 + "." 152 + at.getName() 153 + "'."); 154 } 155 156 String [] types = externalTypesForJdbcType(at.getType()); 157 if (types == null || types.length == 0) { 158 throw new CayenneRuntimeException("Undefined type for attribute '" 159 + ent.getFullyQualifiedName() 160 + "." 161 + at.getName() 162 + "': " 163 + at.getType()); 164 } 165 166 String type = types[0]; 167 buf.append(at.getName()).append(' ').append(type); 168 169 if (typeSupportsLength(at.getType())) { 171 int len = at.getMaxLength(); 172 int scale = TypesMapping.isDecimal(at.getType()) ? at.getScale() : -1; 173 174 if (scale > len) { 176 scale = -1; 177 } 178 179 if (len > 0) { 180 buf.append('(').append(len); 181 182 if (scale >= 0) { 183 buf.append(", ").append(scale); 184 } 185 186 buf.append(')'); 187 } 188 } 189 190 if (at.isMandatory()) { 191 buf.append(" NOT NULL"); 192 } 193 else { 194 buf.append(" NULL"); 195 } 196 } 197 198 Iterator pkit = ent.getPrimaryKey().iterator(); 200 if (pkit.hasNext()) { 201 if (first) 202 first = false; 203 else 204 buf.append(", "); 205 206 buf.append("PRIMARY KEY ("); 207 boolean firstPk = true; 208 while (pkit.hasNext()) { 209 if (firstPk) 210 firstPk = false; 211 else 212 buf.append(", "); 213 214 DbAttribute at = (DbAttribute) pkit.next(); 215 buf.append(at.getName()); 216 } 217 buf.append(')'); 218 } 219 buf.append(')'); 220 return buf.toString(); 221 } 222 223 private boolean typeSupportsLength(int type) { 224 String [] externalTypes = externalTypesForJdbcType(type); 226 if (externalTypes != null && externalTypes.length > 0) { 227 for (int i = 0; i < externalTypes.length; i++) { 228 if ("bytea".equalsIgnoreCase(externalTypes[i])) { 229 return false; 230 } 231 } 232 } 233 234 return TypesMapping.supportsLength(type); 235 } 236 237 242 public String dropTable(DbEntity ent) { 243 return super.dropTable(ent) + " CASCADE"; 244 } 245 246 249 public QualifierTranslator getQualifierTranslator(QueryAssembler queryAssembler) { 250 return new PostgresQualifierTranslator(queryAssembler); 251 } 252 253 256 protected PkGenerator createPkGenerator() { 257 return new PostgresPkGenerator(); 258 } 259 } 260 | Popular Tags |