Project

General

Profile

« Previous | Next » 

Revision 5984

schemas/vegbien.sql: place: Added place_set_canon_place_id_on_*() triggers, analogous to taxonlabel.taxonlabel_2_set_canon_label_id_on_*()

View differences:

schemas/vegbien.my.sql
242 242

  
243 243

  
244 244

  
245

  
246

  
247

  
248

  
245 249
--
250
-- Name: place; Type: TABLE; Schema: public; Owner: -; Tablespace: 
251
--
252

  
253
CREATE TABLE place (
254
    place_id int(11) NOT NULL,
255
    reference_id int(11) NOT NULL,
256
    placecode text,
257
    canon_place_id int(11),
258
    matched_place_id int(11),
259
    coordinates_id int(11),
260
    placename_id int(11),
261
    continent text,
262
    country text,
263
    stateprovince text,
264
    county text,
265
    description text,
266
    georeference_valid int(1),
267
    distance_to_country_m double precision,
268
    distance_to_state_m double precision
269
);
270

  
271

  
272
--
273
-- Name: TABLE place; Type: COMMENT; Schema: public; Owner: -
274
--
275

  
276

  
277

  
278

  
279
--
280
-- Name: COLUMN place.matched_place_id; Type: COMMENT; Schema: public; Owner: -
281
--
282

  
283

  
284

  
285

  
286
--
287
-- Name: place_set_canon_place_id(place, int(11), int(1)); Type: FUNCTION; Schema: public; Owner: -
288
--
289

  
290

  
291

  
292

  
293
--
294
-- Name: place_set_canon_place_id_on_insert(); Type: FUNCTION; Schema: public; Owner: -
295
--
296

  
297

  
298

  
299

  
300
--
301
-- Name: place_set_canon_place_id_on_update(); Type: FUNCTION; Schema: public; Owner: -
302
--
303

  
304

  
305

  
306

  
307
--
246 308
-- Name: placename_update_ancestors(); Type: FUNCTION; Schema: public; Owner: -
247 309
--
248 310

  
......
305 367

  
306 368

  
307 369

  
308

  
309

  
310

  
311

  
312 370
--
313 371
-- Name: taxonlabel; Type: TABLE; Schema: public; Owner: -; Tablespace: 
314 372
--
......
1002 1060

  
1003 1061

  
1004 1062
--
1005
-- Name: place; Type: TABLE; Schema: public; Owner: -; Tablespace: 
1006
--
1007

  
1008
CREATE TABLE place (
1009
    place_id int(11) NOT NULL,
1010
    reference_id int(11) NOT NULL,
1011
    placecode text,
1012
    canon_place_id int(11),
1013
    matched_place_id int(11),
1014
    coordinates_id int(11),
1015
    placename_id int(11),
1016
    continent text,
1017
    country text,
1018
    stateprovince text,
1019
    county text,
1020
    description text,
1021
    georeference_valid int(1),
1022
    distance_to_country_m double precision,
1023
    distance_to_state_m double precision
1024
);
1025

  
1026

  
1027
--
1028
-- Name: TABLE place; Type: COMMENT; Schema: public; Owner: -
1029
--
1030

  
1031

  
1032

  
1033

  
1034
--
1035
-- Name: COLUMN place.matched_place_id; Type: COMMENT; Schema: public; Owner: -
1036
--
1037

  
1038

  
1039

  
1040

  
1041
--
1042 1063
-- Name: plantobservation; Type: TABLE; Schema: public; Owner: -; Tablespace: 
1043 1064
--
1044 1065

  
......
4372 4393

  
4373 4394

  
4374 4395
--
4396
-- Name: place_set_canon_place_id_on_insert; Type: TRIGGER; Schema: public; Owner: -
4397
--
4398

  
4399

  
4400

  
4401

  
4402
--
4403
-- Name: place_set_canon_place_id_on_update; Type: TRIGGER; Schema: public; Owner: -
4404
--
4405

  
4406

  
4407

  
4408

  
4409
--
4375 4410
-- Name: placename_update_ancestors; Type: TRIGGER; Schema: public; Owner: -
4376 4411
--
4377 4412

  
schemas/vegbien.sql
515 515
$$;
516 516

  
517 517

  
518
SET default_tablespace = '';
519

  
520
SET default_with_oids = false;
521

  
518 522
--
523
-- Name: place; Type: TABLE; Schema: public; Owner: -; Tablespace: 
524
--
525

  
526
CREATE TABLE place (
527
    place_id integer NOT NULL,
528
    reference_id integer NOT NULL,
529
    placecode text,
530
    canon_place_id integer,
531
    matched_place_id integer,
532
    coordinates_id integer,
533
    placename_id integer,
534
    continent text,
535
    country text,
536
    stateprovince text,
537
    county text,
538
    description text,
539
    georeference_valid boolean,
540
    distance_to_country_m double precision,
541
    distance_to_state_m double precision,
542
    CONSTRAINT place_required_key CHECK (((((((placecode IS NOT NULL) OR (coordinates_id IS NOT NULL)) OR (continent IS NOT NULL)) OR (country IS NOT NULL)) OR (stateprovince IS NOT NULL)) OR (county IS NOT NULL)))
543
);
544

  
545

  
546
--
547
-- Name: TABLE place; Type: COMMENT; Schema: public; Owner: -
548
--
549

  
550
COMMENT ON TABLE place IS 'The full path to a place. Can be either verbatim or accepted. For accepted names, points to the identified place.
551

  
552
To use a custom hierarchy of placenames with no explicit column, point to the lowest-rank placename in placename_id.';
553

  
554

  
555
--
556
-- Name: COLUMN place.matched_place_id; Type: COMMENT; Schema: public; Owner: -
557
--
558

  
559
COMMENT ON COLUMN place.matched_place_id IS 'The closest match to this place. Places should be linked in a three-level hierarchy of datasource place -> verbatim place -> accepted place.
560

  
561
An accepted place should point to itself in this field. This will happen automatically by setting it to the special value 0.';
562

  
563

  
564
--
565
-- Name: place_set_canon_place_id(place, integer, boolean); Type: FUNCTION; Schema: public; Owner: -
566
--
567

  
568
CREATE FUNCTION place_set_canon_place_id(new place, old_matched_place_id integer DEFAULT NULL::integer, is_update boolean DEFAULT false) RETURNS place
569
    LANGUAGE plpgsql
570
    AS $$
571
BEGIN
572
    IF NOT is_update
573
        OR new.matched_place_id IS DISTINCT FROM old_matched_place_id THEN
574
        IF new.matched_place_id IS NOT NULL THEN
575
            IF new.matched_place_id = new.place_id THEN -- self-reference
576
                new.canon_place_id := new.place_id; -- make self-reference
577
            ELSE -- propagate from matched place
578
                new.canon_place_id := (
579
                    SELECT canon_place_id
580
                    FROM place
581
                    WHERE place_id = new.matched_place_id
582
                );
583
            END IF;
584
            
585
            IF is_update THEN
586
                -- Update canon_place_id on places that resolve to this place
587
                UPDATE place
588
                SET canon_place_id = new.canon_place_id
589
                WHERE matched_place_id = new.place_id
590
                AND place_id != new.place_id -- no infinite recursion
591
                ;
592
            END IF;
593
        ELSE -- no matched place
594
            new.canon_place_id := new.place_id; -- make self-reference
595
        END IF;
596
    END IF;
597
    
598
    RETURN new;
599
END;
600
$$;
601

  
602

  
603
--
604
-- Name: place_set_canon_place_id_on_insert(); Type: FUNCTION; Schema: public; Owner: -
605
--
606

  
607
CREATE FUNCTION place_set_canon_place_id_on_insert() RETURNS trigger
608
    LANGUAGE plpgsql
609
    AS $$
610
BEGIN
611
    new := place_set_canon_place_id(new);
612
    
613
    RETURN new;
614
END;
615
$$;
616

  
617

  
618
--
619
-- Name: place_set_canon_place_id_on_update(); Type: FUNCTION; Schema: public; Owner: -
620
--
621

  
622
CREATE FUNCTION place_set_canon_place_id_on_update() RETURNS trigger
623
    LANGUAGE plpgsql
624
    AS $$
625
BEGIN
626
    new := place_set_canon_place_id(new, old.matched_place_id, true);
627
    
628
    RETURN new;
629
END;
630
$$;
631

  
632

  
633
--
519 634
-- Name: placename_update_ancestors(); Type: FUNCTION; Schema: public; Owner: -
520 635
--
521 636

  
......
689 804
$$;
690 805

  
691 806

  
692
SET default_tablespace = '';
693

  
694
SET default_with_oids = false;
695

  
696 807
--
697 808
-- Name: taxonlabel; Type: TABLE; Schema: public; Owner: -; Tablespace: 
698 809
--
......
1555 1666

  
1556 1667

  
1557 1668
--
1558
-- Name: place; Type: TABLE; Schema: public; Owner: -; Tablespace: 
1559
--
1560

  
1561
CREATE TABLE place (
1562
    place_id integer NOT NULL,
1563
    reference_id integer NOT NULL,
1564
    placecode text,
1565
    canon_place_id integer,
1566
    matched_place_id integer,
1567
    coordinates_id integer,
1568
    placename_id integer,
1569
    continent text,
1570
    country text,
1571
    stateprovince text,
1572
    county text,
1573
    description text,
1574
    georeference_valid boolean,
1575
    distance_to_country_m double precision,
1576
    distance_to_state_m double precision,
1577
    CONSTRAINT place_required_key CHECK (((((((placecode IS NOT NULL) OR (coordinates_id IS NOT NULL)) OR (continent IS NOT NULL)) OR (country IS NOT NULL)) OR (stateprovince IS NOT NULL)) OR (county IS NOT NULL)))
1578
);
1579

  
1580

  
1581
--
1582
-- Name: TABLE place; Type: COMMENT; Schema: public; Owner: -
1583
--
1584

  
1585
COMMENT ON TABLE place IS 'The full path to a place. Can be either verbatim or accepted. For accepted names, points to the identified place.
1586

  
1587
To use a custom hierarchy of placenames with no explicit column, point to the lowest-rank placename in placename_id.';
1588

  
1589

  
1590
--
1591
-- Name: COLUMN place.matched_place_id; Type: COMMENT; Schema: public; Owner: -
1592
--
1593

  
1594
COMMENT ON COLUMN place.matched_place_id IS 'The closest match to this place. Places should be linked in a three-level hierarchy of datasource place -> verbatim place -> accepted place.
1595

  
1596
An accepted place should point to itself in this field. This will happen automatically by setting it to the special value 0.';
1597

  
1598

  
1599
--
1600 1669
-- Name: plantobservation; Type: TABLE; Schema: public; Owner: -; Tablespace: 
1601 1670
--
1602 1671

  
......
5240 5309

  
5241 5310

  
5242 5311
--
5312
-- Name: place_set_canon_place_id_on_insert; Type: TRIGGER; Schema: public; Owner: -
5313
--
5314

  
5315
CREATE TRIGGER place_set_canon_place_id_on_insert BEFORE INSERT ON place FOR EACH ROW EXECUTE PROCEDURE place_set_canon_place_id_on_insert();
5316

  
5317

  
5318
--
5319
-- Name: place_set_canon_place_id_on_update; Type: TRIGGER; Schema: public; Owner: -
5320
--
5321

  
5322
CREATE TRIGGER place_set_canon_place_id_on_update BEFORE UPDATE ON place FOR EACH ROW EXECUTE PROCEDURE place_set_canon_place_id_on_update();
5323

  
5324

  
5325
--
5243 5326
-- Name: placename_update_ancestors; Type: TRIGGER; Schema: public; Owner: -
5244 5327
--
5245 5328

  

Also available in: Unified diff