Skip to content

Commit

Permalink
[#1] Continue adding demo code: More kotlin demo code
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaseder committed May 3, 2022
1 parent adb5b83 commit d091e0b
Show file tree
Hide file tree
Showing 16 changed files with 80 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.jooq.demo.java;
package org.jooq.demo;

import org.jooq.DSLContext;
import org.jooq.SQLDialect;
Expand Down Expand Up @@ -30,7 +30,7 @@ public abstract class AbstractDemo {
@Before
public void setup() throws IOException, SQLException {
Properties properties = new Properties();
properties.load(Demo01Querying.class.getResourceAsStream("/config.properties"));
properties.load(AbstractDemo.class.getResourceAsStream("/config.properties"));
connection = DriverManager.getConnection(
properties.getProperty("db.cockroachdb.url"),
properties.getProperty("db.cockroachdb.username"),
Expand Down
1 change: 1 addition & 0 deletions src/src/test/java/org/jooq/demo/java/Demo01Querying.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jooq.demo.java;

import org.jooq.*;
import org.jooq.demo.AbstractDemo;
import org.jooq.demo.java.db.tables.Actor;
import org.jooq.demo.java.db.tables.FilmActor;
import org.jooq.demo.java.db.tables.records.ActorRecord;
Expand Down
1 change: 1 addition & 0 deletions src/src/test/java/org/jooq/demo/java/Demo02DynamicSQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.jetbrains.annotations.NotNull;
import org.jooq.*;
import org.jooq.demo.AbstractDemo;
import org.jooq.demo.java.db.Tables;
import org.jooq.impl.DSL;
import org.junit.Test;
Expand Down
1 change: 1 addition & 0 deletions src/src/test/java/org/jooq/demo/java/Demo03Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.jooq.Field;
import org.jooq.WindowDefinition;
import org.jooq.demo.AbstractDemo;
import org.jooq.impl.DSL;
import org.junit.Test;

Expand Down
1 change: 1 addition & 0 deletions src/src/test/java/org/jooq/demo/java/Demo04DML.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jooq.demo.java;

import org.jooq.demo.AbstractDemo;
import org.jooq.demo.java.db.tables.records.ActorRecord;
import org.junit.After;
import org.junit.Test;
Expand Down
1 change: 1 addition & 0 deletions src/src/test/java/org/jooq/demo/java/Demo05Batch.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jooq.demo.java;

import org.jooq.demo.AbstractDemo;
import org.jooq.demo.java.db.tables.records.ActorRecord;
import org.jooq.demo.java.db.Tables;
import org.junit.After;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jooq.demo.java;

import org.jooq.demo.AbstractDemo;
import org.jooq.demo.java.db.Tables;
import org.junit.After;
import org.junit.Test;
Expand Down
1 change: 1 addition & 0 deletions src/src/test/java/org/jooq/demo/java/Demo07Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.jooq.Field;
import org.jooq.ParseListener;
import org.jooq.Select;
import org.jooq.demo.AbstractDemo;
import org.jooq.impl.DSL;
import org.jooq.impl.ParserException;
import org.junit.Test;
Expand Down
3 changes: 1 addition & 2 deletions src/src/test/java/org/jooq/demo/java/Demo08ImportExport.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package org.jooq.demo.java;

import org.jooq.*;
import org.jooq.demo.AbstractDemo;
import org.jooq.demo.java.db.tables.records.ActorRecord;
import org.jooq.demo.java.db.Tables;
import org.junit.After;
import org.junit.Test;

import java.io.IOException;
import java.sql.Date;
import java.sql.SQLException;

import static org.jooq.Records.mapping;
import static org.jooq.demo.java.db.Tables.ACTOR;
import static org.jooq.demo.java.db.Tables.PAYMENT;
import static org.jooq.impl.DSL.*;
Expand Down
2 changes: 1 addition & 1 deletion src/src/test/kotlin/org/jooq/demo/kotlin/Demo01Querying.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.jooq.demo.kotlin

import org.jooq.*
import org.jooq.demo.java.AbstractDemo
import org.jooq.demo.AbstractDemo
import org.jooq.demo.kotlin.db.tables.references.*
import org.jooq.impl.DSL
import org.jooq.impl.DSL.*
Expand Down
64 changes: 64 additions & 0 deletions src/src/test/kotlin/org/jooq/demo/kotlin/Demo02DynamicSQL.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.jooq.demo.kotlin

import org.jooq.Condition
import org.jooq.demo.AbstractDemo
import org.jooq.demo.kotlin.db.tables.references.ACTOR
import org.jooq.impl.DSL
import org.jooq.impl.DSL.noCondition
import org.junit.Test

class Demo02DynamicSQL : AbstractDemo() {
@Test
fun testDynamicSQL() {
title("Every jOOQ query is a dynamic SQL query. You just don't see it")
ctx.select(ACTOR.FIRST_NAME, ACTOR.LAST_NAME)
.from(ACTOR)
.where(ACTOR.ACTOR_ID.`in`(1L, 2L, 3L))
.orderBy(ACTOR.FIRST_NAME)
.limit(5)
.fetch()

title("The above and the below are equivalent")
val select = listOf(ACTOR.FIRST_NAME, ACTOR.LAST_NAME)
val from = ACTOR
val where = ACTOR.ACTOR_ID.`in`(1L, 2L, 3L)
val orderBy = listOf(ACTOR.FIRST_NAME)
val limit = DSL.`val`(5)

ctx.select(select)
.from(from)
.where(where)
.orderBy(orderBy)
.limit(limit)
.fetch()

title("Any 'static' query part can be replaced by an expression, function call, etc.")
val ids = listOf(1, 2, 3)
ctx.select(ACTOR.FIRST_NAME, ACTOR.LAST_NAME)
.from(ACTOR)
.where(
if (ids.isEmpty())
noCondition()
else
ACTOR.ACTOR_ID.`in`(ids.map { it.toLong() }.map { DSL.value(it) }.toList())
)
.orderBy(ACTOR.FIRST_NAME)
.limit(5)
.fetch()
}

@Test
fun generateQueryParts() {
println(reduceCondition(listOf()))
println(reduceCondition(listOf(1)))
println(reduceCondition(listOf(1, 2, 3)))
}

private fun reduceCondition(ids: List<Int>): Condition {
title("List: $ids")
return ids
.map { it.toLong() }
.map { ACTOR.ACTOR_ID.eq(it) }
.fold(noCondition()) { c1: Condition, c2: Condition -> c1.or(c2) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.jooq.demo.kotlin

import org.jooq.Condition
import org.jooq.Field
import org.jooq.demo.java.AbstractDemo
import org.jooq.demo.AbstractDemo
import org.jooq.demo.kotlin.db.tables.references.*
import org.jooq.impl.DSL
import org.jooq.impl.DSL.*
Expand Down
2 changes: 1 addition & 1 deletion src/src/test/kotlin/org/jooq/demo/kotlin/Demo04DML.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.jooq.demo.kotlin

import org.jooq.demo.java.AbstractDemo
import org.jooq.demo.AbstractDemo
import org.jooq.demo.kotlin.db.tables.references.*
import org.junit.After
import org.junit.Test
Expand Down
2 changes: 1 addition & 1 deletion src/src/test/kotlin/org/jooq/demo/kotlin/Demo05Batch.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.jooq.demo.kotlin

import org.jooq.Configuration
import org.jooq.demo.java.AbstractDemo
import org.jooq.demo.AbstractDemo
import org.jooq.demo.kotlin.db.tables.references.*
import org.junit.After
import org.junit.Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.jooq.demo.kotlin

import org.jooq.Configuration
import org.jooq.demo.java.AbstractDemo
import org.jooq.demo.AbstractDemo
import org.jooq.demo.kotlin.db.tables.references.*
import org.junit.After
import org.junit.Test
Expand Down
5 changes: 1 addition & 4 deletions src/src/test/kotlin/org/jooq/demo/kotlin/Demo07Parser.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package org.jooq.demo.kotlin

import org.jooq.AggregateFunction
import org.jooq.Field
import org.jooq.ParseContext
import org.jooq.ParseListener
import org.jooq.demo.java.AbstractDemo
import org.jooq.demo.AbstractDemo
import org.jooq.impl.DSL
import org.jooq.impl.ParserException
import org.junit.Test
import java.sql.SQLException

class Demo07Parser : AbstractDemo() {

Expand Down

0 comments on commit d091e0b

Please sign in to comment.