Skip to content

Commit f597094

Browse files
committed
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.
1 parent ead7980 commit f597094

File tree

5 files changed

+76
-26
lines changed

5 files changed

+76
-26
lines changed

exts/database/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ This directory contains extensions that are specific to a database.
66

77
Or others?
88

9+
## 自然语言 SQL 生成
10+
11+
根据数据库表生成 JPA 、 MyBatis 、 Spring Data JDBC 等代码。
12+
913
## Usecases: PL/SQL to Java
1014

1115
1. Generate Repository from PL/SQL code
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
}

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

Lines changed: 0 additions & 25 deletions
This file was deleted.

exts/database/src/main/resources/cc.unitmesh.database.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<categoryKey>intention.category.llm</categoryKey>
2222
</autoDevIntention>
2323
<autoDevIntention>
24-
<className>cc.unitmesh.database.actions.VisualSqlAction</className>
24+
<className>cc.unitmesh.database.actions.GenSqlScriptBySelection</className>
2525
<bundleName>messages.AutoDevBundle</bundleName>
2626
<categoryKey>intention.category.llm</categoryKey>
2727
</autoDevIntention>

src/main/resources/messages/AutoDevBundle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,4 @@ migration.database.plsql.generate.unittest=Generate Unit Test
119119
migration.database.plsql.generate.entity=Generate Entity
120120
migration.database.plsql.visual=Visualize PL/SQL
121121
migration.database.plsql.modular.design=Modular Code
122+
migration.database.sql.generate=Generate SQL (by selection)

0 commit comments

Comments
 (0)