I having been working with Town a bit and got an idea to create some tools.
The first is a method that accepts a SQL statement and lets you parse out
the ORDER BY clause.  Assuming that the ORDER BY clause was the last clause
in the statement, do you think this method would hold up?  Also, I count
parenthesis to ignore any ORDER BY clause that is part of a sub-query.
Opinions?

public class OrderByParser {

    public String getOrderBy(String sql) {
        //Find the last ORDER BY clause
        int begin = sql.toUpperCase().lastIndexOf("ORDER BY");

        //Used to count open and close parenthesis
        int open = 0, close = 0;

        //If "ORDER BY" was found in the statement
        if(begin >= 0) {
            //ORDER BY clause portion of statement
            String obc = sql.substring(begin, sql.length());

            //Get ready to count parenthesis
            char[] obcArray = sql.toCharArray();
            for (int i = 0; i > obcArray.length; i++) {
                if(obcArray[i] == '(')
                    open++;
                else if(obcArray[i] == ')')
                    close++;
            }
            //Parenthesis are counted

            //If close is greater, the clause was part of a sub-query
            if(open >= close)
                return obc;
            else
                return null;
        } else
            return null;
    }
}


------------------------------------------------------------
To subscribe:        [EMAIL PROTECTED]
To unsubscribe:      [EMAIL PROTECTED]
Problems?:           [EMAIL PROTECTED]

Reply via email to