stmt = CCJSqlParserUtil.parse(sql); } catch (Throwable e) { throw new PageException("不支持该SQL转换为分页查询!", e);
/** * Parse an conditional expression. This is the expression after a where clause. * * @param condExpr * @return * @throws JSQLParserException */ public static Expression parseCondExpression(String condExpr) throws JSQLParserException { return parseCondExpression(condExpr, true); }
/** * Parse an expression. * * @param expression * @return * @throws JSQLParserException */ public static Expression parseExpression(String expression) throws JSQLParserException { return parseExpression(expression, true); }
@Test void parser() throws Exception { Select select = (Select) CCJSqlParserUtil.parse("SELECT a,b,c FROM tableName t WHERE t.col = 9 and b=c LIMIT 3, ?"); PlainSelect ps = (PlainSelect) select.getSelectBody(); System.out.println(ps.getWhere().toString()); System.out.println(ps.getSelectItems().get(1).toString()); AndExpression e = (AndExpression) ps.getWhere(); System.out.println(e.getLeftExpression()); }
/** * Parse an conditional expression. This is the expression after a where clause. * * @param condExpr * @return * @throws JSQLParserException */ public static Expression parseCondExpression(String condExpr) throws JSQLParserException { return parseCondExpression(condExpr, true); }
/** * Builds select expr1, expr2 from table. * * @param table * @param expr * @return * @throws net.sf.jsqlparser.JSQLParserException */ public static Select buildSelectFromTableAndExpressions(Table table, String... expr) throws JSQLParserException { SelectItem[] list = new SelectItem[expr.length]; for (int i = 0; i < expr.length; i++) { list[i] = new SelectExpressionItem(CCJSqlParserUtil.parseExpression(expr[i])); } return buildSelectFromTableAndSelectItems(table, list); }
/** * 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 PageException("原SQL[" + sql + "]中的order by包含参数,因此不能使用OrderBy插件进行修改!"); } //新的sql sql = select.toString(); } catch (Throwable e) { throw new PageException("处理排序失败: " + e, e); } return sql + " order by " + orderBy; }
String on = joinField.getOn(); try { Expression onCond = CCJSqlParserUtil.parseCondExpression(on); onCond.accept(new ExpressionVisitorAdapter() { @Override
/** * Parse an expression. * * @param expression * @return * @throws JSQLParserException */ public static Expression parseExpression(String expression) throws JSQLParserException { return parseExpression(expression, true); }
@Override public String onBeforeAnyExecute(StatementInformation statementInformation) throws SQLException { String sql = statementInformation.getSqlWithValues(); // 当前业务链接 DTXLocal.cur().setResource(statementInformation.getStatement().getConnection()); // 拦截处理 try { long startTime = System.currentTimeMillis(); Statement statement = CCJSqlParserUtil.parse(sql); log.debug("statement > {}", statement); statementInformation.setAttachment(statement); if (statement instanceof Update) { sqlExecuteInterceptor.preUpdate((Update) statement); } else if (statement instanceof Delete) { sqlExecuteInterceptor.preDelete((Delete) statement); } else if (statement instanceof Insert) { sqlExecuteInterceptor.preInsert((Insert) statement); } else if (statement instanceof Select) { sqlExecuteInterceptor.preSelect(new LockableSelect((Select) statement)); } log.debug("used time: {} ms", System.currentTimeMillis() - startTime); } catch (JSQLParserException e) { throw new SQLException(e); } return sql; }
String conditionSql = StringUtils.trimToNull(conditionSupplier.apply(tables.values())); if (conditionSql != null) { Expression condition = CCJSqlParserUtil.parseCondExpression(conditionSql); Expression where = plainSelect.getWhere(); if (where == null) {
/** * Builds select expr1, expr2 from table. * * @param table * @param expr * @return * @throws net.sf.jsqlparser.JSQLParserException */ public static Select buildSelectFromTableAndExpressions(Table table, String... expr) throws JSQLParserException { SelectItem[] list = new SelectItem[expr.length]; for (int i = 0; i < expr.length; i++) { list[i] = new SelectExpressionItem(CCJSqlParserUtil.parseExpression(expr[i])); } return buildSelectFromTableAndSelectItems(table, list); }
@Test void updateWhereParser() throws Exception { Update update = (Update) CCJSqlParserUtil.parse("Update tableName t SET t.a=(select c from tn where tn.id=t.id),b=2,c=3 "); Assertions.assertNull(update.getWhere()); }
@Test void deleteWhereParser() throws Exception { Delete delete = (Delete) CCJSqlParserUtil.parse("delete from tableName t"); Assertions.assertNull(delete.getWhere()); } }
stmt = CCJSqlParserUtil.parse(sql); } catch (Throwable e) {
Select selectStatement = (Select) CCJSqlParserUtil.parse(sql); PlainSelect plainSelect = (PlainSelect) selectStatement.getSelectBody(); Distinct distinct = plainSelect.getDistinct();
Statement statement = CCJSqlParserUtil.parse(originalSql); Expression where = null; Table table = null;
private boolean isSelectQuery(String query) { try{ net.sf.jsqlparser.statement.Statement parsedStmt = CCJSqlParserUtil.parse(query); return parsedStmt instanceof Select; } catch(JSQLParserException e){ } return false; }
public static String name_delete_where(String sql) throws JSQLParserException { net.sf.jsqlparser.statement.Statement statement = CCJSqlParserUtil.parse(sql); Delete updateStatement = (Delete) statement; Expression where_expression = updateStatement.getWhere(); if (where_expression == null) return ""; String str = where_expression.toString(); return str; }
private net.sf.jsqlparser.statement.Statement parseStatement(String query) throws StatementExecutionException { net.sf.jsqlparser.statement.Statement stmt; try { stmt = CCJSqlParserUtil.parse(query); } catch (JSQLParserException | net.sf.jsqlparser.parser.TokenMgrError err) { throw new StatementExecutionException("unable to parse query " + query, err); } return stmt; }