type Colony {
name: Str
apiary: Str
frames: Int
}
query findColonies(apiary: Str, minFrames: Int, small: Bool, huge: Bool): Colony[dyn] {
SELECT name, apiary, frames FROM colonies
WHERE {
if apiary != "" { apiary = {apiary} }
if minFrames > 0 { frames >= {minFrames} }
or {
if small { frames < 4 }
if huge { frames > 10 }
}
}
ORDER BY name
}
proc main(): void {
opened := hive.sql.pool(hive.sql.DatabaseDriver.SQLite(), "file::memory:?cache=shared", 1, 1)
if opened is Result.Ok(db) {
using db run raw "CREATE TABLE colonies (name TEXT, apiary TEXT, frames INTEGER)"
using db run raw "INSERT INTO colonies VALUES ('north', 'orchard', 12)"
using db run raw "INSERT INTO colonies VALUES ('south', 'orchard', 2)"
using db run raw "INSERT INTO colonies VALUES ('east', 'meadow', 7)"
// Everything off: no predicate holds, so there is no WHERE at all.
if using db run findColonies("", 0, false, false) is Result.Ok(all) {
echo len(all)
}
// apiary AND frames >= 5 AND (frames > 10).
if using db run findColonies("orchard", 5, false, true) is Result.Ok(some) {
echo some
}
hive.sql.close(db)
}
}