001/* 002 * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package com.mybatisflex.core.dialect; 017 018import com.mybatisflex.core.constant.SqlConsts; 019import com.mybatisflex.core.util.StringUtil; 020 021import java.util.Collections; 022import java.util.Locale; 023import java.util.Set; 024 025/** 026 * 用于对数据库的关键字包装 027 */ 028public class KeywordWrap { 029 030 /** 031 * 无反义处理, 适用于 db2, informix, clickhouse 等 032 */ 033 public static final KeywordWrap NONE = new KeywordWrap("", "") { 034 @Override 035 public String wrap(String keyword) { 036 return keyword; 037 } 038 }; 039 040 /** 041 * 反引号反义处理, 适用于 mysql, h2 等 042 */ 043 public static final KeywordWrap BACK_QUOTE = new KeywordWrap("`", "`"); 044 045 /** 046 * 双引号反义处理, 适用于 postgresql, sqlite, derby, oracle 等 047 */ 048 public static final KeywordWrap DOUBLE_QUOTATION = new KeywordWrap("\"", "\""); 049 050 /** 051 * 方括号反义处理, 适用于 sqlserver 052 */ 053 public static final KeywordWrap SQUARE_BRACKETS = new KeywordWrap("[", "]"); 054 055 /** 056 * 大小写敏感 057 */ 058 private boolean caseSensitive = false; 059 060 /** 061 * 自动把关键字转换为大写 062 */ 063 private boolean keywordsToUpperCase = false; 064 065 /** 066 * 数据库关键字 067 */ 068 private final Set<String> keywords; 069 070 /** 071 * 前缀 072 */ 073 private final String prefix; 074 075 /** 076 * 后缀 077 */ 078 private final String suffix; 079 080 081 public KeywordWrap(String prefix, String suffix) { 082 this(false, Collections.emptySet(), prefix, suffix); 083 } 084 085 public KeywordWrap(Set<String> keywords, String prefix, String suffix) { 086 this(false, keywords, prefix, suffix); 087 } 088 089 public KeywordWrap(boolean caseSensitive, Set<String> keywords, String prefix, String suffix) { 090 this.caseSensitive = caseSensitive; 091 this.keywords = keywords; 092 this.prefix = prefix; 093 this.suffix = suffix; 094 } 095 096 public KeywordWrap(boolean caseSensitive, boolean keywordsToUpperCase, Set<String> keywords, String prefix, String suffix) { 097 this.caseSensitive = caseSensitive; 098 this.keywordsToUpperCase = keywordsToUpperCase; 099 this.keywords = keywords; 100 this.prefix = prefix; 101 this.suffix = suffix; 102 } 103 104 public String wrap(String keyword) { 105 if (StringUtil.isBlank(keyword) || SqlConsts.ASTERISK.equals(keyword.trim())) { 106 return keyword; 107 } 108 109 if (caseSensitive || keywords.isEmpty()) { 110 return prefix + keyword + suffix; 111 } 112 113 if (keywords.contains(keyword.toUpperCase(Locale.ENGLISH))) { 114 if (keywordsToUpperCase) { 115 return prefix + keyword.toUpperCase() + suffix; 116 } else { 117 return prefix + keyword + suffix; 118 } 119 } 120 return keyword; 121 } 122 123}