1 56 57 package org.objectstyle.cayenne.access.trans; 58 59 import java.sql.PreparedStatement ; 60 import java.sql.SQLException ; 61 import java.sql.Types ; 62 import java.util.ArrayList ; 63 import java.util.List ; 64 65 import org.objectstyle.cayenne.dba.DbAdapter; 66 import org.objectstyle.cayenne.map.DbAttribute; 67 import org.objectstyle.cayenne.query.BatchQuery; 68 69 74 75 public abstract class BatchQueryBuilder { 76 77 protected DbAdapter adapter; 78 protected String trimFunction; 79 80 public BatchQueryBuilder() { 81 } 82 83 public BatchQueryBuilder(DbAdapter adapter) { 84 this.adapter = adapter; 85 } 86 87 90 public abstract String createSqlString(BatchQuery batch); 91 92 97 protected void appendDbAttribute(StringBuffer buf, DbAttribute dbAttribute) { 98 99 boolean trim = dbAttribute.getType() == Types.CHAR && trimFunction != null; 101 if (trim) { 102 buf.append(trimFunction).append('('); 103 } 104 105 buf.append(dbAttribute.getName()); 106 107 if (trim) { 108 buf.append(')'); 109 } 110 } 111 112 public void setAdapter(DbAdapter adapter) { 113 this.adapter = adapter; 114 } 115 116 public DbAdapter getAdapter() { 117 return adapter; 118 } 119 120 public String getTrimFunction() { 121 return trimFunction; 122 } 123 124 public void setTrimFunction(String string) { 125 trimFunction = string; 126 } 127 128 133 public void bindParameters( 134 PreparedStatement statement, 135 BatchQuery query, 136 List dbAttributes) throws SQLException , Exception { 137 this.bindParameters(statement, query); 138 } 139 140 145 public void bindParameters(PreparedStatement statement, BatchQuery query) 146 throws SQLException , Exception { 147 148 List dbAttributes = query.getDbAttributes(); 149 int attributeCount = dbAttributes.size(); 150 151 for (int i = 0; i < attributeCount; i++) { 152 Object value = query.getValue(i); 153 DbAttribute attribute = (DbAttribute) dbAttributes.get(i); 154 adapter.bindParameter(statement, value, i + 1, attribute.getType(), attribute 155 .getPrecision()); 156 157 } 158 } 159 160 166 public List getParameterValues(BatchQuery query) { 167 List attributes = query.getDbAttributes(); 168 int len = attributes.size(); 169 List values = new ArrayList (len); 170 for (int i = 0; i < len; i++) { 171 values.add(query.getValue(i)); 172 } 173 return values; 174 } 175 } | Popular Tags |