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.Set; 023 024/** 025 * 用于对数据库的关键字包装 026 */ 027public class KeywordWrap { 028 029 /** 030 * 无反义处理, 适用于 db2, informix, clickhouse 等 031 */ 032 public static final KeywordWrap NONE = new KeywordWrap("", "") { 033 @Override 034 public String wrap(String keyword) { 035 return keyword; 036 } 037 }; 038 039 /** 040 * 反引号反义处理, 适用于 mysql, h2 等 041 */ 042 public static final KeywordWrap BACK_QUOTE = new KeywordWrap("`", "`"); 043 044 /** 045 * 双引号反义处理, 适用于 postgresql, sqlite, derby, oracle 等 046 */ 047 public static final KeywordWrap DOUBLE_QUOTATION = new KeywordWrap("\"", "\""); 048 049 /** 050 * 方括号反义处理, 适用于 sqlserver 051 */ 052 public static final KeywordWrap SQUARE_BRACKETS = new KeywordWrap("[", "]"); 053 054 /** 055 * 大小写敏感 056 */ 057 private boolean caseSensitive = false; 058 059 /** 060 * 自动把关键字转换为大写 061 */ 062 private boolean keywordsToUpperCase = false; 063 064 /** 065 * 数据库关键字 066 */ 067 private final Set<String> keywords; 068 069 /** 070 * 前缀 071 */ 072 private final String prefix; 073 074 /** 075 * 后缀 076 */ 077 private final String suffix; 078 079 080 public KeywordWrap(String prefix, String suffix) { 081 this(false, Collections.emptySet(), prefix, suffix); 082 } 083 084 public KeywordWrap(Set<String> keywords, String prefix, String suffix) { 085 this(false, keywords, prefix, suffix); 086 } 087 088 public KeywordWrap(boolean caseSensitive, Set<String> keywords, String prefix, String suffix) { 089 this.caseSensitive = caseSensitive; 090 this.keywords = keywords; 091 this.prefix = prefix; 092 this.suffix = suffix; 093 } 094 095 public KeywordWrap(boolean caseSensitive, boolean keywordsToUpperCase, Set<String> keywords, String prefix, String suffix) { 096 this.caseSensitive = caseSensitive; 097 this.keywordsToUpperCase = keywordsToUpperCase; 098 this.keywords = keywords; 099 this.prefix = prefix; 100 this.suffix = suffix; 101 } 102 103 public String wrap(String keyword) { 104 if (StringUtil.isBlank(keyword) || SqlConsts.ASTERISK.equals(keyword.trim())) { 105 return keyword; 106 } 107 108 if (caseSensitive || keywords.isEmpty()) { 109 return prefix + keyword + suffix; 110 } 111 112 keyword = keywordsToUpperCase ? keyword.toUpperCase() : keyword; 113 return keywords.contains(keyword) ? (prefix + keyword + suffix) : keyword; 114 } 115 116 public boolean isCaseSensitive() { 117 return caseSensitive; 118 } 119 120 public void setCaseSensitive(boolean caseSensitive) { 121 this.caseSensitive = caseSensitive; 122 } 123 124 public boolean isKeywordsToUpperCase() { 125 return keywordsToUpperCase; 126 } 127 128 public void setKeywordsToUpperCase(boolean keywordsToUpperCase) { 129 this.keywordsToUpperCase = keywordsToUpperCase; 130 } 131 132 public Set<String> getKeywords() { 133 return keywords; 134 } 135 136 public String getPrefix() { 137 return prefix; 138 } 139 140 public String getSuffix() { 141 return suffix; 142 } 143}