mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
新增数据库Wrapper支持反解
This commit is contained in:
@@ -12,11 +12,10 @@
|
||||
|
||||
package org.dromara.hutool.db.sql;
|
||||
|
||||
import org.dromara.hutool.core.array.ArrayUtil;
|
||||
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;
|
||||
import org.dromara.hutool.db.Entity;
|
||||
|
||||
import java.io.Serializable;
|
||||
@@ -33,6 +32,11 @@ import java.util.Map.Entry;
|
||||
public class QuoteWrapper implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 无需包装和解包装的关键字
|
||||
*/
|
||||
private static final String[] IGNORE_WRAPPER_KEYS = {"*", "(", " ", " as "};
|
||||
|
||||
/**
|
||||
* 前置包装符号
|
||||
*/
|
||||
@@ -122,7 +126,7 @@ public class QuoteWrapper implements Serializable {
|
||||
}
|
||||
|
||||
//如果字段中包含通配符或者括号(字段通配符或者函数),不做包装
|
||||
if (StrUtil.containsAnyIgnoreCase(field, "*", "(", " ", " as ")) {
|
||||
if (StrUtil.containsAnyIgnoreCase(field, IGNORE_WRAPPER_KEYS)) {
|
||||
return field;
|
||||
}
|
||||
|
||||
@@ -132,20 +136,20 @@ public class QuoteWrapper implements Serializable {
|
||||
// 用户名和表名不能包含空格
|
||||
SplitUtil.split(field, StrUtil.DOT, 2, true, false),
|
||||
// 用户名和表名都加引号
|
||||
t -> StrUtil.format("{}{}{}", preWrapQuote, t, sufWrapQuote));
|
||||
t -> StrUtil.wrap(t, preWrapQuote, sufWrapQuote));
|
||||
return CollUtil.join(target, StrUtil.DOT);
|
||||
}
|
||||
|
||||
return StrUtil.format("{}{}{}", preWrapQuote, field, sufWrapQuote);
|
||||
return StrUtil.wrap(field, preWrapQuote, sufWrapQuote);
|
||||
}
|
||||
|
||||
/**
|
||||
* 反解包装字段名<br>
|
||||
* 解包装字段名
|
||||
*
|
||||
* @param field 字段名
|
||||
* @return 未包装的字段名
|
||||
*/
|
||||
public String unWrap(String field) {
|
||||
public String unWrap(final String field) {
|
||||
if (preWrapQuote == null || sufWrapQuote == null || StrUtil.isBlank(field)) {
|
||||
return field;
|
||||
}
|
||||
@@ -155,8 +159,8 @@ public class QuoteWrapper implements Serializable {
|
||||
return field;
|
||||
}
|
||||
|
||||
//如果字段中包含通配符或者括号(字段通配符或者函数),不做包装
|
||||
if (StrUtil.containsAnyIgnoreCase(field, "*", "(", " ", " as ")) {
|
||||
//如果字段中包含通配符或者括号(字段通配符或者函数),不做解包
|
||||
if (StrUtil.containsAnyIgnoreCase(field, IGNORE_WRAPPER_KEYS)) {
|
||||
return field;
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,12 @@
|
||||
package org.dromara.hutool.db;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class IssueI9BANETest {
|
||||
@Test
|
||||
@Disabled
|
||||
void metaTest() {
|
||||
final Db db = Db.of("orcl");
|
||||
}
|
||||
}
|
@@ -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 QuoteWrapperTest {
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void test() {
|
||||
final QuoteWrapper wrapper = new QuoteWrapper('`');
|
||||
final String originalName = "name";
|
||||
final String wrapName = wrapper.wrap(originalName);
|
||||
final String unWrapName = wrapper.unWrap(wrapName);
|
||||
Assertions.assertEquals(unWrapName, originalName);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void testDotWrap() {
|
||||
final QuoteWrapper wrapper = new QuoteWrapper('`');
|
||||
final String originalName = "name.age";
|
||||
final String wrapName = wrapper.wrap(originalName);
|
||||
final String unWrapName = wrapper.unWrap(wrapName);
|
||||
Assertions.assertEquals(unWrapName, originalName);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void testError() {
|
||||
final QuoteWrapper wrapper = new QuoteWrapper('`');
|
||||
final String originalName = "name.age*";
|
||||
final String wrapName = wrapper.wrap(originalName);
|
||||
final String unWrapName = wrapper.unWrap(wrapName);
|
||||
Assertions.assertEquals(unWrapName, originalName);
|
||||
}
|
||||
}
|
@@ -1,43 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
@@ -59,8 +59,8 @@ remarks = true
|
||||
|
||||
# 测试用Oracle数据库
|
||||
[orcl]
|
||||
url = jdbc:oracle:thin:@//looly.centos:1521/XE
|
||||
user = looly
|
||||
url = jdbc:oracle:thin:@//localhost:1521/XEPDB1
|
||||
user = system
|
||||
pass = 123456
|
||||
remarks = true
|
||||
|
||||
@@ -84,7 +84,7 @@ remarks = true
|
||||
|
||||
# 测试用dm数据库
|
||||
[dm]
|
||||
url = jdbc:dm://127.0.0.1:30236/schema=dm8_test
|
||||
url = jdbc:dm://localhost:30236/schema=dm8_test
|
||||
user = SYSDBA
|
||||
pass = 123456789
|
||||
pass = 123456
|
||||
remarks = true
|
||||
|
Reference in New Issue
Block a user