|
| 1 | +package cc.unitmesh.database.actions |
| 2 | + |
| 3 | +import cc.unitmesh.devti.AutoDevBundle |
| 4 | +import cc.unitmesh.devti.intentions.action.base.AbstractChatIntention |
| 5 | +import com.intellij.database.model.ObjectKind |
| 6 | +import com.intellij.database.psi.DbElement |
| 7 | +import com.intellij.database.psi.DbPsiFacade |
| 8 | +import com.intellij.database.util.DasUtil |
| 9 | +import com.intellij.openapi.editor.Editor |
| 10 | +import com.intellij.openapi.project.Project |
| 11 | +import com.intellij.psi.PsiFile |
| 12 | + |
| 13 | + |
| 14 | +class GenSqlScriptBySelection : AbstractChatIntention() { |
| 15 | + override fun priority(): Int = 1001 |
| 16 | + |
| 17 | + override fun startInWriteAction(): Boolean = false |
| 18 | + |
| 19 | + override fun getFamilyName(): String = AutoDevBundle.message("migration.database.plsql") |
| 20 | + |
| 21 | + override fun getText(): String = AutoDevBundle.message("migration.database.sql.generate") |
| 22 | + |
| 23 | + override fun isAvailable(project: Project, editor: Editor?, psiFile: PsiFile?): Boolean { |
| 24 | + return true |
| 25 | + } |
| 26 | + |
| 27 | + override fun invoke(project: Project, editor: Editor?, psiFile: PsiFile?) { |
| 28 | + val dbPsiFacade = DbPsiFacade.getInstance(project) |
| 29 | + val dataSource = dbPsiFacade.dataSources.firstOrNull() ?: return |
| 30 | + val dbms = dataSource.delegateDataSource.dbms |
| 31 | + val model = dataSource.delegateDataSource.model |
| 32 | + |
| 33 | + val selectedText = editor?.selectionModel?.selectedText |
| 34 | + |
| 35 | + val rawDataSource = dbPsiFacade.getDataSourceManager(dataSource).dataSources.firstOrNull() ?: return |
| 36 | + val databaseVersion = rawDataSource.databaseVersion |
| 37 | + val schemaName = rawDataSource.name.substringBeforeLast('@') |
| 38 | + val dasTables = rawDataSource.let { |
| 39 | + val tables = DasUtil.getTables(it) |
| 40 | + tables.filter { table -> table.kind == ObjectKind.TABLE && table.dasParent?.name == schemaName } |
| 41 | + }.toList() |
| 42 | + |
| 43 | + val tables = getDbElements(schemaName, dbPsiFacade) |
| 44 | + println("elements: $tables") |
| 45 | + val prompt = """ |
| 46 | + |Database: $databaseVersion |
| 47 | + |Requirement: $selectedText |
| 48 | + |Tables: ${tables.joinToString { it.name }} |
| 49 | + |Tables: ${dasTables.joinToString { it.name }} |
| 50 | + """.trimMargin() |
| 51 | + |
| 52 | + println(prompt) |
| 53 | + } |
| 54 | + |
| 55 | + private fun getDbElements(tableName: String, dbPsiFacade: DbPsiFacade): List<DbElement> { |
| 56 | + return dbPsiFacade.dataSources |
| 57 | + .flatMap { dataSource -> |
| 58 | + val dasTable = DasUtil.getTables(dataSource).filter { |
| 59 | + it.dasParent?.name == tableName |
| 60 | + }.filterNotNull().firstOrNull() ?: return@flatMap emptyList<DbElement>() |
| 61 | + |
| 62 | + val columns = DasUtil.getColumns(dasTable) |
| 63 | + columns.map { column -> |
| 64 | + dataSource.findElement(column) |
| 65 | + }.filterNotNull() |
| 66 | + |
| 67 | + } |
| 68 | + .toList() |
| 69 | + } |
| 70 | +} |
0 commit comments