-
Notifications
You must be signed in to change notification settings - Fork 453
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from Glavin001/master
Add SQL and JSON language support
- Loading branch information
Showing
7 changed files
with
185 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
SELECT ca.proj_id AS proj_id, ca.ca_name AS proj_name, ca.ca_date_start AS proj_start, ca.ca_date_end AS proj_end,(SELECT COUNT(*) FROM rotations r WHERE r.proj_id = proj_id AND r.r_status = 'R' GROUP BY r.proj_id) r_count, (SELECT count(*) FROM rotations r WHERE r.proj_id = proj_id AND r.channel_id = 24 ) r_rtb_count FROM projs ca, clients c, proj_auth caa WHERE ca.client_id = 12345 AND ca.client_id = c.client_id AND ca_type = 'zzz' AND c.agency_id = 0 AND ca.client_id = NVL( caa.client_id, ca.client_id ) AND proj_id = NVL( caa.proj_id, proj_id ) AND caa.contact_id = 7890 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"indent_size": 2, | ||
"indent_char": " ", // test | ||
"indent_level": 0, | ||
"indent_with_tabs": false, /* test */ | ||
"preserve_newlines": true, | ||
"max_preserve_newlines": 2, /* test * | ||
"jslint_happy": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
SELECT ca.proj_id AS proj_id, ca.ca_name AS proj_name, ca.ca_date_start AS proj_start, ca.ca_date_end AS proj_end,(SELECT COUNT(*) FROM rotations r WHERE r.proj_id = proj_id AND r.r_status = 'R' GROUP BY r.proj_id) r_count, (SELECT count(*) FROM rotations r WHERE r.proj_id = proj_id AND r.channel_id = 24 ) r_rtb_count FROM projs ca, clients c, proj_auth caa WHERE ca.client_id = 12345 AND ca.client_id = c.client_id AND ca_type = 'zzz' AND c.agency_id = 0 AND ca.client_id = NVL( caa.client_id, ca.client_id ) AND proj_id = NVL( caa.proj_id, proj_id ) AND caa.contact_id = 7890 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/** | ||
Original SQL Beautifier Source code from https://github.com/vkiryukhin/pretty-data | ||
*/ | ||
'use strict'; | ||
|
||
module.exports = function (text, options) { | ||
|
||
function SQL() { | ||
var self = this; | ||
|
||
self.step = ' '; // 2 spaces | ||
self.shift = ['\n']; // array of shifts | ||
var maxdeep = 100, // nesting level | ||
ix = 0; | ||
|
||
// initialize array with shifts // | ||
for (ix = 0; ix < maxdeep; ix++) { | ||
self.shift.push(self.shift[ix] + self.step); | ||
} | ||
} | ||
|
||
function isSubquery(str, parenthesisLevel) { | ||
return parenthesisLevel - (str.replace(/\(/g, '').length - str.replace(/\)/g, '').length); | ||
} | ||
|
||
function splitSql(str, tab) { | ||
|
||
return str.replace(/\s{1,}/g, ' ') | ||
|
||
.replace(/ AND /ig, '~::~' + tab + tab + 'AND ') | ||
.replace(/ BETWEEN /ig, '~::~' + tab + 'BETWEEN ') | ||
.replace(/ CASE /ig, '~::~' + tab + 'CASE ') | ||
.replace(/ ELSE /ig, '~::~' + tab + 'ELSE ') | ||
.replace(/ END /ig, '~::~' + tab + 'END ') | ||
.replace(/ FROM /ig, '~::~FROM ') | ||
.replace(/ GROUP\s{1,}BY/ig, '~::~GROUP BY ') | ||
.replace(/ HAVING /ig, '~::~HAVING ') | ||
//.replace(/ IN /ig,'~::~'+tab+'IN ') | ||
.replace(/ IN /ig, ' IN ') | ||
.replace(/ JOIN /ig, '~::~JOIN ') | ||
.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 ') | ||
.replace(/\(\s{0,}SELECT /ig, '~::~(SELECT ') | ||
.replace(/\)\s{0,}SELECT /ig, ')~::~SELECT ') | ||
.replace(/ THEN /ig, ' THEN~::~' + tab + '') | ||
.replace(/ UNION /ig, '~::~UNION~::~') | ||
.replace(/ USING /ig, '~::~USING ') | ||
.replace(/ WHEN /ig, '~::~' + tab + 'WHEN ') | ||
.replace(/ WHERE /ig, '~::~WHERE ') | ||
.replace(/ WITH /ig, '~::~WITH ') | ||
//.replace(/\,\s{0,}\(/ig,',~::~( ') | ||
//.replace(/\,/ig,',~::~'+tab+tab+'') | ||
.replace(/ ALL /ig, ' ALL ') | ||
.replace(/ AS /ig, ' AS ') | ||
.replace(/ ASC /ig, ' ASC ') | ||
.replace(/ DESC /ig, ' DESC ') | ||
.replace(/ DISTINCT /ig, ' DISTINCT ') | ||
.replace(/ EXISTS /ig, ' EXISTS ') | ||
.replace(/ NOT /ig, ' NOT ') | ||
.replace(/ NULL /ig, ' NULL ') | ||
.replace(/ LIKE /ig, ' LIKE ') | ||
.replace(/\s{0,}SELECT /ig, 'SELECT ') | ||
.replace(/~::~{1,}/g, '~::~') | ||
.split('~::~'); | ||
} | ||
|
||
SQL.prototype.beautify = function (text, options) { | ||
|
||
/* jshint camelcase: false */ | ||
// Apply options | ||
// options.indent_size = Indentation size [4] | ||
// options.indent_char = Indentation character [" "] | ||
this.step = new Array(options.indent_size).join(options.indent_char); | ||
// Initial indentation level [0] | ||
if (options.indent_level) { | ||
// Not supported. | ||
} | ||
// Indent with tabs, overrides indent_size and indent_char | ||
if (!!options.indent_with_tabs) { | ||
this.step = '\t'; | ||
} | ||
|
||
var arByQuote = text.replace(/\s{1,}/g, ' ') | ||
.replace(/\'/ig, '~::~\'') | ||
.split('~::~'), | ||
len = arByQuote.length, | ||
ar = [], | ||
deep = 0, | ||
tab = this.step, | ||
inComment = true, | ||
inQuote = false, | ||
parenthesisLevel = 0, | ||
str = '', | ||
ix = 0; | ||
|
||
for (ix = 0; ix < len; ix++) { | ||
|
||
if (ix % 2) { | ||
ar = ar.concat(arByQuote[ix]); | ||
} else { | ||
ar = ar.concat(splitSql(arByQuote[ix], tab)); | ||
} | ||
} | ||
|
||
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++; | ||
str += this.shift[deep] + ar[ix]; | ||
} else | ||
if (/\'/.exec(ar[ix])) { | ||
if (parenthesisLevel < 1 && deep) { | ||
deep--; | ||
} | ||
str += ar[ix]; | ||
} else { | ||
str += this.shift[deep] + ar[ix]; | ||
if (parenthesisLevel < 1 && deep) { | ||
deep--; | ||
} | ||
} | ||
} | ||
|
||
str = str.replace(/^\n{1,}/, '').replace(/\n{1,}/g, '\n'); | ||
return str; | ||
|
||
}; | ||
|
||
return new SQL().beautify(text, options); | ||
|
||
}; |