diff --git a/sqldelight-compiler/src/main/kotlin/app/cash/sqldelight/core/compiler/QueryGenerator.kt b/sqldelight-compiler/src/main/kotlin/app/cash/sqldelight/core/compiler/QueryGenerator.kt index c223709b2d5..4ca108d707c 100644 --- a/sqldelight-compiler/src/main/kotlin/app/cash/sqldelight/core/compiler/QueryGenerator.kt +++ b/sqldelight-compiler/src/main/kotlin/app/cash/sqldelight/core/compiler/QueryGenerator.kt @@ -311,7 +311,7 @@ abstract class QueryGenerator( } protected fun addJavadoc(builder: FunSpec.Builder) { - if (query.javadoc != null) javadocText(query.javadoc)?.let { builder.addKdoc(it) } + if (query.javadoc != null) { builder.addKdoc(javadocText(query.javadoc)) } } protected open fun awaiting(): Pair? = "%L" to ".await()" diff --git a/sqldelight-compiler/src/main/kotlin/app/cash/sqldelight/core/compiler/SelectQueryGenerator.kt b/sqldelight-compiler/src/main/kotlin/app/cash/sqldelight/core/compiler/SelectQueryGenerator.kt index a087476b82d..73b5cc7562f 100644 --- a/sqldelight-compiler/src/main/kotlin/app/cash/sqldelight/core/compiler/SelectQueryGenerator.kt +++ b/sqldelight-compiler/src/main/kotlin/app/cash/sqldelight/core/compiler/SelectQueryGenerator.kt @@ -116,7 +116,7 @@ class SelectQueryGenerator( } private fun customResultTypeFunctionInterface(): FunSpec.Builder { - val function = FunSpec.builder(query.name) + val function = FunSpec.builder(query.name).also(::addJavadoc) val params = mutableListOf() query.arguments.sortedBy { it.index }.forEach { (_, argument) -> diff --git a/sqldelight-compiler/src/main/kotlin/app/cash/sqldelight/core/compiler/TableInterfaceGenerator.kt b/sqldelight-compiler/src/main/kotlin/app/cash/sqldelight/core/compiler/TableInterfaceGenerator.kt index 7339af4988c..03adbd93e91 100644 --- a/sqldelight-compiler/src/main/kotlin/app/cash/sqldelight/core/compiler/TableInterfaceGenerator.kt +++ b/sqldelight-compiler/src/main/kotlin/app/cash/sqldelight/core/compiler/TableInterfaceGenerator.kt @@ -45,7 +45,7 @@ internal class TableInterfaceGenerator(private val table: LazyQuery) { SqlDelightStmtIdentifier::class.java, ) identifier?.childOfType(SqlTypes.JAVADOC)?.let { javadoc -> - javadocText(javadoc)?.let { typeSpec.addKdoc(it) } + typeSpec.addKdoc(javadocText(javadoc)) } val constructor = FunSpec.constructorBuilder() diff --git a/sqldelight-compiler/src/main/kotlin/app/cash/sqldelight/core/compiler/integration/JavadocText.kt b/sqldelight-compiler/src/main/kotlin/app/cash/sqldelight/core/compiler/integration/JavadocText.kt index 04870c14e2d..834c8b3ba6f 100644 --- a/sqldelight-compiler/src/main/kotlin/app/cash/sqldelight/core/compiler/integration/JavadocText.kt +++ b/sqldelight-compiler/src/main/kotlin/app/cash/sqldelight/core/compiler/integration/JavadocText.kt @@ -29,9 +29,10 @@ import com.intellij.psi.PsiElement */ private val JAVADOC_TEXT_REGEX = Regex("/\\*\\*|\n \\*[ /]?| \\*/") -internal fun javadocText(javadoc: PsiElement): String? { +internal fun javadocText(javadoc: PsiElement): String { return javadoc.text .split(JAVADOC_TEXT_REGEX) .dropWhile { it.isEmpty() } .joinToString(separator = "\n") { it.trim() } + .removeSuffix("\n*/") } diff --git a/sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/dialects/DialectTypes.kt b/sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/dialects/DialectTypes.kt index cc357bbf511..9739ffb8d4e 100644 --- a/sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/dialects/DialectTypes.kt +++ b/sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/dialects/DialectTypes.kt @@ -13,6 +13,8 @@ import app.cash.sqldelight.core.TestDialect.SQLITE_3_38 import app.cash.sqldelight.dialects.hsql.HsqlDialect import app.cash.sqldelight.dialects.mysql.MySqlDialect import app.cash.sqldelight.dialects.postgresql.PostgreSqlDialect +import com.squareup.kotlinpoet.INT +import com.squareup.kotlinpoet.LONG internal val TestDialect.textType get() = when (this) { @@ -31,6 +33,12 @@ internal val TestDialect.intType MYSQL, SQLITE_3_24, SQLITE_3_18, SQLITE_3_25, SQLITE_3_30, SQLITE_3_35, SQLITE_3_38, POSTGRESQL, HSQL -> "INTEGER" } +internal val TestDialect.intKotlinType + get() = when (this) { + SQLITE_3_24, SQLITE_3_18, SQLITE_3_25, SQLITE_3_30, SQLITE_3_35, SQLITE_3_38 -> LONG + MYSQL, POSTGRESQL, HSQL -> INT + } + /** * The [check] statement generated for the prepared statement type in the binder lambda for this dialect. * @@ -50,11 +58,10 @@ internal val TestDialect.binderCheck * * See [SelectQueryGenerator]. */ -internal val TestDialect.cursorCheck - get() = when { - dialect.isSqlite -> "" - else -> when (dialect) { - is PostgreSqlDialect, is HsqlDialect, is MySqlDialect -> "check(cursor is app.cash.sqldelight.driver.jdbc.JdbcCursor)\n " - else -> throw IllegalStateException("Unknown dialect: $this") - } +internal fun TestDialect.cursorCheck(whitespaces: Int) = when { + dialect.isSqlite -> "" + else -> when (dialect) { + is PostgreSqlDialect, is HsqlDialect, is MySqlDialect -> "check(cursor is app.cash.sqldelight.driver.jdbc.JdbcCursor)\n${" ".repeat(whitespaces)}" + else -> throw IllegalStateException("Unknown dialect: $this") } +} diff --git a/sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/queries/JavadocTest.kt b/sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/queries/JavadocTest.kt index a185b5a27be..e84f4146dec 100644 --- a/sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/queries/JavadocTest.kt +++ b/sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/queries/JavadocTest.kt @@ -5,10 +5,14 @@ import app.cash.sqldelight.core.TestDialect.SQLITE_3_18 import app.cash.sqldelight.core.compiler.MutatorQueryGenerator import app.cash.sqldelight.core.compiler.SelectQueryGenerator import app.cash.sqldelight.core.dialects.binderCheck +import app.cash.sqldelight.core.dialects.cursorCheck +import app.cash.sqldelight.core.dialects.intKotlinType import app.cash.sqldelight.core.dialects.textType import app.cash.sqldelight.test.util.FixtureCompiler import com.google.common.truth.Truth.assertThat import com.squareup.burst.BurstJUnit4 +import com.squareup.kotlinpoet.INT +import com.squareup.kotlinpoet.LONG import org.junit.Rule import org.junit.Test import org.junit.rules.TemporaryFolder @@ -193,6 +197,31 @@ class JavadocTest { | """.trimMargin(), ) + + val int = testDialect.intKotlinType + val toInt = when (int) { + LONG -> "" + INT -> ".toInt()" + else -> error("Unknown kotlinType $int") + } + + assertThat(selectGenerator.customResultTypeFunction().toString()).isEqualTo( + """ + |/** + | * Queries all values. + | */ + |public fun selectAll(mapper: (_id: $int, value_: kotlin.String) -> T): app.cash.sqldelight.Query = app.cash.sqldelight.Query(-585795480, arrayOf("test"), driver, "Test.sq", "selectAll", ""${'"'} + ||SELECT * + ||FROM test + |""${'"'}.trimMargin()) { cursor -> + | ${testDialect.cursorCheck(2)}mapper( + | cursor.getLong(0)!!$toInt, + | cursor.getString(1)!! + | ) + |} + | + """.trimMargin(), + ) } @Test fun `select - misformatted javadoc`(testDialect: TestDialect) { @@ -200,7 +229,7 @@ class JavadocTest { createTable(testDialect) + """ |/** |Queries all values. - | */ + |*/ |selectAll: |SELECT * |FROM test; diff --git a/sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/queries/SelectQueryTypeTest.kt b/sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/queries/SelectQueryTypeTest.kt index a0ed75c8701..9c44cba7669 100644 --- a/sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/queries/SelectQueryTypeTest.kt +++ b/sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/queries/SelectQueryTypeTest.kt @@ -1254,7 +1254,7 @@ class SelectQueryTypeTest { | |SELECT birthday, age | |FROM adults | ""${'"'}.trimMargin()) { cursor -> - | ${dialect.cursorCheck}mapper( + | ${dialect.cursorCheck(4)}mapper( | cursor.getString(0)?.let { childrenAdapter.birthdayAdapter.decode(it) }, | cursor.getString(1) | )