com.jn.sqlhelper.dialect.likeescaper.BaseLikeEscaper Maven / Gradle / Ivy
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the LGPL, Version 3.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.gnu.org/licenses/lgpl-3.0.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jn.sqlhelper.dialect.likeescaper;
import com.jn.langx.util.Emptys;
import com.jn.langx.util.collection.Collects;
import com.jn.langx.util.collection.PrimitiveArrays;
import com.jn.langx.util.function.Consumer;
import java.util.List;
public class BaseLikeEscaper implements LikeEscaper {
protected final List keyChars = Collects.asList(
'\'', '_', '%'
);
protected char escapeChar = '0';
public BaseLikeEscaper() {
}
public BaseLikeEscaper(char escapeChar) {
this.escapeChar = escapeChar;
}
@Override
public List getLikeKeyChars() {
return keyChars;
}
public char getEscapeChar(){
return escapeChar;
}
@Override
public String escape(String pattern) {
final List specifiedChars = getLikeKeyChars();
if (Emptys.isNotEmpty(pattern)) {
final StringBuilder builder = new StringBuilder(pattern.length() + 20);
Collects.forEach(PrimitiveArrays.wrap(pattern.toCharArray(), true), new Consumer() {
@Override
public void accept(Character c) {
if (specifiedChars.contains(c)) {
builder.append(escapeLikeKeyChar(c));
} else {
if (c == escapeChar) {
builder.append(escapeChar).append(escapeChar);
} else {
builder.append(c);
}
}
}
});
return builder.toString();
}
return pattern;
}
protected String escapeLikeKeyChar(char c) {
return escapeChar == '0' ? ("" + c) : ("" + escapeChar + c);
}
@Override
public String appendmentAfterLikeClause() {
return " ESCAPE '" + escapeChar + "' ";
}
@Override
public String toString() {
return "BaseLikeEscaper{" +
"keyChars=" + keyChars +
", escapeChar=" + escapeChar +
'}';
}
}