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.Iterator ; 63 import java.util.List ; 64 65 import org.objectstyle.cayenne.CayenneRuntimeException; 66 import org.objectstyle.cayenne.dba.DbAdapter; 67 import org.objectstyle.cayenne.dba.TypesMapping; 68 import org.objectstyle.cayenne.map.DbAttribute; 69 import org.objectstyle.cayenne.query.BatchQuery; 70 71 76 public abstract class LOBBatchQueryBuilder extends BatchQueryBuilder { 77 78 protected String newClobFunction; 79 protected String newBlobFunction; 80 81 public LOBBatchQueryBuilder(DbAdapter adapter) { 82 super(adapter); 83 } 84 85 public abstract List getValuesForLOBUpdateParameters(BatchQuery query); 86 87 public String createLOBSelectString( 88 BatchQuery updateQuery, 89 List selectedLOBAttributes, 90 List qualifierAttributes) { 91 92 StringBuffer buf = new StringBuffer (); 93 buf.append("SELECT "); 94 95 Iterator it = selectedLOBAttributes.iterator(); 96 while (it.hasNext()) { 97 buf.append(((DbAttribute) it.next()).getName()); 98 99 if (it.hasNext()) { 100 buf.append(", "); 101 } 102 } 103 104 buf 105 .append(" FROM ") 106 .append(updateQuery.getDbEntity().getFullyQualifiedName()) 107 .append(" WHERE "); 108 109 it = qualifierAttributes.iterator(); 110 while (it.hasNext()) { 111 DbAttribute attribute = (DbAttribute) it.next(); 112 appendDbAttribute(buf, attribute); 113 buf.append(" = ?"); 114 if (it.hasNext()) { 115 buf.append(" AND "); 116 } 117 } 118 119 buf.append(" FOR UPDATE"); 120 return buf.toString(); 121 } 122 123 127 protected void appendUpdatedParameter( 128 StringBuffer buf, 129 DbAttribute dbAttribute, 130 Object value) { 131 132 int type = dbAttribute.getType(); 133 134 if (isUpdateableColumn(value, type)) { 135 buf.append('?'); 136 } 137 else { 138 if (type == Types.CLOB) { 139 buf.append(newClobFunction); 140 } 141 else if (type == Types.BLOB) { 142 buf.append(newBlobFunction); 143 } 144 else { 145 throw new CayenneRuntimeException("Unknown LOB column type: " 146 + type 147 + "(" 148 + TypesMapping.getSqlNameByType(type) 149 + "). Query buffer: " 150 + buf); 151 } 152 } 153 } 154 155 158 public void bindParameters(PreparedStatement statement, BatchQuery query) 159 throws SQLException , Exception { 160 161 List dbAttributes = query.getDbAttributes(); 162 int attributeCount = dbAttributes.size(); 163 164 for (int i = 0, j = 1; i < attributeCount; i++) { 167 Object value = query.getValue(i); 168 DbAttribute attribute = (DbAttribute) dbAttributes.get(i); 169 int type = attribute.getType(); 170 171 if (isUpdateableColumn(value, type)) { 173 adapter 174 .bindParameter(statement, value, j, type, attribute 175 .getPrecision()); 176 177 j++; 178 } 179 } 180 } 181 182 protected boolean isUpdateableColumn(Object value, int type) { 183 return value == null || (type != Types.BLOB && type != Types.CLOB); 184 } 185 186 public String getNewBlobFunction() { 187 return newBlobFunction; 188 } 189 190 public String getNewClobFunction() { 191 return newClobFunction; 192 } 193 194 public void setNewBlobFunction(String string) { 195 newBlobFunction = string; 196 } 197 198 public void setNewClobFunction(String string) { 199 newClobFunction = string; 200 } 201 } | Popular Tags |