Skip to content

Commit f3f3b7f

Browse files
authored
Merge pull request #316 from domaframework/feature/sql-format-support-functions
Implement SQL Formatting Rules for Function Names
2 parents 8fe2ce5 + 2ef5af6 commit f3f3b7f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1645
-730
lines changed

src/main/java/org/domaframework/doma/intellij/Sql.bnf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ private item ::= (comment | literal | word |
9494
BOOLEAN )
9595
private comment ::= (block_comment | LINE_COMMENT)
9696
private literal ::= (STRING | NUMBER)
97-
private word ::= (KEYWORD | WORD | DATATYPE)
97+
private word ::= (KEYWORD | WORD | FUNCTION_NAME | DATATYPE)
9898
block_comment ::= "/*" (el_directive | BLOCK_COMMENT_CONTENT?) "*/" {
9999
pin=1
100100
mixin="org.domaframework.doma.intellij.psi.SqlElCommentExprImpl"

src/main/java/org/domaframework/doma/intellij/Sql.flex

Lines changed: 16 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import com.intellij.lexer.FlexLexer;
77

88
import com.intellij.psi.TokenType;
99
import com.intellij.psi.tree.IElementType;
10+
import org.domaframework.doma.intellij.SqlTokenHelper;
1011

12+
import org.domaframework.doma.intellij.psi.SqlTokenType;
1113
import org.domaframework.doma.intellij.psi.SqlTypes;
1214

1315
%%
@@ -19,132 +21,19 @@ import org.domaframework.doma.intellij.psi.SqlTypes;
1921
%function advance
2022
%type IElementType
2123
%{
22-
// SQL keywords
23-
private static final Set<String> KEYWORDS = Set.of(
24-
"add",
25-
"after",
26-
"alter",
27-
"all",
28-
"and",
29-
"as",
30-
"asc",
31-
"between",
32-
"breadth",
33-
"by",
34-
"case",
35-
"change",
36-
"check",
37-
"conflict",
38-
"constraint",
39-
"column",
40-
"collate",
41-
"comment",
42-
"create",
43-
"cross",
44-
"cycle",
45-
"database",
46-
"default",
47-
"delete",
48-
"desc",
49-
"distinct",
50-
"do",
51-
"drop",
52-
"else",
53-
"end",
54-
"except",
55-
"exists",
56-
"first",
57-
"foreign",
58-
"from",
59-
"full",
60-
"group",
61-
"having",
62-
"if",
63-
"in",
64-
"index",
65-
"inner",
66-
"insert",
67-
"intersect",
68-
"into",
69-
"is",
70-
"join",
71-
"key",
72-
"lateral",
73-
"left",
74-
"like",
75-
"limit",
76-
"not",
77-
"nothing",
78-
"null",
79-
"materialized",
80-
"modify",
81-
"offset",
82-
"on",
83-
"outer",
84-
"or",
85-
"order",
86-
"primary",
87-
"references",
88-
"rename",
89-
"returning",
90-
"recursive",
91-
"right",
92-
"search",
93-
"select",
94-
"set",
95-
"table",
96-
"temporary",
97-
"then",
98-
"to",
99-
"truncate",
100-
"union",
101-
"unique",
102-
"update",
103-
"using",
104-
"values",
105-
"view",
106-
"when",
107-
"where",
108-
"with"
109-
);
110-
111-
// COLUMN DataTypes
112-
private static final Set<String> DATATYPES = Set.of(
113-
"int",
114-
"integer",
115-
"smallint",
116-
"bigint",
117-
"tinyint",
118-
"float",
119-
"double",
120-
"decimal",
121-
"numeric",
122-
"char",
123-
"varchar",
124-
"text",
125-
"date",
126-
"time",
127-
"timestamp",
128-
"datetime",
129-
"boolean",
130-
"bit",
131-
"binary",
132-
"varbinary",
133-
"blob",
134-
"clob",
135-
"json",
136-
"enum",
137-
"set"
138-
);
139-
140-
private static boolean isKeyword(CharSequence word) {
141-
// TODO Reads plugin settings and allows users to register arbitrary keywords
142-
return KEYWORDS.contains(word.toString().toLowerCase());
143-
}
144-
145-
private static boolean isColumnDataType(CharSequence word) {
146-
return DATATYPES.contains(word.toString().toLowerCase());
147-
}
24+
private static boolean isKeyword(CharSequence word) {
25+
// TODO Reads plugin settings and allows users to register arbitrary keywords
26+
return SqlTokenHelper.getKeyword().contains(word.toString().toLowerCase());
27+
}
28+
29+
private static boolean isColumnDataType(CharSequence word) {
30+
return SqlTokenHelper.getDataTypeTokens().contains(word.toString().toLowerCase());
31+
}
32+
33+
private static boolean isWindowFunction(CharSequence word) {
34+
// TODO Reads plugin settings and allows users to register arbitrary function names
35+
return SqlTokenHelper.getFunctionTokens().contains(word.toString().toLowerCase());
36+
}
14837
%}
14938

15039
%eof{ return;
@@ -181,7 +70,7 @@ El_NonWordPart = [=<>\-,/*();\R \n\t\f]
18170
{LineComment} { return SqlTypes.LINE_COMMENT; }
18271
{String} { return SqlTypes.STRING; }
18372
{Number} { return SqlTypes.NUMBER; }
184-
{Word} { return isKeyword(yytext()) ? SqlTypes.KEYWORD : isColumnDataType(yytext()) ? SqlTypes.DATATYPE : SqlTypes.WORD; }
73+
{Word} { return isKeyword(yytext()) ? SqlTypes.KEYWORD : isColumnDataType(yytext()) ? SqlTypes.DATATYPE : isWindowFunction(yytext()) ? SqlTypes.FUNCTION_NAME : SqlTypes.WORD; }
18574
"." { return SqlTypes.DOT; }
18675
"," { return SqlTypes.COMMA; }
18776
"+" { return SqlTypes.PLUS;}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright Doma Tools Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.domaframework.doma.intellij;
17+
18+
import java.util.HashSet;
19+
import java.util.Set;
20+
import org.domaframework.doma.intellij.tokens.MySqlFunctionToken;
21+
import org.domaframework.doma.intellij.tokens.OracleFunctionToken;
22+
import org.domaframework.doma.intellij.tokens.PostgresSqlFunctionToken;
23+
import org.domaframework.doma.intellij.tokens.SqlDataTypeTokenUtil;
24+
import org.domaframework.doma.intellij.tokens.SqlFunctionToken;
25+
import org.domaframework.doma.intellij.tokens.SqlKeywordTokenUtil;
26+
27+
public class SqlTokenHelper {
28+
29+
static final Set<String> KEYWORD_TOKENS = new HashSet<>();
30+
static final Set<String> DATA_TYPE_TOKENS = new HashSet<>();
31+
static final Set<String> FUNCTION_TOKENS = new HashSet<>();
32+
33+
static {
34+
// Initialize keyword tokens
35+
KEYWORD_TOKENS.addAll(SqlKeywordTokenUtil.getTokens());
36+
37+
// Initialize DataType tokens
38+
DATA_TYPE_TOKENS.addAll(SqlDataTypeTokenUtil.getTokens());
39+
40+
// Initialize function tokens
41+
FUNCTION_TOKENS.addAll(SqlFunctionToken.getTokens());
42+
FUNCTION_TOKENS.addAll(PostgresSqlFunctionToken.getTokens());
43+
FUNCTION_TOKENS.addAll(MySqlFunctionToken.getTokens());
44+
FUNCTION_TOKENS.addAll(OracleFunctionToken.getTokens());
45+
}
46+
47+
// Keywords
48+
public static Set<String> getKeyword() {
49+
return KEYWORD_TOKENS;
50+
}
51+
52+
// Data Types
53+
public static Set<String> getDataTypeTokens() {
54+
return DATA_TYPE_TOKENS;
55+
}
56+
57+
// Functions
58+
public static Set<String> getFunctionTokens() {
59+
return FUNCTION_TOKENS;
60+
}
61+
}

src/main/java/org/domaframework/doma/intellij/highlighter/SqlColorSettingsPage.java

Lines changed: 57 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class SqlColorSettingsPage implements ColorSettingsPage {
3737
new AttributesDescriptor("SQL//Literal//String", SqlSyntaxHighlighter.STRING),
3838
new AttributesDescriptor("SQL//Literal//Number", SqlSyntaxHighlighter.NUMBER),
3939
new AttributesDescriptor("SQL//Syntax//Keyword", SqlSyntaxHighlighter.KEYWORD),
40+
new AttributesDescriptor("SQL//Syntax//Function name", SqlSyntaxHighlighter.FUNCTION_NAME),
4041
new AttributesDescriptor("SQL//Syntax//DataType", SqlSyntaxHighlighter.DATATYPE),
4142
new AttributesDescriptor("SQL//Syntax//Other", SqlSyntaxHighlighter.OTHER),
4243
new AttributesDescriptor("SQL//Syntax//Word", SqlSyntaxHighlighter.WORD),
@@ -106,59 +107,64 @@ public class SqlColorSettingsPage implements ColorSettingsPage {
106107
/**
107108
* Set highlights as you like
108109
*/
109-
update product set /*%populate*/
110-
category = /* category */'category',
111-
expected_sales = /* price * pieces */0,
112-
unit_price = /* purchase / pieces */0,
113-
/*%if mark != "XXX" */
114-
mark = /* mark */'mark',
115-
/*%end */
116-
/*%! This comment delete */
117-
employee_type = (
118-
select /*%expand "e" */* from employee e
119-
where
120-
/*%for name : names */
121-
orderer = /* name */'orderer'
122-
/*%if name.startWith("AA") && name.contains("_A") */
123-
AND div = 'A'
124-
AND rank >= 5
125-
/*%elseif name.startWith("BB") || name.contains("_B") */
126-
AND div = 'B'
127-
AND rank > 2
128-
AND rank < 5
129-
/*%else */
130-
AND div = 'C'
131-
/*%end */
132-
/*%if name_has_next */
133-
/*# "OR" */
134-
/*%end */
135-
/*%end*/
136-
)
137-
where
138-
type = /* @[email protected]() */'type'
139-
and cost_limit <= /* cost + 100 */0
140-
and cost_limit >= /* cost - 100 */99999;
110+
UPDATE product
111+
SET /*%populate*/
112+
category = /* category */'category'
113+
, expected_sales = /* price * pieces */0
114+
, unit_price = /* purchase / pieces */0
115+
/*%if mark != "XXX" */
116+
, mark = /* mark */'mark'
117+
/*%end */
118+
/*%! This comment delete */
119+
, employee_type = ( SELECT /*%expand "e" */*
120+
FROM employee e
121+
WHERE
122+
/*%for name : names */
123+
orderer = /* name */'orderer'
124+
/*%if name.startWith("AA") && name.contains("_A") */
125+
AND div = 'A'
126+
AND rank >= 5
127+
/*%elseif name.startWith("BB") || name.contains("_B") */
128+
AND div = 'B'
129+
AND rank > 2
130+
AND rank < 5
131+
/*%else */
132+
AND div = 'C'
133+
/*%end */
134+
/*%if name_has_next */
135+
/*# "OR" */
136+
/*%end */
137+
/*%end*/ )
138+
WHERE type = /* @[email protected]() */'type'
139+
AND cost_limit <= /* cost + 100 */0
140+
AND cost_limit >= /* cost - 100 */99999;
141141
142142
-- Demo Text2
143-
select p.project_id
144-
, p.project_name
145-
, p.project_type
146-
, p.project_category
147-
, p.pre_project
148-
from project p
149-
where p.project_type = /* new Project().type */'type'
150-
and(
151-
/*%for project : preProjects */
152-
/*%if project.category != null */
153-
project_category = /* project.category.plus('t') */'category'
154-
/*%elseif project.pre == true */
155-
pre_project = /* project.preProjectId */0
156-
/*%if project_has_next */
157-
/*# "OR" */
158-
/*%end */
159-
/*%end */
160-
/*%end */
161-
)
143+
SELECT p.project_id
144+
, p.project_name
145+
, p.project_type
146+
, p.project_category
147+
, p.pre_project
148+
FROM project p
149+
WHERE p.project_type = /* new Project().type */'type'
150+
AND (
151+
/*%for project : preProjects */
152+
/*%if project.category != null */
153+
project_category = /* project.category.plus('t') */'category'
154+
/*%elseif project.pre == true */
155+
pre_project = /* project.preProjectId */0
156+
/*%if project_has_next */
157+
/*# "OR" */
158+
/*%end */
159+
/*%end */
160+
/*%end */)
161+
162+
-- DemoText3
163+
SELECT common
164+
, amount
165+
, date
166+
, SUM(amount) OVER(PARTITION BY common ORDER BY date) AS common_amount
167+
FROM ammount_table
162168
""";
163169
}
164170

src/main/java/org/domaframework/doma/intellij/highlighter/SqlSyntaxHighlighter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public class SqlSyntaxHighlighter extends SyntaxHighlighterBase {
3838
createTextAttributesKey("DOMA_SQL_WORD", DefaultLanguageHighlighterColors.IDENTIFIER);
3939
public static final TextAttributesKey DATATYPE =
4040
createTextAttributesKey("DOMA_SQL_DATATYPE", DefaultLanguageHighlighterColors.METADATA);
41+
public static final TextAttributesKey FUNCTION_NAME =
42+
createTextAttributesKey(
43+
"DOMA_SQL_FUNCTION_NAME", DefaultLanguageHighlighterColors.FUNCTION_DECLARATION);
4144
public static final TextAttributesKey STRING =
4245
createTextAttributesKey("DOMA_SQL_STRING", DefaultLanguageHighlighterColors.STRING);
4346
public static final TextAttributesKey NUMBER =
@@ -141,6 +144,7 @@ public class SqlSyntaxHighlighter extends SyntaxHighlighterBase {
141144
static {
142145
map.put(SqlTypes.KEYWORD, KEYWORD);
143146
map.put(SqlTypes.DATATYPE, DATATYPE);
147+
map.put(SqlTypes.FUNCTION_NAME, FUNCTION_NAME);
144148
map.put(SqlTypes.STRING, STRING);
145149
map.put(SqlTypes.OTHER, OTHER);
146150
map.put(SqlTypes.WORD, WORD);

0 commit comments

Comments
 (0)