diff --git a/hutool-db/src/main/java/org/dromara/hutool/db/sql/QuoteWrapper.java b/hutool-db/src/main/java/org/dromara/hutool/db/sql/QuoteWrapper.java index f499c376e..10734a200 100644 --- a/hutool-db/src/main/java/org/dromara/hutool/db/sql/QuoteWrapper.java +++ b/hutool-db/src/main/java/org/dromara/hutool/db/sql/QuoteWrapper.java @@ -13,6 +13,7 @@ package org.dromara.hutool.db.sql; import org.dromara.hutool.core.collection.CollUtil; +import org.dromara.hutool.core.text.CharUtil; import org.dromara.hutool.core.text.StrUtil; import org.dromara.hutool.core.text.split.SplitUtil; import org.dromara.hutool.core.array.ArrayUtil; @@ -128,16 +129,48 @@ public class QuoteWrapper implements Serializable { //对于Oracle这类数据库,表名中包含用户名需要单独拆分包装 if (field.contains(StrUtil.DOT)) { final Collection target = CollUtil.edit( - // 用户名和表名不能包含空格 - SplitUtil.split(field, StrUtil.DOT, 2, true, false), - // 用户名和表名都加引号 - t -> StrUtil.format("{}{}{}", preWrapQuote, t, sufWrapQuote)); + // 用户名和表名不能包含空格 + SplitUtil.split(field, StrUtil.DOT, 2, true, false), + // 用户名和表名都加引号 + t -> StrUtil.format("{}{}{}", preWrapQuote, t, sufWrapQuote)); return CollUtil.join(target, StrUtil.DOT); } return StrUtil.format("{}{}{}", preWrapQuote, field, sufWrapQuote); } + /** + * 反解包装字段名
+ * + * @param field 字段名 + * @return 未包装的字段名 + */ + public String unWrap(String field) { + if (preWrapQuote == null || sufWrapQuote == null || StrUtil.isBlank(field)) { + return field; + } + + //如果已经包含包装的引号,返回原字符 + if (!StrUtil.isWrap(field, preWrapQuote, sufWrapQuote)) { + return field; + } + + //如果字段中包含通配符或者括号(字段通配符或者函数),不做包装 + if (StrUtil.containsAnyIgnoreCase(field, "*", "(", " ", " as ")) { + return field; + } + + //对于Oracle这类数据库,表名中包含用户名需要单独拆分包装 + if (field.contains(StrUtil.DOT)) { + final Collection target = CollUtil.edit( + SplitUtil.split(field, StrUtil.DOT, 2, true, false), + t -> StrUtil.unWrap(t, preWrapQuote, sufWrapQuote)); + return CollUtil.join(target, StrUtil.DOT); + } + + return StrUtil.unWrap(field, preWrapQuote, sufWrapQuote); + } + /** * 包装字段名
* 有时字段与SQL的某些关键字冲突,导致SQL出错,因此需要将字段名用单引号或者反引号包装起来,避免冲突 diff --git a/hutool-db/src/test/java/org/dromara/hutool/db/WrapperTest.java b/hutool-db/src/test/java/org/dromara/hutool/db/WrapperTest.java new file mode 100644 index 000000000..de217c175 --- /dev/null +++ b/hutool-db/src/test/java/org/dromara/hutool/db/WrapperTest.java @@ -0,0 +1,43 @@ +package org.dromara.hutool.db; + +import org.dromara.hutool.db.sql.QuoteWrapper; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +/** + * @author bwcx_jzy + * @since 24/3/28 028 + */ +public class WrapperTest { + + @Test + @Disabled + public void test() { + QuoteWrapper wrapper = new QuoteWrapper('`'); + String originalName = "name"; + String wrapName = wrapper.wrap(originalName); + String unWrapName = wrapper.unWrap(wrapName); + Assertions.assertEquals(unWrapName, originalName); + } + + @Test + @Disabled + public void testDotWrap() { + QuoteWrapper wrapper = new QuoteWrapper('`'); + String originalName = "name.age"; + String wrapName = wrapper.wrap(originalName); + String unWrapName = wrapper.unWrap(wrapName); + Assertions.assertEquals(unWrapName, originalName); + } + + @Test + @Disabled + public void testError() { + QuoteWrapper wrapper = new QuoteWrapper('`'); + String originalName = "name.age*"; + String wrapName = wrapper.wrap(originalName); + String unWrapName = wrapper.unWrap(wrapName); + Assertions.assertEquals(unWrapName, originalName); + } +}