Skip to content

Commit 8bf0559

Browse files
authored
Merge pull request #4 from hossain-khan/feature/add-sql-delight
Add SQL Delight Usage
2 parents 4ecc65b + 03ce7c5 commit 8bf0559

File tree

9 files changed

+169
-8
lines changed

9 files changed

+169
-8
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,5 @@ bin/
6464
.vscode/
6565

6666
### Mac OS ###
67-
.DS_Store
67+
.DS_Store
68+
local.properties

build.gradle.kts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,56 @@
11
plugins {
22
kotlin("jvm") version "1.9.0"
3+
4+
// SQLDelight - Generates typesafe Kotlin APIs from SQL
5+
// https://cashapp.github.io/sqldelight/2.0.0/
6+
id("app.cash.sqldelight") version "2.0.0"
7+
38
application
49
}
510

611
group = "dev.hossain.postgresqldelight"
712
version = "1.0-SNAPSHOT"
813

914
repositories {
15+
google()
1016
mavenCentral()
1117
}
1218

19+
// SQLDelight - Generates typesafe Kotlin APIs from SQL
20+
// https://github.com/cashapp/sqldelight
21+
sqldelight {
22+
databases {
23+
create("SportsDatabase") {
24+
packageName.set("dev.hossain.postgresqldelight")
25+
// https://cashapp.github.io/sqldelight/2.0.0/jvm_postgresql/
26+
dialect("app.cash.sqldelight:postgresql-dialect:2.0.0")
27+
}
28+
}
29+
}
30+
31+
java {
32+
toolchain.languageVersion.set(JavaLanguageVersion.of(19))
33+
}
34+
1335
dependencies {
36+
// PostgreSQL is a powerful object-relational database system
37+
// https://www.postgresql.org/
38+
// https://mvnrepository.com/artifact/org.postgresql/postgresql
39+
implementation("org.postgresql:postgresql:42.6.0")
40+
41+
// 光 HikariCP・A solid, high-performance, JDBC connection pool at last.
42+
// https://github.com/brettwooldridge/HikariCP#artifacts
43+
// https://www.baeldung.com/hikaricp
44+
implementation("com.zaxxer:HikariCP:5.0.1")
45+
46+
// SQLDelight - Generates typesafe Kotlin APIs from SQL
47+
// https://cashapp.github.io/sqldelight/2.0.0/jvm_postgresql
48+
implementation("app.cash.sqldelight:jdbc-driver:2.0.0")
49+
50+
// Faker - Generates fake data for testing or populating a development database.
51+
// https://github.com/blocoio/faker
52+
implementation("io.github.serpro69:kotlin-faker:1.14.0")
53+
1454
testImplementation(kotlin("test"))
1555
}
1656

sample-local.properties

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Rename this file to `local.properties`
2+
## These properties are loaded via `AppConfigLoader`
3+
# ----------------------------------------------------
4+
5+
## Data based properties
6+
db_name=YourDbName
7+
db_username=your_db_username
8+
db_password=your_db_password

src/main/kotlin/Main.kt

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package dev.hossain.postgresqldelight
2+
3+
data class AppConfig(val dbName: String, val dbUsername: String, val dbPassword: String)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package dev.hossain.postgresqldelight
2+
3+
import java.io.File
4+
import java.util.*
5+
6+
class AppConfigLoader {
7+
/**
8+
* Loads app config from local properties file.
9+
*/
10+
fun loadAppConfig(): AppConfig {
11+
val properties = Properties().apply { load(File("local.properties").inputStream()) }
12+
return AppConfig(
13+
dbName = properties.getProperty("db_name"),
14+
dbUsername = properties.getProperty("db_username"),
15+
dbPassword = properties.getProperty("db_password"),
16+
)
17+
}
18+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package dev.hossain.postgresqldelight
2+
3+
import app.cash.sqldelight.db.SqlDriver
4+
import app.cash.sqldelight.driver.jdbc.asJdbcDriver
5+
import io.github.serpro69.kfaker.Faker
6+
import io.github.serpro69.kfaker.faker
7+
import javax.sql.DataSource
8+
import kotlin.math.absoluteValue
9+
import kotlin.random.Random
10+
11+
12+
fun main(args: Array<String>) {
13+
println("Begin SQLDelight 2.0 with PostgreSQL Sample!")
14+
val appConfigLoader = AppConfigLoader()
15+
val sportsRepository = SportsRepository()
16+
val dataSource: DataSource = sportsRepository.getSource(appConfigLoader.loadAppConfig())
17+
18+
19+
val driver: SqlDriver = dataSource.asJdbcDriver()
20+
SportsDatabase.Schema.create(driver)
21+
22+
testDriveDatabase(driver, faker { })
23+
}
24+
25+
26+
/**
27+
* @param driver the [SqlDriver] required to create the database.
28+
* @param faker Fake data generator.
29+
*/
30+
fun testDriveDatabase(driver: SqlDriver, faker: Faker) {
31+
val database = SportsDatabase(driver)
32+
33+
val playerQueries: PlayerQueries = database.playerQueries
34+
35+
// Show all players
36+
println(playerQueries.selectAll().executeAsList())
37+
38+
39+
// Uses query param to insert data
40+
playerQueries.insert(
41+
player_number = Random.nextInt().absoluteValue,
42+
full_name = faker.name.name()
43+
)
44+
println(playerQueries.selectAll().executeAsList())
45+
46+
// Uses full data object to insert data
47+
val player = HockeyPlayer(
48+
player_number = Random.nextInt().absoluteValue,
49+
full_name = faker.name.name()
50+
)
51+
playerQueries.insertFullPlayerObject(player)
52+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package dev.hossain.postgresqldelight
2+
3+
import com.zaxxer.hikari.HikariConfig
4+
import com.zaxxer.hikari.HikariDataSource
5+
import javax.sql.DataSource
6+
7+
class SportsRepository constructor(
8+
9+
) {
10+
11+
fun getSource(appConfig: AppConfig): DataSource {
12+
val hikariConfig = HikariConfig()
13+
// https://jdbc.postgresql.org/documentation/use/
14+
hikariConfig.setJdbcUrl("jdbc:postgresql://192.168.2.32:5432/${appConfig.dbName}")
15+
hikariConfig.driverClassName = "org.postgresql.Driver"
16+
hikariConfig.username = appConfig.dbUsername
17+
hikariConfig.password = appConfig.dbPassword
18+
19+
return HikariDataSource(hikariConfig)
20+
}
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
-- Source: https://cashapp.github.io/sqldelight/2.0.0/jvm_postgresql/#fresh-schema
2+
CREATE TABLE IF NOT EXISTS hockeyPlayer (
3+
player_number INTEGER PRIMARY KEY NOT NULL,
4+
full_name TEXT NOT NULL
5+
);
6+
7+
CREATE INDEX IF NOT EXISTS hockeyPlayer_full_name ON hockeyPlayer(full_name);
8+
9+
10+
selectAll:
11+
SELECT *
12+
FROM hockeyPlayer;
13+
14+
insert:
15+
INSERT INTO hockeyPlayer(player_number, full_name)
16+
VALUES (?, ?);
17+
18+
insertFullPlayerObject:
19+
INSERT INTO hockeyPlayer(player_number, full_name)
20+
VALUES ?;
21+
22+
23+
-- tableExists:
24+
-- https://database.guide/5-ways-to-check-if-a-table-exists-in-postgresql/
25+
-- SELECT EXISTS (SELECT FROM pg_tables WHERE schemaname = 'public' AND tablename = 'hockeyPlayer');

0 commit comments

Comments
 (0)