1 56 57 package org.objectstyle.cayenne.access.trans; 58 59 import java.sql.Types ; 60 import java.util.ArrayList ; 61 import java.util.List ; 62 63 import org.apache.log4j.Level; 64 import org.objectstyle.cayenne.map.DbAttribute; 65 import org.objectstyle.cayenne.query.BatchQuery; 66 67 73 public class LOBBatchQueryWrapper { 74 protected BatchQuery query; 75 76 protected List dbAttributes; 77 78 protected boolean[] qualifierAttributes; 80 protected boolean[] allLOBAttributes; 81 protected Object [] updatedLOBAttributes; 82 83 protected boolean hasNext; 84 85 public LOBBatchQueryWrapper(BatchQuery query) { 86 this.query = query; 87 this.dbAttributes = query.getDbAttributes(); 88 89 int len = dbAttributes.size(); 90 this.qualifierAttributes = new boolean[len]; 91 this.allLOBAttributes = new boolean[len]; 92 this.updatedLOBAttributes = new Object [len]; 93 94 indexQualifierAttributes(); 95 } 96 97 public boolean next() { 98 hasNext = query.next(); 99 100 if (hasNext) { 101 indexLOBAttributes(); 102 } 103 104 return hasNext; 105 } 106 107 110 protected void indexQualifierAttributes() { 111 int len = this.dbAttributes.size(); 112 for (int i = 0; i < len; i++) { 113 DbAttribute attribute = (DbAttribute) this.dbAttributes.get(i); 114 int type = attribute.getType(); 115 qualifierAttributes[i] = attribute.isPrimaryKey(); 116 allLOBAttributes[i] = (type == Types.BLOB || type == Types.CLOB); 117 } 118 } 119 120 123 protected void indexLOBAttributes() { 124 int len = updatedLOBAttributes.length; 125 for (int i = 0; i < len; i++) { 126 updatedLOBAttributes[i] = null; 127 128 if (allLOBAttributes[i]) { 129 Object value = query.getValue(i); 131 132 if (value == null) { 133 continue; 134 } 135 136 if (((DbAttribute) dbAttributes.get(i)).getType() == Types.BLOB) { 137 updatedLOBAttributes[i] = convertToBlobValue(value); 138 } 139 else { 140 updatedLOBAttributes[i] = convertToClobValue(value); 141 } 142 } 143 } 144 } 145 146 149 protected byte[] convertToBlobValue(Object value) { 150 if (value instanceof byte[]) { 151 byte[] bytes = (byte[]) value; 152 return bytes.length == 0 ? null : bytes; 153 } 154 155 return null; 156 } 157 158 161 protected Object convertToClobValue(Object value) { 162 163 if (value instanceof char[]) { 164 char[] chars = (char[]) value; 165 return (chars.length == 0) ? null : chars; 166 } 167 else { 168 String strValue = value.toString(); 169 return (strValue.length() == 0) ? null : strValue; 170 } 171 } 172 173 177 public List getDbAttributesForLOBSelectQualifier() { 178 179 int len = qualifierAttributes.length; 180 List attributes = new ArrayList (len); 181 182 for (int i = 0; i < len; i++) { 183 if (this.qualifierAttributes[i]) { 184 attributes.add(this.dbAttributes.get(i)); 185 } 186 } 187 return attributes; 188 } 189 190 195 public List getDbAttributesForUpdatedLOBColumns() { 196 if (!hasNext) { 197 throw new IllegalStateException ("No more rows in the BatchQuery."); 198 } 199 200 int len = updatedLOBAttributes.length; 201 List attributes = new ArrayList (len); 202 203 for (int i = 0; i < len; i++) { 204 if (this.updatedLOBAttributes[i] != null) { 205 attributes.add(this.dbAttributes.get(i)); 206 } 207 } 208 return attributes; 209 } 210 211 public List getValuesForLOBSelectQualifier() { 212 if (!hasNext) { 213 throw new IllegalStateException ("No more rows in the BatchQuery."); 214 } 215 216 int len = this.qualifierAttributes.length; 217 List values = new ArrayList (len); 218 for (int i = 0; i < len; i++) { 219 if (this.qualifierAttributes[i]) { 220 values.add(query.getValue(i)); 221 } 222 } 223 224 return values; 225 } 226 227 public List getValuesForUpdatedLOBColumns() { 228 if (!hasNext) { 229 throw new IllegalStateException ("No more rows in the BatchQuery."); 230 } 231 232 int len = this.updatedLOBAttributes.length; 233 List values = new ArrayList (len); 234 for (int i = 0; i < len; i++) { 235 if (this.updatedLOBAttributes[i] != null) { 236 values.add(this.updatedLOBAttributes[i]); 237 } 238 } 239 240 return values; 241 } 242 243 246 public BatchQuery getQuery() { 247 return query; 248 } 249 250 public Level getLoggingLevel() { 251 return this.query.getLoggingLevel(); 252 } 253 } 254 | Popular Tags |