-
Notifications
You must be signed in to change notification settings - Fork 515
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
806d8c6
commit 41f6396
Showing
17 changed files
with
573 additions
and
7 deletions.
There are no files selected for viewing
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
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
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
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
47 changes: 47 additions & 0 deletions
47
...-idea-plugin/src/main/kotlin/app/cash/sqldelight/intellij/run/ArgumentsInputDialogImpl.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,47 @@ | ||
package app.cash.sqldelight.intellij.run | ||
|
||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.ui.DialogWrapper | ||
import com.intellij.ui.layout.panel | ||
import javax.swing.JComponent | ||
|
||
internal interface ArgumentsInputDialog { | ||
val result: List<SqlParameter> | ||
|
||
fun showAndGet(): Boolean | ||
|
||
interface Factory { | ||
fun create(project: Project, parameters: List<SqlParameter>): ArgumentsInputDialog | ||
} | ||
} | ||
|
||
internal class ArgumentsInputDialogImpl( | ||
project: Project, | ||
private val parameters: List<SqlParameter> | ||
) : DialogWrapper(project), ArgumentsInputDialog { | ||
|
||
init { | ||
init() | ||
} | ||
|
||
private val _result = mutableListOf<SqlParameter>() | ||
override val result: List<SqlParameter> = _result | ||
|
||
override fun createCenterPanel(): JComponent { | ||
return panel { | ||
parameters.forEach { parameter -> | ||
row("${parameter.name}:") { | ||
textField(parameter::value, { | ||
_result.add(parameter.copy(value = it)) | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
internal class ArgumentsInputDialogFactoryImpl : ArgumentsInputDialog.Factory { | ||
override fun create(project: Project, parameters: List<SqlParameter>): ArgumentsInputDialog { | ||
return ArgumentsInputDialogImpl(project, parameters) | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
sqldelight-idea-plugin/src/main/kotlin/app/cash/sqldelight/intellij/run/ConnectionManager.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,47 @@ | ||
package app.cash.sqldelight.intellij.run | ||
|
||
import app.cash.sqldelight.intellij.util.dialectPreset | ||
import app.cash.sqldelight.intellij.util.isSqlite | ||
import com.intellij.openapi.components.ServiceManager | ||
import com.intellij.openapi.project.Project | ||
import java.sql.Connection | ||
import java.sql.DriverManager | ||
import java.sql.SQLException | ||
|
||
internal interface ConnectionManager { | ||
fun getConnection(): Connection | ||
|
||
companion object { | ||
fun getInstance(project: Project): ConnectionManager { | ||
return ServiceManager.getService(project, ConnectionManager::class.java)!! | ||
} | ||
} | ||
} | ||
|
||
internal class ConnectionManagerImpl(private val project: Project) : ConnectionManager { | ||
|
||
private val connectionOptions = ConnectionOptions(project) | ||
|
||
init { | ||
Class.forName("org.sqlite.JDBC") | ||
} | ||
|
||
override fun getConnection(): Connection { | ||
val dialect = project.dialectPreset | ||
if (!dialect.isSqlite) { | ||
throw SQLException("Unsupported dialect $dialect") | ||
} | ||
|
||
val connectionType = connectionOptions.connectionType | ||
if (connectionType != ConnectionType.FILE) { | ||
throw SQLException("Unsupported connection type $connectionType") | ||
} | ||
|
||
val filePath = connectionOptions.filePath | ||
if (filePath.isEmpty()) { | ||
throw SQLException("The file path is empty") | ||
} | ||
|
||
return DriverManager.getConnection("jdbc:sqlite:$filePath") | ||
} | ||
} |
131 changes: 131 additions & 0 deletions
131
sqldelight-idea-plugin/src/main/kotlin/app/cash/sqldelight/intellij/run/RunSqlAction.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,131 @@ | ||
package app.cash.sqldelight.intellij.run | ||
|
||
import app.cash.sqldelight.core.compiler.model.NamedMutator | ||
import app.cash.sqldelight.core.compiler.model.NamedQuery | ||
import app.cash.sqldelight.core.lang.SqlDelightFile | ||
import app.cash.sqldelight.core.lang.SqlDelightFileType | ||
import app.cash.sqldelight.core.lang.psi.StmtIdentifierMixin | ||
import app.cash.sqldelight.core.lang.util.findChildOfType | ||
import app.cash.sqldelight.core.lang.util.range | ||
import app.cash.sqldelight.core.lang.util.rawSqlText | ||
import app.cash.sqldelight.core.psi.SqlDelightStmtClojure | ||
import app.cash.sqldelight.core.psi.SqlDelightStmtClojureStmtList | ||
import com.alecstrong.sql.psi.core.psi.SqlStmt | ||
import com.alecstrong.sql.psi.core.psi.SqlStmtList | ||
import com.intellij.openapi.actionSystem.AnAction | ||
import com.intellij.openapi.actionSystem.AnActionEvent | ||
import com.intellij.openapi.application.runReadAction | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.PsiComment | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiFileFactory | ||
import com.intellij.psi.PsiWhiteSpace | ||
import com.intellij.psi.util.PsiTreeUtil | ||
import org.jetbrains.annotations.VisibleForTesting | ||
|
||
@VisibleForTesting | ||
internal class RunSqlAction( | ||
private val stmt: SqlStmt, | ||
private val project: Project = stmt.project, | ||
private val executor: SqlDelightStatementExecutor = SqlDelightStatementExecutor.getInstance( | ||
project | ||
), | ||
private val dialogFactory: ArgumentsInputDialog.Factory = ArgumentsInputDialogFactoryImpl() | ||
) : AnAction() { | ||
|
||
override fun actionPerformed(e: AnActionEvent) { | ||
val sql = stmt.rawSqlText().trim().replace("\\s+".toRegex(), " ") | ||
|
||
val identifier = stmt.identifier | ||
val parameters = identifier?.let { findParameters(stmt, it) } ?: emptyList() | ||
val sqlStmt = if (parameters.isEmpty()) { | ||
sql | ||
} else { | ||
val dialog = dialogFactory.create(project, parameters) | ||
val ok = dialog.showAndGet() | ||
if (!ok) return | ||
|
||
bindParameters(sql, dialog.result) ?: return | ||
} | ||
executor.execute(sqlStmt) | ||
} | ||
|
||
private val SqlStmt.identifier: StmtIdentifierMixin? | ||
get() { | ||
return when (parent) { | ||
is SqlStmtList -> prevVisibleSibling() as? StmtIdentifierMixin | ||
is SqlDelightStmtClojureStmtList -> { | ||
val stmtClojure = PsiTreeUtil.getParentOfType(this, SqlDelightStmtClojure::class.java) | ||
stmtClojure?.stmtIdentifierClojure as? StmtIdentifierMixin | ||
} | ||
else -> null | ||
} | ||
} | ||
|
||
private fun PsiElement.prevVisibleSibling(): PsiElement? { | ||
return generateSequence(prevSibling) { it.prevSibling } | ||
.firstOrNull { it !is PsiWhiteSpace && it !is PsiComment } | ||
} | ||
|
||
private fun findParameters( | ||
sqlStmt: SqlStmt, | ||
identifier: StmtIdentifierMixin | ||
): List<SqlParameter> { | ||
val bindableQuery = when { | ||
sqlStmt.compoundSelectStmt != null -> NamedQuery( | ||
identifier.name!!, sqlStmt.compoundSelectStmt!!, identifier | ||
) | ||
sqlStmt.deleteStmtLimited != null -> NamedMutator.Delete( | ||
sqlStmt.deleteStmtLimited!!, identifier | ||
) | ||
sqlStmt.insertStmt != null -> NamedMutator.Insert( | ||
sqlStmt.insertStmt!!, identifier | ||
) | ||
sqlStmt.updateStmtLimited != null -> NamedMutator.Update( | ||
sqlStmt.updateStmtLimited!!, identifier | ||
) | ||
else -> null | ||
} ?: return emptyList() | ||
val offset = sqlStmt.textOffset | ||
val argumentList: List<IntRange> = bindableQuery.arguments | ||
.flatMap { it.bindArgs } | ||
.map { | ||
val textRange = it.range | ||
IntRange(textRange.first - offset, textRange.last - offset) | ||
} | ||
val parameters: List<String> = bindableQuery.parameters | ||
.map { it.name } | ||
return argumentList.zip(parameters) { range, name -> | ||
SqlParameter( | ||
name = name, | ||
range = range | ||
) | ||
} | ||
} | ||
|
||
private fun bindParameters( | ||
sql: String, | ||
parameters: List<SqlParameter> | ||
): String? { | ||
val replacements = parameters.mapNotNull { p -> | ||
if (p.value.isEmpty()) { | ||
return@mapNotNull null | ||
} | ||
p.range to "'${p.value}'" | ||
} | ||
if (replacements.isEmpty()) { | ||
return null | ||
} | ||
|
||
val factory = PsiFileFactory.getInstance(project) | ||
return runReadAction { | ||
val dummyFile = factory.createFileFromText( | ||
"_Dummy_.${SqlDelightFileType.EXTENSION}", | ||
SqlDelightFileType, | ||
sql | ||
) as SqlDelightFile | ||
val stmt = dummyFile.findChildOfType<SqlStmt>() | ||
stmt?.rawSqlText(replacements) | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...elight-idea-plugin/src/main/kotlin/app/cash/sqldelight/intellij/run/RunSqliteAnnotator.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,41 @@ | ||
package app.cash.sqldelight.intellij.run | ||
|
||
import com.alecstrong.sql.psi.core.psi.SqlStmt | ||
import com.alecstrong.sql.psi.core.psi.SqlVisitor | ||
import com.intellij.icons.AllIcons | ||
import com.intellij.lang.annotation.AnnotationHolder | ||
import com.intellij.lang.annotation.HighlightSeverity | ||
import com.intellij.openapi.actionSystem.AnAction | ||
import com.intellij.openapi.editor.markup.GutterIconRenderer | ||
import javax.swing.Icon | ||
|
||
internal class RunSqliteAnnotator( | ||
private val holder: AnnotationHolder, | ||
private val connectionOptions: ConnectionOptions, | ||
) : SqlVisitor() { | ||
|
||
override fun visitStmt(o: SqlStmt) { | ||
if (connectionOptions.connectionType != ConnectionType.FILE) { | ||
return | ||
} | ||
|
||
val filePath = connectionOptions.filePath | ||
if (filePath.isEmpty()) { | ||
return | ||
} | ||
|
||
holder.newAnnotation(HighlightSeverity.INFORMATION, "") | ||
.gutterIconRenderer(RunSqliteStatementGutterIconRenderer(o)) | ||
.create() | ||
} | ||
|
||
private data class RunSqliteStatementGutterIconRenderer( | ||
private val stmt: SqlStmt, | ||
) : GutterIconRenderer() { | ||
override fun getIcon(): Icon = AllIcons.RunConfigurations.TestState.Run | ||
|
||
override fun getTooltipText(): String = "Run statement" | ||
|
||
override fun getClickAction(): AnAction = RunSqlAction(stmt) | ||
} | ||
} |
Oops, something went wrong.