Skip to content

Commit f152114

Browse files
committed
record more SimpleTable tests in reference.md
1 parent 88ede3a commit f152114

File tree

2 files changed

+207
-7
lines changed

2 files changed

+207
-7
lines changed

docs/reference.md

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11211,6 +11211,200 @@ db.run(OptDataTypes.select) ==> Seq(rowSome, rowNone)
1121111211

1121211212

1121311213

11214+
### Optional.filter - with SimpleTable
11215+
11216+
`.filter` follows normal Scala semantics, and translates to a `CASE`/`WHEN (foo)`/`ELSE NULL`
11217+
11218+
```scala
11219+
OptCols.select.map(d => d.updates(_.myInt(_.filter(_ < 2))))
11220+
```
11221+
11222+
11223+
*
11224+
```sql
11225+
SELECT
11226+
CASE
11227+
WHEN (opt_cols0.my_int < ?) THEN opt_cols0.my_int
11228+
ELSE NULL
11229+
END AS my_int,
11230+
opt_cols0.my_int2 AS my_int2
11231+
FROM opt_cols opt_cols0
11232+
```
11233+
11234+
11235+
11236+
*
11237+
```scala
11238+
Seq(
11239+
OptCols(None, None),
11240+
OptCols(Some(1), Some(2)),
11241+
OptCols(None, None),
11242+
OptCols(None, Some(4))
11243+
)
11244+
```
11245+
11246+
11247+
11248+
### Optional.getOrElse - with SimpleTable
11249+
11250+
11251+
11252+
```scala
11253+
OptCols.select.map(d => d.updates(_.myInt(_.getOrElse(-1))))
11254+
```
11255+
11256+
11257+
*
11258+
```sql
11259+
SELECT
11260+
COALESCE(opt_cols0.my_int, ?) AS my_int,
11261+
opt_cols0.my_int2 AS my_int2
11262+
FROM opt_cols opt_cols0
11263+
```
11264+
11265+
11266+
11267+
*
11268+
```scala
11269+
Seq(
11270+
OptCols(Some(-1), None),
11271+
OptCols(Some(1), Some(2)),
11272+
OptCols(Some(3), None),
11273+
OptCols(Some(-1), Some(4))
11274+
)
11275+
```
11276+
11277+
11278+
11279+
### Optional.rawGet - with SimpleTable
11280+
11281+
11282+
11283+
```scala
11284+
OptCols.select.map(d => d.updates(_.myInt := d.myInt.get + d.myInt2.get + 1))
11285+
```
11286+
11287+
11288+
*
11289+
```sql
11290+
SELECT
11291+
((opt_cols0.my_int + opt_cols0.my_int2) + ?) AS my_int,
11292+
opt_cols0.my_int2 AS my_int2
11293+
FROM opt_cols opt_cols0
11294+
```
11295+
11296+
11297+
11298+
*
11299+
```scala
11300+
Seq(
11301+
OptCols(None, None),
11302+
OptCols(Some(4), Some(2)),
11303+
// because my_int2 is added to my_int, and my_int2 is null, my_int becomes null too
11304+
OptCols(None, None),
11305+
OptCols(None, Some(4))
11306+
)
11307+
```
11308+
11309+
11310+
11311+
### Optional.orElse - with SimpleTable
11312+
11313+
11314+
11315+
```scala
11316+
OptCols.select.map(d => d.updates(_.myInt(_.orElse(d.myInt2))))
11317+
```
11318+
11319+
11320+
*
11321+
```sql
11322+
SELECT
11323+
COALESCE(opt_cols0.my_int, opt_cols0.my_int2) AS my_int,
11324+
opt_cols0.my_int2 AS my_int2
11325+
FROM opt_cols opt_cols0
11326+
```
11327+
11328+
11329+
11330+
*
11331+
```scala
11332+
Seq(
11333+
OptCols(None, None),
11334+
OptCols(Some(1), Some(2)),
11335+
OptCols(Some(3), None),
11336+
OptCols(Some(4), Some(4))
11337+
)
11338+
```
11339+
11340+
11341+
11342+
### Optional.flatMap - with SimpleTable
11343+
11344+
11345+
11346+
```scala
11347+
OptCols.select
11348+
.map(d => d.updates(_.myInt(_.flatMap(v => d.myInt2.map(v2 => v + v2 + 10)))))
11349+
```
11350+
11351+
11352+
*
11353+
```sql
11354+
SELECT
11355+
((opt_cols0.my_int + opt_cols0.my_int2) + ?) AS my_int,
11356+
opt_cols0.my_int2 AS my_int2
11357+
FROM opt_cols opt_cols0
11358+
```
11359+
11360+
11361+
11362+
*
11363+
```scala
11364+
Seq(
11365+
OptCols(None, None),
11366+
OptCols(Some(13), Some(2)),
11367+
// because my_int2 is added to my_int, and my_int2 is null, my_int becomes null too
11368+
OptCols(None, None),
11369+
OptCols(None, Some(4))
11370+
)
11371+
```
11372+
11373+
11374+
11375+
### Optional.map - with SimpleTable
11376+
11377+
You can use operators like `.map` and `.flatMap` to work with
11378+
your `Expr[Option[V]]` values. These roughly follow the semantics
11379+
that you would be familiar with from Scala.
11380+
11381+
```scala
11382+
OptCols.select.map(d => d.updates(_.myInt(_.map(_ + 10))))
11383+
```
11384+
11385+
11386+
*
11387+
```sql
11388+
SELECT
11389+
(opt_cols0.my_int + ?) AS my_int,
11390+
opt_cols0.my_int2 AS my_int2
11391+
FROM opt_cols opt_cols0
11392+
```
11393+
11394+
11395+
11396+
*
11397+
```scala
11398+
Seq(
11399+
OptCols(None, None),
11400+
OptCols(Some(11), Some(2)),
11401+
OptCols(Some(13), None),
11402+
OptCols(None, Some(4))
11403+
)
11404+
```
11405+
11406+
11407+
1121411408
## PostgresDialect
1121511409
Operations specific to working with Postgres Databases
1121611410
### PostgresDialect.distinctOn

scalasql/namedtuples/test/src/datatypes/SimpleTableOptionalTests.scala

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,8 @@ trait SimpleTableOptionalTests extends ScalaSqlSuite {
255255

256256
}
257257

258-
test("map") - checker(
258+
// !! Important: '- with SimpleTable' so it will be detected by generateDocs.mill
259+
test("map - with SimpleTable") - checker(
259260
query = Text { OptCols.select.map(d => d.updates(_.myInt(_.map(_ + 10)))) },
260261
sql = """
261262
SELECT
@@ -282,7 +283,8 @@ trait SimpleTableOptionalTests extends ScalaSqlSuite {
282283
value = Seq(None, Some(11), Some(13), None)
283284
)
284285

285-
test("flatMap") - checker(
286+
// !! Important: '- with SimpleTable' so it will be detected by generateDocs.mill
287+
test("flatMap - with SimpleTable") - checker(
286288
query = Text {
287289
OptCols.select
288290
.map(d => d.updates(_.myInt(_.flatMap(v => d.myInt2.map(v2 => v + v2 + 10)))))
@@ -327,9 +329,10 @@ trait SimpleTableOptionalTests extends ScalaSqlSuite {
327329
"""
328330
)
329331

330-
test("rawGet") - checker(
332+
// !! Important: '- with SimpleTable' so it will be detected by generateDocs.mill
333+
test("rawGet - with SimpleTable") - checker(
331334
query = Text {
332-
OptCols.select.map(d => d.updates(_.myInt(_.get + d.myInt2.get + 1)))
335+
OptCols.select.map(d => d.updates(_.myInt := d.myInt.get + d.myInt2.get + 1))
333336
},
334337
sql = """
335338
SELECT
@@ -346,7 +349,8 @@ trait SimpleTableOptionalTests extends ScalaSqlSuite {
346349
)
347350
)
348351

349-
test("getOrElse") - checker(
352+
// !! Important: '- with SimpleTable' so it will be detected by generateDocs.mill
353+
test("getOrElse - with SimpleTable") - checker(
350354
query = Text { OptCols.select.map(d => d.updates(_.myInt(_.getOrElse(-1)))) },
351355
sql = """
352356
SELECT
@@ -362,7 +366,8 @@ trait SimpleTableOptionalTests extends ScalaSqlSuite {
362366
)
363367
)
364368

365-
test("orElse") - checker(
369+
// !! Important: '- with SimpleTable' so it will be detected by generateDocs.mill
370+
test("orElse - with SimpleTable") - checker(
366371
query = Text { OptCols.select.map(d => d.updates(_.myInt(_.orElse(d.myInt2)))) },
367372
sql = """
368373
SELECT
@@ -378,7 +383,8 @@ trait SimpleTableOptionalTests extends ScalaSqlSuite {
378383
)
379384
)
380385

381-
test("filter") - checker(
386+
// !! Important: '- with SimpleTable' so it will be detected by generateDocs.mill
387+
test("filter - with SimpleTable") - checker(
382388
query = Text { OptCols.select.map(d => d.updates(_.myInt(_.filter(_ < 2)))) },
383389
sql = """
384390
SELECT

0 commit comments

Comments
 (0)