This repository has been archived by the owner on Jun 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
qgeologistest.sql
8747 lines (6245 loc) · 287 KB
/
qgeologistest.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--
-- PostgreSQL database dump
--
-- Dumped from database version 10.12 (Ubuntu 10.12-0ubuntu0.18.04.1)
-- Dumped by pg_dump version 10.12 (Ubuntu 10.12-0ubuntu0.18.04.1)
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
--
-- Name: measure; Type: SCHEMA; Schema: -; Owner: postgres
--
CREATE SCHEMA measure;
ALTER SCHEMA measure OWNER TO postgres;
--
-- Name: metadata; Type: SCHEMA; Schema: -; Owner: postgres
--
CREATE SCHEMA metadata;
ALTER SCHEMA metadata OWNER TO postgres;
--
-- Name: SCHEMA metadata; Type: COMMENT; Schema: -; Owner: postgres
--
COMMENT ON SCHEMA metadata IS 'Schema where metadata about imported data are stored';
--
-- Name: qgis; Type: SCHEMA; Schema: -; Owner: postgres
--
CREATE SCHEMA qgis;
ALTER SCHEMA qgis OWNER TO postgres;
--
-- Name: SCHEMA qgis; Type: COMMENT; Schema: -; Owner: postgres
--
COMMENT ON SCHEMA qgis IS 'Schema where views for QGIS visualisation are stored';
--
-- Name: ref; Type: SCHEMA; Schema: -; Owner: postgres
--
CREATE SCHEMA ref;
ALTER SCHEMA ref OWNER TO postgres;
--
-- Name: SCHEMA ref; Type: COMMENT; Schema: -; Owner: postgres
--
COMMENT ON SCHEMA ref IS 'Schema where references and constants are stored';
--
-- Name: station; Type: SCHEMA; Schema: -; Owner: postgres
--
CREATE SCHEMA station;
ALTER SCHEMA station OWNER TO postgres;
--
-- Name: SCHEMA station; Type: COMMENT; Schema: -; Owner: postgres
--
COMMENT ON SCHEMA station IS 'Main schema where data about drill station are stored';
--
-- Name: tr; Type: SCHEMA; Schema: -; Owner: postgres
--
CREATE SCHEMA tr;
ALTER SCHEMA tr OWNER TO postgres;
--
-- Name: SCHEMA tr; Type: COMMENT; Schema: -; Owner: postgres
--
COMMENT ON SCHEMA tr IS 'Schema where descriptions and translations are stored';
--
-- Name: utils; Type: SCHEMA; Schema: -; Owner: postgres
--
CREATE SCHEMA utils;
ALTER SCHEMA utils OWNER TO postgres;
--
-- Name: plpgsql; Type: EXTENSION; Schema: -; Owner:
--
CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;
--
-- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner:
--
COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';
--
-- Name: btree_gist; Type: EXTENSION; Schema: -; Owner:
--
CREATE EXTENSION IF NOT EXISTS btree_gist WITH SCHEMA utils;
--
-- Name: EXTENSION btree_gist; Type: COMMENT; Schema: -; Owner:
--
COMMENT ON EXTENSION btree_gist IS 'support for indexing common datatypes in GiST';
--
-- Name: postgis; Type: EXTENSION; Schema: -; Owner:
--
CREATE EXTENSION IF NOT EXISTS postgis WITH SCHEMA public;
--
-- Name: EXTENSION postgis; Type: COMMENT; Schema: -; Owner:
--
COMMENT ON EXTENSION postgis IS 'PostGIS geometry, geography, and raster spatial types and functions';
--
-- Name: rgb_hex; Type: DOMAIN; Schema: ref; Owner: postgres
--
CREATE DOMAIN ref.rgb_hex AS text
CONSTRAINT rgb_hex_check CHECK ((VALUE ~ '^#[0123456789abcdef]{6}$'::text));
ALTER DOMAIN ref.rgb_hex OWNER TO postgres;
--
-- Name: DOMAIN rgb_hex; Type: COMMENT; Schema: ref; Owner: postgres
--
COMMENT ON DOMAIN ref.rgb_hex IS 'Text representation of RGB colors with html format #rrggbb';
--
-- Name: roc_code_authority; Type: TYPE; Schema: ref; Owner: postgres
--
CREATE TYPE ref.roc_code_authority AS ENUM (
'USGS'
);
ALTER TYPE ref.roc_code_authority OWNER TO postgres;
--
-- Name: borehole_type; Type: TYPE; Schema: station; Owner: postgres
--
CREATE TYPE station.borehole_type AS ENUM (
'Piezometer',
'CoreDrill',
'FilledUpDrill',
'GeotechnicDrill',
'Borehole',
'Well',
'DrainWell'
);
ALTER TYPE station.borehole_type OWNER TO postgres;
--
-- Name: chimney_type; Type: TYPE; Schema: station; Owner: postgres
--
CREATE TYPE station.chimney_type AS ENUM (
'Chimney'
);
ALTER TYPE station.chimney_type OWNER TO postgres;
--
-- Name: device_type; Type: TYPE; Schema: station; Owner: postgres
--
CREATE TYPE station.device_type AS ENUM (
'MeasurementDevice',
'DrainagePump'
);
ALTER TYPE station.device_type OWNER TO postgres;
--
-- Name: hydrology_station_type; Type: TYPE; Schema: station; Owner: postgres
--
CREATE TYPE station.hydrology_station_type AS ENUM (
'River',
'Spring'
);
ALTER TYPE station.hydrology_station_type OWNER TO postgres;
--
-- Name: sample_family; Type: TYPE; Schema: station; Owner: postgres
--
CREATE TYPE station.sample_family AS ENUM (
'Air',
'Ground',
'Water',
'Animal',
'Plant'
);
ALTER TYPE station.sample_family OWNER TO postgres;
--
-- Name: station_family; Type: TYPE; Schema: station; Owner: postgres
--
CREATE TYPE station.station_family AS ENUM (
'Borehole',
'Chimney',
'Weather_Station',
'Hydrology_Station',
'Sample',
'Device'
);
ALTER TYPE station.station_family OWNER TO postgres;
--
-- Name: weather_station_type; Type: TYPE; Schema: station; Owner: postgres
--
CREATE TYPE station.weather_station_type AS ENUM (
'Pluviometer',
'WeatherStation',
'WeatherMast'
);
ALTER TYPE station.weather_station_type OWNER TO postgres;
--
-- Name: _create_continuous_measure_over_altitude_table(text, text, text); Type: FUNCTION; Schema: measure; Owner: postgres
--
CREATE FUNCTION measure._create_continuous_measure_over_altitude_table(table_name text, title text, unit text) RETURNS void
LANGUAGE plpgsql
AS $_$
begin
execute format($sql$
create table measure.%s (
id serial primary key
, station_id bigint not null references station(id) on delete cascade
, start_measure_altitude double precision not null
-- interval between each measure point, in meters
, altitude_interval double precision not null
, measures double precision[]
, campaign_id bigint references campaign(id) default 1
, dataset_id int /*not null*/ references metadata.dataset(id) on delete cascade
, unique (station_id, start_measure_altitude, campaign_id)
)$sql$, table_name);
execute format($sql$
insert into measure_metadata (measure_table, name, unit_of_measure, x_axis_type) values ('%s', %s, %s, 'DepthAxis')
$sql$, table_name, quote_literal(title), quote_literal(unit));
end;
$_$;
ALTER FUNCTION measure._create_continuous_measure_over_altitude_table(table_name text, title text, unit text) OWNER TO postgres;
--
-- Name: _create_continuous_measure_over_time_table(text, text, text); Type: FUNCTION; Schema: measure; Owner: postgres
--
CREATE FUNCTION measure._create_continuous_measure_over_time_table(table_name text, title text, unit text) RETURNS void
LANGUAGE plpgsql
AS $_$
begin
execute format($sql$
create table measure.%s (
id serial primary key
, station_id bigint not null references station(id) on delete cascade
, start_measure_time timestamp not null
-- interval between each measure point
, time_interval interval not null
, campaign_id bigint references campaign(id) default 1
, dataset_id int /*not null*/ references metadata.dataset(id) on delete cascade
, measures double precision[]
, unique (station_id, start_measure_time, campaign_id)
)$sql$, table_name);
execute format($sql$
insert into measure_metadata (measure_table, name, unit_of_measure, x_axis_type) values ('%s', %s, %s, 'TimeAxis')
$sql$, table_name, quote_literal(title), quote_literal(unit));
end;
$_$;
ALTER FUNCTION measure._create_continuous_measure_over_time_table(table_name text, title text, unit text) OWNER TO postgres;
--
-- Name: _create_cumulative_measure_over_time_table(text, text, text); Type: FUNCTION; Schema: measure; Owner: postgres
--
CREATE FUNCTION measure._create_cumulative_measure_over_time_table(table_name text, title text, unit text) RETURNS void
LANGUAGE plpgsql
AS $_$
begin
execute format($sql$
create table measure.%s (
id serial primary key
, station_id bigint not null references station(id) on delete cascade
, start_measure_time timestamp not null
, end_measure_time timestamp not null
, measure_value double precision
, periodicity text
, reference text -- original file
, campaign_id bigint references campaign(id) default 1
, dataset_id int /*not null*/ references metadata.dataset(id) on delete cascade
, unique (station_id, start_measure_time, campaign_id)
)$sql$, table_name);
execute format($sql$
insert into measure_metadata (measure_table, name, unit_of_measure, x_axis_type, storage_type) values ('%s', %s, %s, 'TimeAxis', 'Cumulative')
$sql$, table_name, quote_literal(title), quote_literal(unit));
end;
$_$;
ALTER FUNCTION measure._create_cumulative_measure_over_time_table(table_name text, title text, unit text) OWNER TO postgres;
--
-- Name: _create_instantaneous_measure_over_time_table(text, text, text); Type: FUNCTION; Schema: measure; Owner: postgres
--
CREATE FUNCTION measure._create_instantaneous_measure_over_time_table(table_name text, title text, unit text) RETURNS void
LANGUAGE plpgsql
AS $_$
begin
execute format($sql$
create table measure.%s (
id serial primary key
, station_id bigint not null references station(id) on delete cascade
, measure_time timestamp not null
, measure_value double precision
, campaign_id bigint references campaign(id) default 1
, dataset_id int /*not null*/ references metadata.dataset(id) on delete cascade
, unique (station_id, measure_time, campaign_id)
)$sql$, table_name);
execute format($sql$
insert into measure_metadata (measure_table, name, unit_of_measure, x_axis_type, storage_type) values ('%s', %s, %s, 'TimeAxis', 'Instantaneous')
$sql$, table_name, quote_literal(title), quote_literal(unit));
end;
$_$;
ALTER FUNCTION measure._create_instantaneous_measure_over_time_table(table_name text, title text, unit text) OWNER TO postgres;
--
-- Name: delete_children_ft(); Type: FUNCTION; Schema: metadata; Owner: postgres
--
CREATE FUNCTION metadata.delete_children_ft() RETURNS trigger
LANGUAGE plpgsql
AS $$
begin
delete from metadata.dataset
using (
select id, unnest(parent_ids) as parent_id
from metadata.dataset
) to_del
where
to_del.parent_id = old.id
and dataset.id = to_del.id
;
return old;
end;
$$;
ALTER FUNCTION metadata.delete_children_ft() OWNER TO postgres;
--
-- Name: borehole_delete_ft(); Type: FUNCTION; Schema: station; Owner: postgres
--
CREATE FUNCTION station.borehole_delete_ft() RETURNS trigger
LANGUAGE plpgsql
AS $$
begin
delete from station.station_borehole where id = old.id;
delete from station.station where id = old.id;
return old;
end;
$$;
ALTER FUNCTION station.borehole_delete_ft() OWNER TO postgres;
--
-- Name: borehole_insert_ft(); Type: FUNCTION; Schema: station; Owner: postgres
--
CREATE FUNCTION station.borehole_insert_ft() RETURNS trigger
LANGUAGE plpgsql
AS $$
begin
insert into station.station select
nextval('station.station_id_seq'::regclass)
, new.site_id,new.station_family,new.station_type,new.name,new.point,new.orig_srid,new.ground_altitude,new.dataset_id
;
insert into station.station_borehole select
currval('station.station_id_seq'::regclass)
, new.total_depth,new.top_of_casing_altitude,new.casing_height,new.casing_internal_diameter,new.casing_external_diameter,new.driller,new.drilling_date,new.drilling_method,new.associated_barometer,new.location,new.num_bss,new.borehole_type,new.usage,new.condition
;
return new;
end;
$$;
ALTER FUNCTION station.borehole_insert_ft() OWNER TO postgres;
--
-- Name: borehole_update_ft(); Type: FUNCTION; Schema: station; Owner: postgres
--
CREATE FUNCTION station.borehole_update_ft() RETURNS trigger
LANGUAGE plpgsql
AS $$
begin
update station.station set (site_id,station_family,station_type,name,point,orig_srid,ground_altitude,dataset_id) = (new.site_id,new.station_family,new.station_type,new.name,new.point,new.orig_srid,new.ground_altitude,new.dataset_id) where id=old.id;
update station.station_borehole set (total_depth,top_of_casing_altitude,casing_height,casing_internal_diameter,casing_external_diameter,driller,drilling_date,drilling_method,associated_barometer,location,num_bss,borehole_type,usage,condition) = (new.total_depth,new.top_of_casing_altitude,new.casing_height,new.casing_internal_diameter,new.casing_external_diameter,new.driller,new.drilling_date,new.drilling_method,new.associated_barometer,new.location,new.num_bss,new.borehole_type,new.usage,new.condition) where id=old.id;
return new;
end;
$$;
ALTER FUNCTION station.borehole_update_ft() OWNER TO postgres;
--
-- Name: chimney_delete_ft(); Type: FUNCTION; Schema: station; Owner: postgres
--
CREATE FUNCTION station.chimney_delete_ft() RETURNS trigger
LANGUAGE plpgsql
AS $$
begin
delete from station.station_chimney where id = old.id;
delete from station.station where id = old.id;
return old;
end;
$$;
ALTER FUNCTION station.chimney_delete_ft() OWNER TO postgres;
--
-- Name: chimney_insert_ft(); Type: FUNCTION; Schema: station; Owner: postgres
--
CREATE FUNCTION station.chimney_insert_ft() RETURNS trigger
LANGUAGE plpgsql
AS $$
begin
insert into station.station select
nextval('station.station_id_seq'::regclass)
, new.site_id,new.station_family,new.station_type,new.name,new.point,new.orig_srid,new.ground_altitude,new.dataset_id
;
insert into station.station_chimney select
currval('station.station_id_seq'::regclass)
, new.chimney_type,new.nuclear_facility_name,new.facility_name,new.building_name,new.height,new.flow_rate,new.surface
;
return new;
end;
$$;
ALTER FUNCTION station.chimney_insert_ft() OWNER TO postgres;
--
-- Name: chimney_update_ft(); Type: FUNCTION; Schema: station; Owner: postgres
--
CREATE FUNCTION station.chimney_update_ft() RETURNS trigger
LANGUAGE plpgsql
AS $$
begin
update station.station set (site_id,station_family,station_type,name,point,orig_srid,ground_altitude,dataset_id) = (new.site_id,new.station_family,new.station_type,new.name,new.point,new.orig_srid,new.ground_altitude,new.dataset_id) where id=old.id;
update station.station_chimney set (chimney_type,nuclear_facility_name,facility_name,building_name,height,flow_rate,surface) = (new.chimney_type,new.nuclear_facility_name,new.facility_name,new.building_name,new.height,new.flow_rate,new.surface) where id=old.id;
return new;
end;
$$;
ALTER FUNCTION station.chimney_update_ft() OWNER TO postgres;
--
-- Name: device_delete_ft(); Type: FUNCTION; Schema: station; Owner: postgres
--
CREATE FUNCTION station.device_delete_ft() RETURNS trigger
LANGUAGE plpgsql
AS $$
begin
delete from station.station_device where id = old.id;
delete from station.station where id = old.id;
return old;
end;
$$;
ALTER FUNCTION station.device_delete_ft() OWNER TO postgres;
--
-- Name: device_insert_ft(); Type: FUNCTION; Schema: station; Owner: postgres
--
CREATE FUNCTION station.device_insert_ft() RETURNS trigger
LANGUAGE plpgsql
AS $$
begin
insert into station.station select
nextval('station.station_id_seq'::regclass)
, new.site_id,new.station_family,new.station_type,new.name,new.point,new.orig_srid,new.ground_altitude,new.dataset_id
;
insert into station.station_device select
currval('station.station_id_seq'::regclass)
, new.device_type
;
return new;
end;
$$;
ALTER FUNCTION station.device_insert_ft() OWNER TO postgres;
--
-- Name: device_update_ft(); Type: FUNCTION; Schema: station; Owner: postgres
--
CREATE FUNCTION station.device_update_ft() RETURNS trigger
LANGUAGE plpgsql
AS $$
begin
update station.station set (site_id,station_family,station_type,name,point,orig_srid,ground_altitude,dataset_id) = (new.site_id,new.station_family,new.station_type,new.name,new.point,new.orig_srid,new.ground_altitude,new.dataset_id) where id=old.id;
update station.station_device set (device_type) = (new.device_type) where id=old.id;
return new;
end;
$$;
ALTER FUNCTION station.device_update_ft() OWNER TO postgres;
--
-- Name: hydrology_station_delete_ft(); Type: FUNCTION; Schema: station; Owner: postgres
--
CREATE FUNCTION station.hydrology_station_delete_ft() RETURNS trigger
LANGUAGE plpgsql
AS $$
begin
delete from station.station_hydrology where id = old.id;
delete from station.station where id = old.id;
return old;
end;
$$;
ALTER FUNCTION station.hydrology_station_delete_ft() OWNER TO postgres;
--
-- Name: hydrology_station_insert_ft(); Type: FUNCTION; Schema: station; Owner: postgres
--
CREATE FUNCTION station.hydrology_station_insert_ft() RETURNS trigger
LANGUAGE plpgsql
AS $$
begin
insert into station.station select
nextval('station.station_id_seq'::regclass)
, new.site_id,new.station_family,new.station_type,new.name,new.point,new.orig_srid,new.ground_altitude,new.dataset_id
;
insert into station.station_hydrology select
currval('station.station_id_seq'::regclass)
, new.hydrology_station_type,new.a,new.b
;
return new;
end;
$$;
ALTER FUNCTION station.hydrology_station_insert_ft() OWNER TO postgres;
--
-- Name: hydrology_station_update_ft(); Type: FUNCTION; Schema: station; Owner: postgres
--
CREATE FUNCTION station.hydrology_station_update_ft() RETURNS trigger
LANGUAGE plpgsql
AS $$
begin
update station.station set (site_id,station_family,station_type,name,point,orig_srid,ground_altitude,dataset_id) = (new.site_id,new.station_family,new.station_type,new.name,new.point,new.orig_srid,new.ground_altitude,new.dataset_id) where id=old.id;
update station.station_hydrology set (hydrology_station_type,a,b) = (new.hydrology_station_type,new.a,new.b) where id=old.id;
return new;
end;
$$;
ALTER FUNCTION station.hydrology_station_update_ft() OWNER TO postgres;
--
-- Name: sample_delete_ft(); Type: FUNCTION; Schema: station; Owner: postgres
--
CREATE FUNCTION station.sample_delete_ft() RETURNS trigger
LANGUAGE plpgsql
AS $$
begin
delete from station.station_sample where id = old.id;
delete from station.station where id = old.id;
return old;
end;
$$;
ALTER FUNCTION station.sample_delete_ft() OWNER TO postgres;
--
-- Name: sample_insert_ft(); Type: FUNCTION; Schema: station; Owner: postgres
--
CREATE FUNCTION station.sample_insert_ft() RETURNS trigger
LANGUAGE plpgsql
AS $$
begin
insert into station.station select
nextval('station.station_id_seq'::regclass)
, new.site_id,new.station_family,new.station_type,new.name,new.point,new.orig_srid,new.ground_altitude,new.dataset_id
;
insert into station.station_sample select
currval('station.station_id_seq'::regclass)
, new.sample_family,new.sample_type
;
return new;
end;
$$;
ALTER FUNCTION station.sample_insert_ft() OWNER TO postgres;
--
-- Name: sample_update_ft(); Type: FUNCTION; Schema: station; Owner: postgres
--
CREATE FUNCTION station.sample_update_ft() RETURNS trigger
LANGUAGE plpgsql
AS $$
begin
update station.station set (site_id,station_family,station_type,name,point,orig_srid,ground_altitude,dataset_id) = (new.site_id,new.station_family,new.station_type,new.name,new.point,new.orig_srid,new.ground_altitude,new.dataset_id) where id=old.id;
update station.station_sample set (sample_family,sample_type) = (new.sample_family,new.sample_type) where id=old.id;
return new;
end;
$$;
ALTER FUNCTION station.sample_update_ft() OWNER TO postgres;
--
-- Name: weather_station_delete_ft(); Type: FUNCTION; Schema: station; Owner: postgres
--
CREATE FUNCTION station.weather_station_delete_ft() RETURNS trigger
LANGUAGE plpgsql
AS $$
begin
delete from station.station_weather_station where id = old.id;
delete from station.station where id = old.id;
return old;
end;
$$;
ALTER FUNCTION station.weather_station_delete_ft() OWNER TO postgres;
--
-- Name: weather_station_insert_ft(); Type: FUNCTION; Schema: station; Owner: postgres
--
CREATE FUNCTION station.weather_station_insert_ft() RETURNS trigger
LANGUAGE plpgsql
AS $$
begin
insert into station.station select
nextval('station.station_id_seq'::regclass)
, new.site_id,new.station_family,new.station_type,new.name,new.point,new.orig_srid,new.ground_altitude,new.dataset_id
;
insert into station.station_weather_station select
currval('station.station_id_seq'::regclass)
, new.weather_station_type,new.height
;
return new;
end;
$$;
ALTER FUNCTION station.weather_station_insert_ft() OWNER TO postgres;
--
-- Name: weather_station_update_ft(); Type: FUNCTION; Schema: station; Owner: postgres
--
CREATE FUNCTION station.weather_station_update_ft() RETURNS trigger
LANGUAGE plpgsql
AS $$
begin
update station.station set (site_id,station_family,station_type,name,point,orig_srid,ground_altitude,dataset_id) = (new.site_id,new.station_family,new.station_type,new.name,new.point,new.orig_srid,new.ground_altitude,new.dataset_id) where id=old.id;
update station.station_weather_station set (weather_station_type,height) = (new.weather_station_type,new.height) where id=old.id;
return new;
end;
$$;
ALTER FUNCTION station.weather_station_update_ft() OWNER TO postgres;
--
-- Name: create_inheritance_view(text, text, regclass, regclass); Type: FUNCTION; Schema: utils; Owner: postgres
--
CREATE FUNCTION utils.create_inheritance_view(view_schema text, view_table text, child_rel regclass, parent_rel regclass) RETURNS void
LANGUAGE plpgsql
AS $_$
declare
child_table text;
child_schema text;
parent_table text;
parent_schema text;
q text;
begin
-- table and schema name from regclass
select into child_table, child_schema
relname, nspname from pg_class c join pg_namespace ns on ns.oid = c.relnamespace where c.oid = child_rel;
select into parent_table, parent_schema
relname, nspname from pg_class c join pg_namespace ns on ns.oid = c.relnamespace where c.oid = parent_rel;
-- create the aggregate view
q := format($sql$ create view %s as
select
%s
, %s
from
%s c
left join %s p on p.id = c.id$sql$
, view_schema || '.' || view_table
, array_to_string(utils.get_column_list(parent_rel, 'p.'), ',')
, array_to_string(array_remove(utils.get_column_list(child_rel, 'c.'), 'c.id'), ',')
, child_rel::text
, parent_rel::text
);
execute q;
-- create the insert trigger function
q := format($sql$
create function %s_insert_ft() returns trigger
language plpgsql
as $f$
begin
insert into %s select
nextval('%s_id_seq'::regclass)
, %s
;
insert into %s select
currval('%s_id_seq'::regclass)
, %s
;
return new;
end;
$f$;
create trigger %s_insert_t instead of insert on %s
for each row execute procedure %s_insert_ft();
$sql$
, view_table
, parent_schema||'.'||parent_table
, parent_schema||'.'||parent_table
, array_to_string(array_remove(utils.get_column_list(parent_rel, 'new.'), 'new.id'), ',')
, child_schema||'.'||child_table
, parent_schema||'.'||parent_table
, array_to_string(array_remove(utils.get_column_list(child_rel, 'new.'), 'new.id'), ',')
, view_table
, view_schema || '.' || view_table
, view_table
);
execute q;
-- create the delete trigger function
q := format($sql$
create function %s_delete_ft() returns trigger
language plpgsql
as $f$
begin
delete from %s where id = old.id;
delete from %s where id = old.id;
return old;
end;
$f$;
create trigger %s_delete_t instead of delete on %s
for each row execute procedure %s_delete_ft();
$sql$
, view_table
, child_schema||'.'||child_table
, parent_schema||'.'||parent_table
, view_table
, view_schema||'.'||view_table
, view_table
);
execute q;
-- create the update trigger function
q := format($sql$
create function %s_update_ft() returns trigger
language plpgsql
as $f$
begin
update %s set (%s) = (%s) where id=old.id;
update %s set (%s) = (%s) where id=old.id;
return new;
end;
$f$;
create trigger %s_update_t instead of update on %s
for each row execute procedure %s_update_ft();
$sql$
, view_table
, parent_schema||'.'||parent_table
, array_to_string(array_remove(utils.get_column_list(parent_rel), 'id'), ',')
, array_to_string(array_remove(utils.get_column_list(parent_rel, 'new.'), 'new.id'), ',')
, child_schema||'.'||child_table
, array_to_string(array_remove(utils.get_column_list(child_rel), 'id'), ',')
, array_to_string(array_remove(utils.get_column_list(child_rel, 'new.'), 'new.id'), ',')
, view_table
, view_schema||'.'||view_table
, view_table
);
execute q;
end;
$_$;
ALTER FUNCTION utils.create_inheritance_view(view_schema text, view_table text, child_rel regclass, parent_rel regclass) OWNER TO postgres;
--
-- Name: get_column_list(regclass, text); Type: FUNCTION; Schema: utils; Owner: postgres
--
CREATE FUNCTION utils.get_column_list(l_relation regclass, l_col_prefix text DEFAULT ''::text) RETURNS text[]
LANGUAGE sql IMMUTABLE
AS $$
select array_agg(l_col_prefix||attname)
from pg_attribute
where attrelid = l_relation
and attnum > 0;
$$;
ALTER FUNCTION utils.get_column_list(l_relation regclass, l_col_prefix text) OWNER TO postgres;
--
-- Name: stations_from_dataset(integer); Type: FUNCTION; Schema: utils; Owner: postgres
--
CREATE FUNCTION utils.stations_from_dataset(pdataset_id integer) RETURNS TABLE(station_id bigint)
LANGUAGE plpgsql
AS $$
declare
r record;
q text;
begin
q := format('select distinct station_id from measure.stratigraphic_logvalue where dataset_id = %s', pdataset_id);
q := q || format(' union select distinct station_id from measure.chemical_analysis_result where dataset_id = %s', pdataset_id);
for r in select measure_table::text from measure.measure_metadata
loop
q := q || format(' union select distinct station_id from %s where dataset_id=%s', r.measure_table, pdataset_id);
end loop;
return query execute q;
end;
$$;
ALTER FUNCTION utils.stations_from_dataset(pdataset_id integer) OWNER TO postgres;
SET default_tablespace = '';
SET default_with_oids = false;
--
-- Name: acoustic_imagery; Type: TABLE; Schema: measure; Owner: postgres
--
CREATE TABLE measure.acoustic_imagery (
id integer NOT NULL,
station_id bigint NOT NULL,
scan_date date NOT NULL,
depth_range numrange NOT NULL,
image_data bytea,
image_format text,
dataset_id integer
);
ALTER TABLE measure.acoustic_imagery OWNER TO postgres;
--
-- Name: acoustic_imagery_id_seq; Type: SEQUENCE; Schema: measure; Owner: postgres
--
CREATE SEQUENCE measure.acoustic_imagery_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE measure.acoustic_imagery_id_seq OWNER TO postgres;
--
-- Name: acoustic_imagery_id_seq; Type: SEQUENCE OWNED BY; Schema: measure; Owner: postgres
--
ALTER SEQUENCE measure.acoustic_imagery_id_seq OWNED BY measure.acoustic_imagery.id;
--
-- Name: atmospheric_pressure; Type: TABLE; Schema: measure; Owner: postgres
--
CREATE TABLE measure.atmospheric_pressure (
id integer NOT NULL,
station_id bigint NOT NULL,
start_measure_time timestamp without time zone NOT NULL,
end_measure_time timestamp without time zone NOT NULL,
measure_value double precision,
periodicity text,
reference text,
campaign_id bigint DEFAULT 1,
dataset_id integer
);
ALTER TABLE measure.atmospheric_pressure OWNER TO postgres;
--
-- Name: atmospheric_pressure_id_seq; Type: SEQUENCE; Schema: measure; Owner: postgres
--
CREATE SEQUENCE measure.atmospheric_pressure_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE