Skip to content
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

Test/postgres sqldelight snippets #230

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ plugins {
// https://kotlinlang.org/docs/releases.html#release-details
kotlin("jvm") version "1.9.20"
id("org.jmailen.kotlinter") version "3.12.0"

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

// https://kotlinlang.org/docs/ksp-quickstart.html#use-your-own-processor-in-a-project
// id("com.google.devtools.ksp") version "1.9.20-1.0.6" // Not needed yet.
application
Expand All @@ -14,9 +19,23 @@ group = "dev.hossain.githubstats"
version = "1.0-SNAPSHOT"

repositories {
google()
mavenCentral()
}

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

}
}
}

dependencies {
// https://github.com/Kotlin/kotlinx.coroutines
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.1")
Expand Down Expand Up @@ -45,6 +64,22 @@ dependencies {
implementation("com.squareup.moshi:moshi-adapters:1.14.0")
// ksp("com.squareup.moshi:moshi-kotlin-codegen:1.14.0") // Not needed yet.

// https://mvnrepository.com/artifact/org.postgresql/postgresql
implementation("org.postgresql:postgresql:42.6.0")

// https://mvnrepository.com/artifact/org.slf4j/slf4j-api
implementation("org.slf4j:slf4j-api:2.0.9")
// https://mvnrepository.com/artifact/org.slf4j/slf4j-simple
implementation("org.slf4j:slf4j-simple:2.0.9")

// 光 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")

// https://cashapp.github.io/sqldelight/2.0.0/jvm_postgresql
implementation("app.cash.sqldelight:jdbc-driver:2.0.0")

// https://github.com/doyaaaaaken/kotlin-csv
implementation("com.github.doyaaaaaken:kotlin-csv-jvm:1.8.0") //for JVM platform

Expand Down
48 changes: 48 additions & 0 deletions src/main/kotlin/TestDb.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.jdbc.asJdbcDriver
import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import dev.hossain.githubstats.GitHubApiCacheDb
import dev.hossain.githubstats.util.LocalProperties
import javax.sql.DataSource

fun main() {
val newDataSource = getDataSource()
val newDriver: SqlDriver = newDataSource.asJdbcDriver()

doDatabaseThings(newDriver)
}

/**
* Creates a [DataSource] using [HikariDataSource].
*/
private fun getDataSource(): DataSource {
val properties = LocalProperties()
val dbHost = properties.getProperty("db_host")
val dbPort = properties.getProperty("db_port")
val dbName = properties.getProperty("db_name")
val userName = properties.getProperty("db_username")
val dbPassword = properties.getProperty("db_password")

// https://jdbc.postgresql.org/documentation/use/
val config = HikariConfig().apply {
jdbcUrl = "jdbc:postgresql://${dbHost}:${dbPort}/${dbName}"
driverClassName = "org.postgresql.Driver"
username = userName
password = dbPassword
maximumPoolSize = 3
isAutoCommit = false
transactionIsolation = "TRANSACTION_REPEATABLE_READ"
validate()
}
return HikariDataSource(config)
}

fun doDatabaseThings(driver: SqlDriver) {
val database = GitHubApiCacheDb(driver)
val responseCacheQueries = database.responseCacheQueries

responseCacheQueries.selectAll()
println(responseCacheQueries.selectAll().executeAsList())

}
13 changes: 13 additions & 0 deletions src/main/kotlin/dev/hossain/githubstats/io/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import okhttp3.Cache
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import okhttp3.OkHttpClient
import okhttp3.ResponseBody
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
Expand Down Expand Up @@ -78,6 +79,18 @@ object Client {
chain.proceed(requestBuilder.build())
}

// Add another interceptor for caching
builder.addInterceptor { chain ->
println("Intercepting request: ${chain.request().url}")
val originalRequest = chain.request()

val response = chain.proceed(originalRequest)
val responseBody: String = response.peekBody(Long.MAX_VALUE).string()
println("Response body size: ${responseBody.length}")

response
}

// https://square.github.io/okhttp/features/caching/
builder.cache(
Cache(
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/strings.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
# ---------------------------------------------------------------------------------------------------------------------
# This only supports ISO 8859-1 character encoding
# -- Why? See: https://stackoverflow.com/a/59479929/132121
#
# Need special UTF-8 unicode char, see https://www.fileformat.info/info/unicode/char/search.htm
#
# Need formatting help? See
# -- https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html#syntax
# -- https://docs.oracle.com/javase/tutorial/java/data/numberformat.html
Expand Down
21 changes: 21 additions & 0 deletions src/main/sqldelight/dev/hossain/githubstats/Player.sq
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
CREATE TABLE hockeyPlayer (
player_number INTEGER PRIMARY KEY NOT NULL,
full_name TEXT NOT NULL
);

CREATE INDEX hockeyPlayer_full_name ON hockeyPlayer(full_name);

INSERT INTO hockeyPlayer (player_number, full_name)
VALUES (15, 'Ryan Getzlaf');

selectAll:
SELECT *
FROM hockeyPlayer;

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

insertFullPlayerObject:
INSERT INTO hockeyPlayer(player_number, full_name)
VALUES ?;
30 changes: 30 additions & 0 deletions src/main/sqldelight/dev/hossain/githubstats/ResponseCache.sq
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
CREATE TABLE IF NOT EXISTS response_cache
(
request_url character varying(512) NOT NULL,
id integer NOT NULL,
response json NOT NULL,
CONSTRAINT response_cache_pkey PRIMARY KEY (id)
);


selectAll:
SELECT *
FROM response_cache;

insert:
INSERT INTO response_cache(id, request_url, response)
VALUES ( ?, ?, ?);

select:
SELECT * FROM response_cache
WHERE request_url = ?;

delete:
DELETE FROM response_cache
WHERE request_url = ?;

deleteAll:
DELETE FROM response_cache;

count:
SELECT COUNT(*) FROM response_cache;
Loading