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 import com.daffodilwoods.database.utility.P; 11 12 20 21 public class SimpleLikeComparator extends SuperComparator { 22 23 private boolean firstClob, secondClob; 24 25 public SimpleLikeComparator(boolean firstClob0, boolean secondClob0) { 26 firstClob = firstClob0; 27 secondClob = secondClob0; 28 } 29 30 public int compare(_DComparator leftMatchValue0, _DComparator rightPattern0) throws DException { 31 char[] matchValue = firstClob ? getValueForClob(leftMatchValue0) 32 : ( (String ) ( (FieldBase) leftMatchValue0).getObject()).toLowerCase().toCharArray(); 33 char[] matchPattern = secondClob ? getValueForClob(rightPattern0) 34 : ( (String ) ( (FieldBase) rightPattern0).getObject()).toLowerCase().toCharArray(); 35 return compareLike(matchValue, matchPattern); 36 } 37 38 private char[] getValueForClob(_DComparator value) throws DException { 39 DClobUpdatable clob = (DClobUpdatable) value; 40 try { 41 return clob.getSubString(1, (int) clob.length()).toLowerCase().toCharArray(); 42 } catch (SQLException ex) { 43 throw new DException("DSE0", new Object [] {"" + ex}); 44 } 45 } 46 47 protected int compareLike(char[] matchValue, char[] matchPattern) throws DException { 48 int length = matchValue.length > matchPattern.length ? matchValue.length : matchPattern.length; 49 boolean percent = false; 50 int i, j; 51 for (i = 0, j = 0; j < length; i++, j++) { 52 if (j >= matchPattern.length) { 53 break; 54 } 55 switch (matchPattern[j]) { 56 case '_': if (percent) { 58 if (j + 1 >= matchPattern.length) { 59 return 0; 60 } 61 } 62 break; 63 case '%': if (j + 1 >= matchPattern.length || (j + 1 == matchPattern.length && matchPattern[j + 1] == '_')) { 65 return 0; 66 } else { 67 percent = true; 68 i--; 69 } 70 break; 71 default: 72 if (percent) { 73 if (i < matchValue.length) { 74 int[] indexes = getPossibleIndexes(matchValue, i, matchPattern[j]); 75 for (int a = indexes.length - 1; a >= 0; a--) { 76 if (compareLike(extractRestOfChars(indexes[a], matchValue), extractRestOfChars(j, matchPattern)) == 0) { 77 return 0; 78 } 79 } 80 return 1; 81 } else { 82 return 1; 83 } 84 } 85 if (i >= matchValue.length || matchValue[i] != matchPattern[j]) { 86 return 1; 87 } 88 } 89 } 90 if (i == matchValue.length && j == matchPattern.length) { 91 return 0; 92 } else if (i != matchValue.length) { 93 for (; i < matchValue.length; i++) { 94 if (matchValue[i] != ' ') { 95 break; 96 } 97 } 98 if (i == matchValue.length) { 99 return 0; 100 } 101 } 102 return 1; 103 } 104 105 private char[] extractRestOfChars(int fromIndex, char[] sourceArray) throws DException { 106 int length = sourceArray.length - fromIndex; 107 char[] newArray = new char[length]; 108 for (int i = fromIndex, j = 0; i < sourceArray.length; i++, j++) { 109 newArray[j] = sourceArray[i]; 110 } 111 return newArray; 112 } 113 114 private int[] getPossibleIndexes(char[] source, int index1, char b) throws DException { 115 int length = 0; 116 int[] array = new int[length]; 117 for (; index1 < source.length; index1++) { 118 if (source[index1] == b) { 119 length++; 120 int[] tempArray = new int[length]; 121 System.arraycopy(array, 0, tempArray, 0, array.length); 122 tempArray[length - 1] = index1; 123 array = tempArray; 124 } 125 } 126 return array; 127 } 128 129 public String toString() { 130 return "SimpleLikeComparator"; 131 } 132 } 133 | Popular Tags |