新增数据库Wrapper支持反解

This commit is contained in:
Looly
2024-03-28 16:08:17 +08:00
parent 2e372534bd
commit 6854e342cf
7 changed files with 96 additions and 58 deletions

View File

@@ -2212,6 +2212,18 @@ public class CharSequenceUtil extends StrValidator {
return emptyIfNull(prefix).concat(emptyIfNull(str)).concat(emptyIfNull(suffix));
}
/**
* 包装指定字符串
*
* @param str 被包装的字符串
* @param prefix 前缀
* @param suffix 后缀
* @return 包装后的字符串
*/
public static String wrap(final CharSequence str, final char prefix, final char suffix) {
return prefix + emptyIfNull(str) + suffix;
}
/**
* 使用单个字符包装多个字符串
*
@@ -2531,7 +2543,7 @@ public class CharSequenceUtil extends StrValidator {
return str.toString().concat(repeatByLength(padStr, length - strLen));
}
// region
// endregion
// region ----- center
@@ -2628,7 +2640,7 @@ public class CharSequenceUtil extends StrValidator {
}
// endregion
// ------------------------------------------------------------------------ count
// region ----- count
/**
* 统计指定内容中包含指定字符串的数量<br>
@@ -2684,6 +2696,7 @@ public class CharSequenceUtil extends StrValidator {
}
return count;
}
// endregion
// region ----- compare
@@ -3724,6 +3737,7 @@ public class CharSequenceUtil extends StrValidator {
str2.substring(strLength - suffixLength));
}
// region ----- join
/**
* 以 conjunction 为分隔符将多个对象转换为字符串
*
@@ -3750,6 +3764,8 @@ public class CharSequenceUtil extends StrValidator {
return CollUtil.join(iterable, conjunction);
}
// endregion
/**
* 检查字符串是否都为数字组成
*

View File

@@ -153,5 +153,11 @@
<version>2.0.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>23.3.0.23.09</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

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

View File

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

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

View File

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

View File

@@ -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