1 19 20 21 package org.apache.cayenne.access.trans; 22 23 import java.sql.PreparedStatement ; 24 import java.sql.SQLException ; 25 import java.sql.Types ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 29 import org.apache.cayenne.CayenneRuntimeException; 30 import org.apache.cayenne.dba.DbAdapter; 31 import org.apache.cayenne.dba.TypesMapping; 32 import org.apache.cayenne.map.DbAttribute; 33 import org.apache.cayenne.query.BatchQuery; 34 35 40 public abstract class LOBBatchQueryBuilder extends BatchQueryBuilder { 41 42 protected String newClobFunction; 43 protected String newBlobFunction; 44 45 public LOBBatchQueryBuilder(DbAdapter adapter) { 46 super(adapter); 47 } 48 49 public abstract List getValuesForLOBUpdateParameters(BatchQuery query); 50 51 public String createLOBSelectString( 52 BatchQuery updateQuery, 53 List selectedLOBAttributes, 54 List qualifierAttributes) { 55 56 StringBuffer buf = new StringBuffer (); 57 buf.append("SELECT "); 58 59 Iterator it = selectedLOBAttributes.iterator(); 60 while (it.hasNext()) { 61 buf.append(((DbAttribute) it.next()).getName()); 62 63 if (it.hasNext()) { 64 buf.append(", "); 65 } 66 } 67 68 buf 69 .append(" FROM ") 70 .append(updateQuery.getDbEntity().getFullyQualifiedName()) 71 .append(" WHERE "); 72 73 it = qualifierAttributes.iterator(); 74 while (it.hasNext()) { 75 DbAttribute attribute = (DbAttribute) it.next(); 76 appendDbAttribute(buf, attribute); 77 buf.append(" = ?"); 78 if (it.hasNext()) { 79 buf.append(" AND "); 80 } 81 } 82 83 buf.append(" FOR UPDATE"); 84 return buf.toString(); 85 } 86 87 91 protected void appendUpdatedParameter( 92 StringBuffer buf, 93 DbAttribute dbAttribute, 94 Object value) { 95 96 int type = dbAttribute.getType(); 97 98 if (isUpdateableColumn(value, type)) { 99 buf.append('?'); 100 } 101 else { 102 if (type == Types.CLOB) { 103 buf.append(newClobFunction); 104 } 105 else if (type == Types.BLOB) { 106 buf.append(newBlobFunction); 107 } 108 else { 109 throw new CayenneRuntimeException("Unknown LOB column type: " 110 + type 111 + "(" 112 + TypesMapping.getSqlNameByType(type) 113 + "). Query buffer: " 114 + buf); 115 } 116 } 117 } 118 119 122 public void bindParameters(PreparedStatement statement, BatchQuery query) 123 throws SQLException , Exception { 124 125 List dbAttributes = query.getDbAttributes(); 126 int attributeCount = dbAttributes.size(); 127 128 for (int i = 0, j = 1; i < attributeCount; i++) { 131 Object value = query.getValue(i); 132 DbAttribute attribute = (DbAttribute) dbAttributes.get(i); 133 int type = attribute.getType(); 134 135 if (isUpdateableColumn(value, type)) { 137 adapter 138 .bindParameter(statement, value, j, type, attribute 139 .getScale()); 140 141 j++; 142 } 143 } 144 } 145 146 protected boolean isUpdateableColumn(Object value, int type) { 147 return value == null || (type != Types.BLOB && type != Types.CLOB); 148 } 149 150 public String getNewBlobFunction() { 151 return newBlobFunction; 152 } 153 154 public String getNewClobFunction() { 155 return newClobFunction; 156 } 157 158 public void setNewBlobFunction(String string) { 159 newBlobFunction = string; 160 } 161 162 public void setNewClobFunction(String string) { 163 newClobFunction = string; 164 } 165 } 166 | Popular Tags |