Skip to content

Commit ffd2441

Browse files
committed
Add proper formatting for square bracket quoted strings (SQL Server style).
For #63
1 parent 9fcbe73 commit ffd2441

File tree

7 files changed

+34
-15
lines changed

7 files changed

+34
-15
lines changed

lib/SqlFormatter.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @copyright 2013 Jeremy Dorn
1010
* @license http://opensource.org/licenses/MIT
1111
* @link http://github.com/jdorn/sql-formatter
12-
* @version 1.2.17
12+
* @version 1.2.18
1313
*/
1414
class SqlFormatter
1515
{
@@ -102,7 +102,7 @@ class SqlFormatter
102102
);
103103

104104
// Punctuation that can be used as a boundary between other tokens
105-
protected static $boundaries = array(',', ';',':', ']', '[', ')', '(', '.', '=', '<', '>', '+', '-', '*', '/', '!', '^', '%', '|', '&', '#');
105+
protected static $boundaries = array(',', ';',':', ')', '(', '.', '=', '<', '>', '+', '-', '*', '/', '!', '^', '%', '|', '&', '#');
106106

107107
// For HTML syntax highlighting
108108
// Styles applied to different token types
@@ -235,9 +235,9 @@ protected static function getNextToken($string, $previous = null)
235235
}
236236

237237
// Quoted String
238-
if ($string[0]==='"' || $string[0]==='\'' || $string[0]==='`') {
238+
if ($string[0]==='"' || $string[0]==='\'' || $string[0]==='`' || $string[0]==='[') {
239239
$return = array(
240-
self::TOKEN_TYPE => ($string[0]==='`'? self::TOKEN_TYPE_BACKTICK_QUOTE : self::TOKEN_TYPE_QUOTE),
240+
self::TOKEN_TYPE => (($string[0]==='`' || $string[0]==='[')? self::TOKEN_TYPE_BACKTICK_QUOTE : self::TOKEN_TYPE_QUOTE),
241241
self::TOKEN_VALUE => self::getQuotedString($string)
242242
);
243243

@@ -335,9 +335,10 @@ protected static function getQuotedString($string)
335335

336336
// This checks for the following patterns:
337337
// 1. backtick quoted string using `` to escape
338-
// 2. double quoted string using "" or \" to escape
339-
// 3. single quoted string using '' or \' to escape
340-
if ( preg_match('/^(((`[^`]*($|`))+)|(("[^"\\\\]*(?:\\\\.[^"\\\\]*)*("|$))+)|((\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*(\'|$))+))/s', $string, $matches)) {
338+
// 2. square bracket quoted string (SQL Server) using ]] to escape
339+
// 3. double quoted string using "" or \" to escape
340+
// 4. single quoted string using '' or \' to escape
341+
if ( preg_match('/^(((`[^`]*($|`))+)|((\[[^\]]*($|\]))(\][^\]]*($|\]))*)|(("[^"\\\\]*(?:\\\\.[^"\\\\]*)*("|$))+)|((\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*(\'|$))+))/s', $string, $matches)) {
341342
$ret = $matches[1];
342343
}
343344

tests/clihighlight.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -805,5 +805,9 @@
805805
@"weird variable name";
806806

807807
SELECT
808-
"no closing quote
809-

808+
"no closing quote
809+
810+
SELECT
811+
[sqlserver]
812+
FROM
813+
[escap[e]]d style];

tests/compress.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,6 @@
7474

7575
SELECT @"weird variable name";
7676

77-
SELECT "no closing quote
77+
SELECT "no closing quote
78+
79+
SELECT [sqlserver] FROM [escap[e]]d style];

tests/format-highlight.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -805,5 +805,9 @@
805805
<span style="color: orange;">@&quot;weird variable name&quot;</span><span >;</span></pre>
806806

807807
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span>
808-
<span style="color: blue;">&quot;no closing quote
809-
</span></pre>
808+
<span style="color: blue;">&quot;no closing quote</span></pre>
809+
810+
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span>
811+
<span style="color: purple;">[sqlserver]</span>
812+
<span style="font-weight:bold;">FROM</span>
813+
<span style="color: purple;">[escap[e]]d style]</span><span >;</span></pre>

tests/format.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,4 +804,9 @@
804804
@"weird variable name";
805805

806806
SELECT
807-
"no closing quote
807+
"no closing quote
808+
809+
SELECT
810+
[sqlserver]
811+
FROM
812+
[escap[e]]d style];

tests/highlight.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,5 +258,6 @@
258258

259259
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span> <span style="color: orange;">@&quot;weird variable name&quot;</span><span >;</span></pre>
260260

261-
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span> <span style="color: blue;">&quot;no closing quote
262-
</span></pre>
261+
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span> <span style="color: blue;">&quot;no closing quote</span></pre>
262+
263+
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span> <span style="color: purple;">[sqlserver]</span> <span style="font-weight:bold;">FROM</span> <span style="color: purple;">[escap[e]]d style]</span><span >;</span></pre>

tests/sql.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,5 @@ SELECT @ and b;
259259
SELECT @"weird variable name";
260260

261261
SELECT "no closing quote
262+
263+
SELECT [sqlserver] FROM [escap[e]]d style];

0 commit comments

Comments
 (0)