Skip to content

Commit

Permalink
✨ Database クラスを作成
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsutakein committed Dec 11, 2023
1 parent eca6bee commit 8cf9a4a
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package club.nito.core.database

import android.content.Context
import androidx.sqlite.db.SupportSQLiteDatabase
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.android.AndroidSqliteDriver

internal actual class DriverFactory(private val context: Context) {
actual fun createDriver(): SqlDriver {
val schema = Database.Schema

return AndroidSqliteDriver(
schema = schema,
context = context,
name = DATABASE_NAME,
callback = object : AndroidSqliteDriver.Callback(schema) {
override fun onConfigure(db: SupportSQLiteDatabase) {
db.enableWriteAheadLogging()
db.setForeignKeyConstraintsEnabled(true)
}
},
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package club.nito.core.database.di

import club.nito.core.database.DriverFactory
import org.koin.core.scope.Scope

internal actual fun Scope.createDriverFactory(): DriverFactory = DriverFactory(
context = get(),
)
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package club.nito.core.database

import app.cash.sqldelight.EnumColumnAdapter
import app.cash.sqldelight.db.SqlDriver

internal expect class DriverFactory {
fun createDriver(): SqlDriver
}

internal const val DATABASE_NAME = "nito.db"

internal fun createDatabase(driverFactory: DriverFactory): Database {
val driver = driverFactory.createDriver()
val database = Database(
driver = driver,
participantsAdapter = Participants.Adapter(
statusAdapter = EnumColumnAdapter(),
),
)

// Do more work with the database (see below).
return database
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package club.nito.core.database.adapter

import app.cash.sqldelight.ColumnAdapter
import club.nito.core.model.participant.ParticipantStatus

internal data object ParticipantStatusAdapter : ColumnAdapter<ParticipantStatus, String> {
override fun decode(databaseValue: String): ParticipantStatus = ParticipantStatus.valueOf(databaseValue)

override fun encode(value: ParticipantStatus): String = value.name
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package club.nito.core.database.di

import club.nito.core.database.DriverFactory
import club.nito.core.database.createDatabase
import org.koin.core.module.Module
import org.koin.core.scope.Scope
import org.koin.dsl.module

public val databaseModule: Module = module {
single {
createDatabase(
driverFactory = createDriverFactory(),
)
}
}

internal expect fun Scope.createDriverFactory(): DriverFactory
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import club.nito.core.model.participant.ParticipantStatus;

CREATE TABLE `participants` (
`schedule_id` TEXT NOT NULL,
`user_id` TEXT NOT NULL,
`status` TEXT AS ParticipantStatus NOT NULL
);

CREATE UNIQUE INDEX `participants_pkey` ON `participants` (`schedule_id`, `user_id`);

participantsByScheduleId:
SELECT *
FROM participants
WHERE schedule_id = ?;

participantsByUserId:
SELECT *
FROM participants
WHERE user_id = ?;

upsert {
UPDATE participants SET
`schedule_id` = :schedule_id,
`user_id` = :user_id,
`status` = :status
WHERE schedule_id = :schedule_id AND user_id = :user_id;

INSERT OR IGNORE INTO participants (
`schedule_id`,
`user_id`,
`status`
) VALUES (
:schedule_id,
:user_id,
:status
);
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package club.nito.core.database

import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.native.NativeSqliteDriver

internal actual class DriverFactory {
actual fun createDriver(): SqlDriver = NativeSqliteDriver(
schema = Database.Schema,
name = DATABASE_NAME,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package club.nito.core.database.di

import club.nito.core.database.DriverFactory
import org.koin.core.scope.Scope

internal actual fun Scope.createDriverFactory(): DriverFactory = DriverFactory()
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package club.nito.core.database

import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.worker.WebWorkerDriver
import org.w3c.dom.Worker

internal actual class DriverFactory {
actual fun createDriver(): SqlDriver {
return WebWorkerDriver(
Worker(
js("""new URL("@cashapp/sqldelight-sqljs-worker/sqljs.worker.js", import.meta.url)""")
)
).also { Database.Schema.create(it) }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package club.nito.core.database.di

import club.nito.core.database.DriverFactory
import org.koin.core.scope.Scope

internal actual fun Scope.createDriverFactory(): DriverFactory = DriverFactory()

0 comments on commit 8cf9a4a

Please sign in to comment.