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 JSON column support #6

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions src/main/kotlin/dev/hossain/postgresqldelight/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ fun main(args: Array<String>) {
val playerQueries: PlayerQueries = sportsRepository.getPlayerQueries(appConfig)

testDriveDatabase(playerQueries, faker { })

val journal = sportsRepository.getJournal(appConfig)
println(journal.selectAll().executeAsList())

journal.insert("{ \"customer\": \"Alex Cross\", \"items\": {\"product\": \"Tea\",\"qty\": 6}}")
journal.insert("{ \"customer\": \"Barney Stinson\", \"items\": {\"product\": \"chocoloate\",\"qty\": 24}}")
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ class SportsRepository constructor(
return playerQueries
}

fun getJournal(appConfig: AppConfig): JournalQueries {
val dataSource: DataSource = getDataSource(appConfig)

val driver: SqlDriver = dataSource.asJdbcDriver()

execSchema(driver)

val database = SportsDatabase(driver)
return database.journalQueries
}

/**
* @param driver the [SqlDriver] required to create the database.
*/
Expand All @@ -36,7 +47,8 @@ class SportsRepository constructor(
private fun getDataSource(appConfig: AppConfig): DataSource {
val hikariConfig = HikariConfig()
// https://jdbc.postgresql.org/documentation/use/
hikariConfig.setJdbcUrl("jdbc:postgresql://${appConfig.dbHost}/${appConfig.dbName}")
// https://stackoverflow.com/questions/65478350/error-column-is-of-type-json-but-expression-is-of-type-character-varying-in-hib
hikariConfig.setJdbcUrl("jdbc:postgresql://${appConfig.dbHost}/${appConfig.dbName}?stringtype=unspecified")
Comment on lines +50 to +51
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hikariConfig.driverClassName = "org.postgresql.Driver"
hikariConfig.username = appConfig.dbUsername
hikariConfig.password = appConfig.dbPassword
Expand Down
16 changes: 16 additions & 0 deletions src/main/sqldelight/dev/hossain/postgresqldelight/Journal.sq
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CREATE TABLE IF NOT EXISTS Journal (
id serial NOT NULL PRIMARY KEY,
entry json NOT NULL
);


selectAll:
SELECT *
FROM Journal;

insert:
INSERT INTO Journal(entry)
VALUES (?);

totalRecords:
SELECT count(*) AS total_rows FROM Journal;