-
Notifications
You must be signed in to change notification settings - Fork 513
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add run configuration for sqlite (#2718)
* Add run configuration for sqlite * Add some tests * Run spotless * Fix up code so it works with master * Remove need for an identifier * Reuse ConnectionManager in compiler module * Move the connection manager to the dialect API * Remove tests and run spotless * Simplify RunSqlAction * Update sqldelight-idea-plugin/src/main/resources/META-INF/plugin.xml * Address feedback Co-authored-by: Alec Strong <astrong@squareup.com> Co-authored-by: Alec Strong <AlecStrong@users.noreply.github.com>
- Loading branch information
1 parent
85495ce
commit 5395ebf
Showing
22 changed files
with
616 additions
and
21 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
85 changes: 85 additions & 0 deletions
85
...18/src/main/kotlin/app/cash/sqldelight/dialects/sqlite_3_18/SelectConnectionTypeDialog.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,85 @@ | ||
package app.cash.sqldelight.dialects.sqlite_3_18 | ||
|
||
import app.cash.sqldelight.dialect.api.ConnectionManager.ConnectionProperties | ||
import com.intellij.openapi.fileChooser.FileTypeDescriptor | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.ui.DialogWrapper | ||
import com.intellij.openapi.ui.ValidationInfo | ||
import com.intellij.ui.RecentsManager | ||
import com.intellij.ui.TextFieldWithHistoryWithBrowseButton | ||
import com.intellij.ui.layout.ValidationInfoBuilder | ||
import com.intellij.ui.layout.panel | ||
import java.io.File | ||
import javax.swing.JComponent | ||
import javax.swing.JTextField | ||
|
||
private const val RECENT_DB_PATH = "app.cash.sqldelight.recentPath" | ||
|
||
internal class SelectConnectionTypeDialog( | ||
project: Project, | ||
) : DialogWrapper(project) { | ||
private val recentsManager: RecentsManager = RecentsManager.getInstance(project) | ||
|
||
private var connectionName: String = "" | ||
private var filePath: String = "" | ||
|
||
init { | ||
title = "Select Connection Type" | ||
init() | ||
} | ||
|
||
fun connectionProperties(): ConnectionProperties { | ||
return ConnectionProperties(connectionName, filePath) | ||
} | ||
|
||
override fun createCenterPanel(): JComponent { | ||
return panel { | ||
row("Connection Name") { | ||
textField( | ||
getter = { connectionName }, | ||
setter = { connectionName = it } | ||
).withValidationOnApply(validateKey()) | ||
.withValidationOnInput(validateKey()) | ||
} | ||
row(label = "DB File Path") { | ||
textFieldWithHistoryWithBrowseButton( | ||
browseDialogTitle = "Choose File", | ||
getter = { filePath }, | ||
setter = { filePath = it }, | ||
fileChooserDescriptor = FileTypeDescriptor("Choose File", "db"), | ||
historyProvider = { recentsManager.getRecentEntries(RECENT_DB_PATH).orEmpty() }, | ||
fileChosen = { vFile -> | ||
vFile.path.also { path -> | ||
filePath = path | ||
recentsManager.registerRecentEntry(RECENT_DB_PATH, path) | ||
} | ||
} | ||
) | ||
.withValidationOnInput(validateFilePath()) | ||
.withValidationOnApply(validateFilePath()) | ||
} | ||
}.also { | ||
validate() | ||
} | ||
} | ||
} | ||
|
||
private fun validateKey(): ValidationInfoBuilder.(JTextField) -> ValidationInfo? = | ||
{ | ||
if (it.text.isNullOrEmpty()) { | ||
error("You must supply a connection key.") | ||
} else { | ||
null | ||
} | ||
} | ||
|
||
private fun validateFilePath(): ValidationInfoBuilder.(TextFieldWithHistoryWithBrowseButton) -> ValidationInfo? = | ||
{ | ||
if (it.text.isEmpty()) { | ||
error("The file path is empty.") | ||
} else if (!File(it.text).exists()) { | ||
error("This file does not exist.") | ||
} else { | ||
null | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...-3-18/src/main/kotlin/app/cash/sqldelight/dialects/sqlite_3_18/SqliteConnectionManager.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,30 @@ | ||
package app.cash.sqldelight.dialects.sqlite_3_18 | ||
|
||
import app.cash.sqldelight.dialect.api.ConnectionManager | ||
import app.cash.sqldelight.dialect.api.ConnectionManager.ConnectionProperties | ||
import com.intellij.openapi.project.Project | ||
import java.sql.Connection | ||
import java.sql.DriverManager | ||
import java.sql.SQLException | ||
|
||
class SqliteConnectionManager : ConnectionManager { | ||
override fun createNewConnectionProperties(project: Project): ConnectionProperties? { | ||
val dialog = SelectConnectionTypeDialog(project) | ||
if (!dialog.showAndGet()) return null | ||
return dialog.connectionProperties() | ||
} | ||
|
||
override fun getConnection(connectionProperties: ConnectionProperties): Connection { | ||
val path = connectionProperties.serializedProperties | ||
val previousContextLoader = Thread.currentThread().contextClassLoader | ||
return try { | ||
// When it iterates the ServiceLoader we want to make sure its on the plugins classpath. | ||
Thread.currentThread().contextClassLoader = this::class.java.classLoader | ||
DriverManager.getConnection("jdbc:sqlite:$path") | ||
} catch (e: SQLException) { | ||
DriverManager.getConnection("jdbc:sqlite:$path") | ||
} finally { | ||
Thread.currentThread().contextClassLoader = previousContextLoader | ||
} | ||
} | ||
} |
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
15 changes: 15 additions & 0 deletions
15
...ght-compiler/dialect/src/main/kotlin/app/cash/sqldelight/dialect/api/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,15 @@ | ||
package app.cash.sqldelight.dialect.api | ||
|
||
import com.intellij.openapi.project.Project | ||
import java.sql.Connection | ||
|
||
interface ConnectionManager { | ||
fun createNewConnectionProperties(project: Project): ConnectionProperties? | ||
|
||
fun getConnection(connectionProperties: ConnectionProperties): Connection | ||
|
||
data class ConnectionProperties( | ||
val key: String, | ||
val serializedProperties: String | ||
) | ||
} |
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> get() = _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) | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
sqldelight-idea-plugin/src/main/kotlin/app/cash/sqldelight/intellij/run/ConnectionOptions.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 app.cash.sqldelight.intellij.run | ||
|
||
import app.cash.sqldelight.dialect.api.ConnectionManager.ConnectionProperties | ||
import com.intellij.ide.util.propComponentProperty | ||
import com.intellij.openapi.project.Project | ||
import com.squareup.moshi.Moshi | ||
|
||
internal class ConnectionOptions(val project: Project) { | ||
private var options: String by propComponentProperty(project, "connection_options", "") | ||
var selectedOption: String by propComponentProperty(project, "selected_option", "") | ||
|
||
fun addOption(properties: ConnectionProperties) { | ||
val currentOptions = if (options.isEmpty()) StoredOptions() else adapter.fromJson(options)!! | ||
currentOptions.map[properties.key] = properties.serializedProperties | ||
options = adapter.toJson(currentOptions) | ||
selectedOption = properties.key | ||
} | ||
|
||
fun unselectOption() { | ||
selectedOption = "" | ||
} | ||
|
||
fun getKeys(): Collection<String> { | ||
return adapter.fromJson(options.ifEmpty { return emptyList() })!!.map.keys | ||
} | ||
|
||
fun selectedProperties(): ConnectionProperties { | ||
val currentOptions = adapter.fromJson(options)!! | ||
return ConnectionProperties(selectedOption, currentOptions.map[selectedOption]!!) | ||
} | ||
|
||
companion object { | ||
private val adapter = Moshi.Builder().build() | ||
.adapter(StoredOptions::class.java) | ||
} | ||
|
||
private class StoredOptions( | ||
val map: MutableMap<String, String> = linkedMapOf() | ||
) | ||
} |
Oops, something went wrong.