fix \ to double
parent
86b6dc1ef6
commit
8c84a01a69
|
@ -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}";
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package youhong.ai.mymapper.command;
|
||||
|
||||
import youhong.ai.mymapper.command.properties.AbstractCommandProperties;
|
||||
import youhong.ai.mymapper.command.entity.SqlCommandDefinition;
|
||||
|
||||
/**
|
||||
* <h1>sql 指令解析接口</h1>
|
||||
|
@ -11,5 +11,5 @@ import youhong.ai.mymapper.command.properties.AbstractCommandProperties;
|
|||
*/
|
||||
public interface ISqlCommand {
|
||||
|
||||
String execute(AbstractCommandProperties commandProperties);
|
||||
String execute(SqlCommandDefinition sqlCommandDefinition);
|
||||
}
|
||||
|
|
|
@ -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<String, Object> params = ParseSqlUtil.PARSE_PARAM_LOCALE.get();
|
||||
MyForProperties properties = (MyForProperties) commandProperties;
|
||||
String collection = properties.getCollection();
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
||||
/**
|
||||
* <h1>where指令</h1>
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,4 +36,6 @@ public class CommandConsTant {
|
|||
|
||||
public static final char[] COMMAND_PRE_FIX = new char[]{'m', 'y', '-'};
|
||||
|
||||
public static final String SET = "set";
|
||||
|
||||
}
|
||||
|
|
|
@ -16,4 +16,6 @@ public abstract class AbstractCommandProperties {
|
|||
|
||||
private String commandContent;
|
||||
private String commandType;
|
||||
|
||||
private String parseResult;
|
||||
}
|
||||
|
|
|
@ -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,12 +163,14 @@ public class ParseSqlUtil {
|
|||
continue;
|
||||
}
|
||||
if (c == '\\') {
|
||||
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) {
|
||||
commandStart = i;
|
||||
isCommand = true;
|
||||
|
@ -197,12 +197,14 @@ public class ParseSqlUtil {
|
|||
continue;
|
||||
}
|
||||
if (c == '\\') {
|
||||
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);
|
||||
if ('{' == c) {
|
||||
commandSyntaxCount++;
|
||||
|
@ -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];
|
||||
|
|
Loading…
Reference in New Issue