1 package com.quadcap.sql.types; 2 3 40 41 import java.io.Externalizable ; 42 import java.io.IOException ; 43 import java.io.ObjectInput ; 44 import java.io.ObjectOutput ; 45 46 import antlr.RecognitionException; 47 48 import com.quadcap.util.Debug; 49 50 55 public class ValuePattern extends Value implements Externalizable { 56 String pattern; 57 char escape = 0; 58 public static final String defaultEscape = "\\"; 59 60 public ValuePattern() {} 61 62 public ValuePattern(String pattern, String esc) throws ValueException { 63 this.pattern = pattern; 64 if (esc != null) { 65 if (esc.length() != 1) { 66 throw new ValueException( 67 "Pattern escape must be a single character"); 68 } 69 escape = esc.charAt(0); 70 } 71 } 72 73 public Value binop(int op, Value l) throws ValueException { 74 return l.binop(op, this); 75 } 76 77 public static final Value binop(int op, ValueString l, ValuePattern r) 78 throws ValueException 79 { 80 if (op != Op.LIKE) { 81 throw new ValueException("Not implemented: " + 82 Op.toString(op)); 83 } 84 String ls = l.stringValue(); 85 String rs = r.pattern; 86 return new ValueBoolean(pMatch(ls, 0, rs, 0, r.escape)); 87 } 88 89 static final char getChar(final String s, int i) { 90 if (isCaseSensitive) { 91 return s.charAt(i); 92 } else { 93 return Character.toLowerCase(s.charAt(i)); 94 } 95 } 96 97 100 static boolean pMatch(String va, int a, String vb, int b, char escape) 101 throws ValueException 102 { 103 while (b < vb.length()) { 104 char p = getChar(vb, b++); 105 switch (p) { 106 case '%': 107 if (b >= vb.length()) return true; 108 while (a < va.length()) { 109 if (pMatch(va, a++, vb, b, escape)) return true; 110 } 111 return false; 112 case '_': 113 if (a++ >= va.length()) return false; 114 break; 115 default: 116 if (a >= va.length()) return false; 117 if (p == escape) { 118 if (b >= vb.length()) { 119 throw new ValueException("Invalid escape sequence"); 120 } 121 p = getChar(vb, b++); 122 if (p != '_' && p != '%' && p != escape) { 123 throw new ValueException("Invalid escape sequence"); 124 } 125 } 126 if (getChar(va, a++) != p) { 127 return false; 128 } 129 } 130 } 131 return a >= va.length(); 132 } 133 134 public void readExternal(ObjectInput in) 135 throws IOException , ClassNotFoundException 136 { 137 pattern = (String )in.readObject(); 138 escape = in.readChar(); 139 } 140 141 public void writeExternal(ObjectOutput out) throws IOException { 142 out.writeObject(pattern); 143 out.writeChar(escape); 144 } 145 146 public String toString() { 147 StringBuffer sb = new StringBuffer ("'"); 148 sb.append(pattern); 149 sb.append('\''); 150 if (escape != 0) { 151 sb.append(" ESCAPE '"); 152 sb.append(escape); 153 sb.append('\''); 154 } 155 return sb.toString(); 156 } 157 158 public Object asJavaObject() { 159 return pattern; 160 } 161 162 public void fromJavaObject(Object obj) throws ValueException { 163 throw new ValueException("bad type: " + obj); 164 } 165 166 public Type getType() { 167 return TypeVarChar.typeVarChar; 168 } 169 170 public void serializeKey(KeyStream out) throws IOException { 171 throw new IOException ("Can't use 'pattern' as key"); 172 } 173 } 174 | Popular Tags |