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.util.StringUtil; 019 020/** 021 * 用于对数据库的关键字包装 022 */ 023public class KeywordWrap { 024 025 /** 026 * 无反义处理, 适用于 db2, informix, clickhouse 等 027 */ 028 public final static KeywordWrap NONE = new KeywordWrap("", "") { 029 @Override 030 public String wrap(String keyword) { 031 return keyword; 032 } 033 }; 034 035 /** 036 * 反引号反义处理, 适用于 mysql, h2 等 037 */ 038 public final static KeywordWrap BACKQUOTE = new KeywordWrap("`", "`"); 039 040 /** 041 * 双引号反义处理, 适用于 postgresql, sqlite, derby, oracle 等 042 */ 043 public final static KeywordWrap DOUBLE_QUOTATION = new KeywordWrap("\"", "\""); 044 045 /** 046 * 方括号反义处理, 适用于 sqlserver 047 */ 048 public final static KeywordWrap SQUARE_BRACKETS = new KeywordWrap("[", "]"); 049 050 /** 051 * 前缀 052 */ 053 private final String prefix; 054 055 /** 056 * 后缀 057 */ 058 private final String suffix; 059 060 061 public KeywordWrap(String prefix, String suffix) { 062 this.prefix = prefix; 063 this.suffix = suffix; 064 } 065 066 public String wrap(String keyword) { 067 return StringUtil.isBlank(keyword) ? "" : prefix + keyword + suffix; 068 } 069 070}