From 185ebc28dcddfffc72351f0aa30c4536a9f02032 Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Tue, 7 Apr 2020 22:42:24 -0700 Subject: [PATCH 01/14] Add test case for read building. --- Building/ddl/building_functions.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Building/ddl/building_functions.sql b/Building/ddl/building_functions.sql index cc04947..4c428c0 100644 --- a/Building/ddl/building_functions.sql +++ b/Building/ddl/building_functions.sql @@ -136,7 +136,7 @@ select * from read_building(); select * from read_building(in_building_id := 591383); -- blocpower_id only -select * from read_building(in_place_name := '105 CENTRAL AVENUE'); --using named param TODO: add valid place_name test +select * from read_building(in_place_name := '838 Park Place, Brooklyn, New York 11216, United States'); --using named param select * from read_building() limit 100 --selcting all buildings; -- GitLab From c677c8821cab1df41fb22d00c7b9617199ae4a90 Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Wed, 8 Apr 2020 10:15:57 -0700 Subject: [PATCH 02/14] Add commments to vw_building_address materialized view. --- Building/ddl/vw_building_address.sql | 35 ++++++++++++++-------------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/Building/ddl/vw_building_address.sql b/Building/ddl/vw_building_address.sql index 5b0a087..753c1a6 100644 --- a/Building/ddl/vw_building_address.sql +++ b/Building/ddl/vw_building_address.sql @@ -16,31 +16,30 @@ create materialized view vw_building_address as min(building_address.house_number_sequence) AS min_sequence, max(building_address.house_number_sequence) AS max_sequence FROM building_address - GROUP BY building_address.building_id, building_address.pad_range_id) lh_1 ON (( - (b.id = lh_1.building_id) AND (ba.pad_range_id = lh_1.pad_range_id)))) + GROUP BY building_address.building_id, building_address.pad_range_id) lh_1 ON (((b.id = lh_1.building_id) AND (ba.pad_range_id = lh_1.pad_range_id)))) ) - SELECT DISTINCT am.building_id, - am.lot_id, - am.bbl, - am.bin, - am.targeting_score, - am.place_name, + SELECT DISTINCT am.building_id, -- building id from address mapping + am.lot_id, -- lot_if from address mapping + am.bbl, -- bbl from address mapping + am.bin, -- bin from address mapping + am.targeting_score, -- targeting score from address mapping + am.place_name, -- place name from address mapping (( CASE WHEN (lh.house_number_sequence = hh.house_number_sequence) THEN (lh.house_number) :: text ELSE (((lh.house_number) :: text || ' -- ' :: text) || (hh.house_number) :: text) END || ' ' :: text) || (lh.street_name) :: text) AS street_address, -- Generate Address range from sequence - lh.borough_id, - lh.zipcode, - lh.state, - am.pad_range_id, - lh.id AS min_address_id, - hh.id AS max_address_id, - la.address_id AS lot_address_id + lh.borough_id, -- borough id from low house address + lh.zipcode, -- zipcode from low house address + lh.state, -- state from low house address + am.pad_range_id, -- pad_range_id from address mapping + lh.id AS min_address_id, -- min address id from low house address + hh.id AS max_address_id, -- max address id + la.address_id AS lot_address_id -- lot address id FROM (((address_mapping am - JOIN address lh ON (((am.pad_range_id = lh.pad_range_id) AND (am.min_sequence = lh.house_number_sequence)))) - JOIN address hh ON (((am.pad_range_id = hh.pad_range_id) AND (am.max_sequence = hh.house_number_sequence)))) - LEFT JOIN lot_address la ON ((la.lot_id = am.lot_id))) + JOIN address lh ON (((am.pad_range_id = lh.pad_range_id) AND (am.min_sequence = lh.house_number_sequence)))) -- low house address + JOIN address hh ON (((am.pad_range_id = hh.pad_range_id) AND (am.max_sequence = hh.house_number_sequence)))) -- high house address + LEFT JOIN lot_address la ON ((la.lot_id = am.lot_id))) -- join lot_address with address mapping ORDER BY am.lot_id, am.building_id; -- CREATE UNIQUE INDEX sales_summary_seller -- Add index for concurrent refresh view -- GitLab From 3dda5e509a253c07ffd8ab516f66f1ce929007f2 Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Wed, 8 Apr 2020 10:17:46 -0700 Subject: [PATCH 03/14] Start create building building function. --- Building/ddl/building_functions.sql | 89 ++++++++++------------------- 1 file changed, 30 insertions(+), 59 deletions(-) diff --git a/Building/ddl/building_functions.sql b/Building/ddl/building_functions.sql index 4c428c0..82f1f42 100644 --- a/Building/ddl/building_functions.sql +++ b/Building/ddl/building_functions.sql @@ -1,62 +1,33 @@ -- TODO: create_building() --- create function create_building( --- in_place_name character varying DEFAULT NULL::character varying --- returns TABLE( --- building_id integer, --- lot_id integer, --- bbl bigint, --- bin bigint, --- street_address text, --- borough character varying, --- zipcode numeric, --- state character, --- targeting_score double precision, --- place_name VARCHAR) --- language plpgsql --- as $$ --- DECLARE --- in_place_name ALIAS for $1; --- BEGIN --- if (in_place_name is null)then --- return QUERY -- Building ID or BBL Look up --- select v.building_id, --- v.lot_id, --- v.bbl, --- v.bin, --- v.street_address, --- b.description as borough, --- v.zipcode, --- v.state, --- v.targeting_score --- v.place_name --- from vw_building_address v -- from vw_building_address join on Borough to Borough --- join borough b --- on b.id = v.borough_id --- where v.building_id = in_building_id --- ; --- ELSIF (in_building_id is null and (in_place_name is not null))THEN --- Return QUERY -- Address Search Lookup --- select v.building_id, --- v.lot_id, --- v.bbl, --- v.bin, --- v.street_address, --- b.description as borough, --- v.zipcode, --- v.state, --- v.targeting_score, --- v.place_name, --- from vw_building_address v --- join borough b --- on b.id = v.borough_id --- where v.place_name = in_place_name-- use get address with in_address, in_zip, and in_boro --- ; --- END IF; --- END; --- $$; +create function create_building( + in_place_name character varying DEFAULT NULL::character varying) + returns TABLE( + building_id integer, -- generate it? + lot_id integer, -- generate it? + bbl bigint, -- generate it? + bin bigint, -- generate it? + street_address text, -- generate it? + borough character varying, -- generate it? + zipcode numeric, -- generate it? + state character, -- gerenate it? + targeting_score double precision, -- generate it? + place_name VARCHAR) -- insert place_name +language plpgsql +as $$ +DECLARE + in_place_name ALIAS for $1; + BEGIN + if (in_place_name is not null)then + -- insert address + -- insert building + -- insert building_address + return QUERY + END IF; + END; +$$; --- alter function read_building(integer, varchar) --- owner to blocpower; +alter function create_building(varchar) + owner to blocpower; /* TEST CASES: create building @@ -64,7 +35,7 @@ TEST CASES: create building -- building with place name doesn't exist select * from create_building(in_place_name := '936 East 17th Street, New York, New York 10003, United States'); -- building with place name exists -select * from create_building(in_place_name := '936 East 17th Street, New York, New York 10003, United States'); +select * from create_building(in_place_name := '838 Park Place, Brooklyn, New York 11216, United States'); */ -- read_building() create function read_building( @@ -88,7 +59,7 @@ DECLARE in_place_name ALIAS for $2; BEGIN if ( (in_building_id is not null) and in_place_name is null)then - return QUERY -- Building ID or BBL Look up + return QUERY -- Building ID Look up select v.building_id, v.lot_id, v.bbl, -- GitLab From 5e392e587ac66558834023bf0c35858a6cb8adf7 Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Fri, 10 Apr 2020 12:44:10 -0700 Subject: [PATCH 04/14] Add address ddl. --- Building/ddl/address.sql | 50 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Building/ddl/address.sql diff --git a/Building/ddl/address.sql b/Building/ddl/address.sql new file mode 100644 index 0000000..ef7ceed --- /dev/null +++ b/Building/ddl/address.sql @@ -0,0 +1,50 @@ +create table address +( + borough_id smallint + constraint borough__address_fk + references borough, + house_number varchar(20), + house_number_sequence integer, + id serial not null + constraint address_pkey1 + primary key, + load_date date default now(), + pad_range_id integer, + street_name varchar(50), + state char(2), + zipcode numeric(5), + street_address tsvector +) +; + +alter table address owner to blocpower +; + +create index "address__full_address_IDX" + on address (btrim((house_number::text || ' '::text) || street_name::text)) +; + +create index "address__borough_id_IDX" + on address (borough_id) +; + +create index "address__zipcode_IDX" + on address (zipcode) +; + +create index "address__range_seq_IDX" + on address (pad_range_id, house_number_sequence) +; + +create index address__street_address_idx + on address (street_address) +; + +create index address__zip_idx + on address (zipcode) +; + +create index address__bourough_id_idx + on address (borough_id) +; + -- GitLab From 34553d32a411daf222e4fd2e9e1fd649e5575d99 Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Fri, 10 Apr 2020 12:44:45 -0700 Subject: [PATCH 05/14] Add lot ddl. --- Building/ddl/lot.sql | 73 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 Building/ddl/lot.sql diff --git a/Building/ddl/lot.sql b/Building/ddl/lot.sql new file mode 100644 index 0000000..772f150 --- /dev/null +++ b/Building/ddl/lot.sql @@ -0,0 +1,73 @@ +create table lot +( + administrative_deivision_id integer + constraint administrative_division_lot_fk + references administrative_division + deferrable initially deferred, + appointment_bbl bigint, + appointment_date timestamp, + areasource smallint, + bbl bigint, + block smallint, + boro_code char(2), + boro_id smallint, + city_service_id integer + constraint city_service_lot_fk + references city_service + deferrable initially deferred, + commerical_overlay_code_1 varchar(9), + commerical_overlay_code_2 varchar(10), + dof_tax_map_number integer, + e_designation_number varchar(11), + extenstion_code varchar(3), + finance_id integer + constraint finance_lot_fk + references finance + deferrable initially deferred, + historical_district varchar(40), + id integer default nextval('lot_id_seq'::regclass) not null + constraint lot_pkey + primary key, + irregular_shape_flag boolean, + landmark_name varchar(35), + land_use_category_code smallint, + limited_height_district_code varchar(5), + load_date timestamp, + lot_dimentions_id integer + constraint lot_dimentions_lot_fk + references lot_dimentions + deferrable initially deferred, + lot_number smallint, + lot_owner_id integer + constraint lot_owner_lot_fk + references lot_owner + deferrable initially deferred, + number_of_buildings smallint, + number_of_easements smallint, + pluto_id integer + constraint pluto_lot_fk + references pluto + deferrable initially deferred, + split_zone_flag boolean, + type smallint, + zone_map_border_flag varchar(5), + hdc_financed boolean, + housing_code_violations_id integer + constraint housing_code_violations_lot_fk + references housing_code_violations, + owner_occupied boolean, + rent_stabilized boolean +) +; + +alter table lot owner to blocpower +; + +create unique index "lot__bbl_IDX" + on lot (bbl) +; + +create index lot__bbl_idx + on lot (bbl) +; + -- GitLab From fe3e9130e8dda833809a89c633f8156b9109287b Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Fri, 10 Apr 2020 12:45:09 -0700 Subject: [PATCH 06/14] Add building ddl. --- Building/ddl/building.sql | 43 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 Building/ddl/building.sql diff --git a/Building/ddl/building.sql b/Building/ddl/building.sql new file mode 100644 index 0000000..348e6af --- /dev/null +++ b/Building/ddl/building.sql @@ -0,0 +1,43 @@ +create table building +( + basement_description_code smallint, + bbl bigint, + bin bigint, + building_dimentions_id integer + constraint building_dimentions_building_fk + references building_dimentions + deferrable initially deferred, + class char(2), + condo_number smallint, + count_all_units smallint, + count_residential_units smallint, + far numeric(6,2), + id integer default nextval('building_id_seq'::regclass) not null + constraint building_pk + primary key, + load_date timestamp, + lot_id integer + constraint building_lot_fk + references lot + deferrable initially deferred, + number_of_floors numeric(5,2), + proximity_code smallint, + year_altered smallint, + year_altered_previous smallint, + year_built smallint, + year_built_estimate varchar(5), + construction_type_id smallint + constraint tax_construction_type_building_fk + references construction_type, + targeting_score double precision, + place_name varchar(255) +) +; + +alter table building owner to blocpower +; + +create unique index "building_bbl_bin_unique_IDX" + on building (bbl, bin) +; + -- GitLab From 408c4a15817fbd04fee8702a8f8f4308b915ad3e Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Fri, 10 Apr 2020 12:45:43 -0700 Subject: [PATCH 07/14] Add lot address ddl. --- Building/ddl/lot_address.sql | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Building/ddl/lot_address.sql diff --git a/Building/ddl/lot_address.sql b/Building/ddl/lot_address.sql new file mode 100644 index 0000000..b3860fc --- /dev/null +++ b/Building/ddl/lot_address.sql @@ -0,0 +1,16 @@ +create table lot_address +( + lot_id integer not null + constraint lot__lot_address_fk + references lot, + address_id integer not null + constraint address__lot_address_fk + references address, + constraint lot_address_pkey + primary key (lot_id, address_id) +) +; + +alter table lot_address owner to blocpower +; + -- GitLab From 7f1b27c08a0d9ce59f9d5c3c597094936e0a966c Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Fri, 10 Apr 2020 12:46:18 -0700 Subject: [PATCH 08/14] Add building_address ddl. --- Building/ddl/building_address.sql | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Building/ddl/building_address.sql diff --git a/Building/ddl/building_address.sql b/Building/ddl/building_address.sql new file mode 100644 index 0000000..5c4baed --- /dev/null +++ b/Building/ddl/building_address.sql @@ -0,0 +1,20 @@ +create table building_address +( + building_id integer not null + constraint building__building_address_fk + references building, + address_id integer not null + constraint address__building_address_fk + references address + on delete set null, + pad_range_id integer, + house_number_sequence integer, + place_name varchar(255), + constraint building_address_pkey + primary key (building_id, address_id) +) +; + +alter table building_address owner to blocpower +; + -- GitLab From 1f63dbdd6bbaf303dc05f1367210da43c862b03b Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Mon, 13 Apr 2020 22:32:37 -0700 Subject: [PATCH 09/14] Add comments to make view statements clearer. --- Building/ddl/vw_building_address.sql | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/Building/ddl/vw_building_address.sql b/Building/ddl/vw_building_address.sql index 753c1a6..6dd8b6f 100644 --- a/Building/ddl/vw_building_address.sql +++ b/Building/ddl/vw_building_address.sql @@ -9,17 +9,21 @@ create materialized view vw_building_address as ba.pad_range_id, lh_1.min_sequence, lh_1.max_sequence - FROM ((building b - JOIN building_address ba ON ((b.id = ba.building_id))) - JOIN (SELECT building_address.building_id, - building_address.pad_range_id, - min(building_address.house_number_sequence) AS min_sequence, - max(building_address.house_number_sequence) AS max_sequence - FROM building_address - GROUP BY building_address.building_id, building_address.pad_range_id) lh_1 ON (((b.id = lh_1.building_id) AND (ba.pad_range_id = lh_1.pad_range_id)))) + FROM ( + (building b JOIN building_address ba ON ((b.id = ba.building_id))) -- From building b joined to building address ba on building id = building address building id + JOIN (SELECT building_address.building_id, -- Join to select from building address grouped by + building_address.pad_range_id, + min(building_address.house_number_sequence) AS min_sequence, + max(building_address.house_number_sequence) AS max_sequence + FROM building_address + GROUP BY building_address.building_id, building_address.pad_range_id) lh_1 + ON ( + ((b.id = lh_1.building_id) AND (ba.pad_range_id = lh_1.pad_range_id)) + ) + ) ) SELECT DISTINCT am.building_id, -- building id from address mapping - am.lot_id, -- lot_if from address mapping + am.lot_id, -- lot_id from address mapping am.bbl, -- bbl from address mapping am.bin, -- bin from address mapping am.targeting_score, -- targeting score from address mapping @@ -37,7 +41,7 @@ create materialized view vw_building_address as hh.id AS max_address_id, -- max address id la.address_id AS lot_address_id -- lot address id FROM (((address_mapping am - JOIN address lh ON (((am.pad_range_id = lh.pad_range_id) AND (am.min_sequence = lh.house_number_sequence)))) -- low house address + JOIN address lh ON (((am.pad_range_id = lh.pad_range_id) AND (am.min_sequence = lh.house_number_sequence)))) -- low house address on am JOIN address hh ON (((am.pad_range_id = hh.pad_range_id) AND (am.max_sequence = hh.house_number_sequence)))) -- high house address LEFT JOIN lot_address la ON ((la.lot_id = am.lot_id))) -- join lot_address with address mapping ORDER BY am.lot_id, am.building_id; -- GitLab From 28a97e8699678ccb9936a793c58b156f8cab0080 Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Mon, 13 Apr 2020 22:33:20 -0700 Subject: [PATCH 10/14] Add create_building function still need to work through an issue in the create function --- Building/ddl/building_functions.sql | 55 ++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/Building/ddl/building_functions.sql b/Building/ddl/building_functions.sql index 82f1f42..ff5b5b8 100644 --- a/Building/ddl/building_functions.sql +++ b/Building/ddl/building_functions.sql @@ -1,8 +1,8 @@ --- TODO: create_building() +-- create_building() create function create_building( in_place_name character varying DEFAULT NULL::character varying) returns TABLE( - building_id integer, -- generate it? + building_id integer, -- insert building lot_id integer, -- generate it? bbl bigint, -- generate it? bin bigint, -- generate it? @@ -16,12 +16,52 @@ language plpgsql as $$ DECLARE in_place_name ALIAS for $1; + new_address_id integer; + new_lot_id integer; + new_building_id integer; BEGIN - if (in_place_name is not null)then - -- insert address - -- insert building - -- insert building_address + IF (in_place_name is not null)then + -- insert new address + insert into address(borough_id, house_number, house_number_sequence) + values (1,'0000',1) + returning id into new_address_id; + + -- insert new lot + insert into lot (boro_id,number_of_buildings) + values (1,1) + returning id into new_lot_id; + + -- insert new lot address + insert into lot_address + values (new_lot_id,new_address_id); + + -- insert new building + insert into building(bbl,bin,load_date) + values (300000005,1111111,now()) + returning id into new_building_id; + + -- insert new building address + insert into building_address(building_id, address_id, place_name) + values (new_building_id, new_address_id, in_place_name); + + -- refresh view + REFRESH MATERIALIZED VIEW concurrently vw_building_address; + return QUERY + select v.building_id, + v.lot_id, + v.bbl, + v.bin, + v.street_address, + b.description as borough, + v.zipcode, + v.state, + v.targeting_score, + v.place_name + from vw_building_address v -- from vw_building_address join on Borough to Borough + join borough b + on b.id = v.borough_id + where v.place_name = in_place_name; END IF; END; $$; @@ -37,6 +77,7 @@ select * from create_building(in_place_name := '936 East 17th Street, New York, -- building with place name exists select * from create_building(in_place_name := '838 Park Place, Brooklyn, New York 11216, United States'); */ + -- read_building() create function read_building( in_building_id integer DEFAULT NULL::integer, @@ -112,5 +153,3 @@ select * from read_building(in_place_name := '838 Park Place, Brooklyn, New York select * from read_building() limit 100 --selcting all buildings; */ - --- TODO: update_building() -- GitLab From bf1915ae389666eab30ae568dd851fa533797413 Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Tue, 14 Apr 2020 15:55:50 -0700 Subject: [PATCH 11/14] Update create function. --- Building/ddl/building_functions.sql | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/Building/ddl/building_functions.sql b/Building/ddl/building_functions.sql index ff5b5b8..ee9e3a8 100644 --- a/Building/ddl/building_functions.sql +++ b/Building/ddl/building_functions.sql @@ -27,8 +27,8 @@ DECLARE returning id into new_address_id; -- insert new lot - insert into lot (boro_id,number_of_buildings) - values (1,1) + insert into lot (boro_id,number_of_buildings,load_date) + values (1,1,now()) returning id into new_lot_id; -- insert new lot address @@ -36,32 +36,16 @@ DECLARE values (new_lot_id,new_address_id); -- insert new building - insert into building(bbl,bin,load_date) - values (300000005,1111111,now()) + insert into building(load_date, lot_id) + values (now(),new_lot_id) returning id into new_building_id; -- insert new building address insert into building_address(building_id, address_id, place_name) values (new_building_id, new_address_id, in_place_name); - -- refresh view - REFRESH MATERIALIZED VIEW concurrently vw_building_address; - return QUERY - select v.building_id, - v.lot_id, - v.bbl, - v.bin, - v.street_address, - b.description as borough, - v.zipcode, - v.state, - v.targeting_score, - v.place_name - from vw_building_address v -- from vw_building_address join on Borough to Borough - join borough b - on b.id = v.borough_id - where v.place_name = in_place_name; + select new_building_id, new_lot_id,null,null,'address', 'borough', 12345, 'NY', null, in_place_name; END IF; END; $$; -- GitLab From 74f2f0217dd6647b3ad9783686fdf638c23f3468 Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Tue, 14 Apr 2020 15:57:17 -0700 Subject: [PATCH 12/14] Update function to add notes and add return type. --- Building/ddl/building_functions.sql | 91 +++++++++++++---------------- 1 file changed, 42 insertions(+), 49 deletions(-) diff --git a/Building/ddl/building_functions.sql b/Building/ddl/building_functions.sql index ee9e3a8..f86c29e 100644 --- a/Building/ddl/building_functions.sql +++ b/Building/ddl/building_functions.sql @@ -45,7 +45,7 @@ DECLARE values (new_building_id, new_address_id, in_place_name); return QUERY - select new_building_id, new_lot_id,null,null,'address', 'borough', 12345, 'NY', null, in_place_name; + select new_building_id, new_lot_id, null,null, null, null, null, null, null, in_place_name; END IF; END; $$; @@ -63,60 +63,53 @@ select * from create_building(in_place_name := '838 Park Place, Brooklyn, New Yo */ -- read_building() -create function read_building( - in_building_id integer DEFAULT NULL::integer, +create function create_building( in_place_name character varying DEFAULT NULL::character varying) returns TABLE( - building_id integer, - lot_id integer, - bbl bigint, - bin bigint, - street_address text, - borough character varying, - zipcode numeric, - state character, - targeting_score double precision, - place_name VARCHAR) + building_id integer, -- insert building + lot_id integer, -- generate it? + bbl bigint, -- generate it? + bin bigint, -- generate it? + street_address text, -- generate it? + borough character varying, -- generate it? + zipcode numeric, -- generate it? + state character, -- gerenate it? + targeting_score double precision, -- generate it? + place_name VARCHAR) -- insert place_name language plpgsql as $$ DECLARE - in_building_id ALIAS FOR $1; - in_place_name ALIAS for $2; + in_place_name ALIAS for $1; + new_address_id integer; + new_lot_id integer; + new_building_id integer; BEGIN - if ( (in_building_id is not null) and in_place_name is null)then - return QUERY -- Building ID Look up - select v.building_id, - v.lot_id, - v.bbl, - v.bin, - v.street_address, - b.description as borough, - v.zipcode, - v.state, - v.targeting_score, - v.place_name - from vw_building_address v -- from vw_building_address join on Borough to Borough - join borough b - on b.id = v.borough_id - where v.building_id = in_building_id - ; - ELSIF (in_building_id is null and (in_place_name is not null))THEN - Return QUERY -- Address Search Lookup - select v.building_id, - v.lot_id, - v.bbl, - v.bin, - v.street_address, - b.description as borough, - v.zipcode, - v.state, - v.targeting_score, - v.place_name - from vw_building_address v - join borough b - on b.id = v.borough_id - where v.place_name = in_place_name -- use get address with in_address, in_zip, and in_boro - ; + IF (in_place_name is not null)then + -- insert new address + insert into address(borough_id, house_number, house_number_sequence) + values (1,'0000',1) + returning id into new_address_id; + + -- insert new lot + insert into lot (boro_id,number_of_buildings,load_date) + values (1,1,now()) + returning id into new_lot_id; + + -- insert new lot address + insert into lot_address + values (new_lot_id,new_address_id); + + -- insert new building + insert into building(load_date, lot_id) + values (now(),new_lot_id) + returning id into new_building_id; + + -- insert new building address + insert into building_address(building_id, address_id, place_name) + values (new_building_id, new_address_id, in_place_name); + + return QUERY + select new_building_id as building_id, new_lot_id as lot_id, null as bbl,null as bin, null as street_address, null as borough, null as zipcode, null as state, null as targeting_score, in_place_name as place_name; END IF; END; $$; -- GitLab From 8f6ac8a17f60db8e658d1a55fad2a79533f5bb36 Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Tue, 14 Apr 2020 16:09:54 -0700 Subject: [PATCH 13/14] Revery read building function and update create building function. --- Building/ddl/building_functions.sql | 91 ++++++++++++++++------------- 1 file changed, 49 insertions(+), 42 deletions(-) diff --git a/Building/ddl/building_functions.sql b/Building/ddl/building_functions.sql index f86c29e..b26d729 100644 --- a/Building/ddl/building_functions.sql +++ b/Building/ddl/building_functions.sql @@ -45,7 +45,7 @@ DECLARE values (new_building_id, new_address_id, in_place_name); return QUERY - select new_building_id, new_lot_id, null,null, null, null, null, null, null, in_place_name; + select new_building_id as building_id, new_lot_id as lot_id, null::bigint as bbl,null::bigint as bin, null::text as street_address, null::character varying as borough, null::numeric as zipcode, null::character as state, null::double precision as targeting_score, in_place_name as place_name; END IF; END; $$; @@ -63,53 +63,60 @@ select * from create_building(in_place_name := '838 Park Place, Brooklyn, New Yo */ -- read_building() -create function create_building( +create function read_building( + in_building_id integer DEFAULT NULL::integer, in_place_name character varying DEFAULT NULL::character varying) returns TABLE( - building_id integer, -- insert building - lot_id integer, -- generate it? - bbl bigint, -- generate it? - bin bigint, -- generate it? - street_address text, -- generate it? - borough character varying, -- generate it? - zipcode numeric, -- generate it? - state character, -- gerenate it? - targeting_score double precision, -- generate it? - place_name VARCHAR) -- insert place_name + building_id integer, + lot_id integer, + bbl bigint, + bin bigint, + street_address text, + borough character varying, + zipcode numeric, + state character, + targeting_score double precision, + place_name VARCHAR) language plpgsql as $$ DECLARE - in_place_name ALIAS for $1; - new_address_id integer; - new_lot_id integer; - new_building_id integer; + in_building_id ALIAS FOR $1; + in_place_name ALIAS for $2; BEGIN - IF (in_place_name is not null)then - -- insert new address - insert into address(borough_id, house_number, house_number_sequence) - values (1,'0000',1) - returning id into new_address_id; - - -- insert new lot - insert into lot (boro_id,number_of_buildings,load_date) - values (1,1,now()) - returning id into new_lot_id; - - -- insert new lot address - insert into lot_address - values (new_lot_id,new_address_id); - - -- insert new building - insert into building(load_date, lot_id) - values (now(),new_lot_id) - returning id into new_building_id; - - -- insert new building address - insert into building_address(building_id, address_id, place_name) - values (new_building_id, new_address_id, in_place_name); - - return QUERY - select new_building_id as building_id, new_lot_id as lot_id, null as bbl,null as bin, null as street_address, null as borough, null as zipcode, null as state, null as targeting_score, in_place_name as place_name; + if ( (in_building_id is not null) and in_place_name is null)then + return QUERY -- Building ID Look up + select v.building_id, + v.lot_id, + v.bbl, + v.bin, + v.street_address, + b.description as borough, + v.zipcode, + v.state, + v.targeting_score, + v.place_name + from vw_building_address v -- from vw_building_address join on Borough to Borough + join borough b + on b.id = v.borough_id + where v.building_id = in_building_id + ; + ELSIF (in_building_id is null and (in_place_name is not null))THEN + Return QUERY -- Address Search Lookup + select v.building_id, + v.lot_id, + v.bbl, + v.bin, + v.street_address, + b.description as borough, + v.zipcode, + v.state, + v.targeting_score, + v.place_name + from vw_building_address v + join borough b + on b.id = v.borough_id + where v.place_name = in_place_name -- use get address with in_address, in_zip, and in_boro + ; END IF; END; $$; -- GitLab From 64278b52991af9cfb9295a2aca64c61275309b4e Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Wed, 15 Apr 2020 13:30:53 -0700 Subject: [PATCH 14/14] Autopopulate pad_range_id and house_sequence_number to make sure materialized views return values. --- Building/ddl/building_functions.sql | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Building/ddl/building_functions.sql b/Building/ddl/building_functions.sql index b26d729..a66f40b 100644 --- a/Building/ddl/building_functions.sql +++ b/Building/ddl/building_functions.sql @@ -22,8 +22,8 @@ DECLARE BEGIN IF (in_place_name is not null)then -- insert new address - insert into address(borough_id, house_number, house_number_sequence) - values (1,'0000',1) + insert into address(borough_id, house_number, house_number_sequence, pad_range_id) + values (1,'0000',1,1) returning id into new_address_id; -- insert new lot @@ -41,8 +41,8 @@ DECLARE returning id into new_building_id; -- insert new building address - insert into building_address(building_id, address_id, place_name) - values (new_building_id, new_address_id, in_place_name); + insert into building_address(building_id, address_id, place_name, pad_range_id, house_number_sequence) + values (new_building_id, new_address_id, in_place_name, 1, 1); return QUERY select new_building_id as building_id, new_lot_id as lot_id, null::bigint as bbl,null::bigint as bin, null::text as street_address, null::character varying as borough, null::numeric as zipcode, null::character as state, null::double precision as targeting_score, in_place_name as place_name; -- GitLab