Skip to content

Commit e679ca4

Browse files
committed
Add SqlKeyword Management Common Util
1 parent 82db584 commit e679ca4

File tree

1 file changed

+182
-0
lines changed

1 file changed

+182
-0
lines changed
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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.formatter
17+
18+
class SqlKeywordUtil {
19+
companion object {
20+
val TOP_KEYWORDS: Set<String> =
21+
setOf(
22+
"select",
23+
"update",
24+
"insert",
25+
"delete",
26+
"drop",
27+
"alter",
28+
"create",
29+
"truncate",
30+
"rename",
31+
"union",
32+
)
33+
34+
fun isTopKeyword(keyword: String): Boolean = TOP_KEYWORDS.contains(keyword.lowercase())
35+
36+
val SECOND_KEYWORD =
37+
setOf(
38+
"from",
39+
"where",
40+
"order",
41+
"group",
42+
"having",
43+
"limit",
44+
"and",
45+
"or",
46+
)
47+
48+
fun isSecondKeyword(keyword: String): Boolean = SECOND_KEYWORD.contains(keyword.lowercase())
49+
50+
val ATTACHED_KEYWORD =
51+
setOf(
52+
"distinct",
53+
"table",
54+
"left",
55+
"right",
56+
"outer",
57+
"inner",
58+
"join",
59+
)
60+
61+
fun isAttachedKeyword(keyword: String): Boolean = ATTACHED_KEYWORD.contains(keyword.lowercase())
62+
63+
val THIRD_KEYWORDS =
64+
setOf(
65+
"add",
66+
"between",
67+
"modify",
68+
"column",
69+
)
70+
71+
fun isThirdKeyword(keyword: String): Boolean = THIRD_KEYWORDS.contains(keyword.lowercase())
72+
73+
val COLUMN_TYPE_KEYWORDS =
74+
setOf(
75+
"int",
76+
"integer",
77+
"smallint",
78+
"bigint",
79+
"tinyint",
80+
"float",
81+
"double",
82+
"decimal",
83+
"numeric",
84+
"char",
85+
"varchar",
86+
"text",
87+
"date",
88+
"time",
89+
"timestamp",
90+
"datetime",
91+
"boolean",
92+
"bit",
93+
"binary",
94+
"varbinary",
95+
"blob",
96+
"clob",
97+
"json",
98+
"enum",
99+
"set",
100+
)
101+
102+
fun isColumnTypeKeyword(keyword: String): Boolean = COLUMN_TYPE_KEYWORDS.contains(keyword.lowercase())
103+
104+
val LITERAL_KEYWORDS =
105+
setOf(
106+
"null",
107+
"true",
108+
"false",
109+
"current_date",
110+
)
111+
112+
fun isLiteralKeyword(keyword: String): Boolean = LITERAL_KEYWORDS.contains(keyword.lowercase())
113+
114+
val ATTRIBUTE_KEYWORD =
115+
setOf(
116+
"default",
117+
"index",
118+
"key",
119+
"unique",
120+
"primary",
121+
"foreign",
122+
"constraint",
123+
)
124+
125+
fun isAttributeKeyword(keyword: String): Boolean = ATTRIBUTE_KEYWORD.contains(keyword.lowercase())
126+
127+
val INLINE_PARENT_SQL_KEYWORDS =
128+
setOf(
129+
"if",
130+
"case",
131+
"end",
132+
)
133+
134+
fun isInlineParentSqlKeyword(keyword: String): Boolean = INLINE_PARENT_SQL_KEYWORDS.contains(keyword.lowercase())
135+
136+
val INLINE_SQL_KEYWORDS =
137+
setOf(
138+
"when",
139+
"else",
140+
)
141+
142+
fun isInlineSqlKeyword(keyword: String): Boolean = INLINE_SQL_KEYWORDS.contains(keyword.lowercase())
143+
144+
val OPTION_SQL_KEYWORDS =
145+
setOf(
146+
"as",
147+
"by",
148+
"to",
149+
"asc",
150+
"desc",
151+
"all",
152+
"check",
153+
"exists",
154+
"full",
155+
"is",
156+
"like",
157+
"offset",
158+
"then",
159+
"values",
160+
"in",
161+
)
162+
163+
fun isOptionSqlKeyword(keyword: String): Boolean = OPTION_SQL_KEYWORDS.contains(keyword.lowercase())
164+
165+
val SET_LINE_KEYWORDS =
166+
mapOf(
167+
"into" to setOf("insert"),
168+
"from" to setOf("delete"),
169+
"join" to setOf("outer", "inner", "left", "right"),
170+
"outer" to setOf("left", "right"),
171+
"inner" to setOf("left", "right"),
172+
"by" to setOf("group", "order"),
173+
"and" to setOf("between"),
174+
"if" to setOf("table"),
175+
)
176+
177+
fun isSetLineKeyword(
178+
keyword: String,
179+
prevKeyword: String,
180+
): Boolean = SET_LINE_KEYWORDS[keyword.lowercase()]?.contains(prevKeyword.lowercase()) == true
181+
}
182+
}

0 commit comments

Comments
 (0)