Skip to content

Commit

Permalink
feat(devins-lang): add support for parsing and verifying SQL scripts …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
phodal committed Apr 1, 2024
1 parent 29b6e61 commit 6872c07
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
Expand Up @@ -2,9 +2,6 @@ package cc.unitmesh.database.flow

import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.util.parser.parseCodeFromString
import com.intellij.database.console.DatabaseRunners
import com.intellij.database.console.runConfiguration.DatabaseScriptRunner
import com.intellij.database.util.DasUtil
import com.intellij.lang.Language
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.diagnostic.logger
Expand All @@ -13,7 +10,6 @@ import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.progress.Task
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiFileFactory
import com.intellij.sql.psi.SqlLanguage

class AutoSqlBackgroundTask(
private val project: Project,
Expand Down
@@ -1,6 +1,9 @@
package cc.unitmesh.database.flow;

import com.intellij.psi.PsiElement
import com.intellij.psi.PsiErrorElement
import com.intellij.psi.PsiFileFactory
import com.intellij.sql.psi.SqlFile
import com.intellij.sql.psi.SqlLanguage
import com.intellij.testFramework.LightPlatformTestCase

Expand All @@ -9,17 +12,35 @@ class AutoSqlBackgroundTaskTest: LightPlatformTestCase() {
fun testShouldParseCode() {
// Given
val code = """
SELECT * FROM table where;
SELECT * FROM table where id =;
""".trimIndent()
//
// val inspectionManager = InspectionManager.getInstance(project)
// inspectionManager.

// When
val psiFile =
val sqlFile: SqlFile =
PsiFileFactory.getInstance(project).createFileFromText("temp.sql", SqlLanguage.INSTANCE, code)
as SqlFile

// Then
// verify sqlFile syntax correct
// Verify
val errors = mutableListOf<String>()
val visitor = object : SqlSyntaxCheckingVisitor() {
override fun visitElement(element: PsiElement) {
if (element is PsiErrorElement) {
errors.add("Syntax error at position ${element.textRange.startOffset}: ${element.errorDescription}")
}
super.visitElement(element)
}
}
sqlFile.accept(visitor)

// err msg: SQL syntax contains errors: Syntax error at position 30: <expression>, ALL, ANY or SOME expected, got ';'
assertTrue(errors.isNotEmpty())
assertEquals("Syntax error at position 30: <expression>, ALL, ANY or SOME expected, got ';'", errors[0])
}
}

abstract class SqlSyntaxCheckingVisitor : com.intellij.psi.PsiElementVisitor() {
override fun visitElement(element: PsiElement) {
element.children.forEach { it.accept(this) }
}
}
}
4 changes: 4 additions & 0 deletions exts/ext-harmonyos/README.md
Expand Up @@ -7,6 +7,10 @@ openharmony/lib/ohos-cpp-lsp-client-3.1.0.501.jar, 自研 C++ PSI 模块
JS 类:`class class com.huawei.ace.language.psi.impl.JavaScriptIdentifierNameImpl`
CPP 类:`class class com.huawei.ideacpp.psi.impl.CPPIdentifierImpl`

Todos:

- [ ] Regenerate code in Syntax error part.

## 设计思念

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

0 comments on commit 6872c07

Please sign in to comment.