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

SQLDelight 1.x Quick Start Guide #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: "com.squareup.sqldelight"

android {
compileSdkVersion 28
defaultConfig {
Expand All @@ -21,7 +23,9 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation "com.squareup.sqldelight:android-driver:1.2.0"
testImplementation 'junit:junit:4.12'
testImplementation "com.squareup.sqldelight:sqlite-driver:1.2.0"
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,38 @@
package com.handstandsam.sqldelightquickstart

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.squareup.sqldelight.android.AndroidSqliteDriver

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// Don't ever put db code in your activity, but this is a quick start guide
val androidSqlDriver = AndroidSqliteDriver(
schema = Database.Schema,
context = applicationContext,
name = "items.db"
)

val queries = Database(androidSqlDriver).itemInCartEntityQueries

val itemsBefore: List<ItemInCart> = queries.selectAll().executeAsList()
Log.d("ItemDatabase", "Items Before: $itemsBefore")

for (i in 1..3) {
queries.insertOrReplace(
label = "Item $i",
image = "https://localhost/item$i.png",
quantity = i.toLong(),
link = null
)
}

val itemsAfter: List<ItemInCart> = queries.selectAll().executeAsList()
Log.d("ItemDatabase", "Items After: $itemsAfter")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
CREATE TABLE itemInCart (
label TEXT NOT NULL UNIQUE PRIMARY KEY,
image TEXT NOT NULL,
quantity INTEGER NOT NULL DEFAULT 0,
link TEXT
);

selectAll:
SELECT *
FROM itemInCart
ORDER BY label;

insertOrReplace:
INSERT OR REPLACE INTO itemInCart(
label,
image,
quantity,
link
)
VALUES (?, ?, ?, ?);

selectByLabel:
SELECT *
FROM itemInCart
WHERE label = ?;

empty:
DELETE FROM itemInCart;

deleteByLabel:
DELETE
FROM itemInCart
WHERE label = ?;
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.handstandsam.sqldelightquickstart

import com.squareup.sqldelight.sqlite.driver.JdbcSqliteDriver
import org.junit.Assert.assertEquals
import org.junit.Test

class ItemDatabaseTest {

private val inMemorySqlDriver = JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY).apply {
Database.Schema.create(this)
}

private val queries = Database(inMemorySqlDriver).itemInCartEntityQueries

@Test
fun smokeTest() {
val emptyItems: List<ItemInCart> = queries.selectAll().executeAsList()
assertEquals(emptyItems.size, 0)

queries.insertOrReplace(
label = "Pineapple",
image = "https://localhost/pineapple.png",
quantity = 5,
link = null
)

val items: List<ItemInCart> = queries.selectAll().executeAsList()
assertEquals(items.size, 1)

val pineappleItem = queries.selectByLabel("Pineapple").executeAsOneOrNull()
assertEquals(pineappleItem?.image, "https://localhost/pineapple.png")
assertEquals(pineappleItem?.quantity?.toInt(), 5)
}
}
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.squareup.sqldelight:gradle-plugin:1.2.0"
}
}

Expand Down