!1193 v6新增 数据库 wrapper 支持反解

Merge pull request !1193 from 蒋小小/v6-dev
This commit is contained in:
Looly 2024-03-28 07:52:20 +00:00 committed by Gitee
commit 2e372534bd
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 80 additions and 4 deletions

View File

@ -13,6 +13,7 @@
package org.dromara.hutool.db.sql; package org.dromara.hutool.db.sql;
import org.dromara.hutool.core.collection.CollUtil; 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.StrUtil;
import org.dromara.hutool.core.text.split.SplitUtil; import org.dromara.hutool.core.text.split.SplitUtil;
import org.dromara.hutool.core.array.ArrayUtil; import org.dromara.hutool.core.array.ArrayUtil;
@ -128,16 +129,48 @@ public class QuoteWrapper implements Serializable {
//对于Oracle这类数据库表名中包含用户名需要单独拆分包装 //对于Oracle这类数据库表名中包含用户名需要单独拆分包装
if (field.contains(StrUtil.DOT)) { if (field.contains(StrUtil.DOT)) {
final Collection<String> target = CollUtil.edit( final Collection<String> target = CollUtil.edit(
// 用户名和表名不能包含空格 // 用户名和表名不能包含空格
SplitUtil.split(field, StrUtil.DOT, 2, true, false), SplitUtil.split(field, StrUtil.DOT, 2, true, false),
// 用户名和表名都加引号 // 用户名和表名都加引号
t -> StrUtil.format("{}{}{}", preWrapQuote, t, sufWrapQuote)); t -> StrUtil.format("{}{}{}", preWrapQuote, t, sufWrapQuote));
return CollUtil.join(target, StrUtil.DOT); return CollUtil.join(target, StrUtil.DOT);
} }
return StrUtil.format("{}{}{}", preWrapQuote, field, sufWrapQuote); return StrUtil.format("{}{}{}", preWrapQuote, field, sufWrapQuote);
} }
/**
* 反解包装字段名<br>
*
* @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<String> 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);
}
/** /**
* 包装字段名<br> * 包装字段名<br>
* 有时字段与SQL的某些关键字冲突导致SQL出错因此需要将字段名用单引号或者反引号包装起来避免冲突 * 有时字段与SQL的某些关键字冲突导致SQL出错因此需要将字段名用单引号或者反引号包装起来避免冲突

View File

@ -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);
}
}