|
| 1 | +package io.exoquery.example |
| 2 | + |
| 3 | +import io.exoquery.annotation.CapturedFunction |
| 4 | +import io.exoquery.capture |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +fun booleanLogicConversion_20250529() { |
| 9 | + |
| 10 | +data class Client(val name: String, val isRegistered: Boolean) |
| 11 | + |
| 12 | +val q = |
| 13 | + capture.select { |
| 14 | + val c = from(Table<Client>()) |
| 15 | + where { c.isRegistered || c.name == "Joe" } |
| 16 | + Pair(c.name, c.isRegistered || c.name == "Joe") |
| 17 | + } |
| 18 | + |
| 19 | +println(q.buildFor.Postgres().value) |
| 20 | +// SELECT p.name FROM Client AS p WHERE p.isRegistered OR p.name = 'Joe' |
| 21 | + |
| 22 | +println(q.buildFor.SqlServer().value) |
| 23 | +// SELECT p.name FROM Client AS p WHERE p.isRegistered = 1 OR p.name = 'Joe' |
| 24 | + |
| 25 | + |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | +fun dateTypesOutOfTheBox_20250529() { |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +data class Client(val name: String, val birthDate: kotlinx.datetime.LocalDate) |
| 34 | +val minBirthday = kotlinx.datetime.LocalDate(2000, 1, 1) |
| 35 | + |
| 36 | +val q = |
| 37 | + capture.select { |
| 38 | + val c = from(Table<Client>()) |
| 39 | + where { c.birthDate < param(minBirthday) } // use paramCtx for java.time.* |
| 40 | + c.name |
| 41 | + } |
| 42 | + |
| 43 | +println(q.buildFor.Postgres().value) |
| 44 | + |
| 45 | + |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +fun windowFunctions_20250530_A() { |
| 51 | + |
| 52 | +data class Customer(val name: String, val age: Int, val membership: String) |
| 53 | + |
| 54 | +val q = |
| 55 | + capture.select { |
| 56 | + val c = from(Table<Customer>()) |
| 57 | + Pair( |
| 58 | + c.name, |
| 59 | + // Average age of customers in the same membership group, ordered by name |
| 60 | + over().partitionBy(c.membership).orderBy(c.name).avg(c.age) |
| 61 | + ) |
| 62 | + } |
| 63 | + |
| 64 | +println(q.buildFor.Postgres().value) |
| 65 | +//> SELECT p.name, AVG(p.age) OVER (PARTITION BY p.membership ORDER BY p.name) |
| 66 | +// FROM Customer AS p |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +} |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +fun windowFunctions_20250530_B() { |
| 76 | + |
| 77 | +data class Data(val name: String, val a: Int, val b: Int, val c: Int) |
| 78 | + |
| 79 | + |
| 80 | +data class Customer(val name: String, val age: Int, val membership: String) |
| 81 | +data class VipCustomer(val name: String, val statusAge: Int, val tier: Int) |
| 82 | + |
| 83 | +@CapturedFunction |
| 84 | +fun ntile(by: Customer, n: Int) = capture.expression { |
| 85 | + over() |
| 86 | + .partitionBy(by.membership) |
| 87 | + .orderBy(by.age) |
| 88 | + .frame(free("NTILE($n)").invoke<Int>()) |
| 89 | +} |
| 90 | + |
| 91 | + |
| 92 | +@CapturedFunction |
| 93 | +fun ntileByAge(by: String, age: Int, n: Int) = capture.expression { |
| 94 | + over() |
| 95 | + .partitionBy(by) |
| 96 | + .orderBy(age) |
| 97 | + .frame(free("NTILE($n)").invoke<Int>()) |
| 98 | +} |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | +val ntile2 = capture.expression { |
| 105 | + { c: Customer, n: Int -> |
| 106 | + over() |
| 107 | + .partitionBy(c.membership) |
| 108 | + .orderBy(c.age) |
| 109 | + .sum(n) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +val q = |
| 114 | + capture.select { |
| 115 | + val c = from(Table<Customer>()) |
| 116 | + Data( |
| 117 | + c.name, |
| 118 | + ntile(c, 5).use, |
| 119 | + ntile(c, 10).use, |
| 120 | + ntile(c, 20).use, |
| 121 | + ) |
| 122 | + } |
| 123 | + |
| 124 | + /* |
| 125 | + ntileByAge(c.name, c.age, 5).use, |
| 126 | + ntileByAge(c.name, c.age, 10).use, |
| 127 | + ntileByAge(c.name, c.age, 20).use, |
| 128 | + */ |
| 129 | + |
| 130 | + |
| 131 | +println(q.buildFor.Postgres().value) |
| 132 | + |
| 133 | +} |
| 134 | + |
| 135 | + |
| 136 | +fun dateTypesOutOfTheBox_20250529_2() { |
| 137 | + |
| 138 | +data class Client(val name: String, val birthDate: java.time.LocalDate) |
| 139 | +val minBirthday = java.time.LocalDate.of(2000, 1, 1) |
| 140 | +val q = |
| 141 | + capture.select { |
| 142 | + val c = from(Table<Client>()) |
| 143 | + where { c.birthDate < paramCtx(minBirthday) } |
| 144 | + c.name |
| 145 | + } |
| 146 | + |
| 147 | +println(q.buildFor.Postgres().value) |
| 148 | + |
| 149 | +} |
| 150 | + |
| 151 | +fun paramsWithCustomSerialization_20250529() { |
| 152 | + |
| 153 | +} |
0 commit comments