Skip to content

Commit

Permalink
feat(toolsets): add database plugin and related functionalities #9
Browse files Browse the repository at this point in the history
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
phodal committed Aug 27, 2024
1 parent 3176e6c commit d94d0a1
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 1 deletion.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ project(":toolsets:terminal") {
}
}


project(":toolsets:sonarqube") {
intellij {
version.set(prop("ideaVersion"))
Expand All @@ -237,6 +236,16 @@ project(":toolsets:plantuml") {
}
}

project(":toolsets:database") {
intellij {
version.set(prop("ideaVersion"))
plugins.set(ideaPlugins + "com.intellij.database")
}
dependencies {
implementation(project(":core"))
}
}

project(":shirelang") {
apply {
plugin("org.jetbrains.grammarkit")
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ include(
"toolsets:terminal",
"toolsets:sonarqube",
"toolsets:plantuml",
"toolsets:database",
)
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 ""
}
}
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 {

Check warning on line 14 in toolsets/database/src/main/kotlin/com/phodal/shire/database/SqlContextBuilder.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused symbol

Object "SqlContextBuilder" is never used
fun getCurrentNamespace(sqlFile: SqlFile): ObjectPath? {

Check warning on line 15 in toolsets/database/src/main/kotlin/com/phodal/shire/database/SqlContextBuilder.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused symbol

Function "getCurrentNamespace" is never used
val console = JdbcConsoleProvider.getValidConsole(sqlFile.project, sqlFile.virtualFile)
return console?.currentNamespace
}

fun getSchema(ds: DbDataSource?, currentNamespace: ObjectPath?): BasicSchema? {

Check warning on line 20 in toolsets/database/src/main/kotlin/com/phodal/shire/database/SqlContextBuilder.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused symbol

Function "getSchema" is never used
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? {

Check warning on line 26 in toolsets/database/src/main/kotlin/com/phodal/shire/database/SqlContextBuilder.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused symbol

Function "formatSchema" is never used
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 toolsets/database/src/main/resources/com.phodal.shire.database.xml
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>

0 comments on commit d94d0a1

Please sign in to comment.