diff --git a/src/test/java/youhong/ai/mymapper/ParseSqlTest.java b/src/test/java/youhong/ai/mymapper/ParseSqlTest.java index be37f88..c384060 100644 --- a/src/test/java/youhong/ai/mymapper/ParseSqlTest.java +++ b/src/test/java/youhong/ai/mymapper/ParseSqlTest.java @@ -22,7 +22,7 @@ public class ParseSqlTest extends BaseTest { @Test public void test() { String sql = "select $t{table} into set field = #{field} from table :my-where{\n" + - "\t:my-for:item=\"item\":index=\"index\":collection=\"ids\":open=\"\":separator=\"\":close=\"\":nullable{\n" + + "\t:my-for:item=\"item\":index=\"index\":collection=\"ids\":open=\"\":separator=\",\":close=\"\\\":nullable{\n" + "\t\tand $t{index} = #{item}\n" + "\t}\n" + "} order by abase = #{order} $t{desc}"; diff --git a/src/test/java/youhong/ai/mymapper/command/CommandExecutor.java b/src/test/java/youhong/ai/mymapper/command/CommandExecutor.java index 2e247f4..7e4ab50 100644 --- a/src/test/java/youhong/ai/mymapper/command/CommandExecutor.java +++ b/src/test/java/youhong/ai/mymapper/command/CommandExecutor.java @@ -1,9 +1,6 @@ package youhong.ai.mymapper.command; -import org.apache.commons.lang.StringUtils; -import youhong.ai.mymapper.command.constant.CommandConsTant; import youhong.ai.mymapper.command.entity.SqlCommandDefinition; -import youhong.ai.mymapper.command.properties.AbstractCommandProperties; import youhong.ai.mymapper.util.ParseSqlUtil; /** @@ -19,27 +16,8 @@ public class CommandExecutor { public String executor(SqlCommandDefinition commandDefinition) { ParseSqlUtil parseSqlUtil = new ParseSqlUtil(); ISqlCommand actuator = commandDefinition.getActuator(); - String commandType = commandDefinition.getCommandType(); - AbstractCommandProperties properties = commandDefinition.getProperties(); - if (CommandConsTant.WHERE.equals(commandType)) { - String commandContent = commandDefinition.getCommandContent(); - String parse = parseSqlUtil.parse(commandContent); - String sql = parseSqlUtil.parseStatement(parse); - if (StringUtils.isNotBlank(sql)) { - String trim = sql.trim(); - if (trim.toUpperCase().startsWith("AND ")) { - trim = trim.substring(3); - } - if (trim.toUpperCase().startsWith("OR ")) { - trim = trim.substring(2); - } - sql = " where " + trim; - return sql; - } - } - String parseString = actuator.execute(properties); - String sql = parseSqlUtil.parseStatement(parseString); - return sql; + String parseString = actuator.execute(commandDefinition); + return parseSqlUtil.parseStatement(parseString); } diff --git a/src/test/java/youhong/ai/mymapper/command/ISqlCommand.java b/src/test/java/youhong/ai/mymapper/command/ISqlCommand.java index 5a7f67f..e8d9bde 100644 --- a/src/test/java/youhong/ai/mymapper/command/ISqlCommand.java +++ b/src/test/java/youhong/ai/mymapper/command/ISqlCommand.java @@ -1,6 +1,6 @@ package youhong.ai.mymapper.command; -import youhong.ai.mymapper.command.properties.AbstractCommandProperties; +import youhong.ai.mymapper.command.entity.SqlCommandDefinition; /** *

sql 指令解析接口

@@ -11,5 +11,5 @@ import youhong.ai.mymapper.command.properties.AbstractCommandProperties; */ public interface ISqlCommand { - String execute(AbstractCommandProperties commandProperties); + String execute(SqlCommandDefinition sqlCommandDefinition); } diff --git a/src/test/java/youhong/ai/mymapper/command/commandImpl/MyForCommand.java b/src/test/java/youhong/ai/mymapper/command/commandImpl/MyForCommand.java index 2ed0fdc..796a97c 100644 --- a/src/test/java/youhong/ai/mymapper/command/commandImpl/MyForCommand.java +++ b/src/test/java/youhong/ai/mymapper/command/commandImpl/MyForCommand.java @@ -7,6 +7,7 @@ import weaver.common.util.string.StringUtil; import youhong.ai.mymapper.command.ISqlCommand; import youhong.ai.mymapper.command.annotation.SqlCommand; import youhong.ai.mymapper.command.constant.CommandConsTant; +import youhong.ai.mymapper.command.entity.SqlCommandDefinition; import youhong.ai.mymapper.command.properties.AbstractCommandProperties; import youhong.ai.mymapper.command.properties.MyForProperties; import youhong.ai.mymapper.util.ParamValueUtil; @@ -35,7 +36,8 @@ public class MyForCommand implements ISqlCommand { } @Override - public String execute(AbstractCommandProperties commandProperties) { + public String execute(SqlCommandDefinition sqlCommandDefinition) { + AbstractCommandProperties commandProperties = sqlCommandDefinition.getProperties(); Map params = ParseSqlUtil.PARSE_PARAM_LOCALE.get(); MyForProperties properties = (MyForProperties) commandProperties; String collection = properties.getCollection(); diff --git a/src/test/java/youhong/ai/mymapper/command/commandImpl/MySetCommand.java b/src/test/java/youhong/ai/mymapper/command/commandImpl/MySetCommand.java new file mode 100644 index 0000000..d748563 --- /dev/null +++ b/src/test/java/youhong/ai/mymapper/command/commandImpl/MySetCommand.java @@ -0,0 +1,38 @@ +package youhong.ai.mymapper.command.commandImpl; + +import org.apache.commons.lang.StringUtils; +import youhong.ai.mymapper.command.ISqlCommand; +import youhong.ai.mymapper.command.annotation.SqlCommand; +import youhong.ai.mymapper.command.constant.CommandConsTant; +import youhong.ai.mymapper.command.entity.SqlCommandDefinition; +import youhong.ai.mymapper.util.ParseSqlUtil; + +/** + *

set指令

+ * + *

create: 2023/3/3 17:43

+ * + * @author youHong.ai + */ +@SqlCommand(CommandConsTant.SET) +public class MySetCommand implements ISqlCommand { + @Override + public String execute(SqlCommandDefinition sqlCommandDefinition) { + ParseSqlUtil parseSqlUtil = new ParseSqlUtil(); + String commandContent = sqlCommandDefinition.getCommandContent(); + String parse = parseSqlUtil.parse(commandContent); + String sql = parseSqlUtil.parseStatement(parse); + if (StringUtils.isNotBlank(sql)) { + String trim = sql.trim(); + if (trim.startsWith(",")) { + trim = trim.substring(1); + } + if (trim.endsWith(",")) { + trim = trim.substring(0, trim.length() - 1); + } + sql = " set " + trim; + return sql; + } + return null; + } +} diff --git a/src/test/java/youhong/ai/mymapper/command/commandImpl/MyWhereCommand.java b/src/test/java/youhong/ai/mymapper/command/commandImpl/MyWhereCommand.java index c78e328..b0cac46 100644 --- a/src/test/java/youhong/ai/mymapper/command/commandImpl/MyWhereCommand.java +++ b/src/test/java/youhong/ai/mymapper/command/commandImpl/MyWhereCommand.java @@ -1,9 +1,11 @@ package youhong.ai.mymapper.command.commandImpl; +import org.apache.commons.lang.StringUtils; import youhong.ai.mymapper.command.ISqlCommand; import youhong.ai.mymapper.command.annotation.SqlCommand; import youhong.ai.mymapper.command.constant.CommandConsTant; -import youhong.ai.mymapper.command.properties.AbstractCommandProperties; +import youhong.ai.mymapper.command.entity.SqlCommandDefinition; +import youhong.ai.mymapper.util.ParseSqlUtil; /** *

where指令

@@ -16,9 +18,23 @@ import youhong.ai.mymapper.command.properties.AbstractCommandProperties; @SqlCommand(CommandConsTant.WHERE) public class MyWhereCommand implements ISqlCommand { - @Override - public String execute(AbstractCommandProperties commandProperties) { + public String execute(SqlCommandDefinition sqlCommandDefinition) { + ParseSqlUtil parseSqlUtil = new ParseSqlUtil(); + String commandContent = sqlCommandDefinition.getCommandContent(); + String parse = parseSqlUtil.parse(commandContent); + String sql = parseSqlUtil.parseStatement(parse); + if (StringUtils.isNotBlank(sql)) { + String trim = sql.trim(); + if (trim.toUpperCase().startsWith("AND ")) { + trim = trim.substring(3); + } + if (trim.toUpperCase().startsWith("OR ")) { + trim = trim.substring(2); + } + sql = " where " + trim; + return sql; + } return null; } } diff --git a/src/test/java/youhong/ai/mymapper/command/constant/CommandConsTant.java b/src/test/java/youhong/ai/mymapper/command/constant/CommandConsTant.java index 2dd6fff..4fcd63b 100644 --- a/src/test/java/youhong/ai/mymapper/command/constant/CommandConsTant.java +++ b/src/test/java/youhong/ai/mymapper/command/constant/CommandConsTant.java @@ -36,4 +36,6 @@ public class CommandConsTant { public static final char[] COMMAND_PRE_FIX = new char[]{'m', 'y', '-'}; + public static final String SET = "set"; + } diff --git a/src/test/java/youhong/ai/mymapper/command/properties/AbstractCommandProperties.java b/src/test/java/youhong/ai/mymapper/command/properties/AbstractCommandProperties.java index c21a806..aae28a4 100644 --- a/src/test/java/youhong/ai/mymapper/command/properties/AbstractCommandProperties.java +++ b/src/test/java/youhong/ai/mymapper/command/properties/AbstractCommandProperties.java @@ -16,4 +16,6 @@ public abstract class AbstractCommandProperties { private String commandContent; private String commandType; + + private String parseResult; } diff --git a/src/test/java/youhong/ai/mymapper/util/ParseSqlUtil.java b/src/test/java/youhong/ai/mymapper/util/ParseSqlUtil.java index 49f0667..b563bdd 100644 --- a/src/test/java/youhong/ai/mymapper/util/ParseSqlUtil.java +++ b/src/test/java/youhong/ai/mymapper/util/ParseSqlUtil.java @@ -106,8 +106,6 @@ public class ParseSqlUtil { sqlBuilder.append(chars[i + 1]); i += 1; } - } else { - sqlBuilder.append(c); } } if (c == '\'' && i != chars.length - 1) { @@ -165,10 +163,12 @@ public class ParseSqlUtil { continue; } if (c == '\\') { - if (CommandConsTant.TRANSLATION_CHAR.contains(chars[i + 1])) { - commandSb.append(c).append(chars[i + 1]); - i += 1; - continue; + if (i + 1 <= chars.length - 1) { + if (CommandConsTant.TRANSLATION_CHAR.contains(chars[i + 1])) { + commandSb.append(c).append(chars[i + 1]); + i += 1; + continue; + } } } if ('{' == c) { @@ -197,10 +197,12 @@ public class ParseSqlUtil { continue; } if (c == '\\') { - if (CommandConsTant.TRANSLATION_CHAR.contains(chars[i + 1])) { - commandContent.append(c).append(chars[i + 1]); - i += 1; - continue; + if (i + 1 <= chars.length - 1) { + if (CommandConsTant.TRANSLATION_CHAR.contains(chars[i + 1])) { + commandContent.append(c).append(chars[i + 1]); + i += 1; + continue; + } } } commandContent.append(c); @@ -297,7 +299,7 @@ public class ParseSqlUtil { StringBuilder sb = new StringBuilder(); boolean isVariable = false; for (int i = startIndex; i < chars.length; i++) { - char c = chars[i]; + char c; if (!isVariable) { for (char value : prefix) { c = chars[i];