package cc.smtweb.framework.core.mvc.service;

//一个sql及其参数
public class SqlPara {
    public String sql;
    public Object[] paras;
    public int rows;
    public int page;

    public SqlPara() {
    }

    public SqlPara(String sql, Object... paras) {
        this.sql = sql;
        this.paras = paras;
    }

    //获取不带order by 的sql语句
    public String getSqlNoOrderBy() {
        String sSql = sql.toUpperCase();
        int n = sSql.lastIndexOf("ORDER BY ");//截取最后一个 order by
        if (n < 0) return sql;
        String retSql = sql.substring(0, n);
        String s = sSql.substring(n);
        if (s.contains("SELECT")) return sql; //最后一个order by之后，有子查询的不算
        if (!s.contains(")")) return retSql; //校验order by之后带括号的情况，一般是表达式
        n = 0;
        for (int i = 0, len = s.length(); i < len; i++) {
            final char c = s.charAt(i);
            if (c == '(') n++;
            else if (c == ')') n--;
            if (n < 0) return sql;
        }
        return retSql;
    }
}
