SQLDelight in Android: Getting Started

Aug 3 2021 · Kotlin 1.4, Android 11, Android Studio 4.1

Part 1: Preparation & Setup

05. Understand SQLDelight's Type System

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 04. Instantiate the Database Next episode: 06. Add Functions to Tables

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Let’s take a closer look at the Kotlin code that SQLDelight generated from the script files we have created previously.

CREATE TABLE bugAttributes (
  attributesId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  bugId INTEGER NOT NULL,
  size TEXT NOT NULL,
  weight TEXT NOT NULL,
- attack INTEGER NOT NULL,
- defense INTEGER NOT NULL,
+ attack INTEGER AS Int NOT NULL,
+ defense INTEGER AS Int NOT NULL,

  FOREIGN KEY(bugId)
    REFERENCES bug(bugId)
    ON DELETE CASCADE
);
CREATE TABLE inCollection (
  collectionId INTEGER NOT NULL,
  bugId INTEGER NOT NULL,
- quantity INTEGER NOT NULL,
+ quantity INTEGER AS Int NOT NULL,

  FOREIGN KEY(collectionId)
    REFERENCES collection(collectionId)
    ON DELETE CASCADE,

  FOREIGN KEY(bugId)
    REFERENCES bug(bugId)
    ON DELETE CASCADE
);
+import java.time.ZonedDateTime;

CREATE TABLE collection (
  collectionId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
-  creationTime INTEGER NOT NULL,
+  creationTime INTEGER AS ZonedDateTime NOT NULL,
  name TEXT NOT NULL
);
class DatabaseRepository(driver: SqlDriver) {

-    private val database = Database(driver)
+    private val database = Database(
+        driver = driver,
+        collectionAdapter = 
+    )
}
class DatabaseRepository(driver: SqlDriver) {

    private val database = Database(
        driver = driver,
+        collectionAdapter = Collection.Adapter(
+            creationTimeAdapter = 
+        )
    )
}
private val zonedDateTimeAdapter = object : ColumnAdapter<ZonedDateTime, Long> {
}
private val zonedDateTimeAdapter = object : ColumnAdapter<ZonedDateTime, Long> {
    override fun decode(databaseValue: Long): ZonedDateTime {
        TODO()
    }

    override fun encode(value: ZonedDateTime): Long {
        TODO()
    }
}
+private val UTC = ZoneId.of("UTC")

private val zonedDateTimeAdapter = object : ColumnAdapter<ZonedDateTime, Long> {
    override fun decode(databaseValue: Long): ZonedDateTime {
+        return ZonedDateTime.ofInstant(Instant.ofEpochSecond(databaseValue), UTC)
    }

    override fun encode(value: ZonedDateTime): Long {
        TODO()
    }
}
private val UTC = ZoneId.of("UTC")

private val zonedDateTimeAdapter = object : ColumnAdapter<ZonedDateTime, Long> {
    override fun decode(databaseValue: Long): ZonedDateTime {
        return ZonedDateTime.ofInstant(Instant.ofEpochSecond(databaseValue), UTC)
    }

    override fun encode(value: ZonedDateTime): Long {
+        return value.toEpochSecond()
    }
}
class DatabaseRepository(driver: SqlDriver) {

    private val database = Database(
        driver = driver,
        collectionAdapter = Collection.Adapter(
+            creationTimeAdapter = zonedDateTimeAdapter
        )
    )
}
-holder.creationTextView.text = item.creationTime.toString()
+holder.creationTextView.text = item.creationTime.format(formatter)