Paste: site-watcher2

Author: erg
Mode: factor
Date: Tue, 17 Mar 2009 02:07:13
Plain Text |
! 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 <sqlite-db> swap with-db ; inline

TUPLE: account account-id email ;

: <account> ( 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 ;

: <site> ( 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 ;

: <watching-site> ( 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 -- )
    [ <account> maybe-insert account-id>> ]
    [ <site> maybe-insert site-id>> ] bi*
    <watching-site> maybe-insert drop ;

: account-sites ( email -- seq )
    ! "select account.email, site.url from account, site, watching_site 
    <account> select-tuple account-id>> f <watching-site> 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 ;

New Annotation

Summary:
Author:
Mode:
Body: