Skip to content

Commit

Permalink
minor code improvments, edited docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
vkiryukhin committed Feb 27, 2012
1 parent 05a1cc9 commit 36b43af
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
6 changes: 3 additions & 3 deletions html/basicsql.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
<p>
vkBeautify SQL parser covers only SELECT statement as most javascript developers most time have deal with SELECT.
As SQL syntax itself is very large and complex, some complex statements can be formatted not as perfect as you'd
done it manually. But anyway, it helps you to read and understand complex SQL statement.
make it manually. But anyway, this tool helps you to save time on reading unformatted SQL statement.
</p>
<p>Click on "<b>Pretty Print</b>" button to beatify the text. </p>
<p>Click on "<b>Minify</b>" button to minify the text. Not sure if anybody needs it, but I keep it for consistency :)<br/>
<span style="font-size:80%">("Preserve Comments" has no effect as it doesn't make any sense.) </span>
<p>Click on "<b>Minify</b>" button to minify the text (not sure if anybody needs it, but I keep it for consistency :)<br/><br/>
<span style="font-size:80%">"Preserve Comments" is not implemented for SQL part or the application. </span>
</p>
<p>

Expand Down
35 changes: 21 additions & 14 deletions vkbeautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@

function vkbeautify(){
this.shift = ['\n']; // array of shifts
this.step = ' '; // 2 spaces
//this.step = ' '; // 2 spaces
this.step = ' '; // 4 spaces
var maxdeep = 100, // nesting level
ix = 0;

Expand Down Expand Up @@ -184,7 +185,7 @@ vkbeautify.prototype.css = function(text) {

//----------------------------------------------------------------------------

function isSubquery(parenthesisLevel, str) {
function isSubquery(str, parenthesisLevel) {
return parenthesisLevel - (str.replace(/\(/g,'').length - str.replace(/\)/g,'').length )
}

Expand All @@ -200,13 +201,16 @@ function split_sql(str, tab) {
.replace(/ FROM /ig,"~#~FROM ")
.replace(/ GROUP\s{1,}BY/ig,"~#~GROUP BY ")
.replace(/ HAVING /ig,"~#~HAVING ")
.replace(/ IN /ig,"~#~"+tab+"IN ")
//.replace(/ IN /ig,"~#~"+tab+"IN ")
.replace(/ IN /ig," IN ")

.replace(/ JOIN /ig,"~#~JOIN ")
.replace(/ CROSS\s{1,}JOIN /ig,"~#~CROSS JOIN ")
.replace(/ INNER\s{1,}JOIN /ig,"~#~INNER JOIN ")
.replace(/ LEFT\s{1,}JOIN /ig,"~#~LEFT JOIN ")
.replace(/ RIGHT\s{1,}JOIN /ig,"~#~RIGHT JOIN ")
.replace(/ ON /ig,"~#~ON ")
.replace(/ CROSS~#~{1,}JOIN /ig,"~#~CROSS JOIN ")
.replace(/ INNER~#~{1,}JOIN /ig,"~#~INNER JOIN ")
.replace(/ LEFT~#~{1,}JOIN /ig,"~#~LEFT JOIN ")
.replace(/ RIGHT~#~{1,}JOIN /ig,"~#~RIGHT JOIN ")

.replace(/ ON /ig,"~#~"+tab+"ON ")
.replace(/ OR /ig,"~#~"+tab+tab+"OR ")
.replace(/ ORDER\s{1,}BY/ig,"~#~ORDER BY ")
.replace(/ OVER /ig,"~#~"+tab+"OVER ")
Expand Down Expand Up @@ -247,7 +251,7 @@ vkbeautify.prototype.sql = function(text, brakeOnComma) {
len = ar_by_quote.length,
ar = [],
deep = 0,
tab = this.step+this.step;
tab = this.step,//+this.step,
inComment = true,
inQuote = false,
parenthesisLevel = 0,
Expand All @@ -264,30 +268,33 @@ vkbeautify.prototype.sql = function(text, brakeOnComma) {

len = ar.length;
for(ix=0;ix<len;ix++) {


parenthesisLevel = isSubquery(ar[ix], parenthesisLevel);

if( /\s{0,}\s{0,}SELECT\s{0,}/.exec(ar[ix])) {
ar[ix] = ar[ix].replace(/\,/g,",\n"+tab+tab+"")
}

if( /\s{0,}\(\s{0,}SELECT\s{0,}/.exec(ar[ix])) {
deep++;
//parenthesisLevel = isSubquery(ar[ix], parenthesisLevel);
str += this.shift[deep]+ar[ix];
parenthesisLevel = isSubquery(0, ar[ix]);
} else
if( /\'/.exec(ar[ix]) ) {
parenthesisLevel = isSubquery(parenthesisLevel, ar[ix]);
if(!parenthesisLevel && deep) {
//parenthesisLevel = isSubquery(ar[ix], parenthesisLevel);
if(parenthesisLevel<1 && deep) {
deep--;
}
str += ar[ix];
}
else {
parenthesisLevel = isSubquery(parenthesisLevel, ar[ix]);
//parenthesisLevel = isSubquery(ar[ix], parenthesisLevel);
str += this.shift[deep]+ar[ix];
if(parenthesisLevel<1 && deep) {
deep--;
}
}
var junk = 0;
}

str = str.replace(/^\n{1,}/,'')/*.replace(/\n[ \t]{0,}/g,"\n");*/.replace(/\n{1,}/g,"\n");
Expand Down

0 comments on commit 36b43af

Please sign in to comment.