1 19 20 package org.apache.cayenne.access.trans; 21 22 import java.sql.PreparedStatement ; 23 import java.util.ArrayList ; 24 import java.util.List ; 25 26 import org.apache.cayenne.access.QueryLogger; 27 import org.apache.cayenne.access.QueryTranslator; 28 import org.apache.cayenne.map.DbAttribute; 29 import org.apache.cayenne.map.DbEntity; 30 import org.apache.cayenne.map.DbRelationship; 31 32 37 public abstract class QueryAssembler extends QueryTranslator { 38 39 40 protected List values = new ArrayList (); 41 42 45 protected List attributes = new ArrayList (); 46 47 48 public abstract void dbRelationshipAdded(DbRelationship dbRel); 49 50 55 public abstract String createSqlString() throws Exception ; 56 57 public String aliasForTable(DbEntity ent, DbRelationship rel) { 58 return aliasForTable(ent); } 60 61 69 public abstract String aliasForTable(DbEntity dbEnt); 70 71 75 public boolean supportsTableAliases() { 76 return false; 77 } 78 79 85 public void addToParamList(DbAttribute dbAttr, Object anObject) { 86 attributes.add(dbAttr); 87 values.add(anObject); 88 } 89 90 93 public PreparedStatement createStatement() throws Exception { 94 long t1 = System.currentTimeMillis(); 95 String sqlStr = createSqlString(); 96 QueryLogger.logQuery(sqlStr, values, System.currentTimeMillis() - t1); 97 PreparedStatement stmt = connection.prepareStatement(sqlStr); 98 initStatement(stmt); 99 return stmt; 100 } 101 102 106 protected void initStatement(PreparedStatement stmt) throws Exception { 107 if (values != null && values.size() > 0) { 108 int len = values.size(); 109 for (int i = 0; i < len; i++) { 110 Object val = values.get(i); 111 112 DbAttribute attr = (DbAttribute) attributes.get(i); 113 114 if (attr == null) { 119 stmt.setObject(i + 1, val); 120 } 121 else { 122 adapter.bindParameter(stmt, val, i + 1, attr.getType(), attr 123 .getScale()); 124 } 125 } 126 } 127 } 128 } 129 | Popular Tags |