Skip to content

Commit

Permalink
feat(database): add support for parsing and verifying SQL scripts bef…
Browse files Browse the repository at this point in the history
…ore 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.
  • Loading branch information
phodal committed Apr 1, 2024
1 parent 53412a9 commit 7526032
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
Expand Up @@ -16,6 +16,7 @@ import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.progress.impl.BackgroundableProcessIndicator
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiFile
import com.intellij.sql.dialects.sqlite.SqliteDialect


class AutoSqlAction : ChatBaseIntention() {
Expand Down Expand Up @@ -43,7 +44,9 @@ class AutoSqlAction : ChatBaseIntention() {
val schemaName = rawDataSource.name.substringBeforeLast('@')
val dasTables = rawDataSource.let {
val tables = DasUtil.getTables(it)
tables.filter { table -> table.kind == ObjectKind.TABLE && table.dasParent?.name == schemaName }
tables.filter { table -> table.kind == ObjectKind.TABLE &&
(table.dasParent?.name == schemaName || (file.language == SqliteDialect.INSTANCE && table.dasParent?.name == "main"))
}
}.toList()

val genSqlContext = AutoSqlContext(
Expand Down
Expand Up @@ -3,6 +3,7 @@ package cc.unitmesh.database.flow
import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.util.parser.parseCodeFromString
import com.intellij.lang.Language
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.editor.Editor
Expand Down Expand Up @@ -45,9 +46,11 @@ class AutoSqlBackgroundTask(
val sqlScript = flow.design(tableNames)[0]

try {
val sqlFile =
PsiFileFactory.getInstance(project).createFileFromText("temp.sql", language, sqlScript)
val sqlCode = parseCodeFromString(sqlScript).first()
val sqlFile = runReadAction {
PsiFileFactory.getInstance(project).createFileFromText("temp.sql", language, sqlCode)
as SqlFile
}

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

abstract class SqlSyntaxCheckingVisitor : com.intellij.psi.PsiElementVisitor() {
override fun visitElement(element: PsiElement) {
element.children.forEach { it.accept(this) }
runReadAction {
element.children.forEach { it.accept(this) }
}
}
}

0 comments on commit 7526032

Please sign in to comment.