Project

General

Profile

« Previous | Next » 

Revision 5274

schemas/vegbien.sql: Added taxonconcept_ancestor cross-link table

View differences:

schemas/vegbien.my.sql
159 159

  
160 160

  
161 161
--
162
-- Name: taxonconcept_update_ancestors(); Type: FUNCTION; Schema: public; Owner: -
163
--
164

  
165

  
166

  
167

  
168
--
162 169
-- Name: concat(text); Type: AGGREGATE; Schema: public; Owner: -
163 170
--
164 171

  
......
2564 2571

  
2565 2572

  
2566 2573
--
2574
-- Name: taxonconcept_ancestor; Type: TABLE; Schema: public; Owner: -; Tablespace: 
2575
--
2576

  
2577
CREATE TABLE taxonconcept_ancestor (
2578
    taxonconcept_id int(11) NOT NULL,
2579
    ancestor_id int(11) NOT NULL
2580
);
2581

  
2582

  
2583
--
2567 2584
-- Name: taxonconcept_taxonconcept_id_seq; Type: SEQUENCE; Schema: public; Owner: -
2568 2585
--
2569 2586

  
......
3644 3661

  
3645 3662

  
3646 3663
--
3664
-- Name: taxonconcept_ancestor_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: 
3665
--
3666

  
3667
ALTER TABLE taxonconcept_ancestor
3668
    ADD CONSTRAINT taxonconcept_ancestor_pkey PRIMARY KEY (taxonconcept_id, ancestor_id);
3669

  
3670

  
3671
--
3647 3672
-- Name: taxonconcept_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: 
3648 3673
--
3649 3674

  
......
4192 4217

  
4193 4218

  
4194 4219
--
4220
-- Name: taxonconcept_update_ancestors; Type: TRIGGER; Schema: public; Owner: -
4221
--
4222

  
4223

  
4224

  
4225

  
4226
--
4195 4227
-- Name: address_organization_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
4196 4228
--
4197 4229

  
......
5058 5090

  
5059 5091

  
5060 5092
--
5093
-- Name: taxonconcept_ancestor_ancestor_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
5094
--
5095

  
5096
ALTER TABLE taxonconcept_ancestor
5097
    ADD CONSTRAINT taxonconcept_ancestor_ancestor_id_fkey FOREIGN KEY (ancestor_id) REFERENCES taxonconcept(taxonconcept_id) ON UPDATE CASCADE ON DELETE CASCADE;
5098

  
5099

  
5100
--
5101
-- Name: taxonconcept_ancestor_taxonconcept_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
5102
--
5103

  
5104
ALTER TABLE taxonconcept_ancestor
5105
    ADD CONSTRAINT taxonconcept_ancestor_taxonconcept_id_fkey FOREIGN KEY (taxonconcept_id) REFERENCES taxonconcept(taxonconcept_id) ON UPDATE CASCADE ON DELETE CASCADE;
5106

  
5107

  
5108
--
5061 5109
-- Name: taxonconcept_canon_taxonconcept_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
5062 5110
--
5063 5111

  
schemas/vegbien.sql
447 447

  
448 448

  
449 449
--
450
-- Name: taxonconcept_update_ancestors(); Type: FUNCTION; Schema: public; Owner: -
451
--
452

  
453
CREATE FUNCTION taxonconcept_update_ancestors() RETURNS trigger
454
    LANGUAGE plpgsql
455
    AS $$
456
BEGIN
457
    -- Delete existing ancestors
458
    DELETE FROM taxonconcept_ancestor
459
    WHERE taxonconcept_id = new.taxonconcept_id;
460
    
461
    IF new.parent_id IS NOT NULL THEN
462
        -- Copy parent's ancestors to this node's ancestors
463
        INSERT
464
        INTO taxonconcept_ancestor
465
        (taxonconcept_id, ancestor_id)
466
        SELECT
467
            new.taxonconcept_id, ancestor_id
468
        FROM taxonconcept_ancestor
469
        WHERE taxonconcept_id = new.parent_id
470
        ;
471
    END IF;
472
    
473
    -- Add "ancestor" for this node
474
    /* This is useful for queries, because you don't have to separately test if
475
    the leaf node is the one you're looking for, in addition to that leaf node's
476
    ancestors. */
477
    INSERT
478
    INTO taxonconcept_ancestor
479
    (taxonconcept_id, ancestor_id)
480
    VALUES (new.taxonconcept_id, new.taxonconcept_id)
481
    ;
482
    
483
    -- Tell immediate children to update their ancestors lists, which will
484
    -- recursively tell all descendants
485
    UPDATE taxonconcept
486
    SET taxonconcept_id = taxonconcept_id -- need at least one SET statement
487
    -- Add COALESCE() to enable using taxonconcept_unique index for lookup
488
    WHERE COALESCE(parent_id, 2147483647) = new.taxonconcept_id
489
    ;
490
    
491
    /* Note: We don't need an ON DELETE trigger to update the descendants'
492
    ancestors when a node is deleted, because the taxonconcept.parent_id foreign
493
    key is ON DELETE CASCADE, which just removes all the descendants anyway. */
494
    
495
    RETURN new;
496
END;
497
$$;
498

  
499

  
500
--
450 501
-- Name: concat(text); Type: AGGREGATE; Schema: public; Owner: -
451 502
--
452 503

  
......
3181 3232

  
3182 3233

  
3183 3234
--
3235
-- Name: taxonconcept_ancestor; Type: TABLE; Schema: public; Owner: -; Tablespace: 
3236
--
3237

  
3238
CREATE TABLE taxonconcept_ancestor (
3239
    taxonconcept_id integer NOT NULL,
3240
    ancestor_id integer NOT NULL
3241
);
3242

  
3243

  
3244
--
3184 3245
-- Name: taxonconcept_taxonconcept_id_seq; Type: SEQUENCE; Schema: public; Owner: -
3185 3246
--
3186 3247

  
......
4296 4357

  
4297 4358

  
4298 4359
--
4360
-- Name: taxonconcept_ancestor_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: 
4361
--
4362

  
4363
ALTER TABLE ONLY taxonconcept_ancestor
4364
    ADD CONSTRAINT taxonconcept_ancestor_pkey PRIMARY KEY (taxonconcept_id, ancestor_id);
4365

  
4366

  
4367
--
4299 4368
-- Name: taxonconcept_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: 
4300 4369
--
4301 4370

  
......
4844 4913

  
4845 4914

  
4846 4915
--
4916
-- Name: taxonconcept_update_ancestors; Type: TRIGGER; Schema: public; Owner: -
4917
--
4918

  
4919
CREATE TRIGGER taxonconcept_update_ancestors AFTER INSERT OR UPDATE ON taxonconcept FOR EACH ROW EXECUTE PROCEDURE taxonconcept_update_ancestors();
4920

  
4921

  
4922
--
4847 4923
-- Name: address_organization_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
4848 4924
--
4849 4925

  
......
5748 5824

  
5749 5825

  
5750 5826
--
5827
-- Name: taxonconcept_ancestor_ancestor_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
5828
--
5829

  
5830
ALTER TABLE ONLY taxonconcept_ancestor
5831
    ADD CONSTRAINT taxonconcept_ancestor_ancestor_id_fkey FOREIGN KEY (ancestor_id) REFERENCES taxonconcept(taxonconcept_id) ON UPDATE CASCADE ON DELETE CASCADE;
5832

  
5833

  
5834
--
5835
-- Name: taxonconcept_ancestor_taxonconcept_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
5836
--
5837

  
5838
ALTER TABLE ONLY taxonconcept_ancestor
5839
    ADD CONSTRAINT taxonconcept_ancestor_taxonconcept_id_fkey FOREIGN KEY (taxonconcept_id) REFERENCES taxonconcept(taxonconcept_id) ON UPDATE CASCADE ON DELETE CASCADE;
5840

  
5841

  
5842
--
5751 5843
-- Name: taxonconcept_canon_taxonconcept_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
5752 5844
--
5753 5845

  

Also available in: Unified diff