! work in progress ! Copyright (C) 2009 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. USING: db.sqlite db.types db.tuples kernel accessors db io.files io.files.temp locals io.directories continuations ; IN: site-watcher2 : site-watcher-path ( -- path ) "site-watcher.db" temp-file ; inline [ site-watcher-path delete-file ] ignore-errors : with-sqlite-db ( quot -- ) site-watcher-path swap with-db ; inline TUPLE: account account-id email ; : ( email -- account ) account new swap >>email ; account "ACCOUNT" { { "account-id" "ACCOUNT_ID" +db-assigned-id+ } { "email" "EMAIL" VARCHAR } } define-persistent TUPLE: site site-id url ; : ( url -- site ) site new swap >>url ; site "SITE" { { "site-id" "SITE_ID" INTEGER +db-assigned-id+ } { "url" "URL" VARCHAR } } define-persistent TUPLE: watching-site account-id site-id ; : ( account-id site-id -- watching-site ) watching-site new swap >>site-id swap >>account-id ; watching-site "WATCHING_SITE" { { "account-id" "ACCOUNT_ID" INTEGER +user-assigned-id+ } { "site-id" "SITE_ID" INTEGER +user-assigned-id+ } } define-persistent : maybe-insert ( tuple -- tuple ) dup select-tuple [ drop ] [ dup insert-tuple ] if* ; inline : watch-site ( email url -- ) [ maybe-insert account-id>> ] [ maybe-insert site-id>> ] bi* maybe-insert drop ; : account-sites ( email -- seq ) ! "select account.email, site.url from account, site, watching_site select-tuple account-id>> f select-tuples ; :: fake-sites ( -- seq ) [ account ensure-table site ensure-table watching-site ensure-table "erg@factorcode.org" "http://asdofijweoijwewoijfewwoi.com" watch-site "erg@factorcode.org" account-sites ] with-sqlite-db ;