Skip to content

Commit

Permalink
nested select indentation done
Browse files Browse the repository at this point in the history
  • Loading branch information
vkiryukhin committed Feb 26, 2012
1 parent 1946cc2 commit 953ecfe
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
2 changes: 1 addition & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ function loadTemplate(name)
case 'basicsql':
$('#ta').width(800);
$('#leftpanel').show();
document.getElementById('ta').value = 'SELECT aaa from bbb where ccc="ddd"';
document.getElementById('ta').value = "SELECT aaa from bbb where ccc='ddd'";
//$('#rightpanel').empty().load('html/basic.html');
$('#rightpanel').empty();
$('#mode').html('SQL');
Expand Down
48 changes: 42 additions & 6 deletions vkbeautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
function vkbeautify(){
this.shift = ['\n']; // array of shifts
//var step = ' ', // 2 spaces
this.step = ' ', // 2 spaces
this.step = ' ', // 2 spaces
maxdeep = 100, // nesting level
ix = 0;

Expand Down Expand Up @@ -186,6 +186,10 @@ vkbeautify.prototype.css = function(text) {

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

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

function split_sql(str) {

return str.replace(/\s{1,}/g," ")
Expand All @@ -201,29 +205,33 @@ function split_sql(str) {
.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(/ LIKE /ig,"~#~LIKE ")
.replace(/ ON /ig,"~#~ON ")
.replace(/ OR /ig,"~#~\tOR ")
.replace(/ ORDER\s{1,}BY/ig,"~#~ORDER BY ")

//.replace(/\s{0,}SELECT /ig,"~#~SELECT ")
.replace(/\s{0,}SELECT /ig,"~#~SELECT~#~")
//.replace(/\(\s{0,}SELECT /ig," (SELECT ")
.replace(/\(\s{0,}SELECT /ig,"~#~(SELECT ")

.replace(/ UNION /ig,"~#~UNION~#~")
.replace(/ USING /ig,"~#~USING ")
.replace(/ WHEN /ig,"~#~WHEN ")
.replace(/ WHERE /ig,"~#~WHERE ")
.replace(/ WITH /ig,"~#~WITH ")

//.replace(/\,\s{0,}\(/ig,",~#~( ")
.replace(/\,/ig,",~#~")

//.replace(/\,/ig,",~#~")

.replace(/ All /ig," ALL ")
.replace(/ AS /ig," AS ")
.replace(/ ASC /ig," ASC ")
.replace(/ DESC /ig," DESC ")
.replace(/ DISTINCT /ig,"\DISTINCT ")
.replace(/ DISTINCT /ig," DISTINCT ")
.replace(/ EXISTS /ig," EXISTS ")
.replace(/ NOT /ig," NOT ")
.replace(/ NULL /ig," NULL ")
.replace(/ LIKE /ig," LIKE ")

.replace(/~#~{1,}/g,"~#~")
.split('~#~');
Expand All @@ -239,7 +247,7 @@ vkbeautify.prototype.sql = function(text) {
deep = 0,
inComment = true,
inQuote = false,
secondTouch = false,
parenthesisLevel = 0,
str = '',
ix = 0;

Expand All @@ -254,6 +262,33 @@ vkbeautify.prototype.sql = function(text) {
len = ar.length;
for(ix=0;ix<len;ix++) {

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

}

//if( /\'/.exec(ar[ix]) ) {
// str += ar[ix];
//}
//else {
// str += this.shift[deep]+ar[ix];
//}



/*
if( /SELECT/.exec(ar[ix])) {
str += this.shift[deep++]+ar[ix];
secondTouch = true;
Expand All @@ -267,6 +302,7 @@ vkbeautify.prototype.sql = function(text) {
else {
str += this.shift[deep]+ar[ix];
}
*/
}

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

0 comments on commit 953ecfe

Please sign in to comment.