The best place to *find* answers to programming/development questions, imo, however it's the *worst* place to *ask* questions (if your first question/comment doesn't get any up-rating/response, then u can't ask anymore questions--ridiculously unrealistic), but again, a great reference for *finding* answers.

My Music (Nickleus)

20130317

sahi - how to parse the column(s) name(s) in an sql search

in our company's application we have a page in our admin backend where we can run sql queries. we display the results in a table, so i made a script that runs an sql query then checks to see if at least one of the select parameters is visible. here's the code:

// vars
//var $sql_query = "select count(*) from membershipapplications";    // 1 column

var $sql_query = "select id, partyname from membershipapplications where id=1";    // multiple columns

do_admin_sql_search($sql_query);

///////////////FUNCTION CODE/////////////
 

function do_admin_sql_search($sql_query) {
    ...
    var $regex = /(.*select\s+)(.*)(\s+from.*)/;
    var $columns = $sql_query.replace($regex, "$2");
    var $untrimmed_column1 = $columns.substring(0, $columns.indexOf(","));
    var $column1;
    if($untrimmed_column1.length > 0) {
        $column1 = $untrimmed_column1.replace(/^\s+|\s+$/g,"");    // .trim() doesn't work in sahi apparently
    } else {
        $column1 = $columns;
    }
   
    // assert that the query gave some kind of result
    _assert(_isVisible(_tableHeader($column1.toUpperCase())));
}

No comments:

Post a Comment