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