예제 #1
0
 @Override
 public ExecuteSQLOnNodeResponse executeSQLOnNode(ExecuteSQLOnNodeRequest executeSQLOnNodeRequest)
     throws RemoteException {
   Long taskId = executeSQLOnNodeRequest.getTaskId() + delta;
   try {
     Statement stmt = CCJSqlParserUtil.parse(executeSQLOnNodeRequest.getSql());
     SQLStatementVisitor visitor = new SQLStatementVisitor(master, this, taskId);
     stmt.accept(visitor);
     logger.trace("Sending result of query: " + visitor.getResult().getResult());
     return visitor.getResult();
   } catch (JSQLParserException e) {
     logger.error("Sql parsing error: {} - {}", e.getMessage(), e.getStackTrace());
     return new ExecuteSQLOnNodeResponse("", ErrorEnum.SQL_PARSING_ERROR);
   }
 }
예제 #2
0
 private Delete parse(final String statement) {
   Delete delete;
   try {
     Statement stmt;
     stmt = CCJSqlParserUtil.parse(statement);
     checkArgument(stmt instanceof Delete, "Expected DELETE statement: %s", statement);
     delete = (Delete) stmt;
   } catch (JSQLParserException e) {
     Throwables.propagateIfInstanceOf(Throwables.getRootCause(e), IllegalArgumentException.class);
     throw new IllegalArgumentException("Unable to parse query: " + e.getMessage(), e);
   }
   checkArgument(null == delete.getLimit(), "LIMIT is not supported for DELETE statements");
   checkArgument(
       null == delete.getOrderByElements(), "ORDER BY is not supported for DELETE statements");
   return delete;
 }
 /**
  * convert to order by sql
  *
  * @param sql
  * @param orderBy
  * @return
  */
 public static String converToOrderBySql(String sql, String orderBy) {
   // 解析SQL
   Statement stmt = null;
   try {
     stmt = CCJSqlParserUtil.parse(sql);
     Select select = (Select) stmt;
     SelectBody selectBody = select.getSelectBody();
     // 处理body-去最外层order by
     List<OrderByElement> orderByElements = extraOrderBy(selectBody);
     String defaultOrderBy = PlainSelect.orderByToString(orderByElements);
     if (defaultOrderBy.indexOf('?') != -1) {
       throw new RuntimeException("原SQL[" + sql + "]中的order by包含参数,因此不能使用OrderBy插件进行修改!");
     }
     // 新的sql
     sql = select.toString();
   } catch (Throwable e) {
     e.printStackTrace();
   }
   return sql + " order by " + orderBy;
 }
예제 #4
0
 /**
  * 静态调用接口,获取所有表的ID
  *
  * @param sql 传入的sql语句
  * @return 返回-1表示当前语句不是select语句,返回0表示当前SQL有问题,返回>0的数字表示查找出的表数目
  */
 public static int getOriginalTableName(String sql) {
   int num_Table = 0;
   Statement statement = null;
   try {
     statement = CCJSqlParserUtil.parse(sql);
     if (statement instanceof Select) {
       Select selectStatement = (Select) statement;
       System.err.println(sql);
       PlainSelect plainSelect = null;
       plainSelect = (PlainSelect) selectStatement.getSelectBody();
       if (plainSelect != null) {
         System.out.println("\n-------------------------------------------");
         if (plainSelect.getFromItem() != null) {
           plainSelect.getFromItem().toString();
           num_Table++;
         }
         System.out.print(plainSelect.getFromItem().toString() + "\t");
         if (plainSelect.getJoins() != null) {
           for (Join join2 : plainSelect.getJoins()) {
             System.out.print(join2.toString() + "\t");
             num_Table++;
           }
         }
         System.out.println("\n-------------------------------------------");
       } else {
         num_Table = -1;
         return num_Table;
       }
     }
   } catch (JSQLParserException ex) {
     ex.printStackTrace();
     num_Table = 0;
     LogWriter.println(num_Table);
     return num_Table;
   }
   LogWriter.println("共统计出表个数=>" + num_Table);
   return num_Table;
 }