Skip to content

Commit

Permalink
migrate sample to use kotlinx.datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaGulya committed Feb 19, 2024
1 parent 25d474b commit 0f55176
Show file tree
Hide file tree
Showing 15 changed files with 41 additions and 3,073 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ spotless {

allprojects {
repositories {
mavenLocal()
mavenCentral()
google()
maven { url 'https://www.jetbrains.com/intellij-repository/releases' }
Expand Down
3 changes: 2 additions & 1 deletion sample/common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ version = 1.0

kotlin {
jvmToolchain(8)

// Configure common.
sourceSets.commonMain.dependencies {
implementation 'app.cash.sqldelight:primitive-adapters'
api "org.jetbrains.kotlinx:kotlinx-datetime:0.5.0"
}
sourceSets.commonTest.dependencies {
implementation 'org.jetbrains.kotlin:kotlin-test'
Expand Down
21 changes: 0 additions & 21 deletions sample/common/karma.config.d/wasm.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package com.example.sqldelight.hockey.data

import app.cash.sqldelight.ColumnAdapter
import kotlinx.datetime.LocalDate

expect class Date(year: Int, month: Int, day: Int)
class DateAdapter : ColumnAdapter<LocalDate, Long> {
override fun decode(databaseValue: Long): LocalDate {
return LocalDate.fromEpochDays(databaseValue.toInt())
}

expect class DateAdapter() : ColumnAdapter<Date, Long>
override fun encode(value: LocalDate): Long {
return value.toEpochDays().toLong()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import app.cash.sqldelight.db.SqlSchema
import com.example.sqldelight.hockey.HockeyDb
import com.example.sqldelight.hockey.data.PlayerVals.Position
import com.example.sqldelight.hockey.data.PlayerVals.Shoots
import kotlinx.datetime.LocalDate

fun createQueryWrapper(driver: SqlDriver): HockeyDb {
return HockeyDb(
Expand Down Expand Up @@ -38,33 +39,33 @@ object Schema : SqlSchema<QueryResult.Value<Unit>> by HockeyDb.Schema {
val sharks = "San Jose Sharks"

// Populate teams.
teamQueries.insertTeam(ducks, Date(1993, 3, 1), "Randy Carlyle", true)
teamQueries.insertTeam(pens, Date(1966, 2, 8), "Mike Sullivan", true)
teamQueries.insertTeam(sharks, Date(1990, 5, 5), "Peter DeBoer", false)
teamQueries.insertTeam(ducks, LocalDate(1993, 3, 1), "Randy Carlyle", true)
teamQueries.insertTeam(pens, LocalDate(1966, 2, 8), "Mike Sullivan", true)
teamQueries.insertTeam(sharks, LocalDate(1990, 5, 5), "Peter DeBoer", false)

playerQueries.insertPlayer(
"Corey", "Perry", 10, ducks, 30, 210F, Date(1985, 5, 16),
"Corey", "Perry", 10, ducks, 30, 210F, LocalDate(1985, 5, 16),
Shoots.RIGHT, Position.RIGHT_WING,
)
playerQueries.insertPlayer(
"Ryan", "Getzlaf", 15, ducks, 30, 221F, Date(1985, 5, 10),
"Ryan", "Getzlaf", 15, ducks, 30, 221F, LocalDate(1985, 5, 10),
Shoots.RIGHT, Position.CENTER,
)
teamQueries.setCaptain(15, ducks)

playerQueries.insertPlayer(
"Sidney", "Crosby", 87, pens, 28, 200F, Date(1987, 8, 7),
"Sidney", "Crosby", 87, pens, 28, 200F, LocalDate(1987, 8, 7),
Shoots.LEFT, Position.CENTER,
)
teamQueries.setCaptain(87, pens)

playerQueries.insertPlayer(
"Erik", "Karlsson", 65, sharks, 28, 190F, Date(1990, 5, 31),
"Erik", "Karlsson", 65, sharks, 28, 190F, LocalDate(1990, 5, 31),
Shoots.RIGHT, Position.DEFENSE,
)

playerQueries.insertPlayer(
"Joe", "Pavelski", 8, sharks, 31, 194F, Date(1984, 7, 18),
"Joe", "Pavelski", 8, sharks, 31, 194F, LocalDate(1984, 7, 18),
Shoots.RIGHT, Position.CENTER,
)
teamQueries.setCaptain(8, sharks)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.example.sqldelight.hockey.platform

import com.example.sqldelight.hockey.data.Date
import kotlinx.datetime.LocalDate

expect class DateFormatHelper(format: String) {
fun format(d: Date): String
fun format(d: LocalDate): String
}

val defaultFormatter = DateFormatHelper("dd/mm/yyyy")
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import com.example.sqldelight.hockey.data.PlayerVals;
import com.example.sqldelight.hockey.data.Date;
import kotlinx.datetime.LocalDate;
import kotlin.Float;
import kotlin.Int;

Expand All @@ -10,7 +10,7 @@ CREATE TABLE player (
number INTEGER AS Int NOT NULL,
team INTEGER,
age INTEGER AS Int NOT NULL,
birth_date INTEGER AS Date NOT NULL,
birth_date INTEGER AS LocalDate NOT NULL,
weight REAL AS Float NOT NULL,
shoots TEXT AS PlayerVals.Shoots NOT NULL,
position TEXT AS PlayerVals.Position NOT NULL,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import com.example.sqldelight.hockey.data.Date;
import kotlin.Boolean;
import kotlinx.datetime.LocalDate;

CREATE TABLE team (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE,
founded INTEGER AS Date NOT NULL,
founded INTEGER AS LocalDate NOT NULL,
coach TEXT NOT NULL,
captain INTEGER,
won_cup INTEGER AS Boolean NOT NULL DEFAULT 0,
Expand All @@ -27,4 +27,4 @@ FROM team;
updateCoachForTeam:
UPDATE team
SET coach = ?
WHERE name = ?;
WHERE name = ?;
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.example.sqldelight.hockey.platform

import com.example.sqldelight.hockey.data.Date
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlinx.datetime.LocalDate

class DateFormatHelperTest {
@Test
fun format() {
val df = DateFormatHelper("yyyy-MM-dd")
val date = Date(2019, 3, 7)
val date = LocalDate(2019, 4, 7)
val formatted = df.format(date)

assertEquals("2019-04-07", formatted)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.sqldelight.hockey.platform

import com.example.sqldelight.hockey.data.Date
import kotlinx.datetime.LocalDate
import kotlinx.datetime.toNSDateComponents
import platform.Foundation.NSDateFormatter

actual class DateFormatHelper actual constructor(format: String) {
Expand All @@ -11,5 +12,5 @@ actual class DateFormatHelper actual constructor(format: String) {
formatter.dateFormat = format
}

actual fun format(d: Date): String = formatter.stringFromDate(d.nsDate)
actual fun format(d: LocalDate): String = formatter.stringFromDate(d.toNSDateComponents().date!!)
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package com.example.sqldelight.hockey.platform

import com.example.sqldelight.hockey.data.Date
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.*
import kotlinx.datetime.LocalDate
import kotlinx.datetime.TimeZone
import kotlinx.datetime.atStartOfDayIn

actual class DateFormatHelper actual constructor(format: String) {
val dateFormatter = object : ThreadLocal<DateFormat>() {
override fun initialValue(): DateFormat = SimpleDateFormat(format)
}

actual fun format(d: Date): String = dateFormatter.get()!!.format(d.time)
actual fun format(d: LocalDate): String {
val epochMilliseconds = d.atStartOfDayIn(TimeZone.currentSystemDefault()).toEpochMilliseconds()
return dateFormatter.get()!!.format(Date(epochMilliseconds))
}
}
9 changes: 0 additions & 9 deletions sample/common/webpack.config.d/fs.js

This file was deleted.

Loading

0 comments on commit 0f55176

Please sign in to comment.