generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(toolsets): add database plugin and related functionalities #9
Adds a new database plugin to the toolsets project, including its configurations in `build.gradle.kts` and `settings.gradle.kts`. This plugin provides features such as SQL context building and variable resolution for SQL files. Additionally, relevant extensions and XML configurations have been added to support the new plugin within the IDE.
- Loading branch information
Showing
6 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,4 +25,5 @@ include( | |
"toolsets:terminal", | ||
"toolsets:sonarqube", | ||
"toolsets:plantuml", | ||
"toolsets:database", | ||
) |
19 changes: 19 additions & 0 deletions
19
toolsets/database/src/main/kotlin/com/phodal/shire/database/DatabaseVariableProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.phodal.shire.database | ||
|
||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.sql.dialects.SqlLanguageDialect | ||
import com.intellij.sql.psi.SqlLanguage | ||
import com.phodal.shirecore.provider.variable.ToolchainVariableProvider | ||
import com.phodal.shirecore.provider.variable.model.ToolchainVariable | ||
|
||
class DatabaseVariableProvider : ToolchainVariableProvider { | ||
override fun isResolvable(variable: ToolchainVariable, psiElement: PsiElement?): Boolean { | ||
return psiElement?.language is SqlLanguageDialect || psiElement?.language is SqlLanguage | ||
} | ||
|
||
override fun resolve(variable: ToolchainVariable, project: Project, editor: Editor, psiElement: PsiElement?): Any { | ||
return "" | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
toolsets/database/src/main/kotlin/com/phodal/shire/database/SqlContextBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.phodal.shire.database | ||
|
||
import com.intellij.database.console.JdbcConsoleProvider | ||
import com.intellij.database.model.ObjectKind | ||
import com.intellij.database.model.basic.BasicModel | ||
import com.intellij.database.model.basic.BasicSchema | ||
import com.intellij.database.model.basic.BasicTable | ||
import com.intellij.database.model.basic.BasicTableOrViewColumn | ||
import com.intellij.database.psi.DbDataSource | ||
import com.intellij.database.util.ObjectPath | ||
import com.intellij.database.util.QNameUtil | ||
import com.intellij.sql.psi.SqlFile | ||
|
||
object SqlContextBuilder { | ||
fun getCurrentNamespace(sqlFile: SqlFile): ObjectPath? { | ||
val console = JdbcConsoleProvider.getValidConsole(sqlFile.project, sqlFile.virtualFile) | ||
return console?.currentNamespace | ||
} | ||
|
||
fun getSchema(ds: DbDataSource?, currentNamespace: ObjectPath?): BasicSchema? { | ||
val basicModel = ds?.model as? BasicModel ?: return null | ||
val dasObject = QNameUtil.findByPath(basicModel, currentNamespace).firstOrNull() ?: return null | ||
return dasObject as? BasicSchema | ||
} | ||
|
||
fun formatSchema(schema: BasicSchema): String? { | ||
return schema.familyOf(ObjectKind.TABLE)?.jbi() | ||
?.mapNotNull { it as? BasicTable } | ||
?.joinToString("\n\n") { describeTable(it) } | ||
} | ||
|
||
private fun describeTable(table: BasicTable): String = | ||
""" | ||
|create table ${table.name} { | ||
| ${table.columns.joinToString(",\n ") { "${it.name} ${columnType(it)}" }} | ||
|} | ||
""".trimMargin() | ||
} | ||
|
||
fun columnType(it: BasicTableOrViewColumn) = it.dasType.specification |
10 changes: 10 additions & 0 deletions
10
toolsets/database/src/main/resources/com.phodal.shire.database.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<idea-plugin package="com.phodal.shire.database"> | ||
<!--suppress PluginXmlValidity --> | ||
<dependencies> | ||
<plugin id="com.intellij.database"/> | ||
</dependencies> | ||
|
||
<extensions defaultExtensionNs="com.phodal"> | ||
<shireToolchainVariableProvider implementation="com.phodal.shire.database.DatabaseVariableProvider"/> | ||
</extensions> | ||
</idea-plugin> |