1 19 20 21 package org.apache.cayenne.dba.derby; 22 23 import java.sql.Types ; 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.trans.TrimmingQualifierTranslator; 29 import org.apache.cayenne.access.types.ByteType; 30 import org.apache.cayenne.access.types.CharType; 31 import org.apache.cayenne.access.types.ExtendedTypeMap; 32 import org.apache.cayenne.access.types.ShortType; 33 import org.apache.cayenne.dba.JdbcAdapter; 34 import org.apache.cayenne.dba.PkGenerator; 35 import org.apache.cayenne.dba.TypesMapping; 36 import org.apache.cayenne.map.DbAttribute; 37 import org.apache.cayenne.map.DbEntity; 38 39 62 public class DerbyAdapter extends JdbcAdapter { 63 64 static final String FOR_BIT_DATA_SUFFIX = " FOR BIT DATA"; 65 66 public DerbyAdapter() { 67 setSupportsGeneratedKeys(true); 68 setSupportsBatchUpdates(true); 69 } 70 71 protected PkGenerator createPkGenerator() { 72 return new DerbyPkGenerator(); 73 } 74 75 79 protected void configureExtendedTypes(ExtendedTypeMap map) { 80 super.configureExtendedTypes(map); 81 82 map.registerType(new CharType(true, false)); 84 85 map.registerType(new ShortType(true)); 87 map.registerType(new ByteType(true)); 88 } 89 90 96 protected void createTableAppendColumn(StringBuffer sqlBuffer, DbAttribute column) { 97 98 String [] types = externalTypesForJdbcType(column.getType()); 99 if (types == null || types.length == 0) { 100 String entityName = column.getEntity() != null ? ((DbEntity) column 101 .getEntity()).getFullyQualifiedName() : "<null>"; 102 throw new CayenneRuntimeException("Undefined type for attribute '" 103 + entityName 104 + "." 105 + column.getName() 106 + "': " 107 + column.getType()); 108 } 109 110 String type = types[0]; 111 112 String length = ""; 113 if (typeSupportsLength(column.getType())) { 114 int len = column.getMaxLength(); 115 int scale = TypesMapping.isDecimal(column.getType()) 116 ? column.getScale() 117 : -1; 118 119 if (scale > len) { 121 scale = -1; 122 } 123 124 if (len > 0) { 125 length = " (" + len; 126 127 if (scale >= 0) { 128 length += ", " + scale; 129 } 130 131 length += ")"; 132 } 133 } 134 135 139 sqlBuffer.append(column.getName()).append(' '); 140 if (length.length() > 0 && type.endsWith(FOR_BIT_DATA_SUFFIX)) { 141 sqlBuffer.append(type.substring(0, type.length() 142 - FOR_BIT_DATA_SUFFIX.length())); 143 sqlBuffer.append(length); 144 sqlBuffer.append(FOR_BIT_DATA_SUFFIX); 145 } 146 else { 147 sqlBuffer.append(type).append(length); 148 } 149 150 if (column.isMandatory()) { 151 sqlBuffer.append(" NOT NULL"); 152 } 153 154 if (column.isGenerated()) { 155 sqlBuffer.append(" GENERATED BY DEFAULT AS IDENTITY"); 156 } 157 } 158 159 private boolean typeSupportsLength(int type) { 160 switch (type) { 162 case Types.BLOB: 163 case Types.CLOB: 164 return true; 165 default: 166 return TypesMapping.supportsLength(type); 167 } 168 } 169 170 173 public QualifierTranslator getQualifierTranslator(QueryAssembler queryAssembler) { 174 return new TrimmingQualifierTranslator(queryAssembler, "RTRIM"); 175 } 176 177 } 178 | Popular Tags |