SQLDelight in Android: Getting Started

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

Part 1: Preparation & Setup

10. Create Views for Reusable Sub-Queries

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: 09. Write Migrations for Your Database Next episode: 11. Validate & Test Database Code

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

We have learnt that for any SELECT query added to a script file, SQLDelight will generate not only a method,

listBugsInCollectionByName:
SELECT
  bug.bugId,
  name,
  imageUrl,
  quantity
FROM inCollection
NATURAL JOIN bug
WHERE collectionId = :collectionId
ORDER BY name;

listBugsInCollectionByQuantity:
SELECT
  bug.bugId,
  name,
  imageUrl,
  quantity
FROM inCollection
NATURAL JOIN bug
WHERE collectionId = :collectionId
ORDER BY quantity;
CREATE VIEW bugsInCollection AS
SELECT
  collectionId,
  bug.bugId,
  name,
  imageUrl,
  quantity
FROM inCollection
NATURAL JOIN bug;
listBugsInCollectionByName:
-SELECT
-  bug.bugId,
-  name,
-  imageUrl,
-  quantity
-FROM inCollection
-NATURAL JOIN bug
-WHERE collectionId = :collectionId
+SELECT *
+FROM bugsInCollection
+WHERE collectionId = :collectionId
ORDER BY name;

listBugsInCollectionByQuantity:
-SELECT
-  bug.bugId,
-  name,
-  imageUrl,
-  quantity
-FROM inCollection
-NATURAL JOIN bug
-WHERE collectionId = :collectionId
+SELECT *
+FROM bugsInCollection
+WHERE collectionId = :collectionId
ORDER BY quantity;
listBugsInCollection:
-SELECT
-  bug.bugId,
-  name,
-  imageUrl,
-  quantity
-FROM inCollection
-NATURAL JOIN bug
-WHERE collectionId = :collectionId;
+SELECT *
+FROM bugsInCollection
+WHERE collectionId = :collectionId;
fun listBugsInCollection(collectionId: Long): Query<BugWithQuantity> {
    return database.inCollectionQueries
-        .listBugsInCollection(collectionId) { bugId, name, imageUrl, quantity ->
+        .listBugsInCollection(collectionId) { _, bugId, name, imageUrl, quantity ->
            BugWithQuantity(bugId, name, imageUrl, quantity)
        }
}