#lang imperative-syndicate

(require/activate imperative-syndicate/drivers/sqlite)
(require/activate imperative-syndicate/drivers/timer)

(define PATH "t.sqlite")
(define DB (sqlite-db PATH))

(spawn* (with-handlers [(exn:fail:filesystem? void)]
          (delete-file PATH))
        (react (assert DB))

        (sqlite-create-table! DB "x" "y" "z")

        (send! (sqlite-insert DB "x" (list 1 "yy") (gensym 'init)))
        (send! (sqlite-insert DB "x" (list "yy" "hello") (gensym 'init)))
        (send! (sqlite-insert DB "x" (list "yy" "goodbye") (gensym 'init)))
        (send! (sqlite-insert DB "x" (list 1 "yy") (gensym 'init)))

        (react
         (during (sqlite-row DB "x" (list _ $key))
           (during (sqlite-row DB "x" (list key $value))
             (on-start (printf "+ ~a row in x: ~a\n" key value))
             (on-stop (printf "- ~a row in x: ~a\n" key value))))
         (during (sqlite-row DB "x" $columns)
           (on-start (printf "+ row in x: ~a\n" columns))
           (on-stop (printf "- row in x: ~a\n" columns))))

        (sqlite-insert! DB "x" "a" "b")
        (sqlite-insert! DB "x" "a" "c")
        (sqlite-insert! DB "x" "yy" "b")
        (sqlite-insert! DB "x" "yy" "c")
        (sqlite-delete! DB "x" "a" "b")
        (sqlite-delete! DB "x" (discard) "b")
        (sqlite-delete! DB "x" "a" (discard))
        (sqlite-delete! DB "x" (discard) "c"))
