Skip to content

Commit

Permalink
Fixes #3338 (#3339)
Browse files Browse the repository at this point in the history
* Add reproducer for #3338

* Fixes #3338

* Fix not updating cursorGetter

Co-authored-by: hfhbd <hfhbd@users.noreply.github.com>
  • Loading branch information
hfhbd and hfhbd committed Jun 26, 2022
1 parent 573aef9 commit f03d780
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Expand Up @@ -89,7 +89,7 @@ internal fun IntermediateType.cursorGetter(columnIndex: Int): CodeBlock {
var cursorGetter = dialectType.cursorGetter(columnIndex, CURSOR_NAME)

if (!javaType.isNullable) {
cursorGetter = CodeBlock.of("$cursorGetter!!")
cursorGetter = CodeBlock.of("%L!!", cursorGetter)
}

return (column?.columnType as ColumnTypeMixin?)?.adapter()?.let { adapter ->
Expand Down
@@ -1,23 +1,49 @@
package foo

import app.cash.sqldelight.dialect.api.DialectType
import app.cash.sqldelight.dialect.api.IntermediateType
import app.cash.sqldelight.dialect.api.PrimitiveType
import app.cash.sqldelight.dialect.api.SqlDelightDialect
import app.cash.sqldelight.dialect.api.TypeResolver
import app.cash.sqldelight.dialect.api.encapsulatingType
import app.cash.sqldelight.dialects.sqlite_3_18.SqliteDialect
import app.cash.sqldelight.dialects.sqlite_3_18.SqliteTypeResolver
import com.alecstrong.sql.psi.core.psi.SqlFunctionExpr
import com.alecstrong.sql.psi.core.psi.SqlTypeName
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.MemberName
import com.squareup.kotlinpoet.TypeName

class FooDialect : SqlDelightDialect by SqliteDialect() {
override fun typeResolver(parentResolver: TypeResolver) = CustomResolver(parentResolver)

class CustomResolver(private val parentResolver: TypeResolver) : TypeResolver by SqliteTypeResolver(parentResolver) {
override fun functionType(functionExpr: SqlFunctionExpr): IntermediateType? {
return when (functionExpr.functionName.text.lowercase()) {
"foo" -> encapsulatingType(functionExpr.exprList, PrimitiveType.INTEGER)
"foo" -> encapsulatingType(functionExpr.exprList, ExtensionType).asNonNullable()
else -> parentResolver.functionType(functionExpr)
}
}

override fun definitionType(typeName: SqlTypeName): IntermediateType {
return when (typeName) { else -> IntermediateType(ExtensionType) }
}
}

private object ExtensionType : DialectType {
override val javaType: TypeName = ClassName("kotlin.time", "Duration")
override fun cursorGetter(columnIndex: Int, cursorName: String): CodeBlock {
return CodeBlock.builder()
.add(
"$cursorName.getLong($columnIndex)?.%M(%M)",
MemberName("kotlin.time", "toDuration", isExtension = true),
MemberName("kotlin.time.DurationUnit", "SECONDS")
)
.build()
}

override fun prepareStatementBinder(columnIndex: String, value: CodeBlock): CodeBlock {
return CodeBlock.of("""TODO("Not yet implemented")""")
}
}
}

0 comments on commit f03d780

Please sign in to comment.