1 19 20 package org.apache.cayenne.dba.frontbase; 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.types.ExtendedTypeMap; 28 import org.apache.cayenne.dba.JdbcAdapter; 29 import org.apache.cayenne.dba.PkGenerator; 30 import org.apache.cayenne.dba.TypesMapping; 31 import org.apache.cayenne.map.DbAttribute; 32 import org.apache.cayenne.map.DbEntity; 33 import org.apache.cayenne.map.DerivedDbEntity; 34 import org.apache.cayenne.query.Query; 35 import org.apache.cayenne.query.SQLAction; 36 37 54 public class FrontBaseAdapter extends JdbcAdapter { 61 62 public FrontBaseAdapter() { 63 setSupportsBatchUpdates(true); 64 } 65 66 69 public SQLAction getAction(Query query, DataNode node) { 70 return query.createSQLAction(new FrontBaseActionBuilder(this, node 71 .getEntityResolver())); 72 } 73 74 public String tableTypeForTable() { 75 return "BASE TABLE"; 76 } 77 78 protected void configureExtendedTypes(ExtendedTypeMap map) { 79 super.configureExtendedTypes(map); 80 81 map.registerType(new FrontBaseByteArrayType()); 82 map.registerType(new FrontBaseBooleanType()); 83 map.registerType(new FrontBaseCharType()); 84 } 85 86 89 public String createTable(DbEntity ent) { 90 91 if (ent instanceof DerivedDbEntity) { 94 throw new CayenneRuntimeException("Can't create table for derived DbEntity '" 95 + ent.getName() 96 + "'."); 97 } 98 99 StringBuffer buf = new StringBuffer (); 100 buf.append("CREATE TABLE ").append(ent.getFullyQualifiedName()).append(" ("); 101 102 Iterator it = ent.getAttributes().iterator(); 104 boolean first = true; 105 while (it.hasNext()) { 106 if (first) { 107 first = false; 108 } 109 else { 110 buf.append(", "); 111 } 112 113 DbAttribute at = (DbAttribute) it.next(); 114 115 if (at.getType() == TypesMapping.NOT_DEFINED) { 117 throw new CayenneRuntimeException("Undefined type for attribute '" 118 + ent.getFullyQualifiedName() 119 + "." 120 + at.getName() 121 + "'."); 122 } 123 124 String [] types = externalTypesForJdbcType(at.getType()); 125 if (types == null || types.length == 0) { 126 throw new CayenneRuntimeException("Undefined type for attribute '" 127 + ent.getFullyQualifiedName() 128 + "." 129 + at.getName() 130 + "': " 131 + at.getType()); 132 } 133 134 String type = types[0]; 135 buf.append(at.getName()).append(' ').append(type); 136 137 if (at.getType() == Types.LONGVARCHAR) { 141 142 int len = at.getMaxLength() > 0 ? at.getMaxLength() : 1073741824; 143 buf.append("(" + len + ")"); 144 } 145 else if (at.getType() == Types.VARBINARY || at.getType() == Types.BINARY) { 146 147 int len = at.getMaxLength() > 0 ? at.getMaxLength() : 1073741824; 149 len *= 8; 150 buf.append("(" + len + ")"); 151 } 152 else if (TypesMapping.supportsLength(at.getType())) { 153 int len = at.getMaxLength(); 154 int scale = TypesMapping.isDecimal(at.getType()) ? at.getScale() : -1; 155 156 if (scale > len) { 158 scale = -1; 159 } 160 161 if (len > 0) { 162 buf.append('(').append(len); 163 164 if (scale >= 0) { 165 buf.append(", ").append(scale); 166 } 167 168 buf.append(')'); 169 } 170 } 171 172 if (at.isMandatory()) { 173 buf.append(" NOT NULL"); 174 } 175 } 177 178 Iterator pkit = ent.getPrimaryKey().iterator(); 180 if (pkit.hasNext()) { 181 if (first) 182 first = false; 183 else 184 buf.append(", "); 185 186 buf.append("PRIMARY KEY ("); 187 boolean firstPk = true; 188 while (pkit.hasNext()) { 189 if (firstPk) 190 firstPk = false; 191 else 192 buf.append(", "); 193 194 DbAttribute at = (DbAttribute) pkit.next(); 195 buf.append(at.getName()); 196 } 197 buf.append(')'); 198 } 199 buf.append(')'); 200 return buf.toString(); 201 } 202 203 206 public String dropTable(DbEntity ent) { 207 return super.dropTable(ent) + " CASCADE"; 208 } 209 210 protected PkGenerator createPkGenerator() { 211 return new FrontBasePkGenerator(); 212 } 213 } 214
| Popular Tags
|