Skip to content

Add SQL Delight Usage #4

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

Merged
merged 6 commits into from
Sep 6, 2023
Merged
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,5 @@ bin/
.vscode/

### Mac OS ###
.DS_Store
.DS_Store
local.properties
40 changes: 40 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,16 +1,56 @@
plugins {
kotlin("jvm") version "1.9.0"

// SQLDelight - Generates typesafe Kotlin APIs from SQL
// https://cashapp.github.io/sqldelight/2.0.0/
id("app.cash.sqldelight") version "2.0.0"

application
}

group = "dev.hossain.postgresqldelight"
version = "1.0-SNAPSHOT"

repositories {
google()
mavenCentral()
}

// SQLDelight - Generates typesafe Kotlin APIs from SQL
// https://github.com/cashapp/sqldelight
sqldelight {
databases {
create("SportsDatabase") {
packageName.set("dev.hossain.postgresqldelight")
// https://cashapp.github.io/sqldelight/2.0.0/jvm_postgresql/
dialect("app.cash.sqldelight:postgresql-dialect:2.0.0")
}
}
}

java {
toolchain.languageVersion.set(JavaLanguageVersion.of(19))
}

dependencies {
// PostgreSQL is a powerful object-relational database system
// https://www.postgresql.org/
// https://mvnrepository.com/artifact/org.postgresql/postgresql
implementation("org.postgresql:postgresql:42.6.0")

// 光 HikariCP・A solid, high-performance, JDBC connection pool at last.
// https://github.com/brettwooldridge/HikariCP#artifacts
// https://www.baeldung.com/hikaricp
implementation("com.zaxxer:HikariCP:5.0.1")

// SQLDelight - Generates typesafe Kotlin APIs from SQL
// https://cashapp.github.io/sqldelight/2.0.0/jvm_postgresql
implementation("app.cash.sqldelight:jdbc-driver:2.0.0")

// Faker - Generates fake data for testing or populating a development database.
// https://github.com/blocoio/faker
implementation("io.github.serpro69:kotlin-faker:1.14.0")

testImplementation(kotlin("test"))
}

Expand Down
8 changes: 8 additions & 0 deletions sample-local.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## Rename this file to `local.properties`
## These properties are loaded via `AppConfigLoader`
# ----------------------------------------------------

## Data based properties
db_name=YourDbName
db_username=your_db_username
db_password=your_db_password
7 changes: 0 additions & 7 deletions src/main/kotlin/Main.kt

This file was deleted.

3 changes: 3 additions & 0 deletions src/main/kotlin/dev/hossain/postgresqldelight/AppConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package dev.hossain.postgresqldelight

data class AppConfig(val dbName: String, val dbUsername: String, val dbPassword: String)
18 changes: 18 additions & 0 deletions src/main/kotlin/dev/hossain/postgresqldelight/AppConfigLoader.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dev.hossain.postgresqldelight

import java.io.File
import java.util.*

class AppConfigLoader {
/**
* Loads app config from local properties file.
*/
fun loadAppConfig(): AppConfig {
val properties = Properties().apply { load(File("local.properties").inputStream()) }
return AppConfig(
dbName = properties.getProperty("db_name"),
dbUsername = properties.getProperty("db_username"),
dbPassword = properties.getProperty("db_password"),
)
}
}
52 changes: 52 additions & 0 deletions src/main/kotlin/dev/hossain/postgresqldelight/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package dev.hossain.postgresqldelight

import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.jdbc.asJdbcDriver
import io.github.serpro69.kfaker.Faker
import io.github.serpro69.kfaker.faker
import javax.sql.DataSource
import kotlin.math.absoluteValue
import kotlin.random.Random


fun main(args: Array<String>) {
println("Begin SQLDelight 2.0 with PostgreSQL Sample!")
val appConfigLoader = AppConfigLoader()
val sportsRepository = SportsRepository()
val dataSource: DataSource = sportsRepository.getSource(appConfigLoader.loadAppConfig())


val driver: SqlDriver = dataSource.asJdbcDriver()
SportsDatabase.Schema.create(driver)

testDriveDatabase(driver, faker { })
}


/**
* @param driver the [SqlDriver] required to create the database.
* @param faker Fake data generator.
*/
fun testDriveDatabase(driver: SqlDriver, faker: Faker) {
val database = SportsDatabase(driver)

val playerQueries: PlayerQueries = database.playerQueries

// Show all players
println(playerQueries.selectAll().executeAsList())


// Uses query param to insert data
playerQueries.insert(
player_number = Random.nextInt().absoluteValue,
full_name = faker.name.name()
)
println(playerQueries.selectAll().executeAsList())

// Uses full data object to insert data
val player = HockeyPlayer(
player_number = Random.nextInt().absoluteValue,
full_name = faker.name.name()
)
playerQueries.insertFullPlayerObject(player)
}
21 changes: 21 additions & 0 deletions src/main/kotlin/dev/hossain/postgresqldelight/SportsRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dev.hossain.postgresqldelight

import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import javax.sql.DataSource

class SportsRepository constructor(

) {

fun getSource(appConfig: AppConfig): DataSource {
val hikariConfig = HikariConfig()
// https://jdbc.postgresql.org/documentation/use/
hikariConfig.setJdbcUrl("jdbc:postgresql://192.168.2.32:5432/${appConfig.dbName}")
hikariConfig.driverClassName = "org.postgresql.Driver"
hikariConfig.username = appConfig.dbUsername
hikariConfig.password = appConfig.dbPassword

return HikariDataSource(hikariConfig)
}
}
25 changes: 25 additions & 0 deletions src/main/sqldelight/dev/hossain/postgresqldelight/Player.sq
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-- Source: https://cashapp.github.io/sqldelight/2.0.0/jvm_postgresql/#fresh-schema
CREATE TABLE IF NOT EXISTS hockeyPlayer (
player_number INTEGER PRIMARY KEY NOT NULL,
full_name TEXT NOT NULL
);

CREATE INDEX IF NOT EXISTS hockeyPlayer_full_name ON hockeyPlayer(full_name);


selectAll:
SELECT *
FROM hockeyPlayer;

insert:
INSERT INTO hockeyPlayer(player_number, full_name)
VALUES (?, ?);

insertFullPlayerObject:
INSERT INTO hockeyPlayer(player_number, full_name)
VALUES ?;


-- tableExists:
-- https://database.guide/5-ways-to-check-if-a-table-exists-in-postgresql/
-- SELECT EXISTS (SELECT FROM pg_tables WHERE schemaname = 'public' AND tablename = 'hockeyPlayer');