SQL translator

[Total: 0    Average: 0/5]

This script will allow you to use a sql statement to query a data structure.

Head Code:

  <script type=text/javascript>

function parseObject(str) {
 pos1 = str.indexOf("{");
 pos2 = str.indexOf("}");
 array = str.substring(pos1+1,pos2).replace(/:/gmi, "=").split(",");
 obj = {};
 for(var i=0; i<array.length; i++)
  eval("obj." + array[i]);

 return obj;

function translate(SQL,params) {
 SQL = SQL.toLowerCase();
 var foundstr;
 var re = /~[w]+b/gmi;

 while(SQL.match(re)!=null) {
  foundstr = SQL.match(re)[0];
  value    = params[foundstr.substring(1).toLowerCase()];
  newstr   = typeof(value)=="number"? value : """ + value + """;
  SQL = SQL.replace(new RegExp(foundstr,"gmi"), newstr);

 return SQL;

Body Code:

   <td>enter the SQL statement</td>
   <td><input id=SQL type=text size=70 value="Select * from mytable where a=~val_a and b=~val_b"></td>
   <td>enter parameters of statement</td>
   <td><input id=params type=text value="{val_a:1, val_b:'2'}"></td>
   <td><input type=button value="translate" onclick="with(document) {getElementById('result').innerHTML = translate(getElementById('SQL').value, parseObject(getElementById('params').value) )}"></td>
   <td>result of translation</td>
   <td id=result>select * from mytable where a=1 and b="2"</td>


Times Viewed: 7

Leave a Reply

Your email address will not be published. Required fields are marked *