Skip to content

Commit e4b7f4a

Browse files
committedDec 30, 2024··
refactor(database): simplify and rename methods #171
- Simplify `getDataSources` method to use single-expression syntax. - Rename `getAllRawDatasource` to `allRawDatasource` for consistency. - Remove unused `Disposer` import. - Fix minor formatting issues
1 parent 92745e5 commit e4b7f4a

File tree

3 files changed

+9
-13
lines changed

3 files changed

+9
-13
lines changed
 

‎toolsets/database/src/main/kotlin/com/phodal/shire/database/DatabaseSchemaAssistant.kt

+7-11
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,27 @@ import com.intellij.openapi.application.ApplicationManager
3131
import com.intellij.openapi.editor.ex.EditorEx
3232
import com.intellij.openapi.fileEditor.FileEditorManager
3333
import com.intellij.openapi.project.Project
34-
import com.intellij.openapi.util.Disposer
3534
import com.intellij.psi.PsiManager
3635
import com.intellij.sql.psi.SqlPsiFacade
3736
import com.intellij.testFramework.LightVirtualFile
3837
import java.util.concurrent.CompletableFuture
3938

4039
object DatabaseSchemaAssistant {
41-
fun getDataSources(project: Project): List<DbDataSource> {
42-
return DbPsiFacade.getInstance(project).dataSources.toList()
43-
}
40+
fun getDataSources(project: Project): List<DbDataSource> = DbPsiFacade.getInstance(project).dataSources.toList()
4441

45-
fun getAllRawDatasource(project: Project): List<RawDataSource> {
42+
fun allRawDatasource(project: Project): List<RawDataSource> {
4643
val dbPsiFacade = DbPsiFacade.getInstance(project)
4744
return dbPsiFacade.dataSources.map { dataSource ->
4845
dbPsiFacade.getDataSourceManager(dataSource).dataSources
4946
}.flatten()
5047
}
5148

5249
fun getDatabase(project: Project, dbName: String): RawDataSource? {
53-
return getAllRawDatasource(project).firstOrNull { it.name == dbName }
50+
return allRawDatasource(project).firstOrNull { it.name == dbName }
5451
}
5552

5653
fun getAllTables(project: Project): List<DasTable> {
57-
val rawDataSources = getAllRawDatasource(project)
58-
return rawDataSources.map {
54+
return allRawDatasource(project).map {
5955
val schemaName = it.name.substringBeforeLast('@')
6056
DasUtil.getTables(it).filter { table ->
6157
table.kind == ObjectKind.TABLE && (table.dasParent?.name == schemaName || isSQLiteTable(it, table))
@@ -76,7 +72,7 @@ object DatabaseSchemaAssistant {
7672
val file = LightVirtualFile("temp.sql", sql)
7773
val psiFile = PsiManager.getInstance(project).findFile(file)!!
7874

79-
val dataSource = getAllRawDatasource(project).firstOrNull()
75+
val dataSource = allRawDatasource(project).firstOrNull()
8076
?: throw IllegalArgumentException("ShireError[Database]: No database found")
8177

8278
val execOptions = DatabaseSettings.getSettings().execOptions.last()
@@ -98,7 +94,7 @@ object DatabaseSchemaAssistant {
9894

9995
override fun addRows(context: GridDataRequest.Context, rows: MutableList<out GridRow>) {
10096
super.addRows(context, rows)
101-
result += rows;
97+
result += rows
10298
/// TODO: fix this use result.size instead of rows.size
10399
if (rows.size < 100) {
104100
future.complete(result.toString())
@@ -191,6 +187,6 @@ object DatabaseSchemaAssistant {
191187
"${column.name}: ${column.dasType.toDataType()}"
192188
}.joinToString(", ")
193189

194-
return "TableName: ${table.name} Columns: { $columns }"
190+
return "TableName: ${table.name}, Columns: { $columns }"
195191
}
196192
}

‎toolsets/database/src/main/kotlin/com/phodal/shire/database/SqlContextBuilder.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ object SqlContextBuilder {
4040
private fun columnType(it: BasicTableOrViewColumn) = it.dasType.specification
4141

4242
fun buildDatabaseInfo(project: Project): String {
43-
val dataSources = DatabaseSchemaAssistant.getAllRawDatasource(project)
43+
val dataSources = DatabaseSchemaAssistant.allRawDatasource(project)
4444
return dataSources.joinToString("\n") {
4545
"""
4646
DatabaseName: ${it.databaseVersion.name}

‎toolsets/database/src/main/kotlin/com/phodal/shire/database/provider/DatabaseFunctionProvider.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class DatabaseFunctionProvider : ToolchainFunctionProvider {
4343

4444
private fun executeTableFunction(args: List<Any>, project: Project): Any {
4545
if (args.isEmpty()) {
46-
val dataSource = DatabaseSchemaAssistant.getAllRawDatasource(project).firstOrNull()
46+
val dataSource = DatabaseSchemaAssistant.allRawDatasource(project).firstOrNull()
4747
?: return "ShireError[Database]: No database found"
4848
return DatabaseSchemaAssistant.getTableByDataSource(dataSource)
4949
}

0 commit comments

Comments
 (0)
Please sign in to comment.