Skip to content

Commit

Permalink
[SQL] use inheritance to support different dialects (#3046)
Browse files Browse the repository at this point in the history
Rewrite SQL syntax using inheritance to support MySQL, PostgreSQL, T-SQL and Cassandra.

Note: Postegre is still in a somewhat WIP state using most features of MySQL.

Co-authored-by: Johannes Rappen <[email protected]>
Co-authored-by: deathaxe <[email protected]>
Co-authored-by: DeathAxe <[email protected]>
Co-authored-by: Aziz Köksal <[email protected]>
Co-authored-by: Keith Hall <[email protected]>
  • Loading branch information
6 people authored Nov 22, 2024
1 parent 426f883 commit 15ccade
Show file tree
Hide file tree
Showing 33 changed files with 13,795 additions and 756 deletions.
19 changes: 19 additions & 0 deletions Go/Embeddings/Go (Embedded TSQL).sublime-syntax
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
%YAML 1.2
---
name: Go (Embedded T-SQL)
scope: source.go.embedded-backtick-string.tsql
version: 2
hidden: true

extends: Packages/Go/Embeddings/Go (Embedded Backtick String).sublime-syntax

contexts:
match-raw-string:
# replaces Go.sublime-syntax#match-raw-string
- match: '`'
scope: meta.string.go string.quoted.backtick.go punctuation.definition.string.begin.go
embed: scope:source.tsql.go-embedded-backtick-string
embed_scope: meta.string.go meta.embedded.go source.sql.embedded.go
escape: '`'
escape_captures:
0: meta.string.go string.quoted.backtick.go punctuation.definition.string.end.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
---
name: SQL inside Go backtick string
scope: source.sql.go-embedded-backtick-string
version: 1
version: 2
hidden: true

extends: Packages/SQL/SQL.sublime-syntax
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
%YAML 1.2
---
name: T-SQL inside Go backtick string
scope: source.tsql.go-embedded-backtick-string
version: 2
hidden: true

extends: Packages/SQL/TSQL.sublime-syntax

contexts:
prototype:
- meta_prepend: true
- include: scope:source.go#match-raw-text-content

string-escape:
- meta_prepend: true
- include: scope:source.go#match-raw-string-content

string-interpolation:
- meta_prepend: true
- include: scope:source.go#match-raw-string-content
3 changes: 3 additions & 0 deletions Go/Go.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ contexts:
- match: (?i)sql\b
scope: meta.annotation.parameters.go constant.other.language-name.go
set: scope:source.go.embedded-backtick-string.sql#match-comment-magic-language-injection-inner
- match: (?i)t-?sql\b
scope: meta.annotation.parameters.go constant.other.language-name.go
set: scope:source.go.embedded-backtick-string.tsql#match-comment-magic-language-injection-inner
- match: (?i)xml\b
scope: meta.annotation.parameters.go constant.other.language-name.go
set: scope:source.go.embedded-backtick-string.xml#match-comment-magic-language-injection-inner
Expand Down
13 changes: 12 additions & 1 deletion Go/tests/syntax_test_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -5883,7 +5883,7 @@ func lang_embedding() {
// ^ meta.string.go string.quoted.backtick.go punctuation.definition.string.begin.go
// ^ meta.string.go meta.embedded.go source.sql.embedded.go
update schema.table
// ^^^^^^ meta.string.go meta.embedded.go source.sql.embedded.go keyword.other.DML.sql
// ^^^^^^ meta.string.go meta.embedded.go source.sql.embedded.go keyword.other.dml.sql
set
some_field = null
where
Expand All @@ -5907,6 +5907,17 @@ func lang_embedding() {
not_sql_string = `select not sql`
// ^^^^^^^^^^^^^^^^ meta.string.go string.quoted.backtick.go - source.sql
//language=t-sql
// <- comment.line.double-slash.go punctuation.definition.comment.go
//^^^^^^^^^^^^^^^ comment.line.double-slash.go
//^^^^^^^^ meta.annotation.identifier.go
// ^ meta.annotation keyword.operator.assignment.go
// ^^^^^ meta.annotation.parameters.go constant.other.language-name.go
sqlQuery = `
SELECT id
FROM ##global_temp_table;`
// ^^ punctuation.definition.variable
response := &http.Response{
StatusCode: http.StatusUnauthorized,
//language=json
Expand Down
2 changes: 1 addition & 1 deletion JavaScript/Embeddings/SQL (for JS template).sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# http://www.sublimetext.com/docs/syntax.html
# highlight tagged template strings
scope: source.sql.js-template
version: 1
version: 2
hidden: true

extends: Packages/SQL/SQL.sublime-syntax
Expand Down
2 changes: 1 addition & 1 deletion JavaScript/Embeddings/SQL (for TS template).sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# http://www.sublimetext.com/docs/syntax.html
# highlight tagged template strings
scope: source.sql.ts-template
version: 1
version: 2
hidden: true

extends: Packages/SQL/SQL.sublime-syntax
Expand Down
4 changes: 2 additions & 2 deletions JavaScript/tests/syntax_test_js_template.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,13 @@ var sql = sql`
/* ^ - source.sql.embedded */
SELECT *,
/* ^^^ keyword.other.DML.sql */
/* ^^^ keyword.other.dml.sql */
/* ^ constant.other.wildcard.asterisk.sql */
f.id AS database_id
/* ^^ keyword.operator.assignment.alias.sql */
FROM foo
WHERE f.a IS NULL
/* ^^ keyword.other.DML.sql */
/* ^^ keyword.other.dml.sql */
/* ^^ keyword.operator.logical.sql */
/* ^^^^ constant.language.null.sql */
AND f.b IS NOT NULL
Expand Down
4 changes: 2 additions & 2 deletions JavaScript/tests/syntax_test_typescript_template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,13 @@ var sql = sql`
/* ^ - source.sql.embedded */
SELECT *,
/* ^^^ keyword.other.DML.sql */
/* ^^^ keyword.other.dml.sql */
/* ^ constant.other.wildcard.asterisk.sql */
f.id AS database_id
/* ^^ keyword.operator.assignment.alias.sql */
FROM foo
WHERE f.a IS NULL
/* ^^ keyword.other.DML.sql */
/* ^^ keyword.other.dml.sql */
/* ^^ keyword.operator.logical.sql */
/* ^^^^ constant.language.null.sql */
AND f.b IS NOT NULL
Expand Down
19 changes: 15 additions & 4 deletions PHP/Embeddings/SQL (for PHP Interpolated).sublime-syntax
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
%YAML 1.2
---
scope: source.sql.interpolated.php
version: 2
hidden: true

extends: Packages/SQL/SQL.sublime-syntax
Expand All @@ -10,14 +11,24 @@ contexts:
- meta_prepend: true
- include: Packages/PHP/PHP Source.sublime-syntax#interpolations

main:
- meta_prepend: true
# prevent stray bracket highlighting
- match: \)
scope: punctuation.section.group.end.sql

single-identifier:
- meta_prepend: true
- meta_include_prototype: false
- include: Packages/PHP/PHP Source.sublime-syntax#interpolation

inside-like-single-quoted-string:
- meta_prepend: true
- include: Packages/PHP/PHP Source.sublime-syntax#string-interpolations
- include: string-interpolation

string-escape:
# this context is included in anonymous string contexts
inside-single-quoted-string:
- meta_prepend: true
- include: Packages/PHP/PHP Source.sublime-syntax#string-interpolations
- include: string-interpolation

string-interpolation:
# this context is included in anonymous string contexts
Expand Down
7 changes: 7 additions & 0 deletions PHP/Embeddings/SQL (for PHP).sublime-syntax
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
%YAML 1.2
---
scope: source.sql.embedded.php
version: 2
hidden: true

extends: Packages/SQL/SQL.sublime-syntax
Expand All @@ -11,6 +12,12 @@ contexts:
- include: php-single-quoted-strings
- include: php-string-single-quoted-escapes

main:
- meta_prepend: true
# prevent stray bracket highlighting
- match: \)
scope: punctuation.section.group.end.sql

php-string-single-quoted-escapes:
- match: \\[\\']
scope: constant.character.escape.php
Expand Down
18 changes: 18 additions & 0 deletions PHP/PHP Source.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -1811,6 +1811,24 @@ contexts:
- match: (?=\${{identifier_start}})
push: interpolation-literal-variable

interpolation:
# PHP variable/expressions interpolated into foreign
# source code without clearing any string scopes.
#
# Prevents: "c:\{$foo}"
- match: \\\{
# Handles: "foo{$bar}baz"
- match: \{(?=\$)
scope: punctuation.section.interpolation.begin.php
set: interpolation-braced-expression
# Handles: "foo${bar}baz"
- match: \$\{
scope: punctuation.definition.variable.begin.php
set: interpolation-braced-variable
# Handles: $foo, $foo[0], $foo[$bar], $foo->bar
- match: (?=\${{identifier_start}})
set: interpolation-literal-variable

interpolation-braced-expression:
- meta_include_prototype: false
- meta_scope: meta.interpolation.php
Expand Down
62 changes: 32 additions & 30 deletions PHP/tests/syntax_test_php.php
Original file line number Diff line number Diff line change
Expand Up @@ -5191,21 +5191,21 @@ function testTypeCasts()
// ^ meta.string.php string.quoted.double.php punctuation.definition.string.begin.php - meta.interpolation - string string
// ^^^^^^^^^^^^^^^^^^^^ meta.string.php source.sql.embedded.php - string.quoted.double.php
// ^ meta.string.php string.quoted.double.php punctuation.definition.string.end.php - meta.interpolation - string string
// ^^^^^^ keyword.other.create.sql
// ^^^^^^ keyword.other.ddl.sql

$sql = "
CREATE TABLE `version`...
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.string.php source.sql.embedded.php - string.quoted.double.php
// ^^^^^^ keyword.other.create.sql
// ^^^^^^ keyword.other.ddl.sql
";

// Do not highlight plain SQL indicator as SQL
$sql = "SELECT";
// ^^^^^^ - keyword.other.DML
// ^^^^^^ - keyword.other.dml

$sql = "
SELECT
// ^^^^^^ keyword.other.DML
// ^^^^^^ keyword.other.dml
*
FROM users
WHERE first_name = 'Eric'
Expand All @@ -5214,34 +5214,46 @@ function testTypeCasts()
$sql = "SELECT * FROM users WHERE first_name = 'Eric'";
// ^ meta.string.php string.quoted.double.php punctuation.definition.string.begin.php - meta.interpolation - string string
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.string.php source.sql.embedded.php - string.quoted.double.php
// ^ keyword.other.DML
// ^ keyword.other.dml
// ^^^^^^ string.quoted.single.sql
// ^ meta.string.php string.quoted.double.php punctuation.definition.string.end.php - meta.interpolation - string string

// Ensure we properly exist from SQL when hitting PHP end-of-string
$sql = "SELECT * FROM users WHERE first_name = 'Eric";
// ^ meta.string.php string.quoted.double.php punctuation.definition.string.begin.php - meta.interpolation - string string
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.string.php source.sql.embedded.php - string.quoted.double.php
// ^ keyword.other.DML
// ^ keyword.other.dml
// ^^^^^ string.quoted.single.sql
// ^ meta.string.php string.quoted.double.php punctuation.definition.string.end.php - meta.interpolation - string string

$sql = "
SELECT * FROM users WHERE first_name = 'Eric'
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.string.php source.sql.embedded.php - string.quoted.double.php
// ^ keyword.other.DML
// ^ keyword.other.dml
// ^^^^^ keyword.other.dml.sql
// ^^^^^^ string.quoted.single.sql
";
// <- meta.string.php string.quoted.double.php punctuation.definition.string.end.php - meta.interpolation - string string

$sql = "SELECT `$col` FROM 'my$table--name'";
// ^^^^^^ meta.column-name.sql
// ^ punctuation.definition.identifier.begin.sql
// ^^^^ meta.interpolation.php variable.other.php
// ^ punctuation.definition.identifier.end.sql
// ^^^^ keyword.other.dml.sql
// ^^^^^^^^^^^^^^^^ meta.table-name.sql
// ^ punctuation.definition.identifier.begin.sql
// ^^^^^^ meta.interpolation.php variable.other.php
// ^ punctuation.definition.identifier.end.sql

$sql = "SELECT * FROM users where first_name = $user_name";
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.string.php - meta.interpolation
// ^^^^^^^^^^ meta.string.php meta.interpolation.php variable.other.php - string
// ^^^^^^^^^^ meta.string.php meta.interpolation.php variable.other.php - string.quoted.double
// ^ meta.string.php - meta.interpolation

$sql = "SELECT * FROM users where first_name = '$user_name'";
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.string.php - meta.interpolation
// ^^^^^^^^^^ meta.string.php meta.interpolation.php variable.other.php - string
// ^^^^^^^^^^ meta.string.php meta.interpolation.php variable.other.php - string.quoted
// ^ meta.string.php - meta.interpolation

$sql = "SELECT * FROM users where first_name = `$user_name`";
Expand Down Expand Up @@ -5270,22 +5282,12 @@ function testTypeCasts()
// ^ meta.string.php - meta.interpolation
// ^^^^^^^^^^ - meta.string
// ^ meta.string.php - meta.interpolation
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.string.php source.sql.embedded.php
// ^ meta.string.php - meta.interpolation
// ^^^^^^^^^^^ - meta.string
// ^ meta.string.php - meta.interpolation
// ^ meta.string.php source.sql.embedded.php
// ^ meta.string.php - meta.interpolation
// ^^^^^ - meta.string
// ^^^^^^^^^^ meta.string.php string.quoted.double.php - meta.interpolation
// ^ string.quoted.double.php punctuation.definition.string.begin.php
// ^^^^^^ keyword.other.DML.sql
// ^ string.quoted.double.php punctuation.definition.string.end.php
// ^ keyword.operator.concatenation.php
// ^^^^ variable.other.php
// ^ keyword.operator.concatenation.php
// ^ string.quoted.double.php punctuation.definition.string.begin.php
// ^^^^^^ variable.other.php
// ^^^^^ meta.string.php source.sql.embedded.php - meta.interpolation
// ^^^^^^ meta.string.php source.sql.embedded.php meta.table-name.sql meta.interpolation.php
// ^^^^^^^^^^^^^^^ meta.string.php source.sql.embedded.php - meta.interpolation
// ^ punctuation.section.group.begin.sql
// ^^^^^^^^^^ meta.column-name.sql - variable
// ^ keyword.operator.comparison.sql
// ^ string.quoted.double.php punctuation.definition.string.end.php
// ^ keyword.operator.concatenation.php
// ^^^^^ variable.other.php
Expand Down Expand Up @@ -5331,7 +5333,7 @@ function testTypeCasts()
$sql = 'SELECT * FROM users WHERE first_name = \'Eric\'';
// ^ meta.string.php string.quoted.single.php punctuation.definition.string.begin.php - meta.interpolation - string string
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.string.php source.sql.embedded.php - string.quoted.single.php
// ^ keyword.other.DML
// ^ keyword.other.dml
// ^^^^^^^^ meta.string.sql string.quoted.single.sql
// ^^ constant.character.escape.php
// ^^ constant.character.escape.php
Expand All @@ -5340,7 +5342,7 @@ function testTypeCasts()
$sql = '
SELECT * FROM users WHERE first_name = \'Eric\'
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.string.php source.sql.embedded.php - string.quoted.single.php
// ^ keyword.other.DML
// ^ keyword.other.dml
// ^^ constant.character.escape.php
';
// <- meta.string.php string.quoted.single.php punctuation.definition.string.end.php - meta.interpolation - string string
Expand All @@ -5360,7 +5362,7 @@ function testTypeCasts()
// ^^^^^ - meta.string
// ^^^^^^^^^^ meta.string.php string.quoted.single.php - meta.interpolation
// ^ string.quoted.single.php punctuation.definition.string.begin.php
// ^^^^^^ keyword.other.DML.sql
// ^^^^^^ keyword.other.dml.sql
// ^ string.quoted.single.php punctuation.definition.string.end.php
// ^ keyword.operator.concatenation.php
// ^^^^ variable.other.php
Expand Down Expand Up @@ -5557,7 +5559,7 @@ function testTypeCasts()
// ^^^ entity.name.tag.heredoc
SELECT * FROM users WHERE first_name = 'John' LIMIT $limit
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.embedded.sql source.sql.embedded.php
// <- keyword.other.DML
// <- keyword.other.dml
// ^ constant.other.wildcard.asterisk
// ^^^^^^ string.quoted.single
// ^^^^^^ variable.other.php
Expand All @@ -5574,7 +5576,7 @@ function testTypeCasts()
// ^^^ entity.name.tag.heredoc
SELECT * FROM users WHERE first_name = 'John'\n
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.embedded.sql source.sql.embedded.php
// <- keyword.other.DML
// <- keyword.other.dml
// ^ constant.other.wildcard.asterisk
// ^^^^^^ string.quoted.single
// ^^ - constant.character.escape.php
Expand Down
Loading

0 comments on commit 15ccade

Please sign in to comment.