1 19 20 package org.apache.cayenne.dba; 21 22 import java.sql.Connection ; 23 import java.sql.PreparedStatement ; 24 import java.sql.SQLException ; 25 import java.util.Arrays ; 26 import java.util.Collection ; 27 28 import javax.sql.DataSource ; 29 30 import org.apache.cayenne.CayenneRuntimeException; 31 import org.apache.cayenne.access.DataNode; 32 import org.apache.cayenne.access.QueryLogger; 33 import org.apache.cayenne.access.trans.QualifierTranslator; 34 import org.apache.cayenne.access.trans.QueryAssembler; 35 import org.apache.cayenne.access.types.ExtendedTypeMap; 36 import org.apache.cayenne.dba.db2.DB2Sniffer; 37 import org.apache.cayenne.dba.derby.DerbySniffer; 38 import org.apache.cayenne.dba.frontbase.FrontBaseSniffer; 39 import org.apache.cayenne.dba.hsqldb.HSQLDBSniffer; 40 import org.apache.cayenne.dba.ingres.IngresSniffer; 41 import org.apache.cayenne.dba.mysql.MySQLSniffer; 42 import org.apache.cayenne.dba.openbase.OpenBaseSniffer; 43 import org.apache.cayenne.dba.oracle.OracleSniffer; 44 import org.apache.cayenne.dba.postgres.PostgresSniffer; 45 import org.apache.cayenne.dba.sqlserver.SQLServerSniffer; 46 import org.apache.cayenne.dba.sybase.SybaseSniffer; 47 import org.apache.cayenne.map.DbAttribute; 48 import org.apache.cayenne.map.DbEntity; 49 import org.apache.cayenne.map.DbRelationship; 50 import org.apache.cayenne.query.Query; 51 import org.apache.cayenne.query.SQLAction; 52 53 61 public class AutoAdapter implements DbAdapter { 62 63 static final DbAdapterFactory[] DEFAULT_FACTORIES = new DbAdapterFactory[] { 65 new MySQLSniffer(), new PostgresSniffer(), new OracleSniffer(), 66 new SQLServerSniffer(), new HSQLDBSniffer(), new DB2Sniffer(), 67 new SybaseSniffer(), new DerbySniffer(), new OpenBaseSniffer(), 68 new FrontBaseSniffer(), new IngresSniffer() 69 }; 70 71 75 public static DbAdapterFactory getDefaultFactory() { 76 return new DbAdapterFactoryChain(Arrays.asList(DEFAULT_FACTORIES)); 77 } 78 79 protected DbAdapterFactory adapterFactory; 80 protected DataSource dataSource; 81 protected PkGenerator pkGenerator; 82 83 86 DbAdapter adapter; 87 88 91 public AutoAdapter(DataSource dataSource) { 92 this(null, dataSource); 93 } 94 95 99 public AutoAdapter(DbAdapterFactory adapterFactory, DataSource dataSource) { 100 if (dataSource == null) { 102 throw new CayenneRuntimeException("Null dataSource"); 103 } 104 105 this.adapterFactory = adapterFactory != null 106 ? adapterFactory 107 : createDefaultFactory(); 108 this.dataSource = dataSource; 109 } 110 111 115 protected DbAdapterFactory createDefaultFactory() { 116 return getDefaultFactory(); 117 } 118 119 122 protected DbAdapter getAdapter() { 123 if (adapter == null) { 124 synchronized (this) { 125 if (adapter == null) { 126 this.adapter = loadAdapter(); 127 } 128 } 129 } 130 131 return adapter; 132 } 133 134 137 protected DbAdapter loadAdapter() { 138 DbAdapter adapter = null; 139 140 try { 141 Connection c = dataSource.getConnection(); 142 143 try { 144 adapter = adapterFactory.createAdapter(c.getMetaData()); 145 } 146 finally { 147 try { 148 c.close(); 149 } 150 catch (SQLException e) { 151 } 153 } 154 } 155 catch (SQLException e) { 156 throw new CayenneRuntimeException("Error detecting database type", e); 157 } 158 159 if (adapter == null) { 160 QueryLogger.log("Failed to detect database type, using default adapter"); 161 adapter = new JdbcAdapter(); 162 } 163 else { 164 QueryLogger.log("Detected and installed adapter: " 165 + adapter.getClass().getName()); 166 } 167 168 return adapter; 169 } 170 171 173 public String getBatchTerminator() { 174 return getAdapter().getBatchTerminator(); 175 } 176 177 public QualifierTranslator getQualifierTranslator(QueryAssembler queryAssembler) { 178 return getAdapter().getQualifierTranslator(queryAssembler); 179 } 180 181 public SQLAction getAction(Query query, DataNode node) { 182 return getAdapter().getAction(query, node); 183 } 184 185 189 public boolean supportsFkConstraints() { 190 return getAdapter().supportsFkConstraints(); 191 } 192 193 public boolean supportsUniqueConstraints() { 194 return getAdapter().supportsUniqueConstraints(); 195 } 196 197 public boolean supportsGeneratedKeys() { 198 return getAdapter().supportsGeneratedKeys(); 199 } 200 201 public boolean supportsBatchUpdates() { 202 return getAdapter().supportsBatchUpdates(); 203 } 204 205 public String dropTable(DbEntity entity) { 206 return getAdapter().dropTable(entity); 207 } 208 209 public String createTable(DbEntity entity) { 210 return getAdapter().createTable(entity); 211 } 212 213 public String createUniqueConstraint(DbEntity source, Collection columns) { 214 return getAdapter().createUniqueConstraint(source, columns); 215 } 216 217 public String createFkConstraint(DbRelationship rel) { 218 return getAdapter().createFkConstraint(rel); 219 } 220 221 public String [] externalTypesForJdbcType(int type) { 222 return getAdapter().externalTypesForJdbcType(type); 223 } 224 225 public ExtendedTypeMap getExtendedTypes() { 226 return getAdapter().getExtendedTypes(); 227 } 228 229 232 public PkGenerator getPkGenerator() { 233 return (pkGenerator != null) ? pkGenerator : getAdapter().getPkGenerator(); 234 } 235 236 240 public void setPkGenerator(PkGenerator pkGenerator) { 241 this.pkGenerator = pkGenerator; 242 } 243 244 public DbAttribute buildAttribute( 245 String name, 246 String typeName, 247 int type, 248 int size, 249 int precision, 250 boolean allowNulls) { 251 252 return getAdapter().buildAttribute( 253 name, 254 typeName, 255 type, 256 size, 257 precision, 258 allowNulls); 259 } 260 261 public void bindParameter( 262 PreparedStatement statement, 263 Object object, 264 int pos, 265 int sqlType, 266 int precision) throws SQLException , Exception { 267 getAdapter().bindParameter(statement, object, pos, sqlType, precision); 268 } 269 270 public String tableTypeForTable() { 271 return getAdapter().tableTypeForTable(); 272 } 273 274 public String tableTypeForView() { 275 return getAdapter().tableTypeForView(); 276 } 277 } 278 | Popular Tags |