Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #3338 #3339

Merged
merged 3 commits into from
Jun 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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")""")
}
}
}