view tests/tags.ur @ 2253:d665925acff8

Factor out [Monoize.Fm] to make it accessible to [Sqlcache].
author Ziv Scully <ziv@mit.edu>
date Mon, 21 Sep 2015 14:54:07 -0400
parents 884673e5f7d5
children
line wrap: on
line source
table images : { Id : int, Content : blob }
table tags : { Id : int, Tag : string }

datatype mode = Present | Absent
type condition = { Tag : string, Mode : mode }

type tag_query = sql_query [] [] [] [Id = int]

fun addCondition (c : condition) (q : tag_query) : tag_query =
    case c.Mode of
        Present => (SELECT I.Id AS Id
                    FROM ({{q}}) AS I
                      JOIN tags ON tags.Id = I.Id AND tags.Tag = {[c.Tag]})
      | Absent => (SELECT I.Id AS Id
                   FROM ({{q}}) AS I
                     LEFT JOIN tags ON tags.Id = I.Id AND tags.Tag = {[c.Tag]}
                   WHERE tags.Tag IS NULL)

fun withConditions (cs : list condition) : tag_query =
    List.foldl addCondition (SELECT images.Id AS Id FROM images) cs

fun main (cs : list condition) : transaction page =
    x <- queryX (withConditions cs) (fn r => <xml><li>{[r.Id]}</li></xml>);
    return <xml><body>
      {x}
    </body></xml>