Skip to content

Commit b89c490

Browse files
committed
[UPDATE] Code cleanup and proper separation of code.
Also added DB host config
1 parent 8bf0559 commit b89c490

File tree

6 files changed

+59
-20
lines changed

6 files changed

+59
-20
lines changed

sample-local.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# ----------------------------------------------------
44

55
## Data based properties
6+
db_host=localhost:
67
db_name=YourDbName
78
db_username=your_db_username
89
db_password=your_db_password
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
11
package dev.hossain.postgresqldelight
22

3-
data class AppConfig(val dbName: String, val dbUsername: String, val dbPassword: String)
3+
/**
4+
* App configuration data.
5+
*/
6+
data class AppConfig(
7+
/**
8+
* Database host with port.
9+
* See https://jdbc.postgresql.org/documentation/use/
10+
*/
11+
val dbHost: String,
12+
val dbName: String,
13+
val dbUsername: String,
14+
val dbPassword: String,
15+
)

src/main/kotlin/dev/hossain/postgresqldelight/AppConfigLoader.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class AppConfigLoader {
1010
fun loadAppConfig(): AppConfig {
1111
val properties = Properties().apply { load(File("local.properties").inputStream()) }
1212
return AppConfig(
13+
dbHost = properties.getProperty("db_host"),
1314
dbName = properties.getProperty("db_name"),
1415
dbUsername = properties.getProperty("db_username"),
1516
dbPassword = properties.getProperty("db_password"),
Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,34 @@
11
package dev.hossain.postgresqldelight
22

3-
import app.cash.sqldelight.db.SqlDriver
4-
import app.cash.sqldelight.driver.jdbc.asJdbcDriver
53
import io.github.serpro69.kfaker.Faker
64
import io.github.serpro69.kfaker.faker
7-
import javax.sql.DataSource
85
import kotlin.math.absoluteValue
96
import kotlin.random.Random
107

118

9+
/**
10+
* Test application entry point. Run the app with ▶️ icon from left gutter.
11+
*/
1212
fun main(args: Array<String>) {
1313
println("Begin SQLDelight 2.0 with PostgreSQL Sample!")
14-
val appConfigLoader = AppConfigLoader()
14+
val appConfig: AppConfig = AppConfigLoader().loadAppConfig()
1515
val sportsRepository = SportsRepository()
16-
val dataSource: DataSource = sportsRepository.getSource(appConfigLoader.loadAppConfig())
17-
18-
19-
val driver: SqlDriver = dataSource.asJdbcDriver()
20-
SportsDatabase.Schema.create(driver)
16+
val playerQueries: PlayerQueries = sportsRepository.getPlayerQueries(appConfig)
2117

22-
testDriveDatabase(driver, faker { })
18+
testDriveDatabase(playerQueries, faker { })
2319
}
2420

2521

2622
/**
27-
* @param driver the [SqlDriver] required to create the database.
23+
* Test drives different functions from [PlayerQueries].
24+
*
25+
* @param playerQueries SQLDelight class for doing player queries
2826
* @param faker Fake data generator.
2927
*/
30-
fun testDriveDatabase(driver: SqlDriver, faker: Faker) {
31-
val database = SportsDatabase(driver)
32-
33-
val playerQueries: PlayerQueries = database.playerQueries
34-
28+
fun testDriveDatabase(playerQueries: PlayerQueries, faker: Faker) {
3529
// Show all players
36-
println(playerQueries.selectAll().executeAsList())
30+
val hockeyPlayers = playerQueries.selectAll().executeAsList()
31+
println("Existing ${hockeyPlayers.size} records: $hockeyPlayers")
3732

3833

3934
// Uses query param to insert data
@@ -49,4 +44,7 @@ fun testDriveDatabase(driver: SqlDriver, faker: Faker) {
4944
full_name = faker.name.name()
5045
)
5146
playerQueries.insertFullPlayerObject(player)
47+
48+
val totalRecords = playerQueries.totalRecords().executeAsOne()
49+
println("Total players: $totalRecords.")
5250
}

src/main/kotlin/dev/hossain/postgresqldelight/SportsRepository.kt

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,42 @@
11
package dev.hossain.postgresqldelight
22

3+
import app.cash.sqldelight.db.SqlDriver
4+
import app.cash.sqldelight.driver.jdbc.asJdbcDriver
35
import com.zaxxer.hikari.HikariConfig
46
import com.zaxxer.hikari.HikariDataSource
57
import javax.sql.DataSource
68

9+
/**
10+
* Repository for creating database and getting SQLDelight classes needed for testing.
11+
*/
712
class SportsRepository constructor(
813

914
) {
1015

11-
fun getSource(appConfig: AppConfig): DataSource {
16+
fun getPlayerQueries(appConfig: AppConfig): PlayerQueries {
17+
val dataSource: DataSource = getDataSource(appConfig)
18+
19+
val driver: SqlDriver = dataSource.asJdbcDriver()
20+
21+
execSchema(driver)
22+
23+
val database = SportsDatabase(driver)
24+
val playerQueries: PlayerQueries = database.playerQueries
25+
26+
return playerQueries
27+
}
28+
29+
/**
30+
* @param driver the [SqlDriver] required to create the database.
31+
*/
32+
private fun execSchema(driver: SqlDriver) {
33+
SportsDatabase.Schema.create(driver)
34+
}
35+
36+
private fun getDataSource(appConfig: AppConfig): DataSource {
1237
val hikariConfig = HikariConfig()
1338
// https://jdbc.postgresql.org/documentation/use/
14-
hikariConfig.setJdbcUrl("jdbc:postgresql://192.168.2.32:5432/${appConfig.dbName}")
39+
hikariConfig.setJdbcUrl("jdbc:postgresql://${appConfig.dbHost}/${appConfig.dbName}")
1540
hikariConfig.driverClassName = "org.postgresql.Driver"
1641
hikariConfig.username = appConfig.dbUsername
1742
hikariConfig.password = appConfig.dbPassword

src/main/sqldelight/dev/hossain/postgresqldelight/Player.sq

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ insertFullPlayerObject:
1919
INSERT INTO hockeyPlayer(player_number, full_name)
2020
VALUES ?;
2121

22+
totalRecords:
23+
SELECT count(*) AS total_rows FROM hockeyPlayer;
2224

2325
-- tableExists:
2426
-- https://database.guide/5-ways-to-check-if-a-table-exists-in-postgresql/

0 commit comments

Comments
 (0)