| 1 package com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.predicates; 2 3 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.*; 4 import com.daffodilwoods.daffodildb.utils.*; 5 import com.daffodilwoods.daffodildb.utils.comparator.*; 6 import com.daffodilwoods.daffodildb.utils.field.*; 7 import com.daffodilwoods.database.resource.*; 8 9 17 18 public class EscapeComparator extends SuperComparator { 19 20 protected byte escapeByte; 21 protected SRESERVEDWORD1206543922escapecharacter escpaeCharacter; 22 protected Object object; 23 24 protected boolean firstClob, secondClob, thirdClob; 25 26 public EscapeComparator(SRESERVEDWORD1206543922escapecharacter escpaeCharacter0, Object object0, boolean firstClob0, boolean secondClob0, boolean thirdClob0) { 27 escpaeCharacter = escpaeCharacter0; 28 object = object0; 29 firstClob = firstClob0; 30 secondClob = secondClob0; 31 thirdClob = thirdClob0; 32 } 33 34 public int compare(_DComparator leftMatchValue, _DComparator rightPattern) throws DException { 35 FieldBase escape = (FieldBase) escpaeCharacter.run(object); 36 byte[] matchValue = firstClob ? ( (DClobUpdatable) leftMatchValue).getBytes() 37 : ( (FieldBase) leftMatchValue).getBufferRange().getBytes(); 38 byte[] matchPattern = secondClob ? ( (DClobUpdatable) rightPattern).getBytes() 39 : ( (FieldBase) rightPattern).getBufferRange().getBytes(); 40 byte[] escapeArray = thirdClob ? ( (DClobUpdatable) escape).getBytes() 41 : escape.getBufferRange().getBytes(); 42 if (escapeArray.length != 1) { 43 throw new DException("DSE0", new Object [] {"Invalid escape character '" + new String (escapeArray) + "' was specified in a LIKE predicate."}); 44 } 45 escapeByte = escapeArray[0]; 46 return compareEscapeLike(matchValue, matchPattern); 47 } 48 49 protected int compareEscapeLike(byte[] matchValue, byte[] matchPattern) throws DException { 50 int length = matchValue.length > matchPattern.length ? matchValue.length : matchPattern.length; 51 boolean percent = false; 52 boolean flag = false; 53 int i, j; 54 for (i = 0, j = 0; j < length; i++, j++) { 55 if (j >= matchPattern.length) { 56 break; 57 } 58 if (matchPattern[j] == escapeByte) { 59 flag = true; 60 j++; 61 if (j >= matchPattern.length) { 62 return 1; 63 } 64 } 65 if (matchPattern[j] == 95 && !flag) { if (percent) { 67 if (j + 1 >= matchPattern.length) { 68 return 0; 69 } 70 } 71 } else if (matchPattern[j] == 37 && !flag) { if (j + 1 >= matchPattern.length || (j + 1 == matchPattern.length && matchPattern[j + 1] == '_')) { 73 return 0; 74 } else { 75 percent = true; 76 i--; 77 } 78 } else { 79 if (percent) { 80 if (i < matchValue.length) { 81 int[] indexes = getPossibleIndexes(matchValue, i, matchPattern[j]); 82 for (int a = indexes.length - 1; a >= 0; a--) { 83 if (compareEscapeLike(extractRestOfBytes(indexes[a], matchValue), extractRestOfBytes(j, matchPattern)) == 0) { 84 return 0; 85 } 86 } 87 return 1; 88 } else { 89 return 1; 90 } 91 } 92 if (i >= matchValue.length || getAppropriateByte(matchValue[i]) != getAppropriateByte(matchPattern[j])) { 93 return 1; 94 } 95 flag = false; 96 } 97 } 98 if (i == matchValue.length && j == matchPattern.length) { 99 return 0; 100 } else if (i != matchValue.length) { for (; i < matchValue.length; i++) { 102 if (matchValue[i] != 32) { 103 break; 104 } 105 } 106 if (i == matchValue.length) { 107 return 0; 108 } 109 } 110 111 return 1; 112 } 113 114 private byte getAppropriateByte(byte b) { 115 return b >= 97 && b <= 122 ? (byte) (b - 32) : b; 116 } 117 118 private byte[] extractRestOfBytes(int fromIndex, byte[] sourceArray) throws DException { 119 int length = sourceArray.length - fromIndex; 120 byte[] newArray = new byte[length]; 121 for (int i = fromIndex, j = 0; i < sourceArray.length; i++, j++) { 122 newArray[j] = sourceArray[i]; 123 } 124 return newArray; 125 } 126 127 private int[] getPossibleIndexes(byte[] source, int index1, byte b) throws DException { 128 int length = 0; 129 int[] array = new int[length]; 130 for (; index1 < source.length; index1++) { 131 if (source[index1] == b) { 132 length++; 133 int[] tempArray = new int[length]; 134 System.arraycopy(array, 0, tempArray, 0, array.length); 135 tempArray[length - 1] = index1; 136 array = tempArray; 137 } 138 } 139 return array; 140 } 141 142 private byte[] convertToLowerCase(byte[] values){ 143 byte[] resultantValues = new byte[values.length]; 144 for(int i=0;i<values.length;i++) 145 convertToLowerCase(values[i]); 146 return resultantValues; 147 } 148 149 private byte convertToLowerCase(byte value) { 150 if(value>=65 && value<=91) 151 return (byte)(value+32); 152 return value; 153 } 154 155 public String toString() { 156 return "EscapeComparator "; 157 } 158 } 159 | Popular Tags |