我想将插入sql语句转换为使用JSQLParser删除sql语句,如何实现? 例如,这里是示例插入sql
INSERT INTO emp (empno, ename, job, sal, comm, deptno, joinedon) VALUES (4160, 'STURDEVIN', 'SECURITY GUARD', 2045, NULL, 30, TO_TIMESTAMP('2014-07-02 06:14:00.742000000', 'YYYY-MM-DD HH24:MI:SS.FF'));
将在转换后删除sql
DELETE FROM emp WHERE empno=4160 AND ename='STURDEVIN' AND job='SECURITY GUARD' AND sal=2045 AND comm=NULL AND deptno=30 AND joinedon=TO_TIMESTAMP('2014-07-02 06:14:00.742000000', 'YYYY-MM-DD HH24:MI:SS.FF');
到目前为止,这里是代码
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.operators.relational.ItemsList;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.delete.Delete;
import net.sf.jsqlparser.statement.insert.Insert;
import java.util.List;
public class Main {
public static void main(String[] args) {
String insertSQL = "INSERT INTO emp (empno, ename, job, sal, comm, deptno, joinedon) VALUES (4160, 'STURDEVIN', 'SECURITY GUARD', 2045, NULL, 30, TO_TIMESTAMP('2014-07-02 06:14:00.742000000', 'YYYY-MM-DD HH24:MI:SS.FF'));";
final Statement statement;
try {
statement = CCJSqlParserUtil.parse(insertSQL);
if (statement instanceof Insert) {
final Insert insertStatement = (Insert) statement;
System.out.println("insertStatement: " + insertStatement);
List<Column> columns = insertStatement.getColumns();
for (final Column column : columns) {
System.out.println("column: " + column);
}
ItemsList itemsList = insertStatement.getItemsList();
System.out.println("itemsList: " + itemsList);
final Delete deleteStatement = new Delete();
deleteStatement.setTable(insertStatement.getTable());
System.out.println("deleteStatement: " + deleteStatement);
}
} catch (JSQLParserException e) {
e.printStackTrace();
}
}
}
我正在使用maven构建此项目,这是pom依赖项。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>oracle.insert.delete.jsqlparser</groupId>
<artifactId>oracle_from_insert_to_delete_using_JSqlParser</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</project>
遗留代码的一部分,这些insert语句用于设置测试数据,我想在测试完成后自动清理测试数据。而且不依赖于拆解中写得不好的删除语句。
答案 0 :(得分:1)
这是一种实现方法(添加的代码从“ // get the list of values
开始”):
public class Main {
public static void main(String[] args) {
String insertSQL = "INSERT INTO emp (empno, ename, job, sal, comm, deptno, joinedon) VALUES (4160, 'STURDEVIN', 'SECURITY GUARD', 2045, NULL, 30, TO_TIMESTAMP('2014-07-02 06:14:00.742000000', 'YYYY-MM-DD HH24:MI:SS.FF'));";
final Statement statement;
try {
statement = CCJSqlParserUtil.parse(insertSQL);
if (statement instanceof Insert) {
final Insert insertStatement = (Insert) statement;
System.out.println("insertStatement: " + insertStatement);
List<Column> columns = insertStatement.getColumns();
for (final Column column : columns) {
System.out.println("column: " + column);
}
ItemsList itemsList = insertStatement.getItemsList();
System.out.println("itemsList: " + itemsList);
final Delete deleteStatement = new Delete();
deleteStatement.setTable(insertStatement.getTable());
// get the list of values
ExpressionList expressionList = (ExpressionList) itemsList;
List<Expression> values = expressionList.getExpressions();
// create the "column = value" expressions list
List<Expression> expressions = new ArrayList<>();
for (int i = 0; i < columns.size(); i++) {
// create the "column = value" expression
EqualsTo equalsTo = new EqualsTo();
equalsTo.setLeftExpression(columns.get(i));
equalsTo.setRightExpression(values.get(i));
// add it to the list
expressions.add(equalsTo);
}
// glue together the expressions with "ANDs"
// it is now our where expression
MultiAndExpression whereExpression = new MultiAndExpression(expressions);
deleteStatement.setWhere(whereExpression);
System.out.println("deleteStatement: " + deleteStatement);
}
} catch (JSQLParserException e) {
e.printStackTrace();
}
}
}
它生成以下删除语句:
DELETE FROM emp WHERE (empno = 4160 AND ename = 'STURDEVIN' AND job = 'SECURITY GUARD' AND sal = 2045 AND comm = NULL AND deptno = 30 AND joinedon = TO_TIMESTAMP('2014-07-02 06:14:00.742000000', 'YYYY-MM-DD HH24:MI:SS.FF'))