Skip to content

Commit c0b3984

Browse files
committed
[ADDED] Properties file and config loader.
1 parent 415cc6b commit c0b3984

File tree

5 files changed

+80
-4
lines changed

5 files changed

+80
-4
lines changed

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
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: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,35 @@
11
package dev.hossain.postgresqldelight
22

3+
import app.cash.sqldelight.db.SqlDriver
4+
import app.cash.sqldelight.driver.jdbc.asJdbcDriver
5+
import javax.sql.DataSource
6+
7+
38
fun main(args: Array<String>) {
4-
println("Hello World!")
9+
println("Begin SQLDelight 2.0 with PostgreSQL Sample!")
10+
val appConfigLoader = AppConfigLoader()
11+
val sportsRepository = SportsRepository()
12+
val dataSource: DataSource = sportsRepository.getSource(appConfigLoader.loadAppConfig())
13+
14+
15+
val driver: SqlDriver = dataSource.asJdbcDriver()
16+
17+
doDatabaseThings(driver)
18+
}
19+
20+
21+
22+
fun doDatabaseThings(driver: SqlDriver) {
23+
val database = SportsDatabase(driver)
24+
val playerQueries: PlayerQueries = database.playerQueries
25+
26+
println(playerQueries.selectAll().executeAsList())
27+
// [HockeyPlayer(15, "Ryan Getzlaf")]
28+
29+
playerQueries.insert(player_number = 10, full_name = "Corey Perry")
30+
println(playerQueries.selectAll().executeAsList())
31+
// [HockeyPlayer(15, "Ryan Getzlaf"), HockeyPlayer(10, "Corey Perry")]
532

6-
// Try adding program arguments via Run/Debug configuration.
7-
// Learn more about running applications: https://www.jetbrains.com/help/idea/running-applications.html.
8-
println("Program arguments: ${args.joinToString()}")
33+
val player = HockeyPlayer(10, "Ronald McDonald")
34+
playerQueries.insertFullPlayerObject(player)
935
}
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+
}

0 commit comments

Comments
 (0)