Skip to content

Commit 26180fc

Browse files
committed
sql parse
1 parent 1e9102a commit 26180fc

11 files changed

Lines changed: 1775 additions & 7 deletions

chat2db-server/chat2db-spi/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@
8585
<groupId>org.mongodb</groupId>
8686
<artifactId>bson</artifactId>
8787
</dependency>
88+
<dependency>
89+
<groupId>org.antlr</groupId>
90+
<artifactId>antlr4</artifactId>
91+
<version>4.9.1</version>
92+
</dependency>
93+
<dependency>
94+
<groupId>com.oceanbase</groupId>
95+
<artifactId>ob-sql-parser</artifactId>
96+
<version>1.2.1</version>
97+
</dependency>
8898
</dependencies>
8999
<build>
90100
<plugins>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2023 OceanBase.
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+
* http://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 ai.chat2db.spi.util;
17+
18+
import com.oceanbase.tools.sqlparser.oboracle.PLLexer;
19+
import com.oceanbase.tools.sqlparser.oracle.PlSqlLexer;
20+
import org.antlr.v4.runtime.Lexer;
21+
22+
public class LexerFactories {
23+
private static final LexerFactory OB_ORACLE_LEXER_FACTORY = new OBOraclePLLexerFactory();
24+
private static final LexerFactory ORACLE_LEXER_FACTORY = new OracleLexerFactory();
25+
26+
public static LexerFactory of(Class<? extends Lexer> lexerType) {
27+
if (PLLexer.class == lexerType) {
28+
return OB_ORACLE_LEXER_FACTORY;
29+
}
30+
if (PlSqlLexer.class == lexerType) {
31+
return ORACLE_LEXER_FACTORY;
32+
}
33+
throw new RuntimeException("LexerType not supported, lexerType=" + lexerType.getName());
34+
}
35+
36+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2023 OceanBase.
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+
* http://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 ai.chat2db.spi.util;
17+
18+
import org.antlr.v4.runtime.CharStream;
19+
import org.antlr.v4.runtime.Lexer;
20+
21+
interface LexerFactory {
22+
Lexer create(CharStream input);
23+
}
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
/*
2+
* Copyright (c) 2023 OceanBase.
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+
* http://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 ai.chat2db.spi.util;
17+
18+
/**
19+
* SQL 语法 Token 定义。<br>
20+
* 如果某个 Token 不支持,对应方法返回 1 {@link org.antlr.v4.runtime.Token#MIN_USER_TOKEN_TYPE}
21+
*/
22+
interface LexerTokenDefinition {
23+
/**
24+
* 除号
25+
*/
26+
int DIV();
27+
28+
/**
29+
* 空白字符,包括空格、制表符、换行符等
30+
*/
31+
int SPACES();
32+
33+
/**
34+
* OB 语法文件不区分单行注释和多行注释,统一为 ANTLR_SKIP
35+
*/
36+
int ANTLR_SKIP();
37+
38+
/**
39+
* 单行注释
40+
*/
41+
int SINGLE_LINE_COMMENT();
42+
43+
/**
44+
* 多行注释
45+
*/
46+
int MULTI_LINE_COMMENT();
47+
48+
/**
49+
* DECLARE 关键字
50+
*/
51+
int DECLARE();
52+
53+
/**
54+
* BEGIN 关键字
55+
*/
56+
int BEGIN();
57+
58+
/**
59+
* END 关键字
60+
*/
61+
int END();
62+
63+
/**
64+
* CREATE 关键字
65+
*/
66+
int CREATE();
67+
68+
/**
69+
* OR 关键字
70+
*/
71+
int OR();
72+
73+
/**
74+
* REPLACE 关键字
75+
*/
76+
int REPLACE();
77+
78+
/**
79+
* EDITIONABLE 关键字
80+
*/
81+
int EDITIONABLE();
82+
83+
/**
84+
* NONEDITIONABLE 关键字
85+
*/
86+
int NONEDITIONABLE();
87+
88+
/**
89+
* PROCEDURE 关键字
90+
*/
91+
int PROCEDURE();
92+
93+
/**
94+
* FUNCTION 关键字
95+
*/
96+
int FUNCTION();
97+
98+
/**
99+
* PACKAGE 关键字
100+
*/
101+
int PACKAGE();
102+
103+
/**
104+
* TYPE 关键字
105+
*/
106+
int TYPE();
107+
108+
/**
109+
* TRIGGER 关键字
110+
*/
111+
int TRIGGER();
112+
113+
/**
114+
* BODY 关键字
115+
*/
116+
int BODY();
117+
118+
/**
119+
* IDENT, may {@link #REGULAR_ID} OR {@link #DELIMITED_ID}
120+
*/
121+
int IDENT();
122+
123+
int REGULAR_ID();
124+
125+
int DELIMITED_ID();
126+
127+
int FOR();
128+
129+
/**
130+
* LOOP 关键字
131+
*/
132+
int LOOP();
133+
134+
/**
135+
* IF 关键字
136+
*/
137+
int IF();
138+
139+
/**
140+
* CASE 关键字
141+
*/
142+
int CASE();
143+
144+
/**
145+
* LANGUAGE 关键字
146+
*/
147+
int LANGUAGE();
148+
149+
/**
150+
* EXTERNAL 关键字
151+
*/
152+
int EXTERNAL();
153+
154+
/**
155+
* IS 关键字
156+
*/
157+
int IS();
158+
159+
/**
160+
* AS 关键字
161+
*/
162+
int AS();
163+
164+
/**
165+
* MEMBER 关键字
166+
*/
167+
int MEMBER();
168+
169+
/**
170+
* STATIC 关键字
171+
*/
172+
int STATIC();
173+
174+
int SEMICOLON();
175+
176+
int ELSE();
177+
178+
int THEN();
179+
180+
int RIGHTBRACKET();
181+
182+
int LEFTBRACKET();
183+
184+
int GREATER_THAN_OP();
185+
186+
int WHILE();
187+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2023 OceanBase.
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+
* http://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 ai.chat2db.spi.util;
17+
18+
import com.oceanbase.tools.sqlparser.oboracle.PLLexer;
19+
import com.oceanbase.tools.sqlparser.oracle.PlSqlLexer;
20+
import org.antlr.v4.runtime.Lexer;
21+
22+
class LexerTokenDefinitions {
23+
private static final LexerTokenDefinition OB_PL_LEXER_DEFINITION = new OBOraclePLLexerDefinition();
24+
private static final LexerTokenDefinition ORACLE_LEXER_DEFINITION = new OracleLexerDefinition();
25+
26+
public static LexerTokenDefinition of(Class<? extends Lexer> lexerType) {
27+
if (PLLexer.class == lexerType) {
28+
return OB_PL_LEXER_DEFINITION;
29+
}
30+
if (PlSqlLexer.class == lexerType) {
31+
return ORACLE_LEXER_DEFINITION;
32+
}
33+
throw new RuntimeException("LexerType not supported, lexerType=" + lexerType.getName());
34+
}
35+
36+
}

0 commit comments

Comments
 (0)