All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.arextest.diff.handler.parse.sqlparse.action.DeleteParse Maven / Gradle / Ivy

There is a newer version: 0.2.15
Show newest version
package com.arextest.diff.handler.parse.sqlparse.action;

import com.arextest.diff.handler.parse.sqlparse.Parse;
import com.arextest.diff.handler.parse.sqlparse.constants.DbParseConstants;
import com.arextest.diff.handler.parse.sqlparse.select.ArexExpressionVisitorAdapter;
import com.arextest.diff.handler.parse.sqlparse.select.ArexOrderByVisitorAdapter;
import com.arextest.diff.handler.parse.sqlparse.select.utils.JoinParseUtil;
import com.arextest.diff.utils.JacksonHelperUtil;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.List;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.delete.Delete;
import net.sf.jsqlparser.statement.select.Join;
import net.sf.jsqlparser.statement.select.Limit;
import net.sf.jsqlparser.statement.select.OrderByElement;

/**
 * Created by rchen9 on 2023/1/6. the example of parsed delete sql: { "action": "DELETE", "table":
 * "Exam", "where": { "andor": [ "and", "and" ], "columns": { "a.rnk <= 3": "", "a.per_id in (select
 * per_id from colle_subject)": "" } } }
 */
public class DeleteParse implements Parse {

  @Override
  public ObjectNode parse(Delete parseObj) {
    ObjectNode sqlObject = JacksonHelperUtil.getObjectNode();
    sqlObject.put(DbParseConstants.ACTION, DbParseConstants.DELETE);

    // tables parse
    List tables = parseObj.getTables();
    if (tables != null && !tables.isEmpty()) {
      ObjectNode delTableObj = JacksonHelperUtil.getObjectNode();
      tables.forEach(item -> {
        delTableObj.put(item.getFullyQualifiedName(), DbParseConstants.EMPTY);
      });
      sqlObject.set(DbParseConstants.DEL_TABLES, delTableObj);
    }

    // table parse
    Table table = parseObj.getTable();
    if (table != null) {
      sqlObject.put(DbParseConstants.TABLE, table.getFullyQualifiedName());
    }

    // join parse
    List joins = parseObj.getJoins();
    if (joins != null && !joins.isEmpty()) {
      ArrayNode joinArr = JacksonHelperUtil.getArrayNode();
      joins.forEach(item -> {
        joinArr.add(JoinParseUtil.parse(item));
      });
      sqlObject.set(DbParseConstants.JOIN, joinArr);
    }

    // where parse
    Expression where = parseObj.getWhere();
    if (where != null) {
      ObjectNode whereObj = JacksonHelperUtil.getObjectNode();
      whereObj.set(DbParseConstants.AND_OR, JacksonHelperUtil.getArrayNode());
      whereObj.set(DbParseConstants.COLUMNS, JacksonHelperUtil.getObjectNode());

      where.accept(new ArexExpressionVisitorAdapter(whereObj));
      sqlObject.set(DbParseConstants.WHERE, whereObj);
    }

    // orderby parse
    List orderByElements = parseObj.getOrderByElements();
    if (orderByElements != null && !orderByElements.isEmpty()) {
      ObjectNode orderByObj = JacksonHelperUtil.getObjectNode();
      ArexOrderByVisitorAdapter arexOrderByVisitorAdapter = new ArexOrderByVisitorAdapter(
          orderByObj);
      orderByElements.forEach(item -> {
        item.accept(arexOrderByVisitorAdapter);
      });
      sqlObject.set(DbParseConstants.ORDER_BY, orderByObj);
    }

    // limit parse
    Limit limit = parseObj.getLimit();
    if (limit != null) {
      sqlObject.put(DbParseConstants.LIMIT, limit.toString());
    }
    return sqlObject;
  }
}