--
-- create_saif.sql must be run before this file
--
--
-- create the tables.
--
create table rasters (
	rtime       int,
	location    Box,
	frequency   int,
	sample_bytes int
) under StructuredData, Grid;

--
-- After inheriting StructuredData and Grid, 'rasters' now looks like this:
--
--	rasters:
--		contentStructure    ContentStructure  \ StructuredData
--		contentValues       raster_t          /
--		positionOfOrigin    CoordXY           \
--		orderAndOrigin      GridReference     | Grid
--		spacing             GridFramework     /
--		rtime               int
--		location            Box
--		frequency           int
--
create table points (location Pnt, name text, county text); 
create table polygons (landuse int, location Poly); 
create table islands (location Poly);

create table s2k_audit (
        s2k_rule        text,   -- name of the rule that fired (rules.rule_name)
        s2k_user        text,   -- user that caused rule to fire
        s2k_obj         text,   -- name of object rule is on (rules.rule_table)
        s2k_ops         char,   -- (d)elete, (i)insert, (s)elect, (u)pdate
        s2k_info        text,   -- any additional info
        s2k_date        abstime -- date & time rule fired
        );

--
-- alerters and alerter rules
--
create alerter SantaB (mechanism = 'callback');

create rule sb_select_rule on
        select to points where current.county = 'Santa Barbara'
        do
                begin
                        insert into s2k_audit
                        values(
                                'sb_rule',
                                user,
                                'points',
                                's',
                                current.name,
                                'now');
                        alert SantaB;
                end ;

create rule sb_delete_rule on
        delete to points where current.county = 'Santa Barbara'
        do
                begin
                        insert into s2k_audit
                        values(
                                'sb_rule',
                                user,
                                'points',
                                'd',
                                current.name,
                                'now');
                        alert SantaB;
                end ;

-- more spatial support from sequoia2000 bench example
--
-- functions operating on the builtin, but undocumented Pnt, Poly
-- and Box types
--
create function make_box (Pnt, double precision) returns Box
    as external name '/usr/montage/public/tahoe_demo/src/polyfuncs.so'
    language c not variant;
create function box_make_poly (Box) returns Poly
    as external name '/usr/montage/public/tahoe_demo/src/polyfuncs.so'
    language c not variant;
--
-- returns the size of the Poly as its area (rather the area of the
-- the Poly's bounding Box
--
create function size (Poly) returns double precision
    as external name '/usr/montage/public/tahoe_demo/src/polyfuncs.so'
    language c not variant;
--
-- Pnt to (degenerate) Poly conversion function
--
create function pt_make_poly(Pnt) returns Poly
    as external name '/usr/montage/public/tahoe_demo/src/polyfuncs.so'
    language c not variant;

-- operator bindings
--
-- bind Overlap to the & operator
--
create binary operator binding & to Overlap;

--
-- bind Contained to the ^ operator
--
create binary operator binding ^ to Contained; 
