/**
  * Create the compatible Query object based on the SQL query.
  *
  * @param sql The SQL query.
  * @return Query object.
  */
 public static QueryAction create(Client client, String sql)
     throws SqlParseException, SQLFeatureNotSupportedException {
   sql = sql.replaceAll("\n", " ");
   String firstWord = sql.substring(0, sql.indexOf(' '));
   switch (firstWord.toUpperCase()) {
     case "SELECT":
       SQLQueryExpr sqlExpr = (SQLQueryExpr) toSqlExpr(sql);
       if (isMulti(sqlExpr)) {
         MultiQuerySelect multiSelect =
             new SqlParser().parseMultiSelect((SQLUnionQuery) sqlExpr.getSubQuery().getQuery());
         handleSubQueries(client, multiSelect.getFirstSelect());
         handleSubQueries(client, multiSelect.getSecondSelect());
         return new MultiQueryAction(client, multiSelect);
       } else if (isJoin(sqlExpr, sql)) {
         JoinSelect joinSelect = new SqlParser().parseJoinSelect(sqlExpr);
         handleSubQueries(client, joinSelect.getFirstTable());
         handleSubQueries(client, joinSelect.getSecondTable());
         return ESJoinQueryActionFactory.createJoinAction(client, joinSelect);
       } else {
         Select select = new SqlParser().parseSelect(sqlExpr);
         handleSubQueries(client, select);
         return handleSelect(client, select);
       }
     case "DELETE":
       SQLStatementParser parser = createSqlStatementParser(sql);
       SQLDeleteStatement deleteStatement = parser.parseDeleteStatement();
       Delete delete = new SqlParser().parseDelete(deleteStatement);
       return new DeleteQueryAction(client, delete);
     case "SHOW":
       return new ShowQueryAction(client, sql);
     default:
       throw new SQLFeatureNotSupportedException(String.format("Unsupported query: %s", sql));
   }
 }
예제 #2
0
  public static boolean visit(SQLEvalVisitor visitor, SQLQueryExpr x) {
    if (x.getSubQuery().getQuery() instanceof SQLSelectQueryBlock) {
      SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) x.getSubQuery().getQuery();

      boolean nullFrom = false;
      if (queryBlock.getFrom() == null) {
        nullFrom = true;
      } else if (queryBlock.getFrom() instanceof SQLExprTableSource) {
        SQLExpr expr = ((SQLExprTableSource) queryBlock.getFrom()).getExpr();
        if (expr instanceof SQLIdentifierExpr) {
          if ("dual".equalsIgnoreCase(((SQLIdentifierExpr) expr).getName())) {
            nullFrom = true;
          }
        }
      }

      if (nullFrom) {
        List<Object> row = new ArrayList<Object>(queryBlock.getSelectList().size());
        for (int i = 0; i < queryBlock.getSelectList().size(); ++i) {
          SQLSelectItem item = queryBlock.getSelectList().get(i);
          item.getExpr().accept(visitor);
          Object cell = item.getExpr().getAttribute(EVAL_VALUE);
          row.add(cell);
        }
        List<List<Object>> rows = new ArrayList<List<Object>>(1);
        rows.add(row);

        Object result = rows;
        queryBlock.putAttribute(EVAL_VALUE, result);
        x.getSubQuery().putAttribute(EVAL_VALUE, result);
        x.putAttribute(EVAL_VALUE, result);

        return false;
      }
    }

    return false;
  }
 private static boolean isMulti(SQLQueryExpr sqlExpr) {
   return sqlExpr.getSubQuery().getQuery() instanceof SQLUnionQuery;
 }
 private static boolean isJoin(SQLQueryExpr sqlExpr, String sql) {
   MySqlSelectQueryBlock query = (MySqlSelectQueryBlock) sqlExpr.getSubQuery().getQuery();
   return query.getFrom() instanceof SQLJoinTableSource && sql.toLowerCase().contains("join");
 }