新增数据库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)); 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)); return str.toString().concat(repeatByLength(padStr, length - strLen));
} }
// region // endregion
// region ----- center // region ----- center
@@ -2628,7 +2640,7 @@ public class CharSequenceUtil extends StrValidator {
} }
// endregion // endregion
// ------------------------------------------------------------------------ count // region ----- count
/** /**
* 统计指定内容中包含指定字符串的数量<br> * 统计指定内容中包含指定字符串的数量<br>
@@ -2684,6 +2696,7 @@ public class CharSequenceUtil extends StrValidator {
} }
return count; return count;
} }
// endregion
// region ----- compare // region ----- compare
@@ -3724,6 +3737,7 @@ public class CharSequenceUtil extends StrValidator {
str2.substring(strLength - suffixLength)); str2.substring(strLength - suffixLength));
} }
// region ----- join
/** /**
* 以 conjunction 为分隔符将多个对象转换为字符串 * 以 conjunction 为分隔符将多个对象转换为字符串
* *
@@ -3750,6 +3764,8 @@ public class CharSequenceUtil extends StrValidator {
return CollUtil.join(iterable, conjunction); return CollUtil.join(iterable, conjunction);
} }
// endregion
/** /**
* 检查字符串是否都为数字组成 * 检查字符串是否都为数字组成
* *

View File

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

View File

@@ -12,11 +12,10 @@
package org.dromara.hutool.db.sql; 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.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.db.Entity; import org.dromara.hutool.db.Entity;
import java.io.Serializable; import java.io.Serializable;
@@ -33,6 +32,11 @@ import java.util.Map.Entry;
public class QuoteWrapper implements Serializable { public class QuoteWrapper implements Serializable {
private static final long serialVersionUID = 1L; 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; return field;
} }
@@ -132,20 +136,20 @@ public class QuoteWrapper implements Serializable {
// 用户名和表名不能包含空格 // 用户名和表名不能包含空格
SplitUtil.split(field, StrUtil.DOT, 2, true, false), 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 CollUtil.join(target, StrUtil.DOT);
} }
return StrUtil.format("{}{}{}", preWrapQuote, field, sufWrapQuote); return StrUtil.wrap(field, preWrapQuote, sufWrapQuote);
} }
/** /**
* 解包装字段名<br> * 解包装字段名
* *
* @param field 字段名 * @param field 字段名
* @return 未包装的字段名 * @return 未包装的字段名
*/ */
public String unWrap(String field) { public String unWrap(final String field) {
if (preWrapQuote == null || sufWrapQuote == null || StrUtil.isBlank(field)) { if (preWrapQuote == null || sufWrapQuote == null || StrUtil.isBlank(field)) {
return field; return field;
} }
@@ -155,8 +159,8 @@ public class QuoteWrapper implements Serializable {
return field; return field;
} }
//如果字段中包含通配符或者括号(字段通配符或者函数),不做包 //如果字段中包含通配符或者括号(字段通配符或者函数),不做
if (StrUtil.containsAnyIgnoreCase(field, "*", "(", " ", " as ")) { if (StrUtil.containsAnyIgnoreCase(field, IGNORE_WRAPPER_KEYS)) {
return field; 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数据库 # 测试用Oracle数据库
[orcl] [orcl]
url = jdbc:oracle:thin:@//looly.centos:1521/XE url = jdbc:oracle:thin:@//localhost:1521/XEPDB1
user = looly user = system
pass = 123456 pass = 123456
remarks = true remarks = true
@@ -84,7 +84,7 @@ remarks = true
# 测试用dm数据库 # 测试用dm数据库
[dm] [dm]
url = jdbc:dm://127.0.0.1:30236/schema=dm8_test url = jdbc:dm://localhost:30236/schema=dm8_test
user = SYSDBA user = SYSDBA
pass = 123456789 pass = 123456
remarks = true remarks = true