https://kotlinlang.org logo
#squarelibraries
Title
# squarelibraries
d

Daniele B

09/19/2023, 4:36 PM
I just migrated my sqldelight to 2.0.0 and I found out the syntax got much uglier. I used to instantiate my db in this way:
Copy code
internal val localDb by lazy { LocalDb(sqlDriver) }
and now it seems I need to do this way:
Copy code
internal val localDb by lazy { LocalDb(sqlDriver, Countries.Adapter(IntColumnAdapter,IntColumnAdapter,IntColumnAdapter)) }
because I have 3 integers, which for some reason require an adapter. Wasn't it possible to implicitly avoid such verbose definition?
j

jw

09/19/2023, 4:38 PM
We chose not to. You only do this once so it really doesn't matter
1
d

Daniele B

09/19/2023, 4:39 PM
This is the table I defined in Countries.sq:
Copy code
import <http://kotlin.Int|kotlin.Int>;
import kotlin.Boolean;

CREATE TABLE Countries (
    name TEXT NOT NULL PRIMARY KEY,
    population INTEGER AS Int NOT NULL,
    first_doses INTEGER AS Int NOT NULL,
    fully_vaccinated INTEGER AS Int NOT NULL,
    favorite INTEGER AS Boolean NOT NULL DEFAULT 0
);
This is what got generated:
Copy code
public data class Countries(
  public val name: String,
  public val population: Int,
  public val first_doses: Int,
  public val fully_vaccinated: Int,
  public val favorite: Boolean,
) {
  public class Adapter(
    public val populationAdapter: ColumnAdapter<Int, Long>,
    public val first_dosesAdapter: ColumnAdapter<Int, Long>,
    public val fully_vaccinatedAdapter: ColumnAdapter<Int, Long>,
  )
}
and this is the "invoke" function that got generated:
Copy code
public operator fun invoke(driver: SqlDriver, CountriesAdapter: Countries.Adapter): LocalDb =
    LocalDb::class.newInstance(driver, CountriesAdapter)
d

Daniele B

09/19/2023, 5:55 PM
Yes, I saw that, but it doesn't specify that you need to pass an adapter object to the DB instance invoker. I guess it shouldn't be a difficult task to automatically generate the invoker function to still get a single parameter:
Copy code
public operator fun invoke(driver: SqlDriver): LocalDb {
      val adapter = Countries.Adapter(IntColumnAdapter,IntColumnAdapter, IntColumnAdapter)
      return LocalDb::class.newInstance(driver, adapter)
    }
j

jw

09/19/2023, 5:56 PM
That behavior is generally unchanged in relation to 1.x. You always had to supply adapters where they were needed to do column mapping. I guess the difference is now that more adapters are generated whereas before they may have not been needed.
d

Daniele B

09/19/2023, 6:02 PM
Ok, I see. That must be because `Int`s now require an adapter. But I believe the API of the invoke function could be simplified very easily. Not much effort required.
Afterall you are already generating the invoke function. It's just a matter of adding an extra line.
3 Views