Skip to content

Commit 7526032

Browse files
committed
feat(database): add support for parsing and verifying SQL scripts before inserting them into the editor.
This commit introduces a new feature to the database extension that enhances the user experience by adding support for parsing and verifying SQL scripts before they are inserted into the editor. This ensures that the scripts are syntactically correct and adhere to the specified dialect, improving the overall quality and reliability of the SQL code.
1 parent 53412a9 commit 7526032

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

exts/database/src/main/kotlin/cc/unitmesh/database/actions/AutoSqlAction.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import com.intellij.openapi.progress.ProgressManager
1616
import com.intellij.openapi.progress.impl.BackgroundableProcessIndicator
1717
import com.intellij.openapi.project.Project
1818
import com.intellij.psi.PsiFile
19+
import com.intellij.sql.dialects.sqlite.SqliteDialect
1920

2021

2122
class AutoSqlAction : ChatBaseIntention() {
@@ -43,7 +44,9 @@ class AutoSqlAction : ChatBaseIntention() {
4344
val schemaName = rawDataSource.name.substringBeforeLast('@')
4445
val dasTables = rawDataSource.let {
4546
val tables = DasUtil.getTables(it)
46-
tables.filter { table -> table.kind == ObjectKind.TABLE && table.dasParent?.name == schemaName }
47+
tables.filter { table -> table.kind == ObjectKind.TABLE &&
48+
(table.dasParent?.name == schemaName || (file.language == SqliteDialect.INSTANCE && table.dasParent?.name == "main"))
49+
}
4750
}.toList()
4851

4952
val genSqlContext = AutoSqlContext(

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cc.unitmesh.database.flow
33
import cc.unitmesh.devti.AutoDevBundle
44
import cc.unitmesh.devti.util.parser.parseCodeFromString
55
import com.intellij.lang.Language
6+
import com.intellij.openapi.application.runReadAction
67
import com.intellij.openapi.command.WriteCommandAction
78
import com.intellij.openapi.diagnostic.logger
89
import com.intellij.openapi.editor.Editor
@@ -45,9 +46,11 @@ class AutoSqlBackgroundTask(
4546
val sqlScript = flow.design(tableNames)[0]
4647

4748
try {
48-
val sqlFile =
49-
PsiFileFactory.getInstance(project).createFileFromText("temp.sql", language, sqlScript)
49+
val sqlCode = parseCodeFromString(sqlScript).first()
50+
val sqlFile = runReadAction {
51+
PsiFileFactory.getInstance(project).createFileFromText("temp.sql", language, sqlCode)
5052
as SqlFile
53+
}
5154

5255
val errors = sqlFile.verifySqlElement()
5356
if (errors.isNotEmpty()) {
@@ -89,6 +92,8 @@ fun SqlFile.verifySqlElement(): MutableList<String> {
8992

9093
abstract class SqlSyntaxCheckingVisitor : com.intellij.psi.PsiElementVisitor() {
9194
override fun visitElement(element: PsiElement) {
92-
element.children.forEach { it.accept(this) }
95+
runReadAction {
96+
element.children.forEach { it.accept(this) }
97+
}
9398
}
9499
}

0 commit comments

Comments
 (0)