generated from JetBrains/intellij-platform-plugin-template
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(database): add GenSqlScriptBySelection action
Add a new action called GenSqlScriptBySelection to generate SQL scripts based on selected text in the editor. This action is available for all projects and can be invoked from the editor context menu. It retrieves the necessary information from the database and displays a prompt with the selected text, database version, and relevant tables.
- Loading branch information
Showing
5 changed files
with
76 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
exts/database/src/main/kotlin/cc/unitmesh/database/actions/GenSqlScriptBySelection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package cc.unitmesh.database.actions | ||
|
||
import cc.unitmesh.devti.AutoDevBundle | ||
import cc.unitmesh.devti.intentions.action.base.AbstractChatIntention | ||
import com.intellij.database.model.ObjectKind | ||
import com.intellij.database.psi.DbElement | ||
import com.intellij.database.psi.DbPsiFacade | ||
import com.intellij.database.util.DasUtil | ||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.PsiFile | ||
|
||
|
||
class GenSqlScriptBySelection : AbstractChatIntention() { | ||
override fun priority(): Int = 1001 | ||
|
||
override fun startInWriteAction(): Boolean = false | ||
|
||
override fun getFamilyName(): String = AutoDevBundle.message("migration.database.plsql") | ||
|
||
override fun getText(): String = AutoDevBundle.message("migration.database.sql.generate") | ||
|
||
override fun isAvailable(project: Project, editor: Editor?, psiFile: PsiFile?): Boolean { | ||
return true | ||
} | ||
|
||
override fun invoke(project: Project, editor: Editor?, psiFile: PsiFile?) { | ||
val dbPsiFacade = DbPsiFacade.getInstance(project) | ||
val dataSource = dbPsiFacade.dataSources.firstOrNull() ?: return | ||
val dbms = dataSource.delegateDataSource.dbms | ||
val model = dataSource.delegateDataSource.model | ||
|
||
val selectedText = editor?.selectionModel?.selectedText | ||
|
||
val rawDataSource = dbPsiFacade.getDataSourceManager(dataSource).dataSources.firstOrNull() ?: return | ||
val databaseVersion = rawDataSource.databaseVersion | ||
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 } | ||
}.toList() | ||
|
||
val tables = getDbElements(schemaName, dbPsiFacade) | ||
println("elements: $tables") | ||
val prompt = """ | ||
|Database: $databaseVersion | ||
|Requirement: $selectedText | ||
|Tables: ${tables.joinToString { it.name }} | ||
|Tables: ${dasTables.joinToString { it.name }} | ||
""".trimMargin() | ||
|
||
println(prompt) | ||
} | ||
|
||
private fun getDbElements(tableName: String, dbPsiFacade: DbPsiFacade): List<DbElement> { | ||
return dbPsiFacade.dataSources | ||
.flatMap { dataSource -> | ||
val dasTable = DasUtil.getTables(dataSource).filter { | ||
it.dasParent?.name == tableName | ||
}.filterNotNull().firstOrNull() ?: return@flatMap emptyList<DbElement>() | ||
|
||
val columns = DasUtil.getColumns(dasTable) | ||
columns.map { column -> | ||
dataSource.findElement(column) | ||
}.filterNotNull() | ||
|
||
} | ||
.toList() | ||
} | ||
} |
25 changes: 0 additions & 25 deletions
25
exts/database/src/main/kotlin/cc/unitmesh/database/actions/VisualSqlAction.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters