mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
commit
2e372534bd
@ -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出错,因此需要将字段名用单引号或者反引号包装起来,避免冲突
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user