Skip to content

Commit 410c2c6

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

File tree

2 files changed

+243
-7
lines changed

2 files changed

+243
-7
lines changed

docs/reference.md

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11211,6 +11211,235 @@ 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(_.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.mapGet - with SimpleTable
11343+
11344+
You can use `.get` to turn an `Expr[Option[V]]` into an `Expr[V]`. This follows
11345+
SQL semantics, such that `NULL`s anywhere in that selected column automatically
11346+
will turn the whole column `None` (if it's an `Expr[Option[V]]` column) or `null`
11347+
(if it's not an optional column)
11348+
11349+
```scala
11350+
OptCols.select.map(d => d.updates(_.myInt(_.map(_ + d.myInt2.get + 1))))
11351+
```
11352+
11353+
11354+
*
11355+
```sql
11356+
SELECT
11357+
((opt_cols0.my_int + opt_cols0.my_int2) + ?) AS my_int,
11358+
opt_cols0.my_int2 AS my_int2
11359+
FROM opt_cols opt_cols0
11360+
```
11361+
11362+
11363+
11364+
*
11365+
```scala
11366+
Seq(
11367+
OptCols(None, None),
11368+
OptCols(Some(4), Some(2)),
11369+
// because my_int2 is added to my_int, and my_int2 is null, my_int becomes null too
11370+
OptCols(None, None),
11371+
OptCols(None, Some(4))
11372+
)
11373+
```
11374+
11375+
11376+
11377+
### Optional.flatMap - with SimpleTable
11378+
11379+
11380+
11381+
```scala
11382+
OptCols.select
11383+
.map(d => d.updates(_.myInt(_.flatMap(v => d.myInt2.map(v2 => v + v2 + 10)))))
11384+
```
11385+
11386+
11387+
*
11388+
```sql
11389+
SELECT
11390+
((opt_cols0.my_int + opt_cols0.my_int2) + ?) AS my_int,
11391+
opt_cols0.my_int2 AS my_int2
11392+
FROM opt_cols opt_cols0
11393+
```
11394+
11395+
11396+
11397+
*
11398+
```scala
11399+
Seq(
11400+
OptCols(None, None),
11401+
OptCols(Some(13), Some(2)),
11402+
// because my_int2 is added to my_int, and my_int2 is null, my_int becomes null too
11403+
OptCols(None, None),
11404+
OptCols(None, Some(4))
11405+
)
11406+
```
11407+
11408+
11409+
11410+
### Optional.map - with SimpleTable
11411+
11412+
You can use operators like `.map` and `.flatMap` to work with
11413+
your `Expr[Option[V]]` values. These roughly follow the semantics
11414+
that you would be familiar with from Scala.
11415+
11416+
```scala
11417+
OptCols.select.map(d => d.updates(_.myInt(_.map(_ + 10))))
11418+
```
11419+
11420+
11421+
*
11422+
```sql
11423+
SELECT
11424+
(opt_cols0.my_int + ?) AS my_int,
11425+
opt_cols0.my_int2 AS my_int2
11426+
FROM opt_cols opt_cols0
11427+
```
11428+
11429+
11430+
11431+
*
11432+
```scala
11433+
Seq(
11434+
OptCols(None, None),
11435+
OptCols(Some(11), Some(2)),
11436+
OptCols(Some(13), None),
11437+
OptCols(None, Some(4))
11438+
)
11439+
```
11440+
11441+
11442+
1121411443
## PostgresDialect
1121511444
Operations specific to working with Postgres Databases
1121611445
### PostgresDialect.distinctOn

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

Lines changed: 14 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)))))
@@ -302,7 +304,8 @@ trait SimpleTableOptionalTests extends ScalaSqlSuite {
302304
)
303305
)
304306

305-
test("mapGet") - checker(
307+
// !! Important: '- with SimpleTable' so it will be detected by generateDocs.mill
308+
test("mapGet - with SimpleTable") - checker(
306309
query = Text {
307310
OptCols.select.map(d => d.updates(_.myInt(_.map(_ + d.myInt2.get + 1))))
308311
},
@@ -327,7 +330,8 @@ trait SimpleTableOptionalTests extends ScalaSqlSuite {
327330
"""
328331
)
329332

330-
test("rawGet") - checker(
333+
// !! Important: '- with SimpleTable' so it will be detected by generateDocs.mill
334+
test("rawGet - with SimpleTable") - checker(
331335
query = Text {
332336
OptCols.select.map(d => d.updates(_.myInt(_.get + d.myInt2.get + 1)))
333337
},
@@ -346,7 +350,8 @@ trait SimpleTableOptionalTests extends ScalaSqlSuite {
346350
)
347351
)
348352

349-
test("getOrElse") - checker(
353+
// !! Important: '- with SimpleTable' so it will be detected by generateDocs.mill
354+
test("getOrElse - with SimpleTable") - checker(
350355
query = Text { OptCols.select.map(d => d.updates(_.myInt(_.getOrElse(-1)))) },
351356
sql = """
352357
SELECT
@@ -362,7 +367,8 @@ trait SimpleTableOptionalTests extends ScalaSqlSuite {
362367
)
363368
)
364369

365-
test("orElse") - checker(
370+
// !! Important: '- with SimpleTable' so it will be detected by generateDocs.mill
371+
test("orElse - with SimpleTable") - checker(
366372
query = Text { OptCols.select.map(d => d.updates(_.myInt(_.orElse(d.myInt2)))) },
367373
sql = """
368374
SELECT
@@ -378,7 +384,8 @@ trait SimpleTableOptionalTests extends ScalaSqlSuite {
378384
)
379385
)
380386

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

0 commit comments

Comments
 (0)