1 19 20 package org.apache.cayenne.access.trans; 21 22 import java.sql.PreparedStatement ; 23 import java.sql.SQLException ; 24 import java.sql.Types ; 25 import java.util.ArrayList ; 26 import java.util.List ; 27 28 import org.apache.cayenne.dba.DbAdapter; 29 import org.apache.cayenne.map.DbAttribute; 30 import org.apache.cayenne.query.BatchQuery; 31 32 37 38 public abstract class BatchQueryBuilder { 39 40 protected DbAdapter adapter; 41 protected String trimFunction; 42 43 public BatchQueryBuilder() { 44 } 45 46 public BatchQueryBuilder(DbAdapter adapter) { 47 this.adapter = adapter; 48 } 49 50 53 public abstract String createSqlString(BatchQuery batch); 54 55 60 protected void appendDbAttribute(StringBuffer buf, DbAttribute dbAttribute) { 61 62 boolean trim = dbAttribute.getType() == Types.CHAR && trimFunction != null; 64 if (trim) { 65 buf.append(trimFunction).append('('); 66 } 67 68 buf.append(dbAttribute.getName()); 69 70 if (trim) { 71 buf.append(')'); 72 } 73 } 74 75 public void setAdapter(DbAdapter adapter) { 76 this.adapter = adapter; 77 } 78 79 public DbAdapter getAdapter() { 80 return adapter; 81 } 82 83 public String getTrimFunction() { 84 return trimFunction; 85 } 86 87 public void setTrimFunction(String string) { 88 trimFunction = string; 89 } 90 91 96 public void bindParameters(PreparedStatement statement, BatchQuery query) 97 throws SQLException , Exception { 98 99 List dbAttributes = query.getDbAttributes(); 100 int attributeCount = dbAttributes.size(); 101 102 for (int i = 0; i < attributeCount; i++) { 103 Object value = query.getValue(i); 104 DbAttribute attribute = (DbAttribute) dbAttributes.get(i); 105 adapter.bindParameter(statement, value, i + 1, attribute.getType(), attribute 106 .getScale()); 107 108 } 109 } 110 111 117 public List getParameterValues(BatchQuery query) { 118 List attributes = query.getDbAttributes(); 119 int len = attributes.size(); 120 List values = new ArrayList (len); 121 for (int i = 0; i < len; i++) { 122 values.add(query.getValue(i)); 123 } 124 return values; 125 } 126 } 127 | Popular Tags |