sql-formatter
Advanced tools
Comparing version 1.2.2 to 1.3.0
@@ -30,2 +30,4 @@ "use strict"; | ||
* @param {String[]} cfg.namedPlaceholderTypes Prefixes for named placeholders, like @ and : | ||
* @param {String[]} cfg.lineCommentTypes Line comments to enable, like # and -- | ||
* @param {String[]} cfg.specialWordChars Special chars that can be found inside of words, like @ and # | ||
*/ | ||
@@ -35,9 +37,9 @@ function Tokenizer(cfg) { | ||
this.WORD_REGEX = /^(\w+)/; | ||
this.WHITESPACE_REGEX = /^(\s+)/; | ||
this.LINE_COMMENT_REGEX = /^((?:#|--).*?(?:\n|$))/; | ||
this.BLOCK_COMMENT_REGEX = /^(\/\*[^]*?(?:\*\/|$))/; | ||
this.NUMBER_REGEX = /^((-\s*)?[0-9]+(\.[0-9]+)?|0x[0-9a-fA-F]+|0b[01]+)\b/; | ||
this.OPERATOR_REGEX = /^(!=|<>|==|<=|>=|!<|!>|\|\||::|->>|->|.)/; | ||
this.BLOCK_COMMENT_REGEX = /^(\/\*[^]*?(?:\*\/|$))/; | ||
this.LINE_COMMENT_REGEX = this.createLineCommentRegex(cfg.lineCommentTypes); | ||
this.RESERVED_TOPLEVEL_REGEX = this.createReservedWordRegex(cfg.reservedToplevelWords); | ||
@@ -47,2 +49,3 @@ this.RESERVED_NEWLINE_REGEX = this.createReservedWordRegex(cfg.reservedNewlineWords); | ||
this.WORD_REGEX = this.createWordRegex(cfg.specialWordChars); | ||
this.STRING_REGEX = this.createStringRegex(cfg.stringTypes); | ||
@@ -58,2 +61,8 @@ | ||
Tokenizer.prototype.createLineCommentRegex = function createLineCommentRegex(lineCommentTypes) { | ||
return new RegExp("^((?:" + lineCommentTypes.map(function (c) { | ||
return _lodash2["default"].escapeRegExp(c); | ||
}).join("|") + ").*?(?:\n|$))"); | ||
}; | ||
Tokenizer.prototype.createReservedWordRegex = function createReservedWordRegex(reservedWords) { | ||
@@ -64,2 +73,8 @@ var reservedWordsPattern = reservedWords.join("|").replace(/ /g, "\\s+"); | ||
Tokenizer.prototype.createWordRegex = function createWordRegex() { | ||
var specialChars = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; | ||
return new RegExp("^([\\w" + specialChars.join("") + "]+)"); | ||
}; | ||
Tokenizer.prototype.createStringRegex = function createStringRegex(stringTypes) { | ||
@@ -79,7 +94,7 @@ return new RegExp("^(" + this.createStringPattern(stringTypes) + ")"); | ||
var patterns = { | ||
"``": "((`[^`]*($|`))+)", | ||
"[]": "((\\[[^\\]]*($|\\]))(\\][^\\]]*($|\\]))*)", | ||
"\"\"": "((\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*(\"|$))+)", | ||
"''": "(('[^'\\\\]*(?:\\\\.[^'\\\\]*)*('|$))+)", | ||
"N''": "((N'[^N'\\\\]*(?:\\\\.[^N'\\\\]*)*('|$))+)", | ||
"``": "((`[^`]*($|`))+)", | ||
"[]": "((\\[[^\\]]*($|\\]))(\\][^\\]]*($|\\]))*)" | ||
"N''": "((N'[^N'\\\\]*(?:\\\\.[^N'\\\\]*)*('|$))+)" | ||
}; | ||
@@ -86,0 +101,0 @@ |
@@ -54,3 +54,4 @@ "use strict"; | ||
closeParens: [")", "]", "}"], | ||
namedPlaceholderTypes: ["$"] | ||
namedPlaceholderTypes: ["$"], | ||
lineCommentTypes: ["#", "--"] | ||
}); | ||
@@ -57,0 +58,0 @@ } |
@@ -55,3 +55,4 @@ "use strict"; | ||
indexedPlaceholderTypes: ["?"], | ||
namedPlaceholderTypes: ["@", ":"] | ||
namedPlaceholderTypes: ["@", ":"], | ||
lineCommentTypes: ["#", "--"] | ||
}); | ||
@@ -58,0 +59,0 @@ } |
@@ -5,2 +5,6 @@ "use strict"; | ||
var _Db2Formatter = require("./languages/Db2Formatter"); | ||
var _Db2Formatter2 = _interopRequireDefault(_Db2Formatter); | ||
var _N1qlFormatter = require("./languages/N1qlFormatter"); | ||
@@ -31,2 +35,4 @@ | ||
switch (cfg.language) { | ||
case "db2": | ||
return new _Db2Formatter2["default"](cfg).format(query); | ||
case "n1ql": | ||
@@ -33,0 +39,0 @@ return new _N1qlFormatter2["default"](cfg).format(query); |
{ | ||
"name": "sql-formatter", | ||
"version": "1.2.2", | ||
"version": "1.3.0", | ||
"description": "Formats whitespaces in a SQL query to make it more readable", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -5,3 +5,3 @@ # SQL Formatter [![NPM version](https://img.shields.io/npm/v/sql-formatter.svg)](https://npmjs.com/package/sql-formatter) [![Build Status](https://travis-ci.org/zeroturnaround/sql-formatter.svg?branch=master)](https://travis-ci.org/zeroturnaround/sql-formatter) [![Coverage Status](https://coveralls.io/repos/github/zeroturnaround/sql-formatter/badge.svg?branch=master)](https://coveralls.io/github/zeroturnaround/sql-formatter?branch=master) | ||
It started as a port of a [PHP Library][], but has since considerably diverged. | ||
It supports [Standard SQL][] and [Couchbase N1QL][] dialects. | ||
It supports [Standard SQL][], [Couchbase N1QL][] and [IBM DB2][] dialects. | ||
@@ -39,3 +39,3 @@ → [Try the demo.](https://zeroturnaround.github.io/sql-formatter/) | ||
sqlFormatter.format("SELECT *", { | ||
language: "n1ql" // Defaults to "sql" | ||
language: "n1ql", // Defaults to "sql" | ||
indent: " " // Defaults to two spaces | ||
@@ -45,6 +45,7 @@ }); | ||
Currently just two SQL dialects are supported: | ||
Currently just three SQL dialects are supported: | ||
- **sql** - [Standard SQL][] | ||
- **n1ql** - [Couchbase N1QL][] | ||
- **db2** - [IBM DB2][] | ||
@@ -97,1 +98,2 @@ ### Placeholders replacement | ||
[Couchbase N1QL]: http://www.couchbase.com/n1ql | ||
[IBM DB2]: https://www.ibm.com/analytics/us/en/technology/db2/ |
@@ -15,11 +15,13 @@ import _ from "lodash"; | ||
* @param {String[]} cfg.namedPlaceholderTypes Prefixes for named placeholders, like @ and : | ||
* @param {String[]} cfg.lineCommentTypes Line comments to enable, like # and -- | ||
* @param {String[]} cfg.specialWordChars Special chars that can be found inside of words, like @ and # | ||
*/ | ||
constructor(cfg) { | ||
this.WORD_REGEX = /^(\w+)/; | ||
this.WHITESPACE_REGEX = /^(\s+)/; | ||
this.LINE_COMMENT_REGEX = /^((?:#|--).*?(?:\n|$))/; | ||
this.BLOCK_COMMENT_REGEX = /^(\/\*[^]*?(?:\*\/|$))/; | ||
this.NUMBER_REGEX = /^((-\s*)?[0-9]+(\.[0-9]+)?|0x[0-9a-fA-F]+|0b[01]+)\b/; | ||
this.OPERATOR_REGEX = /^(!=|<>|==|<=|>=|!<|!>|\|\||::|->>|->|.)/; | ||
this.BLOCK_COMMENT_REGEX = /^(\/\*[^]*?(?:\*\/|$))/; | ||
this.LINE_COMMENT_REGEX = this.createLineCommentRegex(cfg.lineCommentTypes); | ||
this.RESERVED_TOPLEVEL_REGEX = this.createReservedWordRegex(cfg.reservedToplevelWords); | ||
@@ -29,2 +31,3 @@ this.RESERVED_NEWLINE_REGEX = this.createReservedWordRegex(cfg.reservedNewlineWords); | ||
this.WORD_REGEX = this.createWordRegex(cfg.specialWordChars); | ||
this.STRING_REGEX = this.createStringRegex(cfg.stringTypes); | ||
@@ -43,2 +46,6 @@ | ||
createLineCommentRegex(lineCommentTypes) { | ||
return new RegExp(`^((?:${lineCommentTypes.map(c => _.escapeRegExp(c)).join("|")}).*?(?:\n|$))`); | ||
} | ||
createReservedWordRegex(reservedWords) { | ||
@@ -49,2 +56,6 @@ const reservedWordsPattern = reservedWords.join("|").replace(/ /g, "\\s+"); | ||
createWordRegex(specialChars = []) { | ||
return new RegExp(`^([\\w${specialChars.join("")}]+)`); | ||
} | ||
createStringRegex(stringTypes) { | ||
@@ -64,7 +75,7 @@ return new RegExp( | ||
const patterns = { | ||
"``": "((`[^`]*($|`))+)", | ||
"[]": "((\\[[^\\]]*($|\\]))(\\][^\\]]*($|\\]))*)", | ||
"\"\"": "((\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*(\"|$))+)", | ||
"''": "(('[^'\\\\]*(?:\\\\.[^'\\\\]*)*('|$))+)", | ||
"N''": "((N'[^N'\\\\]*(?:\\\\.[^N'\\\\]*)*('|$))+)", | ||
"``": "((`[^`]*($|`))+)", | ||
"[]": "((\\[[^\\]]*($|\\]))(\\][^\\]]*($|\\]))*)", | ||
}; | ||
@@ -71,0 +82,0 @@ |
@@ -31,10 +31,28 @@ import Formatter from "../core/Formatter"; | ||
const reservedToplevelWords = [ | ||
"DELETE FROM", "EXCEPT ALL", "EXCEPT", "EXPLAIN DELETE FROM", "EXPLAIN UPDATE", "EXPLAIN UPSERT", "FROM", | ||
"GROUP BY", "HAVING", "INFER", "INSERT INTO", "INTERSECT ALL", "INTERSECT", "LET", "LIMIT", "MERGE", "NEST", | ||
"ORDER BY", "PREPARE", "SELECT", "SET CURRENT SCHEMA", "SET SCHEMA", "SET", "UNION ALL", "UNION", "UNNEST", | ||
"UPDATE", "UPSERT", "USE KEYS", "VALUES", "WHERE" | ||
"DELETE FROM", | ||
"EXCEPT ALL", "EXCEPT", "EXPLAIN DELETE FROM", "EXPLAIN UPDATE", "EXPLAIN UPSERT", | ||
"FROM", | ||
"GROUP BY", | ||
"HAVING", | ||
"INFER", "INSERT INTO", "INTERSECT ALL", "INTERSECT", | ||
"LET", "LIMIT", | ||
"MERGE", | ||
"NEST", | ||
"ORDER BY", | ||
"PREPARE", | ||
"SELECT", "SET CURRENT SCHEMA", "SET SCHEMA", "SET", | ||
"UNION ALL", "UNION", "UNNEST", "UPDATE", "UPSERT", "USE KEYS", | ||
"VALUES", | ||
"WHERE" | ||
]; | ||
const reservedNewlineWords = [ | ||
"AND", "INNER JOIN", "JOIN", "LEFT JOIN", "LEFT OUTER JOIN", "OR", "OUTER JOIN", "RIGHT JOIN", "RIGHT OUTER JOIN", "XOR" | ||
"AND", | ||
"INNER JOIN", | ||
"JOIN", | ||
"LEFT JOIN", | ||
"LEFT OUTER JOIN", | ||
"OR", "OUTER JOIN", | ||
"RIGHT JOIN", "RIGHT OUTER JOIN", | ||
"XOR" | ||
]; | ||
@@ -67,3 +85,4 @@ | ||
closeParens: [")", "]", "}"], | ||
namedPlaceholderTypes: ["$"] | ||
namedPlaceholderTypes: ["$"], | ||
lineCommentTypes: ["#", "--"] | ||
}); | ||
@@ -70,0 +89,0 @@ } |
@@ -45,9 +45,27 @@ import Formatter from "../core/Formatter"; | ||
const reservedToplevelWords = [ | ||
"ADD", "AFTER", "ALTER COLUMN", "ALTER TABLE", "DELETE FROM", "EXCEPT", "FROM", "GROUP BY", "GO", "HAVING", "INSERT INTO", "INTERSECT", | ||
"LIMIT", "MODIFY", "ORDER BY", "SELECT", "SET CURRENT SCHEMA", "SET SCHEMA", "SET", "UNION ALL", "UNION", "UPDATE", "VALUES", "WHERE" | ||
"ADD", "AFTER", "ALTER COLUMN", "ALTER TABLE", | ||
"DELETE FROM", | ||
"EXCEPT", | ||
"FROM", | ||
"GROUP BY", "GO", | ||
"HAVING", | ||
"INSERT INTO", "INTERSECT", | ||
"LIMIT", | ||
"MODIFY", | ||
"ORDER BY", | ||
"SELECT", "SET CURRENT SCHEMA", "SET SCHEMA", "SET", | ||
"UNION ALL", "UNION", "UPDATE", | ||
"VALUES", | ||
"WHERE" | ||
]; | ||
const reservedNewlineWords = [ | ||
"AND", "CROSS APPLY", "CROSS JOIN", "INNER JOIN", "JOIN", "LEFT JOIN", "LEFT OUTER JOIN", "OR", "OUTER APPLY", "OUTER JOIN", | ||
"RIGHT JOIN", "RIGHT OUTER JOIN", "XOR" | ||
"AND", | ||
"CROSS APPLY", "CROSS JOIN", | ||
"INNER JOIN", | ||
"JOIN", | ||
"LEFT JOIN", "LEFT OUTER JOIN", | ||
"OR", "OUTER APPLY", "OUTER JOIN", | ||
"RIGHT JOIN", "RIGHT OUTER JOIN", | ||
"XOR" | ||
]; | ||
@@ -81,3 +99,4 @@ | ||
indexedPlaceholderTypes: ["?"], | ||
namedPlaceholderTypes: ["@", ":"] | ||
namedPlaceholderTypes: ["@", ":"], | ||
lineCommentTypes: ["#", "--"] | ||
}); | ||
@@ -84,0 +103,0 @@ } |
@@ -0,1 +1,2 @@ | ||
import Db2Formatter from "./languages/Db2Formatter"; | ||
import N1qlFormatter from "./languages/N1qlFormatter"; | ||
@@ -19,2 +20,4 @@ import StandardSqlFormatter from "./languages/StandardSqlFormatter"; | ||
switch (cfg.language) { | ||
case "db2": | ||
return new Db2Formatter(cfg).format(query); | ||
case "n1ql": | ||
@@ -21,0 +24,0 @@ return new N1qlFormatter(cfg).format(query); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
794631
25
19138
96
1