Add missing sqlite conditional functions.
This commit is contained in:
parent
bdbbc4d58d
commit
f93636eab0
4 changed files with 251 additions and 1 deletions
|
|
@ -810,3 +810,75 @@ func TestScanNumericToNumber(t *testing.T) {
|
|||
require.Equal(t, number.Float32, float32(1.234568e+09))
|
||||
require.Equal(t, number.Float64, float64(1.234567890111e+09))
|
||||
}
|
||||
|
||||
func TestConditionalFunctions(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
EXISTS(
|
||||
Film.SELECT(Film.FilmID).WHERE(Film.RentalDuration.GT(Int(5))),
|
||||
).AS("exists"),
|
||||
CASE(Film.Length.GT(Int(120))).
|
||||
WHEN(Bool(true)).THEN(String("long film")).
|
||||
ELSE(String("short film")).AS("case"),
|
||||
COALESCE(Film.Description, String("none")).AS("coalesce"),
|
||||
NULLIF(Film.ReleaseYear, Int(200)).AS("null_if"),
|
||||
).FROM(
|
||||
Film,
|
||||
).WHERE(
|
||||
Film.FilmID.LT(Int(5)),
|
||||
).ORDER_BY(
|
||||
Film.FilmID,
|
||||
)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
SELECT (EXISTS (
|
||||
SELECT film.film_id AS "film.film_id"
|
||||
FROM film
|
||||
WHERE film.rental_duration > 5
|
||||
)) AS "exists",
|
||||
(CASE (film.length > 120) WHEN TRUE THEN 'long film' ELSE 'short film' END) AS "case",
|
||||
COALESCE(film.description, 'none') AS "coalesce",
|
||||
NULLIF(film.release_year, 200) AS "null_if"
|
||||
FROM film
|
||||
WHERE film.film_id < 5
|
||||
ORDER BY film.film_id;
|
||||
`)
|
||||
|
||||
var res []struct {
|
||||
Exists bool
|
||||
Case string
|
||||
Coalesce string
|
||||
NullIf string
|
||||
}
|
||||
|
||||
err := stmt.Query(db, &res)
|
||||
require.NoError(t, err)
|
||||
|
||||
testutils.AssertJSON(t, res, `
|
||||
[
|
||||
{
|
||||
"Exists": true,
|
||||
"Case": "short film",
|
||||
"Coalesce": "A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies",
|
||||
"NullIf": "2006"
|
||||
},
|
||||
{
|
||||
"Exists": true,
|
||||
"Case": "short film",
|
||||
"Coalesce": "A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China",
|
||||
"NullIf": "2006"
|
||||
},
|
||||
{
|
||||
"Exists": true,
|
||||
"Case": "short film",
|
||||
"Coalesce": "A Astounding Reflection of a Lumberjack And a Car who must Sink a Lumberjack in A Baloon Factory",
|
||||
"NullIf": "2006"
|
||||
},
|
||||
{
|
||||
"Exists": true,
|
||||
"Case": "short film",
|
||||
"Coalesce": "A Fanciful Documentary of a Frisbee And a Lumberjack who must Chase a Monkey in A Shark Tank",
|
||||
"NullIf": "2006"
|
||||
}
|
||||
]
|
||||
`)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue