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
|
-- ==============================================================
-- GNUmed database schema change script
--
-- License: GPL v2 or later
-- Author: karsten.hilbert@gmx.net
--
-- ==============================================================
\set ON_ERROR_STOP 1
--set default_transaction_read_only to off;
set check_function_bodies to on;
-- --------------------------------------------------------------
delete from audit.audited_tables where schema = 'dem' and table_name = 'state';
select audit.add_table_for_audit('dem', 'region');
drop function if exists audit.ft_ins_state() cascade;
drop function if exists audit.ft_upd_state() cascade;
drop function if exists audit.ft_del_state() cascade;
COMMENT on table dem.region is
'region codes (country specific);
Richard agreed we should require pre-existence,
allow user to mail details for adding a state to developers';
COMMENT on column dem.region.code is
'region code';
-- --------------------------------------------------------------
alter table dem.urb drop constraint if exists fk_dem_urb_dem_region_pk cascade;
-- remnants of old
alter table dem.urb drop constraint if exists "$1" cascade ;
alter table dem.urb
add constraint fk_dem_urb_dem_region_pk
foreign key (fk_region)
references dem.region(pk)
on update cascade
on delete restrict
;
alter table dem.urb
drop constraint if exists urb_id_state_fkey cascade
;
COMMENT on column dem.urb.fk_region IS
'reference to information about country and region';
-- --------------------------------------------------------------
COMMENT on column dem.street.id_urb IS
'reference to information postcode, city, country and region';
-- --------------------------------------------------------------
drop function if exists dem.gm_upd_default_states() cascade;
create or replace function dem.gm_upd_default_regions()
returns boolean
language 'plpgsql'
as '
declare
_region_code text;
_region_name text;
_country_row record;
begin
_region_code := ''??'';
_region_name := ''state/territory/province/region not available'';
-- add default region to countries needing one
for _country_row in
select distinct code from dem.country
where code not in (
select country from dem.region where code = _region_code
)
loop
raise notice ''adding default region for [%]'', _country_row.code;
execute ''insert into dem.region (code, country, name) values (''
|| quote_literal(_region_code) || '', ''
|| quote_literal(_country_row.code) || '', ''
|| quote_literal(_region_name) || '');'';
end loop;
return true;
end;
';
select dem.gm_upd_default_regions();
-- --------------------------------------------------------------
DROP function if exists dem.create_urb(text, text, text, text);
CREATE function dem.create_urb(text, text, text, text)
RETURNS integer
AS '
DECLARE
_urb ALIAS FOR $1;
_urb_postcode ALIAS FOR $2;
_region_code ALIAS FOR $3;
_country_code ALIAS FOR $4;
_region_pk integer;
_urb_id integer;
msg text;
BEGIN
-- get region
SELECT INTO _region_pk d_r.pk from dem.region d_r WHERE d_r.code = _region_code and d_r.country = _country_code;
IF NOT FOUND THEN
msg := ''combination of region + country not registered [''
|| ''country:'' || coalesce(_country_code, ''NULL'')
|| '', region:'' || coalesce(_region_code, ''NULL'')
|| '', urb:'' || coalesce(_urb, ''NULL'')
|| '', urb_zip:'' || coalesce(_urb_postcode, ''NULL'')
|| '']'';
RAISE EXCEPTION ''=> %'', msg;
END IF;
-- get/create and return urb
SELECT INTO _urb_id u.id from dem.urb u WHERE u.name ILIKE _urb AND u.fk_region = _region_pk;
IF FOUND THEN
RETURN _urb_id;
END IF;
INSERT INTO dem.urb (name, postcode, fk_region) VALUES (_urb, _urb_postcode, _region_pk);
RETURN currval(''dem.urb_id_seq'');
END;' LANGUAGE 'plpgsql';
COMMENT ON function dem.create_urb(text, text, text, text) IS
'This function takes a parameters the name of the urb,\n
the postcode of the urb, the name of the region and the\n
name of the country.\n
If the country or the region does not exists in the tables,\n
the function fails.\n
At first, the urb is tried to be retrieved according to the\n
supplied information. If the fields do not match exactly an\n
existing row, a new urb is created and returned.';
-- --------------------------------------------------------------
drop function if exists dem.address_exists(text, text, text, text, text, text, text) cascade;
create or replace function dem.address_exists(text, text, text, text, text, text, text)
returns integer
language 'plpgsql'
as '
DECLARE
_code_country alias for $1;
_code_region alias for $2;
_urb alias for $3;
_postcode alias for $4;
_street alias for $5;
_number alias for $6;
_subunit alias for $7;
__subunit text;
_pk_address integer;
msg text;
BEGIN
if (_code_country || _code_region || _urb || _postcode || _street || _number) is NULL then
msg := ''[dem.address_exists]: insufficient or invalid address definition: ''
|| ''country code <'' || coalesce(_code_country, ''NULL'') || ''>, ''
|| ''region code <'' || coalesce(_code_region, ''NULL'') || ''>, ''
|| ''urb <'' || coalesce(_urb, ''NULL'') || ''>, ''
|| ''zip <'' || coalesce(_postcode, ''NULL'') || ''>, ''
|| ''street <'' || coalesce(_street, ''NULL'') || ''>, ''
|| ''number <'' || coalesce(_number, ''NULL'') || ''>''
;
raise exception ''%'', msg;
end if;
__subunit := nullif(trim(_subunit), '''');
if __subunit is null then
select
pk_address into _pk_address
from
dem.v_address
where
code_country = trim(_code_country)
and
code_region = trim(_code_region)
and
urb = trim(_urb)
and
postcode = trim(_postcode)
and
street = trim(_street)
and
number = trim(_number)
and
subunit is null;
else
select
pk_address into _pk_address
from
dem.v_address
where
code_country = trim(_code_country)
and
code_region = trim(_code_region)
and
urb = trim(_urb)
and
postcode = trim(_postcode)
and
street = trim(_street)
and
number = trim(_number)
and
subunit = __subunit;
end if;
return _pk_address;
END;';
comment on function dem.address_exists(text, text, text, text, text, text, text) is
E'This function checks whether a given address exists in
the database and returns the primary key if found.
It takes the following parameters:
country code,
region code,
urb (location),
postcode,
street,
number,
subunit (can be NULL)
';
--------------------------------------------------------------
drop function if exists dem.create_address(text, text, text, text, text, text, text);
create or replace function dem.create_address(text, text, text, text, text, text, text)
returns integer
LANGUAGE 'plpgsql'
AS '
DECLARE
_number ALIAS FOR $1;
_street ALIAS FOR $2;
_postcode ALIAS FOR $3;
_urb ALIAS FOR $4;
_region_code ALIAS FOR $5;
_country_code ALIAS FOR $6;
_subunit alias for $7;
_street_id integer;
_pk_address integer;
__subunit text;
msg text;
BEGIN
select into _pk_address dem.address_exists (
_country_code,
_region_code,
_urb,
_postcode,
_street,
_number,
_subunit
);
if _pk_address is not null then
return _pk_address;
end if;
-- this either creates dem.street and possible dem.urb rows or
-- or else it fails (because region and/or country do not exist)
select into _street_id dem.create_street(_street, _postcode, _urb, _region_code, _country_code);
-- create address
__subunit := nullif(trim(_subunit), '''');
insert into dem.address (
number,
id_street,
subunit
) values (
_number,
_street_id,
__subunit
)
returning id
into _pk_address;
return _pk_address;
END;';
comment on function dem.create_address(text, text, text, text, text, text, text) is
E'This function creates an address. It first
checks whether the address already exists.
It takes the following parameters:
number,
street,
postcode,
urb (location),
region code,
country code,
subunit (can be NULL)
If the country or the region do not exist
in the database, the function fails.
';
-- --------------------------------------------------------------
drop view if exists dem.v_state cascade;
drop view if exists dem.v_region cascade;
create view dem.v_region as
select
d_r.pk as pk_region,
d_r.code as code_region,
d_r.name as region,
_(d_r.name) as l10n_region,
d_r.country as code_country,
c.name as country,
_(c.name) as l10n_country,
c.deprecated as country_deprecated,
d_r.xmin as xmin_region
from
dem.region as d_r
left join dem.country c on (d_r.country = c.code)
;
comment on view dem.v_region is 'denormalizes region information';
grant select on dem.v_region to group "gm-public";
-- --------------------------------------------------------------
drop view if exists dem.v_urb cascade;
create view dem.v_urb as
select
d_u.id as pk_urb,
d_u.name as urb,
d_u.postcode as postcode_urb,
d_u.lat_lon as lat_lon_urb,
d_vr.code_region,
d_vr.region,
d_vr.l10n_region,
d_vr.code_country,
d_vr.country,
d_vr.l10n_country,
d_vr.country_deprecated,
d_u.fk_region as pk_region,
d_u.xmin as xmin_urb
from
dem.urb d_u
left join dem.v_region as d_vr on (d_vr.pk_region = d_u.fk_region)
;
comment on view dem.v_urb is 'denormalizes urb data';
grant select on dem.v_urb to group "gm-public";
-- --------------------------------------------------------------
drop view if exists dem.v_street cascade;
create view dem.v_street as
select
d_st.id as pk_street,
d_st.name as street,
coalesce(d_st.postcode, d_vu.postcode_urb) as postcode,
d_st.postcode as postcode_street,
d_st.lat_lon as lat_lon_street,
d_st.suburb as suburb,
d_vu.urb,
d_vu.postcode_urb,
d_vu.lat_lon_urb,
d_vu.code_region,
d_vu.region,
d_vu.l10n_region,
d_vu.code_country,
d_vu.country,
d_vu.l10n_country,
d_vu.country_deprecated,
d_st.id_urb as pk_urb,
d_vu.pk_region,
d_st.xmin as xmin_street
from
dem.street d_st
left join dem.v_urb d_vu on (d_st.id_urb = d_vu.pk_urb)
;
comment on view dem.v_street is 'denormalizes street data';
grant select on dem.v_street to group "gm-public";
-- ------------------------------------------------------------
drop view if exists dem.v_address cascade;
create view dem.v_address as
select
d_adr.id
as pk_address,
d_str.name
as street,
coalesce(d_str.postcode, d_u.postcode)
as postcode,
d_adr.aux_street
as notes_street,
d_adr.number,
d_adr.subunit,
d_adr.addendum
as notes_subunit,
d_adr.lat_lon
as lat_lon_address,
d_str.postcode
as postcode_street,
d_str.lat_lon
as lat_lon_street,
d_str.suburb,
d_u.name
as urb,
d_u.postcode
as postcode_urb,
d_u.lat_lon
as lat_lon_urb,
d_r.code
as code_region,
d_r.name
as region,
_(d_r.name)
as l10n_region,
d_r.country
as code_country,
d_c.name
as country,
_(d_c.name)
as l10n_country,
d_c.deprecated
as country_deprecated,
d_adr.id_street
as pk_street,
d_u.id
as pk_urb,
d_r.pk
as pk_region,
d_adr.xmin
as xmin_address
from
dem.address d_adr
left join dem.street d_str on (d_adr.id_street = d_str.id)
left join dem.urb d_u on (d_str.id_urb = d_u.id)
left join dem.region d_r on (d_u.fk_region = d_r.pk)
left join dem.country d_c on (d_c.code = d_r.country)
;
comment on view dem.v_address is 'fully denormalizes data about addresses as entities in themselves';
grant select on dem.v_address to group "gm-public";
-- --------------------------------------------------------------
drop view if exists dem.v_zip2street cascade;
create view dem.v_zip2street as
select
coalesce (d_str.postcode, d_u.postcode) as postcode,
d_str.name as street,
d_str.suburb as suburb,
d_r.name as region,
d_r.code as code_region,
d_u.name as urb,
d_c.name as country,
_(d_c.name) as l10n_country,
d_r.country as code_country
from
dem.street d_str,
dem.urb d_u,
dem.region d_r,
dem.country d_c
where
d_str.postcode is not null
and
d_str.id_urb = d_u.id
and
d_u.fk_region = d_r.pk
and
d_r.country = d_c.code
;
comment on view dem.v_zip2street is
'list known data for streets that have a zip code';
grant select on dem.v_zip2street to group "gm-public";
-- --------------------------------------------------------------
drop view if exists dem.v_uniq_zipped_urbs cascade;
create view dem.v_uniq_zipped_urbs as
-- all the cities that
select
d_u.postcode as postcode,
d_u.name as name,
d_r.name as region,
d_r.code as code_region,
d_c.name as country,
_(d_c.name) as l10n_country,
d_r.country as code_country
from
dem.urb d_u,
dem.region d_r,
dem.country d_c
where
-- have a zip code
d_u.postcode is not null
and
-- are not found in "street" with this zip code
not exists (
select 1 from
dem.v_zip2street d_vz2str
where
d_vz2str.postcode = d_u.postcode
and
d_vz2str.urb = d_u.name
) and
d_u.fk_region = d_r.pk
and
d_r.country = d_c.code
;
comment on view dem.v_uniq_zipped_urbs is
'convenience view that selects urbs which:
- have a zip code
- are not referenced in table "street" with that zip code';
grant select on dem.v_uniq_zipped_urbs to group "gm-public";
-- --------------------------------------------------------------
drop view if exists dem.v_zip2data;
create view dem.v_zip2data as
select
d_vz2s.postcode as zip,
d_vz2s.street,
d_vz2s.suburb,
d_vz2s.urb,
d_vz2s.region,
d_vz2s.code_region,
d_vz2s.country,
d_vz2s.l10n_country,
d_vz2s.code_country
from dem.v_zip2street d_vz2s
union
select
d_vuzu.postcode as zip,
null as street,
null as suburb,
d_vuzu.name as urb,
d_vuzu.region,
d_vuzu.code_region,
d_vuzu.country,
d_vuzu.l10n_country,
d_vuzu.code_country
from
dem.v_uniq_zipped_urbs d_vuzu
;
comment on view dem.v_zip2data is
'aggregates nearly all known data per zip code';
grant select on dem.v_zip2data to group "gm-public";
-- --------------------------------------------------------------
drop view if exists dem.v_zip2urb cascade;
create view dem.v_zip2urb as
select
d_u.postcode as postcode,
d_u.name as urb,
d_r.name as region,
d_r.code as code_region,
_(d_c.name) as country,
d_r.country as code_country
from
dem.urb d_u,
dem.region d_r,
dem.country d_c
where
d_u.postcode is not null
and
d_u.fk_region = d_r.pk
and
d_r.country = d_c.code
;
comment on view dem.v_zip2urb is
'list known data for urbs that have a zip code';
grant select on dem.v_zip2urb to group "gm-public";
-- --------------------------------------------------------------
drop view if exists dem.v_basic_address cascade;
create view dem.v_basic_address as
select
d_adr.id as id,
d_r.country as country_code,
d_r.code as region_code,
d_r.name as region,
d_c.name as country,
coalesce (d_str.postcode, d_u.postcode) as postcode,
d_u.name as urb,
d_adr.number as number,
d_str.name as street,
d_adr.addendum as addendum,
coalesce (d_adr.lat_lon, d_str.lat_lon, d_u.lat_lon) as lat_lon
from
dem.address d_adr,
dem.region d_r,
dem.country d_c,
dem.urb d_u,
dem.street d_str
where
d_r.country = d_c.code
and
d_adr.id_street = d_str.id
and
d_str.id_urb = d_u.id
and
d_u.fk_region = d_r.pk;
grant select on dem.v_basic_address to group "gm-public";
-- --------------------------------------------------------------
select gm.log_script_insertion('v21-dem-region-dynamic.sql', '21.3');
|