SQLDelight Database

This commit is contained in:
shabinder 2021-01-27 13:27:42 +05:30
parent be23c0e4c5
commit 6b024ec430
7 changed files with 77 additions and 40 deletions

View File

@ -0,0 +1,10 @@
package com.shabinder.common
data class DownloadRecord(
var id:Int = 0,
var type:String,
var name:String,
var link:String,
var coverUrl:String,
var totalFiles:Int = 1,
)

View File

@ -5,7 +5,7 @@ plugins {
} }
sqldelight { sqldelight {
database("TodoDatabase") { database("DownloadRecordDatabase") {
packageName = "com.shabinder.database" packageName = "com.shabinder.database"
} }
} }

View File

@ -0,0 +1,12 @@
package com.shabinder.common.database
import android.content.Context
import com.shabinder.database.DownloadRecordDatabase
import com.squareup.sqldelight.android.AndroidSqliteDriver
import com.squareup.sqldelight.db.SqlDriver
actual class DatabaseDriverFactory(private val context: Context) {
actual fun createDriver(): SqlDriver {
return AndroidSqliteDriver(DownloadRecordDatabase.Schema, context, "DownloadRecordDatabase.db")
}
}

View File

@ -0,0 +1,7 @@
package com.shabinder.common.database
import com.squareup.sqldelight.db.SqlDriver
expect class DatabaseDriverFactory {
fun createDriver(): SqlDriver
}

View File

@ -0,0 +1,31 @@
CREATE TABLE IF NOT EXISTS DownloadRecord (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
type TEXT NOT NULL,
name TEXT NOT NULL,
link TEXT NOT NULL UNIQUE ON CONFLICT REPLACE ,
coverUrl TEXT NOT NULL,
totalFiles INTEGER NOT NULL DEFAULT 1
);
selectAll:
SELECT *
FROM DownloadRecord;
select:
SELECT *
FROM DownloadRecord
WHERE link = :link;
add:
INSERT OR REPLACE INTO DownloadRecord (type, name, link, coverUrl, totalFiles)
VALUES (?,?,?,?,?);
delete:
DELETE FROM DownloadRecord
WHERE link = :link;
getLastInsertId:
SELECT last_insert_rowid();
clear:
DELETE FROM DownloadRecord;

View File

@ -1,39 +0,0 @@
CREATE TABLE IF NOT EXISTS TodoItemEntity (
id INTEGER PRIMARY KEY AUTOINCREMENT,
orderNum INTEGER NOT NULL,
text TEXT NOT NULL,
isDone INTEGER AS Boolean NOT NULL DEFAULT 0
);
selectAll:
SELECT *
FROM TodoItemEntity;
select:
SELECT *
FROM TodoItemEntity
WHERE id = :id;
add:
INSERT INTO TodoItemEntity (orderNum, text)
VALUES ((CASE (SELECT COUNT(*) FROM TodoItemEntity) WHEN 0 THEN 1 ELSE (SELECT MAX(orderNum)+1 FROM TodoItemEntity) END), :text);
setText:
UPDATE TodoItemEntity
SET text = :text
WHERE id = :id;
setDone:
UPDATE TodoItemEntity
SET isDone = :isDone
WHERE id = :id;
delete:
DELETE FROM TodoItemEntity
WHERE id = :id;
getLastInsertId:
SELECT last_insert_rowid();
clear:
DELETE FROM TodoItemEntity;

View File

@ -0,0 +1,16 @@
package com.shabinder.common.database
import com.shabinder.database.DownloadRecordDatabase
import com.squareup.sqldelight.db.SqlDriver
import com.squareup.sqldelight.sqlite.driver.JdbcSqliteDriver
import java.io.File
actual class DatabaseDriverFactory {
actual fun createDriver(): SqlDriver {
val databasePath = File(System.getProperty("java.io.tmpdir"), "DownloadRecordDatabase.db")
val driver = JdbcSqliteDriver(url = "jdbc:sqlite:${databasePath.absolutePath}")
DownloadRecordDatabase.Schema.create(driver)
return driver
}
}