Debian Codegraph @

src/main/org/h2/expression/function/SoundexFunction.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.expression.function;
7
8 import java.nio.charset.StandardCharsets;
9
10 import org.h2.engine.SessionLocal;
11 import org.h2.expression.Expression;
12 import org.h2.expression.TypedValueExpression;
13 import org.h2.message.DbException;
14 import org.h2.value.TypeInfo;
15 import org.h2.value.Value;
16 import org.h2.value.ValueInteger;
17 import org.h2.value.ValueVarchar;
18
19 /**
20 * A SOUNDEX or DIFFERENCE function.
21 */
22 public final class SoundexFunction extends Function1_2 {
23
24 /**
25 * SOUNDEX() (non-standard).
26 */
27 public static final int SOUNDEX = 0;
28
29 /**
30 * DIFFERENCE() (non-standard).
31 */
32 public static final int DIFFERENCE = SOUNDEX + 1;
33
34 private static final String[] NAMES = { //
35 "SOUNDEX", "DIFFERENCE" //
36 };
37
38 private static final byte[] SOUNDEX_INDEX = //
39 "71237128722455712623718272\000\000\000\000\000\00071237128722455712623718272"
40 .getBytes(StandardCharsets.ISO_8859_1);
41
42 private final int function;
43
44 public SoundexFunction(Expression arg1, Expression arg2, int function) {
45 super(arg1, arg2);
46 this.function = function;
47 }
48
49 @Override
50 public Value getValue(SessionLocal session, Value v1, Value v2) {
51 switch (function) {
52 case SOUNDEX:
53 v1 = ValueVarchar.get(new String(getSoundex(v1.getString()), StandardCharsets.ISO_8859_1), session);
54 break;
55 case DIFFERENCE: {
56 v1 = ValueInteger.get(getDifference(v1.getString(), v2.getString()));
57 break;
58 }
59 default:
60 throw DbException.getInternalError("function=" + function);
61 }
62 return v1;
63 }
64
65 private static int getDifference(String s1, String s2) {
66 // TODO function difference: compatibility with SQL Server and HSQLDB
67 byte[] b1 = getSoundex(s1), b2 = getSoundex(s2);
68 int e = 0;
69 for (int i = 0; i < 4; i++) {
70 if (b1[i] == b2[i]) {
71 e++;
72 }
73 }
74 return e;
75 }
76
77 private static byte[] getSoundex(String s) {
78 byte[] chars = { '0', '0', '0', '0' };
79 byte lastDigit = '0';
80 for (int i = 0, j = 0, l = s.length(); i < l && j < 4; i++) {
81 char c = s.charAt(i);
82 if (c >= 'A' && c <= 'z') {
83 byte newDigit = SOUNDEX_INDEX[c - 'A'];
84 if (newDigit != 0) {
85 if (j == 0) {
86 chars[j++] = (byte) (c & 0xdf); // Converts a-z to A-Z
87 lastDigit = newDigit;
88 } else if (newDigit <= '6') {
89 if (newDigit != lastDigit) {
90 chars[j++] = lastDigit = newDigit;
91 }
92 } else if (newDigit == '7') {
93 lastDigit = newDigit;
94 }
95 }
96 }
97 }
98 return chars;
99 }
100
101 @Override
102 public Expression optimize(SessionLocal session) {
103 left = left.optimize(session);
104 if (right != null) {
105 right = right.optimize(session);
106 }
107 switch (function) {
108 case SOUNDEX:
109 type = TypeInfo.getTypeInfo(Value.VARCHAR, 4, 0, null);
110 break;
111 case DIFFERENCE:
112 type = TypeInfo.TYPE_INTEGER;
113 break;
114 default:
115 throw DbException.getInternalError("function=" + function);
116 }
117 if (left.isConstant() && (right == null || right.isConstant())) {
118 return TypedValueExpression.getTypedIfNull(getValue(session), type);
119 }
120 return this;
121 }
122
123 @Override
124 public String getName() {
125 return NAMES[function];
126 }
127
128 }