1 56 package org.objectstyle.cayenne.access.trans; 57 58 import java.sql.PreparedStatement ; 59 import java.util.ArrayList ; 60 import java.util.List ; 61 62 import org.apache.log4j.Level; 63 import org.objectstyle.cayenne.access.QueryLogger; 64 import org.objectstyle.cayenne.access.QueryTranslator; 65 import org.objectstyle.cayenne.map.DbAttribute; 66 import org.objectstyle.cayenne.map.DbEntity; 67 import org.objectstyle.cayenne.map.DbRelationship; 68 69 74 public abstract class QueryAssembler extends QueryTranslator { 75 76 protected List values = new ArrayList (); 77 78 82 protected List attributes = new ArrayList (); 83 84 85 public abstract void dbRelationshipAdded(DbRelationship dbRel); 86 87 93 public abstract String createSqlString() throws Exception ; 94 95 public String aliasForTable(DbEntity ent, DbRelationship rel) { 96 return aliasForTable(ent); } 98 99 110 public abstract String aliasForTable(DbEntity dbEnt); 111 112 114 public boolean supportsTableAliases() { 115 return false; 116 } 117 118 123 public void addToParamList(DbAttribute dbAttr, Object anObject) { 124 attributes.add(dbAttr); 125 values.add(anObject); 126 } 127 128 129 public PreparedStatement createStatement(Level logLevel) throws Exception { 130 long t1 = System.currentTimeMillis(); 131 String sqlStr = createSqlString(); 132 QueryLogger.logQuery( 133 logLevel, 134 sqlStr, 135 values, 136 System.currentTimeMillis() - t1); 137 PreparedStatement stmt = connection.prepareStatement(sqlStr); 138 initStatement(stmt); 139 return stmt; 140 } 141 142 147 protected void initStatement(PreparedStatement stmt) throws Exception { 148 if (values != null && values.size() > 0) { 149 int len = values.size(); 150 for (int i = 0; i < len; i++) { 151 Object val = values.get(i); 152 153 DbAttribute attr = (DbAttribute) attributes.get(i); 154 155 if (attr == null) { 160 stmt.setObject(i + 1, val); 161 } else { 162 int type = attr.getType(); 163 int precision = attr.getPrecision(); 164 adapter.bindParameter(stmt, val, i + 1, type, precision); 165 } 166 } 167 } 168 } 169 } | Popular Tags |