| 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 LikeComparator extends SuperComparator { 19 20 private boolean firstClob, secondClob; 21 22 public LikeComparator(boolean firstClob0, boolean secondClob0) { 23 firstClob = firstClob0; 24 secondClob = secondClob0; 25 } 26 27 public int compare(_DComparator leftMatchValue0, _DComparator rightPattern0) throws DException { 28 byte[] matchValue = firstClob ? ( (DClobUpdatable) leftMatchValue0).getBytes() 29 : ( (FieldBase) leftMatchValue0).getBufferRange().getBytes(); 30 byte[] matchPattern = secondClob ? ( (DClobUpdatable) rightPattern0).getBytes() 31 : ( (FieldBase) rightPattern0).getBufferRange().getBytes(); 32 int cmp = compareLike(ConvertToLowerCase(matchValue) , ConvertToLowerCase(matchPattern),0,0 ); 33 return cmp; 34 } 35 36 37 private byte[] ConvertToLowerCase(byte[] values){ 38 for(int i=0;i<values.length;i++){ 39 if(values[i]>=65 && values[i]<=91){ 40 values[i] = (byte)(values[i]+32); 41 } 42 } 43 return values; 44 } 45 46 47 private void printBytes(byte[] b){ 48 for (int i = 0; i < b.length; i++) { 49 System.out.print((char)b[i]); 50 } 51 } 52 53 111 112 protected int compareLike(byte[] matchValue, byte[] matchPattern,int i1, int j1) throws DException { 113 int length = matchValue.length > matchPattern.length ? matchValue.length : matchPattern.length; 114 boolean percent = false; 115 int i,j; 116 for (i = i1, j = j1; j < length; i++, j++) { 117 if (j >= matchPattern.length) { 118 break; 119 } 120 switch (matchPattern[j]) { 121 case 95: if (percent) { 123 if (j + 1 >= matchPattern.length) { 124 return 0; 125 } 126 } 127 break; 128 case 37: if (j + 1 >= matchPattern.length || (j + 1 == matchPattern.length && matchPattern[j + 1] == '_')) { 130 return 0; 131 } else { 132 percent = true; 133 i--; 134 } 135 break; 136 default: 137 if (percent) { 138 if (i < matchValue.length) { 139 int[] indexes = getPossibleIndexes(matchValue, i, matchPattern[j]); 140 for (int a = indexes.length - 1; a >= 0; a--) { 141 if (compareLike(matchValue, matchPattern,indexes[a],j) == 0) { 142 return 0; 143 } 144 } 145 return 1; 146 } else { 147 return 1; 148 } 149 } 150 if (i >= matchValue.length || getAppropriateByte(matchValue[i]) != getAppropriateByte(matchPattern[j])) { 151 return 1; 152 } 153 } 154 } 155 if (i == matchValue.length && j == matchPattern.length) { 156 return 0; 157 } else if (i != matchValue.length) { 158 for (; i < matchValue.length; i++) { 159 if (matchValue[i] != 32) { 160 break; 161 } 162 } 163 if (i == matchValue.length) { 164 return 0; 165 } 166 } 167 return 1; 168 } 169 170 171 private byte getAppropriateByte(byte b) { 172 return b >= 97 && b <= 122 ? (byte) (b - 32) : b; 173 } 174 175 private byte[] extractRestOfBytes(int fromIndex, byte[] sourceArray) throws DException { 176 int length = sourceArray.length - fromIndex; 177 byte[] newArray = new byte[length]; 178 for (int i = fromIndex, j = 0; i < sourceArray.length; i++, j++) { 179 newArray[j] = sourceArray[i]; 180 } 181 return newArray; 182 } 183 184 private int[] getPossibleIndexes(byte[] source, int index1, byte b) throws DException { 185 int length = 0; 186 int[] array = new int[length]; 187 for (; index1 < source.length; index1++) { 188 if (source[index1] == b) { 189 length++; 190 int[] tempArray = new int[length]; 191 System.arraycopy(array, 0, tempArray, 0, array.length); 192 tempArray[length - 1] = index1; 193 array = tempArray; 194 } 195 } 196 return array; 197 } 198 199 public String toString() { 200 return "LikeComparator "; 201 } 202 203 } 204 | Popular Tags |