Skip to content
This repository has been archived by the owner on Oct 28, 2022. It is now read-only.

ColumnAdapter Generation

Kaustubh Patange edited this page Aug 23, 2021 · 4 revisions

API might be unstable if you find any issues feel free to report them.

Eliminates the need for writing a custom ColumnAdapter for SQLDelight, check sample-ktx app to see a running sample.

Supports serialization with Gson, Moshi, kotlinx-serialization.

Setup

implementation 'io.github.kaustubhpatange:autobindings-sqldelight:<version>'

// Kotlin
apply plugin: 'kotlin-kapt' // at top of your module build.gradle file
kapt 'io.github.kaustubhpatange:autobindings-compiler:<version>'
// Java
annotationProcessor 'io.github.kaustubhpatange:autobindings-compiler:<version>'

Example

  • Each column adapter is backed (enclosed) by an interface. So you can declare multiple adapters in the same interface which will generate a Impl class ready for use.
@JsonClass(generateAdapter = true) // Required for Moshi
data class User(val name: String, val age: Int)

// Serialize with Moshi. Make sure to add the dependency
@AutoGenerateSQLDelightAdapters(using = ConverterType.MOSHI)
interface SQLDelightAdapters {
    fun dataConverter(): ColumnAdapter<User, String>
    // Support for List<T>
    fun dataListConverter(): ColumnAdapter<List<User>, String>
    // Support for Map<K, V>, Pair<K, V>, Dictionary<K, V>
    fun dataMapConverter(): ColumnAdapter<Map<String, User>, String>
}
  • You must always ensure that the second argument for ColumnAdapter must be String so they can be mapped to a TEXT data otherwise processor will throw an exception.
  • The resulting class will implement the interface & will generate all the necessary code.
  • The Impl class can then be used as shown below. You can also inject this interface with a dependency injection framework and use it (similar to retrofit).
// usage
val database = Database(driver, DataDomainAdapter = DataDomain.Adapter(
    dataListAdapter = SQLDelightAdaptersImpl.dataListConverter
))
Clone this wiki locally