Skip to main content

Sql queries result table column selecting javascript function

I was looking for a script which do the above. I couldn't find any. So I wrote a one. The following function takes a sql query string such as
"SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons INNER JOIN Orders ON Persons.P_Id=Orders.P_Id ORDER BY Persons.LastName SELECT AVG(OrderPrice) AS OrderAverage FROM Orders" and returns a string array containing LastName,FirstName,OrderNo.


function brakeColumns(query) {
var columnList = new Array();
var patten1 = /^[\w\$][\w]*/i;
var patten2 = /^[\w\$][\w]*\.[\w\$][\w]*$/i;
var numEnd = query.search(/\sfrom\s/i);
var elems = (query.substring(6, numEnd)).split(",");
for (var i = 0; i < elems.length; i++) {
var elem = elems[i].toString();
elem = trim(elem);
var stPoint = "";
var wanted = "";
if (elem.search(/\sas\s/i) != -1) {
stPoint = elem.search(/\sas\s/i);
wanted = elem.substring(stPoint + 4);
wanted = trim(wanted);
if (patten1.test(wanted)) {
columnList.push(wanted);
}
} else if (patten2.test(elem)) { //check for tablename.column name pattern
stPoint = elem.search(/\./);
wanted = elem.substring(stPoint + 1);
wanted = trim(wanted);
if (patten1.test(wanted)) {
columnList.push(wanted);
}
} else { //check for straight column name like username,passsword etc
if (patten1.test(elem)) {
columnList.push(elem);
}
}
}
return(columnList);
}


function trim(stringToTrim) {
return stringToTrim.replace(/^\s+|\s+$/g, "");
}

Comments

Popular posts from this blog

Using javascript to Include a html file inside another html file

When there is no server side functionality needed we create the whole site in plain html. Usually these sites have left/right side menu, top header, footer etc.. If the site grows in to 20, 30 pages, it will be a headache to do a simple change like changing footer text. We will have to change each page. If we were using a server side technology like PHP, JSP, etc.., we will have the chance to keep the common areas in the site in different pages and include these parts in each page using a “include” statements. We can do the same thing with the plain old html and javascript. But how? First you need to create the site main layout using divs and give them unique ids. <html> <title>HTML Includes</title> <script language="javascript" src="js/main.js"></script> <script src="js/prototype.js" type="text/javascript"></script> <script language="javascript" xml:space="preserve"> // <...

Overriding default look and feel of GREG - 5.3.0

Following list explains what are the best approach for different use cases. 1 ) - You created a new asset type, and you need to change the look and feel of the details page in the listing page just for that new asset type. To create a new asset type you need to login to the carbon console (username:admin, password:admin) https:// :9443/carbon/ Navigate to Extensions > Configure > Artifacts Click "Add new Artifact" link at the bottom of the page. By default in the "Generic Artifact" area "application" asset type is loaded. Note the shortName="applications" in the root node. "applications" is the name of the asset type. Browse in to /repository/deployment/server/jaggeryapps/store/extensions/assets Create a folder with name "applications"    Now we can override the files in /repository/deployment/server/jaggeryapps/store/extensions/app/greg-store-defaults   Since we are overriding the details page we need to...

Adding custom validations to WSO2 Enterprice Store - Publisher asset creation page.

Writing an asset extension Asset extensions allows to create custom functionalities and look and feel to a certain asset type in WSO2 Enterprise Store. With this example, I am going to create a new asset type and add a custom validation to the asset creation page. Download WSO2ES product distribution from wso2.com and extract it to your local drive. Start the server and fire up browser with the admin console url. https:// :9443/carbon/ Navigate to Extensions > Configure > Artifact Types and click the "Add new Artifact" link. This will load the UI with a new default artifact. Notice that it has following attributes in it's root node. shortName="applications" singularLabel="Enterprise Application" pluralLabel="Enterprise Applications" "applications" is the name of new asset type we are going to add. In our example we are going to do a custom validation to the asset creation page. ...