-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option for specifying case for identifiers (#663)
- Loading branch information
Showing
11 changed files
with
239 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# identifierCase | ||
|
||
Converts identifiers to upper or lowercase. | ||
|
||
Note: An identifier is a name of a SQL object. | ||
There are two types of SQL identifiers: ordinary identifiers and quoted identifiers. | ||
Only ordinary identifiers are subject to be converted. | ||
|
||
## Options | ||
|
||
- `"preserve"` (default) preserves the original case. | ||
- `"upper"` converts to uppercase. | ||
- `"lower"` converts to lowercase. | ||
|
||
### preserve | ||
|
||
``` | ||
select | ||
count(a.Column1), | ||
max(a.Column2 + a.Column3), | ||
a.Column4 AS myCol | ||
from | ||
Table1 as a | ||
where | ||
Column6 | ||
and Column7 | ||
group by Column4 | ||
``` | ||
|
||
### upper | ||
|
||
``` | ||
select | ||
count(A.COLUMN1), | ||
max(A.COLUMN2 + A.COLUMN3), | ||
A.COLUMN4 AS MYCOL | ||
from | ||
TABLE1 as A | ||
where | ||
COLUMN6 | ||
and COLUMN7 | ||
group by COLUMN4 | ||
``` | ||
|
||
### lower | ||
|
||
``` | ||
select | ||
count(a.column1), | ||
max(a.column2 + a.column3), | ||
a.column4 AS mycol | ||
from | ||
table1 as a | ||
where | ||
column6 | ||
and column7 | ||
group by column4 | ||
``` |
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
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
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,81 @@ | ||
import dedent from 'dedent-js'; | ||
|
||
import { FormatFn } from '../../src/sqlFormatter.js'; | ||
|
||
export default function supportsIdentifierCase(format: FormatFn) { | ||
it('preserves identifier case by default', () => { | ||
const result = format( | ||
dedent` | ||
select Abc, 'mytext' as MyText from tBl1 left join Tbl2 where colA > 1 and colB = 3` | ||
); | ||
expect(result).toBe(dedent` | ||
select | ||
Abc, | ||
'mytext' as MyText | ||
from | ||
tBl1 | ||
left join Tbl2 | ||
where | ||
colA > 1 | ||
and colB = 3 | ||
`); | ||
}); | ||
|
||
it('converts identifiers to uppercase', () => { | ||
const result = format( | ||
dedent` | ||
select Abc, 'mytext' as MyText from tBl1 left join Tbl2 where colA > 1 and colB = 3`, | ||
{ identifierCase: 'upper' } | ||
); | ||
expect(result).toBe(dedent` | ||
select | ||
ABC, | ||
'mytext' as MYTEXT | ||
from | ||
TBL1 | ||
left join TBL2 | ||
where | ||
COLA > 1 | ||
and COLB = 3 | ||
`); | ||
}); | ||
|
||
it('converts identifiers to lowercase', () => { | ||
const result = format( | ||
dedent` | ||
select Abc, 'mytext' as MyText from tBl1 left join Tbl2 where colA > 1 and colB = 3`, | ||
{ identifierCase: 'lower' } | ||
); | ||
expect(result).toBe(dedent` | ||
select | ||
abc, | ||
'mytext' as mytext | ||
from | ||
tbl1 | ||
left join tbl2 | ||
where | ||
cola > 1 | ||
and colb = 3 | ||
`); | ||
}); | ||
|
||
it('does not uppercase quoted identifiers', () => { | ||
const result = format(`select "abc" as foo`, { | ||
identifierCase: 'upper', | ||
}); | ||
expect(result).toBe(dedent` | ||
select | ||
"abc" as FOO | ||
`); | ||
}); | ||
|
||
it('converts multi-part identifiers to uppercase', () => { | ||
const result = format('select Abc from Part1.Part2.Part3', { identifierCase: 'upper' }); | ||
expect(result).toBe(dedent` | ||
select | ||
ABC | ||
from | ||
PART1.PART2.PART3 | ||
`); | ||
}); | ||
} |
Oops, something went wrong.