1 19 20 21 package org.apache.cayenne.access.trans; 22 23 import java.sql.Types ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 27 import org.apache.cayenne.map.DbAttribute; 28 import org.apache.cayenne.query.BatchQuery; 29 30 36 public class LOBBatchQueryWrapper { 37 38 protected BatchQuery query; 39 40 protected List dbAttributes; 41 42 protected boolean[] qualifierAttributes; 44 protected boolean[] allLOBAttributes; 45 protected Object [] updatedLOBAttributes; 46 47 protected boolean hasNext; 48 49 public LOBBatchQueryWrapper(BatchQuery query) { 50 this.query = query; 51 this.dbAttributes = query.getDbAttributes(); 52 53 int len = dbAttributes.size(); 54 this.qualifierAttributes = new boolean[len]; 55 this.allLOBAttributes = new boolean[len]; 56 this.updatedLOBAttributes = new Object [len]; 57 58 indexQualifierAttributes(); 59 } 60 61 public boolean next() { 62 hasNext = query.next(); 63 64 if (hasNext) { 65 indexLOBAttributes(); 66 } 67 68 return hasNext; 69 } 70 71 74 protected void indexQualifierAttributes() { 75 int len = this.dbAttributes.size(); 76 for (int i = 0; i < len; i++) { 77 DbAttribute attribute = (DbAttribute) this.dbAttributes.get(i); 78 int type = attribute.getType(); 79 qualifierAttributes[i] = attribute.isPrimaryKey(); 80 allLOBAttributes[i] = (type == Types.BLOB || type == Types.CLOB); 81 } 82 } 83 84 87 protected void indexLOBAttributes() { 88 int len = updatedLOBAttributes.length; 89 for (int i = 0; i < len; i++) { 90 updatedLOBAttributes[i] = null; 91 92 if (allLOBAttributes[i]) { 93 Object value = query.getValue(i); 95 96 if (value == null) { 97 continue; 98 } 99 100 if (((DbAttribute) dbAttributes.get(i)).getType() == Types.BLOB) { 101 updatedLOBAttributes[i] = convertToBlobValue(value); 102 } 103 else { 104 updatedLOBAttributes[i] = convertToClobValue(value); 105 } 106 } 107 } 108 } 109 110 113 protected byte[] convertToBlobValue(Object value) { 114 if (value instanceof byte[]) { 115 byte[] bytes = (byte[]) value; 116 return bytes.length == 0 ? null : bytes; 117 } 118 119 return null; 120 } 121 122 125 protected Object convertToClobValue(Object value) { 126 127 if (value instanceof char[]) { 128 char[] chars = (char[]) value; 129 return (chars.length == 0) ? null : chars; 130 } 131 else { 132 String strValue = value.toString(); 133 return (strValue.length() == 0) ? null : strValue; 134 } 135 } 136 137 141 public List getDbAttributesForLOBSelectQualifier() { 142 143 int len = qualifierAttributes.length; 144 List attributes = new ArrayList (len); 145 146 for (int i = 0; i < len; i++) { 147 if (this.qualifierAttributes[i]) { 148 attributes.add(this.dbAttributes.get(i)); 149 } 150 } 151 return attributes; 152 } 153 154 159 public List getDbAttributesForUpdatedLOBColumns() { 160 if (!hasNext) { 161 throw new IllegalStateException ("No more rows in the BatchQuery."); 162 } 163 164 int len = updatedLOBAttributes.length; 165 List attributes = new ArrayList (len); 166 167 for (int i = 0; i < len; i++) { 168 if (this.updatedLOBAttributes[i] != null) { 169 attributes.add(this.dbAttributes.get(i)); 170 } 171 } 172 return attributes; 173 } 174 175 public List getValuesForLOBSelectQualifier() { 176 if (!hasNext) { 177 throw new IllegalStateException ("No more rows in the BatchQuery."); 178 } 179 180 int len = this.qualifierAttributes.length; 181 List values = new ArrayList (len); 182 for (int i = 0; i < len; i++) { 183 if (this.qualifierAttributes[i]) { 184 values.add(query.getValue(i)); 185 } 186 } 187 188 return values; 189 } 190 191 public List getValuesForUpdatedLOBColumns() { 192 if (!hasNext) { 193 throw new IllegalStateException ("No more rows in the BatchQuery."); 194 } 195 196 int len = this.updatedLOBAttributes.length; 197 List values = new ArrayList (len); 198 for (int i = 0; i < len; i++) { 199 if (this.updatedLOBAttributes[i] != null) { 200 values.add(this.updatedLOBAttributes[i]); 201 } 202 } 203 204 return values; 205 } 206 207 210 public BatchQuery getQuery() { 211 return query; 212 } 213 } 214 | Popular Tags |