-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
995 additions
and
27 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
jOOQ-demo-oss/jOOQ-demo-java/src/test/java/org/jooq/demo/java/Demo13Reactive.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.jooq.demo.java; | ||
|
||
import org.jooq.demo.AbstractDemo; | ||
import org.junit.After; | ||
import org.junit.Test; | ||
import reactor.core.publisher.Flux; | ||
|
||
import java.sql.SQLException; | ||
import java.util.List; | ||
|
||
import static org.jooq.Records.mapping; | ||
import static org.jooq.demo.java.db.Tables.ACTOR; | ||
|
||
public class Demo13Reactive extends AbstractDemo { | ||
|
||
@Test | ||
public void reactiveQuerying() { | ||
record Actor(String firstName, String lastName) {} | ||
|
||
Flux.from(ctx | ||
.select(ACTOR.FIRST_NAME, ACTOR.LAST_NAME) | ||
.from(ACTOR) | ||
.orderBy(ACTOR.ACTOR_ID) | ||
.limit(5)) | ||
.map(mapping(Actor::new)) | ||
.collectList() | ||
.block() | ||
.forEach(System.out::println); | ||
} | ||
|
||
@Test | ||
public void reactiveTransactions() { | ||
Flux.from(ctx | ||
|
||
// Just like synchronous, JDBC based transactions, reactive transactions commit by default, and rollback | ||
// on error. Nested transactions using SAVEPOINT are supported by default. See this blog for details: | ||
// https://blog.jooq.org/nested-transactions-in-jooq/ | ||
.transactionPublisher(c -> Flux | ||
.from(c.dsl() | ||
.insertInto(ACTOR) | ||
.columns(ACTOR.ACTOR_ID, ACTOR.FIRST_NAME, ACTOR.LAST_NAME) | ||
.values(201L, "A", "A")) | ||
|
||
// Within the transactional scope, the above record is visible, and we can log it | ||
.thenMany(c.dsl() | ||
.selectFrom(ACTOR) | ||
.where(ACTOR.ACTOR_ID.eq(201L))) | ||
.log() | ||
|
||
// This should produces a constraint violation exception, rolling back the transaction | ||
.thenMany(c.dsl() | ||
.insertInto(ACTOR) | ||
.columns(ACTOR.ACTOR_ID, ACTOR.FIRST_NAME, ACTOR.LAST_NAME) | ||
.values(201L, "A", "A")))) | ||
|
||
// Outside of the scope, we have committed or rollbacked the transaction, so on error, we can see the | ||
// Rollback reason: | ||
.collectList() | ||
.doOnError(Throwable::printStackTrace) | ||
.onErrorReturn(List.of()) | ||
|
||
// This record is visible only if the transaction has been committed: | ||
.thenMany(ctx.select(ACTOR.ACTOR_ID).from(ACTOR).where(ACTOR.ACTOR_ID.eq(201L))) | ||
.collectList() | ||
.block() | ||
.forEach(System.out::println); | ||
} | ||
|
||
@After | ||
public void teardown() throws SQLException { | ||
cleanup(ACTOR, ACTOR.ACTOR_ID); | ||
super.teardown(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
jOOQ-demo-oss/jOOQ-demo-kotlin/src/test/kotlin/org/jooq/demo/kotlin/Demo13Reactive.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package org.jooq.demo.kotlin | ||
|
||
import kotlinx.coroutines.reactive.awaitFirstOrNull | ||
import kotlinx.coroutines.runBlocking | ||
import org.jooq.Records.mapping | ||
import org.jooq.demo.AbstractDemo | ||
import org.jooq.demo.kotlin.db.tables.records.ActorRecord | ||
import org.jooq.demo.kotlin.db.tables.references.ACTOR | ||
import org.junit.After | ||
import org.junit.Test | ||
import reactor.core.publisher.Flux | ||
|
||
|
||
class Demo13Reactive : AbstractDemo() { | ||
|
||
@Test | ||
fun reactiveQuerying() { | ||
data class Actor(val firstName: String?, val lastName: String?) | ||
|
||
Flux.from(ctx | ||
.select(ACTOR.FIRST_NAME, ACTOR.LAST_NAME) | ||
.from(ACTOR) | ||
.orderBy(ACTOR.ACTOR_ID) | ||
.limit(5)) | ||
.map(mapping(::Actor)) | ||
.toIterable() | ||
.forEach { println(it) } | ||
} | ||
|
||
@Test | ||
fun reactiveTransactions() { | ||
Flux.from(ctx | ||
|
||
// Just like synchronous, JDBC based transactions, reactive transactions commit by default, and rollback | ||
// on error. Nested transactions using SAVEPOINT are supported by default. See this blog for details: | ||
// https://blog.jooq.org/nested-transactions-in-jooq/ | ||
.transactionPublisher { c -> Flux | ||
.from(c.dsl() | ||
.insertInto(ACTOR) | ||
.columns(ACTOR.ACTOR_ID, ACTOR.FIRST_NAME, ACTOR.LAST_NAME) | ||
.values(201L, "A", "A")) | ||
|
||
// Within the transactional scope, the above record is visible, and we can log it | ||
.thenMany(c.dsl() | ||
.selectFrom(ACTOR) | ||
.where(ACTOR.ACTOR_ID.eq(201L))) | ||
.log() | ||
|
||
// This should produces a constraint violation exception, rolling back the transaction | ||
.thenMany(c.dsl() | ||
.insertInto(ACTOR) | ||
.columns(ACTOR.ACTOR_ID, ACTOR.FIRST_NAME, ACTOR.LAST_NAME) | ||
.values(201L, "A", "A")) | ||
}) | ||
|
||
// Outside of the scope, we have committed or rollbacked the transaction, so on error, we can see the | ||
// Rollback reason: | ||
.collectList() | ||
.doOnError(Throwable::printStackTrace) | ||
.onErrorReturn(listOf()) | ||
|
||
// This record is visible only if the transaction has been committed: | ||
.thenMany(ctx | ||
.select(ACTOR.ACTOR_ID) | ||
.from(ACTOR) | ||
.where(ACTOR.ACTOR_ID.eq(201L))) | ||
.toIterable() | ||
.forEach { println(it) } | ||
} | ||
|
||
@Test | ||
fun coroutines() { | ||
val actor: ActorRecord? = runBlocking { | ||
findActor(1) | ||
} | ||
|
||
println(actor) | ||
} | ||
|
||
suspend fun findActor(id: Long): ActorRecord? { | ||
return ctx | ||
.selectFrom(ACTOR) | ||
.where(ACTOR.ACTOR_ID.eq(id)) | ||
.awaitFirstOrNull() | ||
} | ||
|
||
@After | ||
override fun teardown() { | ||
cleanup(ACTOR, ACTOR.ACTOR_ID) | ||
super.teardown() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
jOOQ-demo-oss/jOOQ-demo-scala/src/test/scala/org/jooq/demo/skala/Demo09DAOs.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.jooq.demo.skala | ||
|
||
import org.jooq.demo.AbstractDemo | ||
import org.jooq.demo.AbstractDemo._ | ||
import org.jooq.demo.skala.db.Tables.ACTOR | ||
import org.jooq.demo.skala.db.tables.daos.ActorDao | ||
import org.jooq.demo.skala.db.tables.pojos.Actor | ||
import org.junit.{After, Test} | ||
|
||
|
||
class Demo09DAOs extends AbstractDemo { | ||
|
||
@Test | ||
def pojos(): Unit = { | ||
title("jOOQ's code generator produces a POJO for every table") | ||
val actors = ctx | ||
.selectFrom(ACTOR) | ||
.where(ACTOR.ACTOR_ID.lt(4L)) | ||
.fetchInto(classOf[Actor]) | ||
actors.forEach(println(_)) | ||
} | ||
|
||
@Test | ||
def daos(): Unit = { | ||
title("Daos further simplify CRUD when working with jOOQ") | ||
val dao = new ActorDao(ctx.configuration) | ||
|
||
dao.insert( | ||
Actor(201L, "John", "Doe", null), | ||
Actor(202L, "Jane", "Smith", null)) | ||
dao.fetchByActorId(201L, 202L).forEach(println(_)) | ||
} | ||
|
||
@After | ||
override def teardown(): Unit = { | ||
cleanup(ACTOR, ACTOR.ACTOR_ID) | ||
super.teardown() | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
jOOQ-demo-oss/jOOQ-demo-scala/src/test/scala/org/jooq/demo/skala/Demo10SPIs.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.jooq.demo.skala | ||
|
||
import org.jooq.{ExecuteListener, VisitListener} | ||
import org.jooq.demo.AbstractDemo | ||
import org.jooq.demo.AbstractDemo._ | ||
import org.jooq.demo.skala.db.Tables.ACTOR | ||
import org.jooq.impl.DSL.using | ||
import org.jooq.scalaextensions.Conversions._ | ||
import org.junit.Test | ||
|
||
|
||
class Demo10SPIs extends AbstractDemo { | ||
|
||
@Test | ||
def executeListener(): Unit = { | ||
title("The ExecuteListener SPI allows for intercepting the execution lifecycle") | ||
val c = ctx.configuration | ||
.derive(ExecuteListener | ||
.onRenderEnd(e => e.sql("/* some telemetry comment */ " + e.sql)) | ||
.onExecuteStart(e => println("Executing: " + e.sql)) | ||
.onRecordEnd(e => println("Fetched record: " + e.record.formatJSON))) | ||
.dsl | ||
|
||
c.select(ACTOR.FIRST_NAME, ACTOR.LAST_NAME) | ||
.from(ACTOR) | ||
.where(ACTOR.ACTOR_ID < 4L) | ||
.fetch | ||
} | ||
|
||
@Test def visitListener(): Unit = { | ||
title("The VisitListener SPI allows for intercepting the SQL rendering process") | ||
val c = ctx.configuration | ||
.derive(VisitListener.onVisitStart(vc => println("Visiting: " + using(ctx.family).render(vc.queryPart)))) | ||
.dsl | ||
|
||
c.select(ACTOR.FIRST_NAME, ACTOR.LAST_NAME) | ||
.from(ACTOR) | ||
.where(ACTOR.ACTOR_ID < 4L) | ||
.fetch | ||
} | ||
// There are many more SPIs, check out Configuration::derive methods! | ||
} |
Oops, something went wrong.