Skip to content

Commit 737a47a

Browse files
committed
feat(database): add DbContextActionProvider class #80
This commit adds the `DbContextActionProvider` class, which retrieves the columns of specified tables. It includes a `getTableColumns` function that takes a list of table names and returns a list of column names from those tables. The implementation uses the `DasUtil.getColumns` method to retrieve the columns and formats them as "TableName: ColumnName: DataType". This class is added to the `database` package in both the `222` and `233` directories. Additionally, the `GenSqlScriptBySelection.kt` file is modified to import and use the `DbContextActionProvider` class.
1 parent ec3bf1d commit 737a47a

File tree

4 files changed

+77
-25
lines changed

4 files changed

+77
-25
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cc.unitmesh.database
2+
3+
import com.intellij.database.model.DasTable
4+
import com.intellij.database.util.DasUtil
5+
6+
data class DbContextActionProvider(val dasTables: List<DasTable>) {
7+
/**
8+
* Retrieves the columns of the specified tables.
9+
*
10+
* @param tables A list of table names to retrieve the columns from.
11+
* @return A list of column names from the specified tables.
12+
*/
13+
fun getTableColumns(tables: List<String>): List<String> {
14+
return dasTables.mapNotNull { tableName ->
15+
if (tables.contains(tableName.name)) {
16+
val columns = DasUtil.getColumns(tableName).map {
17+
"${it.name}: ${it.dataType}"
18+
}.joinToString(", ")
19+
20+
"TableName: ${tableName.name}, Columns: $columns"
21+
} else {
22+
null
23+
}
24+
}
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cc.unitmesh.database
2+
3+
import com.intellij.database.model.DasTable
4+
import com.intellij.database.util.DasUtil
5+
6+
data class DbContextActionProvider(val dasTables: List<DasTable>) {
7+
/**
8+
* Retrieves the columns of the specified tables.
9+
*
10+
* @param tables A list of table names to retrieve the columns from.
11+
* @return A list of column names from the specified tables.
12+
*/
13+
fun getTableColumns(tables: List<String>): List<String> {
14+
return dasTables.mapNotNull { tableName ->
15+
if (tables.contains(tableName.name)) {
16+
val columns = DasUtil.getColumns(tableName).map {
17+
"${it.name}: ${it.dasType.toDataType()}"
18+
}.joinToString(", ")
19+
20+
"TableName: ${tableName.name}, Columns: $columns"
21+
} else {
22+
null
23+
}
24+
}
25+
}
26+
}

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

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cc.unitmesh.database.actions
22

3+
import cc.unitmesh.database.DbContextActionProvider
34
import cc.unitmesh.devti.AutoDevBundle
45
import cc.unitmesh.devti.gui.chat.ChatCodingPanel
56
import cc.unitmesh.devti.gui.sendToChatPanel
@@ -9,7 +10,6 @@ import cc.unitmesh.devti.llms.LlmFactory
910
import cc.unitmesh.devti.template.TemplateRender
1011
import cc.unitmesh.devti.util.LLMCoroutineScope
1112
import cc.unitmesh.devti.util.parser.parseCodeFromString
12-
import com.intellij.database.model.DasTable
1313
import com.intellij.database.model.ObjectKind
1414
import com.intellij.database.psi.DbPsiFacade
1515
import com.intellij.database.util.DasUtil
@@ -22,9 +22,6 @@ import com.intellij.openapi.progress.Task
2222
import com.intellij.openapi.progress.impl.BackgroundableProcessIndicator
2323
import com.intellij.openapi.project.Project
2424
import com.intellij.psi.PsiFile
25-
import com.intellij.util.awaitCancellationAndInvoke
26-
import kotlinx.coroutines.async
27-
import kotlinx.coroutines.launch
2825
import kotlinx.coroutines.runBlocking
2926

3027

@@ -209,24 +206,3 @@ data class DbContext(
209206
var tableInfos: List<String> = emptyList(),
210207
)
211208

212-
data class DbContextActionProvider(val dasTables: List<DasTable>) {
213-
/**
214-
* Retrieves the columns of the specified tables.
215-
*
216-
* @param tables A list of table names to retrieve the columns from.
217-
* @return A list of column names from the specified tables.
218-
*/
219-
fun getTableColumns(tables: List<String>): List<String> {
220-
return dasTables.mapNotNull { tableName ->
221-
if (tables.contains(tableName.name)) {
222-
val columns = DasUtil.getColumns(tableName).map {
223-
"${it.name}: ${it.dasType.toDataType()}"
224-
}.joinToString(", ")
225-
226-
"TableName: ${tableName.name}, Columns: $columns"
227-
} else {
228-
null
229-
}
230-
}
231-
}
232-
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import com.intellij.database.model.DasTable
2+
import com.intellij.database.util.DasUtil
3+
4+
data class DbContextActionProvider(val dasTables: List<DasTable>) {
5+
/**
6+
* Retrieves the columns of the specified tables.
7+
*
8+
* @param tables A list of table names to retrieve the columns from.
9+
* @return A list of column names from the specified tables.
10+
*/
11+
fun getTableColumns(tables: List<String>): List<String> {
12+
return dasTables.mapNotNull { tableName ->
13+
if (tables.contains(tableName.name)) {
14+
val columns = DasUtil.getColumns(tableName).map {
15+
"${it.name}: ${it.dataType.typeName}"
16+
}.joinToString(", ")
17+
18+
"TableName: ${tableName.name}, Columns: $columns"
19+
} else {
20+
null
21+
}
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)