[Racine des codes] [Page précédente]

formatSqlSelect

/SQL/formatSqlSelect.js
// ----------------------------------------------------------------------------
// formate une requête SQL server.
// Bookmarklet :
// javascript:s=document.body.appendChild(document.createElement('script'));s.id='fs';s.language='javascript';void(s.src='http://siroccov2/tests_flag/Domi/formatSqlSelect.js');
// ----------------------------------------------------------------------------
function myPop(txtHtml) {
  var Fwidth = 700;
  var sty, d = document;
  var pop = d.getElementById("myPop");
  if (pop!=null) d.getElementsByTagName("body")[0].removeChild(pop);

  var pop = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
  pop.id = "myPop";
  sty = pop.style;
  sty.width= Fwidth+'px';
  sty.fontSize='12px';
  sty.position='absolute';
  sty.right='2px';
  sty.top='2px';
  sty.backgroundColor='#CCC';
  sty.border='2px solid #777';
  var btC = pop.appendChild(d.createElement("div"));
  btC.appendChild(d.createTextNode("X"));
  btC.onclick = function() { document.getElementsByTagName("body")[0].removeChild(document.getElementById("myPop")) };
  sty = btC.style;
  sty.width="14px";
  sty.marginLeft= (Fwidth - 18) +"px";
  sty.backgroundColor="#FFF";
  sty.border='1px solid #777';
  sty.cursor='pointer';
  //pop.appendChild(d.createTextNode(txt));
  var Feui = pop.appendChild(d.createElement("div"));
  Feui.style.padding='0px 0px 3px 3px';
  Feui.style.backgroundColor='#FFF';
  Feui.innerHTML = txtHtml;
}

function PromptAndFormat() {
  var div = document.createElement("div");
  div.id = "panelMyPrompt";
  div.style.position = "fixed";
  div.style.top = "5em";
  div.style.right = "2em";
  div.style.padding = "1em";
  div.style.backgroundColor = "#FFF";
  div.style.border = "1px solid #000";
  var texta = document.createElement("textarea");
  texta.id = "sqlSelect";
  texta.cols = "60";
  texta.rows = "10";
  var bt = document.createElement("input");
  bt.setAttribute("type", "button");
  bt.setAttribute("value", "Valider");
  bt.onclick = function() { formatSql('takeInTextarea'); }
  var btCancel = document.createElement("input");
  btCancel.setAttribute("type", "button");
  btCancel.setAttribute("value", "Annuler");
  btCancel.onclick = function() {var d=document.getElementById("panelMyPrompt");d.parentNode.removeChild(d);}
  btCancel.style.marginLeft = "7em";
  var sousDiv = document.createElement("div");
  sousDiv.style.textAlign = "center";
  sousDiv.style.marginTop = "1em";
  sousDiv.appendChild(bt);
  sousDiv.appendChild(btCancel);
  div.appendChild(texta);
  div.appendChild(sousDiv);
  document.body.appendChild(div);
  texta.focus();
}

//-----------------------------------------------------------------------------
function formatSql(sql) {
  if (sql=='takeInTextarea') {
    sql = document.getElementById("sqlSelect").value;
    var div = document.getElementById("panelMyPrompt");
    div.parentNode.removeChild(div);
  }
  var keyW = new Array("select ", "from ", "inner ", "left ", "where ", "and ", "group by ", "having ", "order by ");
  var sqlC = sql;
  for (var i=0; i<keyW.length; i++) {
    sqlC = sqlC.replace(new RegExp(keyW[i], "ig"), "<br>\n<font color=blue>"+keyW[i]+"</font>");
    sql = sql.replace(new RegExp(keyW[i], "ig"), "\n"+keyW[i]);
  }
  sqlC = sqlC.replace(new RegExp(" on ", "ig"), " <font color=blue>on</font> ");
  sqlC = sqlC.replace(new RegExp(" as ", "ig"), " <font color=blue>as</font> ");
  sqlC = sqlC.replace(new RegExp(" in\\(", "ig"), " <font color=blue>in</font> (");
  sqlC = sqlC.replace(new RegExp(",| =|>=|<=|\\(|\\)", "g"), "<font color=red>$&</font>");
  sqlC += "<br><br>";
  myPop(sqlC + "<textarea id='sqlTextarea' cols='90' rows='10' style='font-size:12px'>"+sql+"</textarea>");
  document.getElementById("sqlTextarea").select();
}

//-----------------------------------------------------------------------------
var sql=(window.clipboardData ? window.clipboardData.getData("Text") : "");
if (sql!="")
  formatSql(sql);
else
  PromptAndFormat();
[edit]