Skip to content

Commit 6872c07

Browse files
committed
feat(devins-lang): add support for parsing and verifying SQL scripts before inserting them into the editor.
This commit introduces the ability to parse and verify SQL scripts before they are inserted into the editor, ensuring that the scripts are syntactically correct. The changes include the addition of support for the SqlLanguage and the use of the SqlFile class for parsing SQL scripts. The test cases have been updated to verify the syntax of the SQL scripts using a custom SqlSyntaxCheckingVisitor class.
1 parent 29b6e61 commit 6872c07

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

exts/database/src/main/kotlin/cc/unitmesh/database/flow/AutoSqlBackgroundTask.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ package cc.unitmesh.database.flow
22

33
import cc.unitmesh.devti.AutoDevBundle
44
import cc.unitmesh.devti.util.parser.parseCodeFromString
5-
import com.intellij.database.console.DatabaseRunners
6-
import com.intellij.database.console.runConfiguration.DatabaseScriptRunner
7-
import com.intellij.database.util.DasUtil
85
import com.intellij.lang.Language
96
import com.intellij.openapi.command.WriteCommandAction
107
import com.intellij.openapi.diagnostic.logger
@@ -13,7 +10,6 @@ import com.intellij.openapi.progress.ProgressIndicator
1310
import com.intellij.openapi.progress.Task
1411
import com.intellij.openapi.project.Project
1512
import com.intellij.psi.PsiFileFactory
16-
import com.intellij.sql.psi.SqlLanguage
1713

1814
class AutoSqlBackgroundTask(
1915
private val project: Project,
Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package cc.unitmesh.database.flow;
22

3+
import com.intellij.psi.PsiElement
4+
import com.intellij.psi.PsiErrorElement
35
import com.intellij.psi.PsiFileFactory
6+
import com.intellij.sql.psi.SqlFile
47
import com.intellij.sql.psi.SqlLanguage
58
import com.intellij.testFramework.LightPlatformTestCase
69

@@ -9,17 +12,35 @@ class AutoSqlBackgroundTaskTest: LightPlatformTestCase() {
912
fun testShouldParseCode() {
1013
// Given
1114
val code = """
12-
SELECT * FROM table where;
15+
SELECT * FROM table where id =;
1316
""".trimIndent()
14-
//
15-
// val inspectionManager = InspectionManager.getInstance(project)
16-
// inspectionManager.
1717

1818
// When
19-
val psiFile =
19+
val sqlFile: SqlFile =
2020
PsiFileFactory.getInstance(project).createFileFromText("temp.sql", SqlLanguage.INSTANCE, code)
21+
as SqlFile
2122

22-
// Then
23+
// verify sqlFile syntax correct
24+
// Verify
25+
val errors = mutableListOf<String>()
26+
val visitor = object : SqlSyntaxCheckingVisitor() {
27+
override fun visitElement(element: PsiElement) {
28+
if (element is PsiErrorElement) {
29+
errors.add("Syntax error at position ${element.textRange.startOffset}: ${element.errorDescription}")
30+
}
31+
super.visitElement(element)
32+
}
33+
}
34+
sqlFile.accept(visitor)
2335

36+
// err msg: SQL syntax contains errors: Syntax error at position 30: <expression>, ALL, ANY or SOME expected, got ';'
37+
assertTrue(errors.isNotEmpty())
38+
assertEquals("Syntax error at position 30: <expression>, ALL, ANY or SOME expected, got ';'", errors[0])
2439
}
25-
}
40+
41+
abstract class SqlSyntaxCheckingVisitor : com.intellij.psi.PsiElementVisitor() {
42+
override fun visitElement(element: PsiElement) {
43+
element.children.forEach { it.accept(this) }
44+
}
45+
}
46+
}

exts/ext-harmonyos/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ openharmony/lib/ohos-cpp-lsp-client-3.1.0.501.jar, 自研 C++ PSI 模块
77
JS 类:`class class com.huawei.ace.language.psi.impl.JavaScriptIdentifierNameImpl`
88
CPP 类:`class class com.huawei.ideacpp.psi.impl.CPPIdentifierImpl`
99

10+
Todos:
11+
12+
- [ ] Regenerate code in Syntax error part.
13+
1014
## 设计思念
1115

1216
三个新要素:新的语言、遗留系统迁移、新的 UI 框架。

0 commit comments

Comments
 (0)