| 1 package com.daffodilwoods.daffodildb.server.sql99.utils; 2 3 import com.daffodilwoods.daffodildb.server.datasystem.utility.*; 4 import com.daffodilwoods.daffodildb.server.serversystem.*; 5 import com.daffodilwoods.daffodildb.server.sql99.common.*; 6 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 7 import com.daffodilwoods.daffodildb.utils.field.*; 8 import com.daffodilwoods.database.resource.*; 9 import com.daffodilwoods.database.utility.P; 10 import java.util.ArrayList ; 11 12 public class VariableValues implements _VariableValues { 13 14 protected Object [][] mapping; protected _Iterator iterator; 16 protected RecordVersion triggerRecordVersion; 17 protected String oldAlias, newAlias; 18 protected Object [][] underlyingReferenceMapping; 19 protected _ServerSession serverSession; 21 public VariableValues(_ServerSession serverSession0) { 22 serverSession = serverSession0; 23 } 24 25 public VariableValues(_Reference[] references, _ServerSession serverSession0) { 26 serverSession = serverSession0; 27 if (references == null) { 28 return; 29 } 30 31 32 33 34 ArrayList aList = new ArrayList (); 35 int length = references.length; 36 for (int i = 0; i < length; i++) { 37 if(!aList.contains(references[i])) 38 aList.add(references[i]); 39 } 40 length = aList.size(); 41 mapping = new Object [length][3]; 42 for (int i = 0; i < length; i++) { 43 mapping[i][0] = aList.get(i); 44 } 45 46 } 47 48 public void addColumnValues(_Reference[] references, Object [] values, int priority) throws DException { 49 61 int count =0; 62 for (int i = 0; i < values.length; i++) { 63 if( values[i] instanceof _Iterator) 64 count++; 65 } 66 Object [][] temp = new Object [values.length-count][3]; 67 for (int i = 0,j=0; i < values.length; i++) { 68 if( values[i] instanceof _Iterator == false){ 69 temp[j][0] = references[i]; 70 temp[j++][1] = values[i]; 71 } 72 } 73 if (mapping == null) { 74 this.mapping = temp; 75 } 76 else 77 addInMapping(temp); 78 if( count > 0 ){ 79 Object [][] temp2 = new Object [count][3]; 80 for (int i = 0,j=0; i < values.length; i++) { 81 if( values[i] instanceof _Iterator){ 82 temp2[j][0] = references[i]; 83 temp2[j++][1] = values[i]; 84 } 85 } 86 if( underlyingReferenceMapping == null ){ 87 underlyingReferenceMapping = temp2; 88 } 89 else{ 90 int length = underlyingReferenceMapping.length, len = temp2.length; 91 Object [][] newMapping = new Object [length + len][3]; 92 System.arraycopy(underlyingReferenceMapping, 0, newMapping, 0, length); 93 System.arraycopy(temp2, 0, newMapping, length, len); 94 underlyingReferenceMapping = newMapping; 95 } 96 } 97 98 99 } 100 101 private void addInMapping(Object [][] temp) throws DException { 102 int length = mapping.length, len = temp.length; 103 Object [][] newMapping = new Object [length + len][3]; 104 System.arraycopy(mapping, 0, newMapping, 0, length); 105 System.arraycopy(temp, 0, newMapping, length, len); 106 this.mapping = newMapping; 107 } 108 109 public void setConditionVariableValue(_Reference[] references, Object [] values, int priority) throws DException { 110 111 boolean b = false; if (references == null || mapping == null) { 113 if (b) 114 ; return; 116 } 117 int len1 = references.length; 118 int len2 = mapping.length; 119 if (b) 120 ; 122 for (int i = 0; i < len1; i++) { 123 for (int j = 0; j < len2; j++) { 124 if (b) { 125 ; } 127 128 if (references[i] == mapping[j][0]) { 129 if (values[i] instanceof _Iterator) { 130 addReferenceInUnderlyingReferenceMapping(new Object [] {references[i], values[i]}); 131 removeReferenceFromMapping(j); 132 j--; 133 len2 = mapping.length; 134 } else { 135 mapping[j][1] = values[i]; 136 } 137 break; 138 } 139 } 140 } 141 if (b) { 142 ; showMapping(mapping, " Mapping"); 144 } 145 } 146 147 public Object getColumnValues(_Reference references) throws DException { 148 int index = -1; 149 if (mapping != null) { 150 index = getIndexOfReference(mapping, references); 151 if (index != -1) { 152 return mapping[index][1]; 153 } 154 } 155 if (underlyingReferenceMapping != null) { 156 index = getIndexOfReference(underlyingReferenceMapping, references); 157 if (index != -1) { 158 Object temp = ( (_Iterator) underlyingReferenceMapping[index][1]).getColumnValues(references); 159 return temp; 160 } 161 } 162 if (iterator != null) { 163 try { 164 return iterator.getColumnValues(references); 165 } catch (DException ex) { 166 throw ex; 167 } 168 } 169 throw ExceptionFactory.columnNotFoundException; 170 } 171 172 public FieldBase field(_Reference references) throws com.daffodilwoods.database.resource.DException { 173 int index = -1; 174 if (mapping != null) { 175 index = getIndexOfReference(mapping, references); 176 if (index != -1) { 177 return (FieldBase) mapping[index][1]; 178 } 179 } 180 181 if (underlyingReferenceMapping != null) { 182 index = getIndexOfReference(underlyingReferenceMapping, references); 183 if (index != -1) { 184 FieldBase temp = ( (_Iterator) underlyingReferenceMapping[index][1]).field(references); 185 return temp; 186 } 187 } 188 if (iterator != null) { 189 try { 190 return iterator.field(references); 191 } catch (DException ex) { 192 throw ex; 193 } 194 } 195 throw ExceptionFactory.columnNotFoundException; 196 } 197 198 public Object getColumnValues(_Reference[] references) throws DException { 199 int length = references.length; 200 Object [] returningValues = new Object [length]; 201 for (int i = 0; i < length; i++) { 202 returningValues[i] = getColumnValues(references[i]); 203 } 204 return returningValues; 205 } 206 207 public void addReferences(_Reference[] references) throws com.daffodilwoods.database.resource.DException { 208 if (references == null) { 209 return; 210 } 211 int length = references.length; 212 Object [][] temp = new Object [length][3]; 213 for (int i = 0; i < length; i++) { 214 temp[i][0] = references[i]; 215 } 216 if (mapping == null) { 217 this.mapping = temp; 218 221 return; 222 } 223 addInMapping(temp); 224 } 225 226 public _Reference[] getReferences() throws DException { 227 if (mapping == null) { 228 return null; 229 } 230 int length = mapping.length; 231 _Reference[] reference = new _Reference[length]; 232 for (int i = 0; i < length; i++) { 233 reference[i] = (_Reference) mapping[i][0]; 234 } 235 return reference; 236 } 237 238 public Object [] getValues() throws DException { 239 if (mapping == null) { 240 return null; 241 } 242 int length = mapping.length; 243 Object [] values = new Object [length]; 244 for (int i = 0; i < length; i++) { 245 values[i] = mapping[i][1]; 246 } 247 return values; 248 } 249 250 private void addReferenceInUnderlyingReferenceMapping(Object [] pair) throws DException { 251 if (underlyingReferenceMapping == null) { 252 underlyingReferenceMapping = new Object [][] {pair}; 253 } 254 int length = underlyingReferenceMapping.length; 255 Object [][] tempMapping = new Object [length + 1][2]; 256 for (int j = 0; j < length; j++) { 257 tempMapping[j] = underlyingReferenceMapping[j]; 258 } 259 tempMapping[length] = pair; 260 underlyingReferenceMapping = tempMapping; 261 } 262 263 private void removeReferenceFromMapping(int position) throws DException { 264 int length = mapping.length; 265 Object [][] tempMapping = new Object [length - 1][3]; 266 for (int j = 0; j < position; j++) { 267 tempMapping[j] = mapping[j]; 268 } 269 for (int j = position + 1; j < length; j++) { 270 tempMapping[j - 1] = mapping[j]; 271 } 272 mapping = tempMapping; 273 } 274 275 private int getIndexOfReference(Object [][] mapp, _Reference reference) { 277 for (int j = 0, length = mapp.length; j < length; j++) { 278 if (reference.equals(mapp[j][0])) { 279 return j; 280 } 281 } 282 return -1; 283 } 284 285 public void setIterator(_Iterator iterator) throws com.daffodilwoods.database.resource.DException { 286 this.iterator = iterator; 287 } 288 289 public Object [][] getReferenceAndValuePair() throws com.daffodilwoods.database.resource.DException { 290 int length = mapping==null? 0: mapping.length; 291 length += underlyingReferenceMapping == null ? 0 : underlyingReferenceMapping.length; 292 if ( length == 0 ) 293 return null; 294 Object [][] referenceAndValue = new Object [length][2]; 295 int j =0; 296 if (mapping != null ) 297 for (int i = 0; i < mapping.length; i++,j++) { 298 referenceAndValue[j][0] = mapping[i][0]; 299 referenceAndValue[j][1] = mapping[i][1]; 300 } 301 302 if (underlyingReferenceMapping != null ) 303 for (int i = 0; i < underlyingReferenceMapping.length; i++,j++) { 304 referenceAndValue[j][0] = underlyingReferenceMapping[i][0]; 305 referenceAndValue[j][1] = underlyingReferenceMapping[i][1]; 306 } 307 return referenceAndValue; 308 } 309 310 public void setServerSession(_ServerSession serverSession0) { 311 serverSession = serverSession0; 312 } 313 314 public _ServerSession getServerSession() { 315 return serverSession; 316 } 317 318 public String toString() { 319 String str = " THE VARIABLEVALUES " + hashCode() + " "; 320 if (mapping != null && mapping.length != 0) { 321 str += "\nMAPPING"; 322 for (int i = 0; i < mapping.length; i++) { 323 str += "[" + mapping[i][0] + " , "; } 325 } 326 if (underlyingReferenceMapping != null && underlyingReferenceMapping.length != 0) { 327 str += "\nUNDERLYINGREFERENCEMAPPING"; 328 for (int i = 0; i < underlyingReferenceMapping.length; i++) { 329 str += "[" + underlyingReferenceMapping[i][0] + "]" + " , "; } 331 } 332 str += iterator != null ? "Iterator's class " + iterator.getClass() : " ITERATOR IS NULL"; 333 return str; 334 } 335 336 public _Record getRecord(String recordType) throws DException { 337 if (recordType.equalsIgnoreCase("NEW") || recordType.equalsIgnoreCase(newAlias)) { 338 return triggerRecordVersion.getCurrentRecord(); 339 } 340 if (recordType.equalsIgnoreCase("OLD") || recordType.equalsIgnoreCase(oldAlias)) { 341 return triggerRecordVersion.getPreviousRecord(); 342 } 343 return null; 344 } 345 346 private void showMapping(Object [][] tempMapping, String mappingName) throws DException { 347 if (tempMapping != null) { 348 for (int i = 0; i < tempMapping.length; i++) { 349 ; } 351 } else 352 ; } 354 355 public void releaseResource() throws DException { 356 if (mapping != null) { 357 for (int i = 0; i < mapping.length; i++) { 358 _Reference reference = (_Reference) mapping[i][0]; 359 if (reference.getReferenceType() == SimpleConstants.VARIABLECOLUMN) { 360 mapping[i][1] = null; 361 } 362 } 363 } 364 } 365 366 private boolean check(_Reference[] references) throws DException { 367 if (references != null) { 368 for (int i = 0; i < references.length; i++) { 369 try { 370 if (references[i].getReferenceType() == SimpleConstants.COLUMNDETAIL && 371 references[i].getColumn().equalsIgnoreCase("DateAcct")) { 372 return true; 373 } 374 } catch (DException ex) { 375 if (references[i].toString().indexOf("( Select max ( Categories . categoryId + productId ) , min ( Categories . categoryId + productId ) from Categories , products )") >= 0) { 376 return true; 377 } 378 } 379 } 380 } 381 return false; 382 } 383 384 public _Iterator getIterator() { 385 return iterator; 386 } 387 } 388 | Popular Tags |