这里写自定义目录标题
public static void main(String[] args) {
String sql = "select a.*,b.class_name " +
"from (select b.user_id, b.class_name from class_name b where b.id>1 order by b.id desc) a " +
"left join class_user b on b.user_id = a.id " +
"where a.id >1 " +
"order by a.id asc";
String sql1 = "select a.* from (select b.user_id, b.class_name from class_user b) a";
String sql2 = "SELECT id FROM user WHERE status = 1;\n" +
"SELECT id FROM order WHERE create_time > '2018-01-01'";
String dbType = JdbcConstants.MYSQL;
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, dbType);
SQLStatement statement = statementList.get(0);
boolean hasWhere = false;
boolean hasGroupBy = false;
boolean hasOrderBy = false;
SQLSelectStatement sqlSelectStatement = (SQLSelectStatement) statement;
SQLSelectQuery sqlSelectQuery = sqlSelectStatement.getSelect().getQuery();
if (sqlSelectQuery instanceof SQLSelectQueryBlock) {
SQLSelectQueryBlock sqlSelectQueryBlock = (SQLSelectQueryBlock) sqlSelectQuery;
SQLExpr where = sqlSelectQueryBlock.getWhere();
if (where != null) hasWhere = true;
SQLSelectGroupByClause groupBy = sqlSelectQueryBlock.getGroupBy();
if (groupBy != null) hasGroupBy = true;
SQLOrderBy orderBy = sqlSelectQueryBlock.getOrderBy();
if (orderBy != null) hasOrderBy = true;
}
String permissionSql = " where 1=1 /permission ********";
permissionSql = hasWhere ? permissionSql.substring(10) : permissionSql;
StringBuilder sb = new StringBuilder(sql);
int i = 0;
if (hasGroupBy) {
i = sql.lastIndexOf(" group by");
} else {
if (hasOrderBy) {
i = sql.lastIndexOf(" order by");
}
}
if (i > 0) {
sb.insert(i, permissionSql);
sql = sb.toString();
}
System.out.println(sql);
SQLDeleteStatement sqlDeleteStatement = (SQLDeleteStatement) statement;
SQLExpr where = sqlDeleteStatement.getWhere();
if (where != null) hasWhere = true;
SQLInsertStatement sqlInsertStatement = (SQLInsertStatement) statement;
where = sqlInsertStatement.
if (where != null) hasWhere = true;
}