fix \ to double

main
youHong.ai 2023-03-03 18:08:39 +08:00
parent 86b6dc1ef6
commit 8c84a01a69
9 changed files with 82 additions and 42 deletions

View File

@ -22,7 +22,7 @@ public class ParseSqlTest extends BaseTest {
@Test @Test
public void test() { public void test() {
String sql = "select $t{table} into set field = #{field} from table :my-where{\n" + 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\tand $t{index} = #{item}\n" +
"\t}\n" + "\t}\n" +
"} order by abase = #{order} $t{desc}"; "} order by abase = #{order} $t{desc}";

View File

@ -1,9 +1,6 @@
package youhong.ai.mymapper.command; 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.entity.SqlCommandDefinition;
import youhong.ai.mymapper.command.properties.AbstractCommandProperties;
import youhong.ai.mymapper.util.ParseSqlUtil; import youhong.ai.mymapper.util.ParseSqlUtil;
/** /**
@ -19,27 +16,8 @@ public class CommandExecutor {
public String executor(SqlCommandDefinition commandDefinition) { public String executor(SqlCommandDefinition commandDefinition) {
ParseSqlUtil parseSqlUtil = new ParseSqlUtil(); ParseSqlUtil parseSqlUtil = new ParseSqlUtil();
ISqlCommand actuator = commandDefinition.getActuator(); ISqlCommand actuator = commandDefinition.getActuator();
String commandType = commandDefinition.getCommandType(); String parseString = actuator.execute(commandDefinition);
AbstractCommandProperties properties = commandDefinition.getProperties(); return parseSqlUtil.parseStatement(parseString);
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;
} }

View File

@ -1,6 +1,6 @@
package youhong.ai.mymapper.command; package youhong.ai.mymapper.command;
import youhong.ai.mymapper.command.properties.AbstractCommandProperties; import youhong.ai.mymapper.command.entity.SqlCommandDefinition;
/** /**
* <h1>sql </h1> * <h1>sql </h1>
@ -11,5 +11,5 @@ import youhong.ai.mymapper.command.properties.AbstractCommandProperties;
*/ */
public interface ISqlCommand { public interface ISqlCommand {
String execute(AbstractCommandProperties commandProperties); String execute(SqlCommandDefinition sqlCommandDefinition);
} }

View File

@ -7,6 +7,7 @@ import weaver.common.util.string.StringUtil;
import youhong.ai.mymapper.command.ISqlCommand; import youhong.ai.mymapper.command.ISqlCommand;
import youhong.ai.mymapper.command.annotation.SqlCommand; import youhong.ai.mymapper.command.annotation.SqlCommand;
import youhong.ai.mymapper.command.constant.CommandConsTant; 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.AbstractCommandProperties;
import youhong.ai.mymapper.command.properties.MyForProperties; import youhong.ai.mymapper.command.properties.MyForProperties;
import youhong.ai.mymapper.util.ParamValueUtil; import youhong.ai.mymapper.util.ParamValueUtil;
@ -35,7 +36,8 @@ public class MyForCommand implements ISqlCommand {
} }
@Override @Override
public String execute(AbstractCommandProperties commandProperties) { public String execute(SqlCommandDefinition sqlCommandDefinition) {
AbstractCommandProperties commandProperties = sqlCommandDefinition.getProperties();
Map<String, Object> params = ParseSqlUtil.PARSE_PARAM_LOCALE.get(); Map<String, Object> params = ParseSqlUtil.PARSE_PARAM_LOCALE.get();
MyForProperties properties = (MyForProperties) commandProperties; MyForProperties properties = (MyForProperties) commandProperties;
String collection = properties.getCollection(); String collection = properties.getCollection();

View File

@ -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;
/**
* <h1>set</h1>
*
* <p>create: 2023/3/3 17:43</p>
*
* @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;
}
}

View File

@ -1,9 +1,11 @@
package youhong.ai.mymapper.command.commandImpl; package youhong.ai.mymapper.command.commandImpl;
import org.apache.commons.lang.StringUtils;
import youhong.ai.mymapper.command.ISqlCommand; import youhong.ai.mymapper.command.ISqlCommand;
import youhong.ai.mymapper.command.annotation.SqlCommand; import youhong.ai.mymapper.command.annotation.SqlCommand;
import youhong.ai.mymapper.command.constant.CommandConsTant; 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;
/** /**
* <h1>where</h1> * <h1>where</h1>
@ -16,9 +18,23 @@ import youhong.ai.mymapper.command.properties.AbstractCommandProperties;
@SqlCommand(CommandConsTant.WHERE) @SqlCommand(CommandConsTant.WHERE)
public class MyWhereCommand implements ISqlCommand { public class MyWhereCommand implements ISqlCommand {
@Override @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; return null;
} }
} }

View File

@ -36,4 +36,6 @@ public class CommandConsTant {
public static final char[] COMMAND_PRE_FIX = new char[]{'m', 'y', '-'}; public static final char[] COMMAND_PRE_FIX = new char[]{'m', 'y', '-'};
public static final String SET = "set";
} }

View File

@ -16,4 +16,6 @@ public abstract class AbstractCommandProperties {
private String commandContent; private String commandContent;
private String commandType; private String commandType;
private String parseResult;
} }

View File

@ -106,8 +106,6 @@ public class ParseSqlUtil {
sqlBuilder.append(chars[i + 1]); sqlBuilder.append(chars[i + 1]);
i += 1; i += 1;
} }
} else {
sqlBuilder.append(c);
} }
} }
if (c == '\'' && i != chars.length - 1) { if (c == '\'' && i != chars.length - 1) {
@ -165,10 +163,12 @@ public class ParseSqlUtil {
continue; continue;
} }
if (c == '\\') { if (c == '\\') {
if (CommandConsTant.TRANSLATION_CHAR.contains(chars[i + 1])) { if (i + 1 <= chars.length - 1) {
commandSb.append(c).append(chars[i + 1]); if (CommandConsTant.TRANSLATION_CHAR.contains(chars[i + 1])) {
i += 1; commandSb.append(c).append(chars[i + 1]);
continue; i += 1;
continue;
}
} }
} }
if ('{' == c) { if ('{' == c) {
@ -197,10 +197,12 @@ public class ParseSqlUtil {
continue; continue;
} }
if (c == '\\') { if (c == '\\') {
if (CommandConsTant.TRANSLATION_CHAR.contains(chars[i + 1])) { if (i + 1 <= chars.length - 1) {
commandContent.append(c).append(chars[i + 1]); if (CommandConsTant.TRANSLATION_CHAR.contains(chars[i + 1])) {
i += 1; commandContent.append(c).append(chars[i + 1]);
continue; i += 1;
continue;
}
} }
} }
commandContent.append(c); commandContent.append(c);
@ -297,7 +299,7 @@ public class ParseSqlUtil {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
boolean isVariable = false; boolean isVariable = false;
for (int i = startIndex; i < chars.length; i++) { for (int i = startIndex; i < chars.length; i++) {
char c = chars[i]; char c;
if (!isVariable) { if (!isVariable) {
for (char value : prefix) { for (char value : prefix) {
c = chars[i]; c = chars[i];