Skip to content

Commit 6017221

Browse files
committed
feat(database): add DbContext and DbContextProvider classes #80
This commit adds the `DbContext` and `DbContextProvider` classes to the `GenSqlScriptBySelection.kt` file in the `exts/database/src/main/kotlin/cc/unitmesh/database/actions/` directory. The `DbContext` class is a data class that holds information about the database version, schema name, and table names. The `DbContextProvider` class is responsible for retrieving the columns of specified tables. It has a `getTableColumns` function that takes a list of table names and returns a list of column names from those tables.
1 parent 851fc52 commit 6017221

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package cc.unitmesh.database.actions
33
import cc.unitmesh.devti.AutoDevBundle
44
import cc.unitmesh.devti.intentions.action.base.AbstractChatIntention
55
import com.intellij.database.model.DasColumn
6+
import com.intellij.database.model.DasTable
67
import com.intellij.database.model.ObjectKind
8+
import com.intellij.database.model.RawDataSource
79
import com.intellij.database.psi.DbElement
810
import com.intellij.database.psi.DbPsiFacade
911
import com.intellij.database.util.DasUtil
@@ -39,12 +41,42 @@ class GenSqlScriptBySelection : AbstractChatIntention() {
3941
tables.filter { table -> table.kind == ObjectKind.TABLE && table.dasParent?.name == schemaName }
4042
}.toList()
4143

44+
val tableColumns = DbContextProvider(dasTables).getTableColumns(dasTables.map { it.name })
45+
4246
val prompt = """
4347
|Database: $databaseVersion
4448
|Requirement: $selectedText
4549
|Tables: ${dasTables.joinToString { it.name }}
50+
|Columns: ${tableColumns.joinToString { it }}
4651
""".trimMargin()
4752

4853
println(prompt)
4954
}
5055
}
56+
57+
data class DbContext(
58+
val databaseVersion: String,
59+
val schemaName: String,
60+
val tableNames: List<String>,
61+
) {
62+
}
63+
64+
data class DbContextProvider(val dasTables: List<DasTable>) {
65+
/**
66+
* Retrieves the columns of the specified tables.
67+
*
68+
* @param tables A list of table names to retrieve the columns from.
69+
* @return A list of column names from the specified tables.
70+
*/
71+
fun getTableColumns(tables: List<String>): List<String> {
72+
return dasTables.flatMap { tableName ->
73+
if (tables.contains(tableName.name)) {
74+
DasUtil.getColumns(tableName).map {
75+
"${it.name}: ${it.dasType.toDataType()}"
76+
}
77+
} else {
78+
emptyList()
79+
}
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)