From 4371a7504e4168d337cf4743f53fc2872a82be05 Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Wed, 18 Mar 2020 13:26:42 -0700 Subject: [PATCH 01/24] Adding ddl from Bloclink I forgot to push. --- BlocLink/ddl/00_create_all_october_11.sql | 528 ++++++++++++++++++++++ 1 file changed, 528 insertions(+) create mode 100644 BlocLink/ddl/00_create_all_october_11.sql diff --git a/BlocLink/ddl/00_create_all_october_11.sql b/BlocLink/ddl/00_create_all_october_11.sql new file mode 100644 index 0000000..c5e83e0 --- /dev/null +++ b/BlocLink/ddl/00_create_all_october_11.sql @@ -0,0 +1,528 @@ +create database bloclink; + +create sequence pna.ashp_score_ashp_score_id_seq; + +create sequence pna.criteria_criteria_id_seq; + +create sequence pna.possible_answers_answer_id_seq; + +create sequence pna.questions_quetion_id_seq; + +create sequence pna.submitted_answers_id_seq; + +create sequence pna.que_ans_sw_id_seq; + +create sequence pna.building_unit_info_id_seq; + +create sequence pna.building_details_id_seq; + +create sequence pna.criteria_scores_id_seq; + +create sequence pna.report_file_id_seq; + +create sequence external_pna.building_unit_info_id_seq; + +create sequence external_pna.criterias_id_seq; + +create sequence external_pna.pna_score_id_seq; + +create sequence external_pna.pna_score_id_seq1; + +create sequence external_pna.possible_answers_id_seq; + +create sequence external_pna.que_ans_scoreweight_id_seq; + +create sequence external_pna.questions_id_seq; + +create sequence external_pna.report_file_id_seq; + +create sequence external_pna.submitted_answers_id_seq; + +create sequence external_pna.user_building_info_id_seq; + +create sequence external_pna.user_info_id_seq; + +create sequence external_pna.questions_id_seq1; + +create sequence external_pna.possible_answers_id_seq1; + +create sequence external_pna.user_info_id_seq1; + +create sequence external_pna.user_building_info_id_seq1; + +create sequence external_pna.submitted_answers_id_seq1; + +create sequence external_pna.building_info_id_seq; + +create sequence external_pna.mapbox_building_id_seq; + +create table public.django_content_type +( + id serial not null + constraint django_content_type_pkey + primary key, + app_label varchar(100) not null, + model varchar(100) not null, + constraint django_content_type_app_label_76bd3d3b_uniq + unique (app_label, model) +); + +create table public.auth_permission +( + id serial not null + constraint auth_permission_pkey + primary key, + name varchar(255) not null, + content_type_id integer not null + constraint auth_permiss_content_type_id_2f476e4b_fk_django_content_type_id + references django_content_type + deferrable initially deferred, + codename varchar(100) not null, + constraint auth_permission_content_type_id_01ab375a_uniq + unique (content_type_id, codename) +); + +create index auth_permission_417f1b1c + on public.auth_permission (content_type_id); + +create table public.auth_group +( + id serial not null + constraint auth_group_pkey + primary key, + name varchar(80) not null + constraint auth_group_name_key + unique +); + +create index auth_group_name_a6ea08ec_like + on public.auth_group (name); + +create table public.auth_group_permissions +( + id serial not null + constraint auth_group_permissions_pkey + primary key, + group_id integer not null + constraint auth_group_permissions_group_id_b120cbf9_fk_auth_group_id + references auth_group + deferrable initially deferred, + permission_id integer not null + constraint auth_group_permiss_permission_id_84c5c92e_fk_auth_permission_id + references auth_permission + deferrable initially deferred, + constraint auth_group_permissions_group_id_0cd325b0_uniq + unique (group_id, permission_id) +); + +create index auth_group_permissions_0e939a4f + on public.auth_group_permissions (group_id); + +create index auth_group_permissions_8373b171 + on public.auth_group_permissions (permission_id); + +create table public.auth_user +( + id serial not null + constraint auth_user_pkey + primary key, + password varchar(128) not null, + last_login timestamp with time zone, + is_superuser boolean not null, + username varchar(150) not null + constraint auth_user_username_key + unique, + first_name varchar(30) not null, + last_name varchar(30) not null, + email varchar(254) not null, + is_staff boolean not null, + is_active boolean not null, + date_joined timestamp with time zone not null +); + +create index auth_user_username_6821ab7c_like + on public.auth_user (username); + +create table public.auth_user_groups +( + id serial not null + constraint auth_user_groups_pkey + primary key, + user_id integer not null + constraint auth_user_groups_user_id_6a12ed8b_fk_auth_user_id + references auth_user + deferrable initially deferred, + group_id integer not null + constraint auth_user_groups_group_id_97559544_fk_auth_group_id + references auth_group + deferrable initially deferred, + constraint auth_user_groups_user_id_94350c0c_uniq + unique (user_id, group_id) +); + +create index auth_user_groups_e8701ad4 + on public.auth_user_groups (user_id); + +create index auth_user_groups_0e939a4f + on public.auth_user_groups (group_id); + +create table public.auth_user_user_permissions +( + id serial not null + constraint auth_user_user_permissions_pkey + primary key, + user_id integer not null + constraint auth_user_user_permissions_user_id_a95ead1b_fk_auth_user_id + references auth_user + deferrable initially deferred, + permission_id integer not null + constraint auth_user_user_per_permission_id_1fbb5f2c_fk_auth_permission_id + references auth_permission + deferrable initially deferred, + constraint auth_user_user_permissions_user_id_14a6b632_uniq + unique (user_id, permission_id) +); + +create index auth_user_user_permissions_e8701ad4 + on public.auth_user_user_permissions (user_id); + +create index auth_user_user_permissions_8373b171 + on public.auth_user_user_permissions (permission_id); + +create table public.django_admin_log +( + id serial not null + constraint django_admin_log_pkey + primary key, + action_time timestamp with time zone not null, + object_id text, + object_repr varchar(200) not null, + action_flag smallint not null + constraint django_admin_log_action_flag_check + check (action_flag >= 0), + change_message text not null, + content_type_id integer + constraint django_admin_content_type_id_c4bce8eb_fk_django_content_type_id + references django_content_type + deferrable initially deferred, + user_id integer not null + constraint django_admin_log_user_id_c564eba6_fk_auth_user_id + references auth_user + deferrable initially deferred +); + +create index django_admin_log_417f1b1c + on public.django_admin_log (content_type_id); + +create index django_admin_log_e8701ad4 + on public.django_admin_log (user_id); + +create table public.django_session +( + session_key varchar(40) not null + constraint django_session_pkey + primary key, + session_data text not null, + expire_date timestamp with time zone not null +); + +create index django_session_de54fa62 + on public.django_session (expire_date); + +create index django_session_session_key_c0390e0f_like + on public.django_session (session_key); + +create table public.django_migrations +( + id serial not null + constraint django_migrations_pkey + primary key, + app varchar(255) not null, + name varchar(255) not null, + applied timestamp with time zone not null +); + +create table pna.building_info +( + building_id integer not null + constraint building_info_pk + primary key, + address varchar not null, + owner_first_name text not null, + owner_last_name text not null, + owner_email varchar, + owner_phone_number bigint +); + +create unique index "building_info_building-id" + on pna.building_info (building_id); + +create table pna.building_unit_info +( + building_id integer not null + constraint building_unit_info_fk + references building_info, + unit_type varchar not null, + count integer not null, + id serial not null + constraint building_unit_info_pk_2 + primary key, + constraint building_unit_info_pk + unique (building_id, unit_type) +); + +create unique index building_unit_info_id_uindex + on pna.building_unit_info (id); + +create table pna.dropdown_answers +( + id serial not null + constraint possible_answers_pk + primary key, + answer varchar not null +); + +create unique index "possible_answers_answer-id" + on pna.dropdown_answers (id); + +create table pna.criterias +( + id serial not null + constraint criteria_pk + primary key, + criteria_name varchar not null +); + +create unique index "criteria_criteria-id" + on pna.criterias (id); + +create unique index "criteria_criteria-name" + on pna.criterias (criteria_name); + +create table pna.pna_costs +( + id serial not null + constraint ashp_score_pk + primary key, + building_id integer not null + constraint ashp_score_fk + references building_info, + ashp_low integer not null, + ashp_high integer not null, + solar_low integer not null, + solar_high integer not null, + weather_low integer not null, + weather_high integer, + weather_doi integer, + ashp_doi integer, + solar_doi integer, + weather_payback varchar, + ashp_payback varchar, + solar_payback varchar +); + +create unique index "ashp_score_ashp-score-id" + on pna.pna_costs (id); + +create table pna.questions +( + id serial not null + constraint questions_pk + primary key, + question text not null +); + +create unique index "questions_quetion-id" + on pna.questions (id); + +create table pna.submitted_answers +( + building_id integer not null + constraint submitted_answers_fk + references building_info, + question_id integer not null + constraint submitted_answers_fk1 + references questions, + answer_id integer not null + constraint submitted_answers_fk2 + references dropdown_answers, + id serial not null + constraint submitted_answers_pk_2 + primary key, + constraint submitted_answers_pk + unique (building_id, question_id) +); + +create unique index submitted_answers_id_uindex + on pna.submitted_answers (id); + +create table pna.que_ans_sw +( + question_id integer not null + constraint que_ans_sw_fk1 + references questions, + answer_id integer not null + constraint que_ans_sw_fk2 + references dropdown_answers, + score_weight integer not null, + criteria_id integer not null + constraint que_ans_sw_fk3 + references criterias, + id serial not null + constraint que_ans_sw_pk_2 + primary key, + constraint que_ans_sw_pk + unique (question_id, answer_id) +); + +create table pna.criteria_scores +( + criteria_id integer not null + constraint criteria_score_fk1 + references criterias, + building_id integer not null + constraint criteria_score_fk + references building_info, + criteria_score integer not null, + id serial not null + constraint criteria_scores_pk + primary key, + constraint criteria_score_pk + unique (criteria_id, building_id) +); + +create table pna.building_details +( + building_id integer not null, + num_of_apts integer, + building_sqft integer, + num_of_floors integer, + num_of_heating_violations varchar, + num_of_dob_violations varchar, + legal_ownership varchar, + id serial not null + constraint building_details_pk + primary key +); + +create unique index building_details_building_id_uindex + on pna.building_details (building_id); + +create unique index building_details_id_uindex + on pna.building_details (id); + +create table pna.report_file +( + id serial not null + constraint report_file_pk + primary key, + resource varchar, + last_updated timestamp, + building_id integer + constraint report_file_building_info_building_id_fk + references building_info +); + +create table external_pna.questions +( + id serial not null + constraint questions_pk + primary key, + question varchar not null +); + +create unique index questions_id_uindex + on external_pna.questions (id); + +create table external_pna.possible_answers +( + id serial not null + constraint possible_answers_pk + primary key, + answer varchar not null +); + +create unique index possible_answers_id_uindex + on external_pna.possible_answers (id); + +create table external_pna.user_info +( + id serial not null + constraint user_info_pk + primary key, + first_name varchar not null, + last_name varchar not null, + email varchar not null, + phone_number varchar, + user_confirmed boolean, + token varchar, + token_time varchar +); + +create unique index user_info_id_uindex + on external_pna.user_info (id); + +create unique index user_info_token_uindex + on external_pna.user_info (token); + +create table external_pna.building_info +( + id serial not null + constraint building_info_pk + primary key, + building_id integer not null, + address varchar not null +); + +create table external_pna.user_building_info +( + id serial not null + constraint user_building_info_pk + primary key, + user_id integer + constraint user_building_info_user_info_id_fk + references user_info, + building_id integer not null + constraint user_building_info_building_info_building_id_fk + references building_info (building_id), + completed boolean default false +); + +create unique index user_building_info_id_uindex + on external_pna.user_building_info (id); + +create table external_pna.submitted_answers +( + id serial not null + constraint submitted_answers_pk + primary key, + user_building_id integer + constraint submitted_answers_user_building_info_id_fk + references user_building_info, + question_id integer + constraint submitted_answers_questions_id_fk + references questions, + answer_id integer + constraint submitted_answers_possible_answers_id_fk + references possible_answers +); + +create unique index submitted_answers_id_uindex + on external_pna.submitted_answers (id); + +create unique index building_info_address_uindex + on external_pna.building_info (address); + +create unique index building_info_building_id_uindex + on external_pna.building_info (building_id); + +create unique index building_info_id_uindex + on external_pna.building_info (id); + +create table external_pna.mapbox_building +( + id serial not null, + mapbox_address varchar, + building_id integer +); + -- GitLab From b490bf44eb6add262ed28e3e07b553c8971aa618 Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Wed, 18 Mar 2020 13:28:49 -0700 Subject: [PATCH 02/24] Adding Baltimore get from datagrip to review. --- Building/DAL/baltimore_get_building.sql | 34 +++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Building/DAL/baltimore_get_building.sql diff --git a/Building/DAL/baltimore_get_building.sql b/Building/DAL/baltimore_get_building.sql new file mode 100644 index 0000000..7d16982 --- /dev/null +++ b/Building/DAL/baltimore_get_building.sql @@ -0,0 +1,34 @@ +create function baltimore_building_get(in_building_id integer DEFAULT NULL::integer) + 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) +language plpgsql +as $$ +DECLARE + in_building_id ALIAS FOR $1; + + + BEGIN + if ( in_building_id is not null )then + return QUERY + SELECT b.id as building_id, + b.lot_id, + b.bbl, + b.bin, + CONCAT(a.house_number, ' ', a.street_name) :: text, + 'MANHATTAN' :: varchar(15) as borough, + a.zipcode :: numeric(5,0), + a.state, + 75 :: double precision as targeting_score + FROM public.building b + join public.building_address ba + on b.id = ba.building_id + join public.address a + on ba.address_id = a.id + WHERE b.id = in_building_id + ; + END IF; + + END; +$$; + +alter function baltimore_building_get(integer) + owner to blocpower; -- GitLab From 9e22445dab1cc44505f4d4f84ac93683a4c0d734 Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Wed, 18 Mar 2020 14:06:41 -0700 Subject: [PATCH 03/24] Add building search to procedure to review. --- Building/DAL/building_search.sql | 63 ++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 Building/DAL/building_search.sql diff --git a/Building/DAL/building_search.sql b/Building/DAL/building_search.sql new file mode 100644 index 0000000..585664f --- /dev/null +++ b/Building/DAL/building_search.sql @@ -0,0 +1,63 @@ +create function building_search(in_bbl bigint DEFAULT NULL::bigint, in_building_id integer DEFAULT NULL::integer, in_address character varying DEFAULT NULL::character varying, in_zip numeric DEFAULT NULL::numeric, in_boro integer DEFAULT NULL::integer) + 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) +language plpgsql +as $$ +DECLARE + in_bbl ALIAS FOR $1; + in_building_id ALIAS for $2; + in_address ALIAS for $3; + in_zip ALIAS for $4; + in_boro ALIAS for $5; + + found_address_id integer default NULL; + found_lot_id integer default null; + + BEGIN + if ( (in_bbl is not null or in_building_id is not null) and in_address is null and in_zip is null and in_boro is null )then + 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 + from vw_building_address v + join borough b + on b.id = v.borough_id + where v.building_id = in_building_id + or v.bbl = in_bbl + ; + ELSIF (in_bbl is null and in_building_id is null and (in_address is not null or in_zip is not null or in_boro is not null) )THEN + select id + into found_address_id + from get_address(in_address, in_zip, in_boro) + ; + --raise notice 'outer else'; + Return QUERY --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 + from vw_building_address v + join borough b + on b.id = v.borough_id + WHERE v.pad_range_id in (select pad_range_id from get_address(in_address, in_zip, in_boro)) + or v.lot_address_id in (select id from get_address(in_address, in_zip, in_boro)) + ; + + END IF; + + + END; +$$; + +alter function building_search(bigint, integer, varchar, numeric, integer) + owner to blocpower; -- GitLab From aed9ca668552cb35c848d7c5c765968e5efbcd84 Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Wed, 18 Mar 2020 15:35:56 -0700 Subject: [PATCH 04/24] Make building search a bit easier to read. --- Building/DAL/building_search.sql | 56 +++++++++++++++++++++++++++- Building/ddl/vw_building_address.sql | 46 +++++++++++++++++++++++ 2 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 Building/ddl/vw_building_address.sql diff --git a/Building/DAL/building_search.sql b/Building/DAL/building_search.sql index 585664f..bfcba08 100644 --- a/Building/DAL/building_search.sql +++ b/Building/DAL/building_search.sql @@ -1,5 +1,18 @@ -create function building_search(in_bbl bigint DEFAULT NULL::bigint, in_building_id integer DEFAULT NULL::integer, in_address character varying DEFAULT NULL::character varying, in_zip numeric DEFAULT NULL::numeric, in_boro integer DEFAULT NULL::integer) - 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) +create function building_search( + in_bbl bigint DEFAULT NULL::bigint, + in_building_id integer DEFAULT NULL::integer, + in_address character varying DEFAULT NULL::character varying, + in_zip numeric DEFAULT NULL::numeric, in_boro integer DEFAULT NULL::integer) + 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) language plpgsql as $$ DECLARE @@ -61,3 +74,42 @@ $$; alter function building_search(bigint, integer, varchar, numeric, integer) owner to blocpower; + +--TEST CASES: +------------------------------------------------------------------------------------------------------------------ + + +/* +--inital search hack +select * from get_building(NULL,NULL,null,null) +order by address; + +select * from get_building(); + +select * from get_building('105 CENTRAL AVENUE',NULL,null,null); --address wihout zip + +select * from get_building('49 11 STREET 11101',NULL,null,NULL); --address wihout zip + +select * from get_building('49 11 STREET',3021560124,null,NULL); --address wihout zip + bbl + +select * from get_building('49 11 STREET',3021560124,591383); --address wihout zip + bbl+ blocpwer_id + +select * from get_building('49 11 STREET',NULL,591383); --address wihout zip + blocpwer_id + +select * from get_building('49 11 STREET',3021560124,591497); --address wihout zip + bbl+ wrong blocpwer_id + +select * from get_building(NULL,3021560124,NULL); -- bbl only + +select * from get_building(NULL,NULL,591383); -- blocpower_id only + + + +select * from get_building('49 11 STREET'); --using ordered defaults parameters + +select * from get_building(NULL,3021560124); ----using + +select * from get_building(in_bbl := 3017860039); --using named parameters + +select * from get_building() limit 100 --selcting all buildings; + +*/ diff --git a/Building/ddl/vw_building_address.sql b/Building/ddl/vw_building_address.sql new file mode 100644 index 0000000..59b303f --- /dev/null +++ b/Building/ddl/vw_building_address.sql @@ -0,0 +1,46 @@ +create materialized view vw_building_address as + WITH address_mapping AS ( + SELECT DISTINCT b.bbl, + b.bin, + b.id AS building_id, + b.lot_id, + b.targeting_score, + 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)))) + ) + SELECT DISTINCT aa.building_id, + aa.lot_id, + aa.bbl, + aa.bin, + aa.targeting_score, + (( + 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, + lh.borough_id, + lh.zipcode, + lh.state, + aa.pad_range_id, + lh.id AS min_address_id, + hh.id AS max_address_id, + la.address_id AS lot_address_id + FROM (((address_mapping aa + JOIN address lh ON (((aa.pad_range_id = lh.pad_range_id) AND (aa.min_sequence = lh.house_number_sequence)))) + JOIN address hh ON (((aa.pad_range_id = hh.pad_range_id) AND (aa.max_sequence = hh.house_number_sequence)))) + LEFT JOIN lot_address la ON ((la.lot_id = aa.lot_id))) + ORDER BY aa.lot_id, aa.building_id; + +alter materialized view vw_building_address + owner to blocpower; + -- GitLab From e0822ed9283ae45619ba07660540cc51fc73e73f Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Mon, 23 Mar 2020 11:51:47 -0700 Subject: [PATCH 05/24] Add comments to building search, reorganize baltimore get, and add get address to review. --- Building/DAL/baltimore_get_building.sql | 11 ++++++- Building/DAL/building_search.sql | 10 +++---- Building/DAL/get_address.sql | 38 +++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 Building/DAL/get_address.sql diff --git a/Building/DAL/baltimore_get_building.sql b/Building/DAL/baltimore_get_building.sql index 7d16982..c11e192 100644 --- a/Building/DAL/baltimore_get_building.sql +++ b/Building/DAL/baltimore_get_building.sql @@ -1,5 +1,14 @@ create function baltimore_building_get(in_building_id integer DEFAULT NULL::integer) - 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) + 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) language plpgsql as $$ DECLARE diff --git a/Building/DAL/building_search.sql b/Building/DAL/building_search.sql index bfcba08..365ecc7 100644 --- a/Building/DAL/building_search.sql +++ b/Building/DAL/building_search.sql @@ -27,7 +27,7 @@ DECLARE BEGIN if ( (in_bbl is not null or in_building_id is not null) and in_address is null and in_zip is null and in_boro is null )then - return QUERY + return QUERY -- Building ID or BBL Look up select v.building_id, v.lot_id, v.bbl, @@ -37,7 +37,7 @@ DECLARE v.zipcode, v.state, v.targeting_score - from vw_building_address v + 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 @@ -49,7 +49,7 @@ DECLARE from get_address(in_address, in_zip, in_boro) ; --raise notice 'outer else'; - Return QUERY --Query!!!!!!!!!!!!!!!!!!!!! + Return QUERY -- Address Search Lookup select v.building_id, v.lot_id, v.bbl, @@ -62,8 +62,8 @@ DECLARE from vw_building_address v join borough b on b.id = v.borough_id - WHERE v.pad_range_id in (select pad_range_id from get_address(in_address, in_zip, in_boro)) - or v.lot_address_id in (select id from get_address(in_address, in_zip, in_boro)) + WHERE v.pad_range_id in (select pad_range_id from get_address(in_address, in_zip, in_boro)) + or v.lot_address_id in (select id from get_address(in_address, in_zip, in_boro))-- use get address with in_address, in_zip, and in_boro ; END IF; diff --git a/Building/DAL/get_address.sql b/Building/DAL/get_address.sql new file mode 100644 index 0000000..e7bb281 --- /dev/null +++ b/Building/DAL/get_address.sql @@ -0,0 +1,38 @@ +create function get_address(in_address character varying DEFAULT NULL::character varying, in_zip numeric DEFAULT NULL::numeric, in_boro integer DEFAULT NULL::integer) + returns TABLE(borough_id smallint, house_number character varying, house_number_sequence integer, id integer, pad_range_id integer, street_name character varying, state character, zipcode numeric) +language plpgsql +as $$ +DECLARE + in_addres ALIAS FOR $1; + in_zip ALIAS for $2; + in_boro ALIAS FOR $3; + address_id integer; + + + BEGIN + SELECT replace(in_address,' ','&') into in_address; + + + return QUERY + select a.borough_id, + a.house_number, + a.house_number_sequence, + a.id, + a.pad_range_id, + a.street_name, + a.state, + a.zipcode + from address a + where street_address @@ to_tsquery(in_address) + and (a.borough_id = in_boro or in_boro is null) + and (a.zipcode = in_zip or in_zip is null) + + ; + + END; + +$$; + +alter function get_address(varchar, numeric, integer) + owner to blocpower; + -- GitLab From bf654eb5d768ad1c70be75034baee8798baf3d66 Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Tue, 24 Mar 2020 20:12:00 -0700 Subject: [PATCH 06/24] Reviewing the get_address and get_building functions. --- Building/DAL/get_address.sql | 2 +- Building/pad_based/add_bin_addresses/get_building.sql | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Building/DAL/get_address.sql b/Building/DAL/get_address.sql index e7bb281..679496a 100644 --- a/Building/DAL/get_address.sql +++ b/Building/DAL/get_address.sql @@ -23,7 +23,7 @@ DECLARE a.state, a.zipcode from address a - where street_address @@ to_tsquery(in_address) + where street_address @@ to_tsquery(in_address) -- text search of in_address will be deprecated and (a.borough_id = in_boro or in_boro is null) and (a.zipcode = in_zip or in_zip is null) diff --git a/Building/pad_based/add_bin_addresses/get_building.sql b/Building/pad_based/add_bin_addresses/get_building.sql index 0d8c549..33a9e69 100644 --- a/Building/pad_based/add_bin_addresses/get_building.sql +++ b/Building/pad_based/add_bin_addresses/get_building.sql @@ -259,7 +259,7 @@ select * from get_building( NULL,null,872208) select * from get_building( null,1122410) - +-- This version of building search is deprecated and no longer being used so get_building is to be deprecated create or replace function building_search( in_bbl bigint DEFAULT NULL, in_building_id integer DEFAULT NULL, @@ -401,4 +401,4 @@ select ascii('100A') --49 select '100A'::bytea, '100Z'::bytea -select * from lot where bbl = 1000151001 \ No newline at end of file +select * from lot where bbl = 1000151001 -- GitLab From 90a0b55e5c065abea1c1e88116e208477a436962 Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Tue, 24 Mar 2020 21:03:01 -0700 Subject: [PATCH 07/24] Remove temp architect file. --- Building/erd/OLTP Building ERD.architect~ | 1242 --------------------- 1 file changed, 1242 deletions(-) delete mode 100644 Building/erd/OLTP Building ERD.architect~ diff --git a/Building/erd/OLTP Building ERD.architect~ b/Building/erd/OLTP Building ERD.architect~ deleted file mode 100644 index ca98f88..0000000 --- a/Building/erd/OLTP Building ERD.architect~ +++ /dev/null @@ -1,1242 +0,0 @@ - - - OLTP BUuilding ERD - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-- GitLab From 7f9e798cacb6a7c854e3498c76a4fdf27057f0ad Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Wed, 25 Mar 2020 11:37:59 -0700 Subject: [PATCH 08/24] Remove redundant files. --- ..._building__public_pre_phase_3_snapshot.sql | 886 ------------------ 1 file changed, 886 deletions(-) delete mode 100644 Building/ddl/app_dev_building__public_pre_phase_3_snapshot.sql diff --git a/Building/ddl/app_dev_building__public_pre_phase_3_snapshot.sql b/Building/ddl/app_dev_building__public_pre_phase_3_snapshot.sql deleted file mode 100644 index 9aed2f2..0000000 --- a/Building/ddl/app_dev_building__public_pre_phase_3_snapshot.sql +++ /dev/null @@ -1,886 +0,0 @@ --- --- PostgreSQL database dump --- - --- Dumped from database version 9.6.1 --- Dumped by pg_dump version 9.6.2 - -SET statement_timeout = 0; -SET lock_timeout = 0; -SET idle_in_transaction_session_timeout = 0; -SET client_encoding = 'UTF8'; -SET standard_conforming_strings = on; -SET check_function_bodies = false; -SET client_min_messages = warning; -SET row_security = off; - -SET search_path = public, pg_catalog; - --- --- Name: building_search(bigint, integer, character varying, numeric, integer); Type: FUNCTION; Schema: public; Owner: - --- - -CREATE FUNCTION building_search(in_bbl bigint DEFAULT NULL::bigint, in_building_id integer DEFAULT NULL::integer, in_address character varying DEFAULT NULL::character varying, in_zip numeric DEFAULT NULL::numeric, in_boro integer DEFAULT NULL::integer) RETURNS TABLE(building_id integer, lot_id integer, bbl bigint, bin bigint, street_address text, borough character varying, zipcode numeric, state character, targeting_score float) - LANGUAGE plpgsql - AS $_$ - - DECLARE - in_bbl ALIAS FOR $1; - in_building_id ALIAS for $2; - in_address ALIAS for $3; - in_zip ALIAS for $4; - in_boro ALIAS for $5; - - found_address_id integer default NULL; - found_lot_id integer default null; - - BEGIN - if ( (in_bbl is not null or in_building_id is not null) and in_address is null and in_zip is null and in_boro is null )then - 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 - from vw_building_address v - join borough b - on b.id = v.borough_id - where v.building_id = in_building_id - or v.bbl = in_bbl - ; - ELSIF (in_bbl is null and in_building_id is null and (in_address is not null or in_zip is not null or in_boro is not null) )THEN - select id - into found_address_id - from get_address(in_address, in_zip, in_boro) - ; - --raise notice 'outer else'; - Return QUERY --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 - from vw_building_address v - join borough b - on b.id = v.borough_id - WHERE v.pad_range_id in (select pad_range_id from get_address(in_address, in_zip, in_boro)) - or v.lot_address_id in (select id from get_address(in_address, in_zip, in_boro)) - ; - - END IF; - - - END; -$_$; - - --- --- Name: get_address(character varying, numeric, integer); Type: FUNCTION; Schema: public; Owner: - --- - -CREATE FUNCTION get_address(in_address character varying DEFAULT NULL::character varying, in_zip numeric DEFAULT NULL::numeric, in_boro integer DEFAULT NULL::integer) RETURNS TABLE(borough_id smallint, house_number character varying, house_number_sequence integer, id integer, pad_range_id integer, street_name character varying, state character, zipcode numeric) - LANGUAGE plpgsql - AS $_$ - - DECLARE - in_addres ALIAS FOR $1; - in_zip ALIAS for $2; - in_boro ALIAS FOR $3; - address_id integer; - - - BEGIN - SELECT replace(in_address,' ','&') into in_address; - - - return QUERY - select a.borough_id, - a.house_number, - a.house_number_sequence, - a.id, - a.pad_range_id, - a.street_name, - a.state, - a.zipcode - from address a - where street_address @@ to_tsquery(in_address) - and (a.borough_id = in_boro or in_boro is null) - and (a.zipcode = in_zip or in_zip is null) - - ; - - END; - -$_$; - - --- --- Name: get_building(character varying, bigint, integer, integer); Type: FUNCTION; Schema: public; Owner: - --- - -CREATE FUNCTION get_building(in_address character varying DEFAULT NULL::character varying, in_bbl bigint DEFAULT NULL::bigint, in_lot_id integer DEFAULT NULL::integer, in_building_id integer DEFAULT NULL::integer) RETURNS TABLE(building_id integer, address character varying, bbl bigint, borough character, zipcode numeric, lot_id integer, bin_id bigint) - LANGUAGE plpgsql - AS $_$ - - DECLARE - address_input ALIAS FOR $1; - bbl_input ALIAS FOR $2; - lot_id_input ALIAS FOR $3; - building_id_input ALIAS for $4; - - - BEGIN - SELECT replace(address_input,' ','&') into address_input; - - if ( address_input is null and bbl_input is null and lot_id_input is null and building_id_input is null) then - return QUERY - SELECT p.id as building_id, - a.street_line_1, - p.bbl, - l.boro_code, - a.zip::numeric(5,0), - p.lot_id, - p.bin - FROM public.building p - join public.lot l - on p.lot_id = l.id - join public.address a - on l.address_id = a.id - join kf_bbl_list k - on p.bbl = k.bbl -; - else - RETURN QUERY - SELECT p.id as building_id, - a.street_line_1, - p.bbl, - l.boro_code, - a.zip::numeric(5,0), - p.lot_id, - p.bin - FROM public.building p - join public.lot l - on p.lot_id = l.id - join public.address a - on l.address_id = a.id - WHERE (address_input is NULL - or textsearchable_address @@ to_tsquery(address_input) ) - AND (bbl_input is null - or p.bbl = bbl_input) - AND (lot_id_input is NULL - or p.lot_id = lot_id_input) - AND (building_id_input is NULL - or p.id = building_id_input) - ; - -END IF; - - END; -$_$; - - -SET default_tablespace = ''; - -SET default_with_oids = false; - --- --- Name: address; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE address ( - borough_id smallint, - house_number character varying(20), - house_number_sequence integer, - id integer NOT NULL, - load_date date DEFAULT now(), - pad_range_id integer, - street_name character varying(50), - state character(2), - zipcode numeric(5,0), - street_address tsvector -); - - --- --- Name: address_id_seq; Type: SEQUENCE; Schema: public; Owner: - --- - -CREATE SEQUENCE address_id_seq - START WITH 1 - INCREMENT BY 1 - NO MINVALUE - NO MAXVALUE - CACHE 1; - - --- --- Name: address_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - --- - -ALTER SEQUENCE address_id_seq OWNED BY address.id; - - --- --- Name: administrative_division; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE administrative_division ( - census_block_2010 smallint, - census_track_2010 numeric(6,2), - census_tract_2_2010 integer, - city_council_district smallint, - community_district smallint, - health_area integer, - id integer NOT NULL, - load_date timestamp without time zone, - school_district smallint, - special_purpose_district_1 character varying(10), - special_purpose_district_2 character varying(10), - zone_class_1 character varying(17), - zone_class_2 character varying(15), - zone_district_clssification_1 character varying(9), - zone_district_clssification_2 character varying(9), - zone_district_clssification_3 character varying(9), - zone_district_clssification_4 character varying(9) -); - - --- --- Name: borough; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE borough ( - id smallint NOT NULL, - description character varying(15) -); - - --- --- Name: building_id_seq; Type: SEQUENCE; Schema: public; Owner: - --- - -CREATE SEQUENCE building_id_seq - START WITH 1 - INCREMENT BY 1 - NO MINVALUE - NO MAXVALUE - CACHE 1; - - --- --- Name: building; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE building ( - basement_description_code smallint, - bbl bigint, - bin bigint, - building_dimentions_id integer, - class character(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, - load_date timestamp without time zone, - lot_id integer, - number_of_floors numeric(5,2), - proximity_code smallint, - year_altered smallint, - year_altered_previous smallint, - year_built smallint, - year_built_estimate character varying(5), - targeting_score float -); - - --- --- Name: building_address; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE building_address ( - building_id integer NOT NULL, - address_id integer NOT NULL, - pad_range_id integer, - house_number_sequence integer -); - - --- --- Name: building_address_bkup; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE building_address_bkup ( - building_id integer, - address_id integer, - pad_range_id integer, - house_number_sequence integer -); - - --- --- Name: building_dimentions; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE building_dimentions ( - area integer, - id integer NOT NULL, - load_date timestamp without time zone, - primary_structure_depth numeric(6,2), - primary_structure_frontage numeric(6,2) -); - - --- --- Name: city_service; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE city_service ( - fire_company character varying(4), - id integer NOT NULL, - load_date timestamp without time zone, - police_precinct smallint, - sanitation_boro smallint, - sanitation_district smallint, - sanitation_subsection character varying(2) -); - - --- --- Name: document; Type: FOREIGN TABLE; Schema: public; Owner: - --- - -CREATE FOREIGN TABLE document ( - id integer NOT NULL, - created timestamp without time zone, - updated timestamp without time zone, - content_type character varying(255), - key uuid NOT NULL, - name character varying(255) NOT NULL, - tags character varying(255), - building_id integer NOT NULL -) -SERVER fdw_documents -OPTIONS ( - schema_name 'public', - table_name 'document' -); -ALTER FOREIGN TABLE document ALTER COLUMN id OPTIONS ( - column_name 'id' -); -ALTER FOREIGN TABLE document ALTER COLUMN created OPTIONS ( - column_name 'created' -); -ALTER FOREIGN TABLE document ALTER COLUMN updated OPTIONS ( - column_name 'updated' -); -ALTER FOREIGN TABLE document ALTER COLUMN content_type OPTIONS ( - column_name 'content_type' -); -ALTER FOREIGN TABLE document ALTER COLUMN key OPTIONS ( - column_name 'key' -); -ALTER FOREIGN TABLE document ALTER COLUMN name OPTIONS ( - column_name 'name' -); -ALTER FOREIGN TABLE document ALTER COLUMN tags OPTIONS ( - column_name 'tags' -); -ALTER FOREIGN TABLE document ALTER COLUMN building_id OPTIONS ( - column_name 'building_id' -); - - --- --- Name: finance; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE finance ( - assessed_land_value bigint, - assessed_lot_value bigint, - exempt_land_value bigint, - exempt_total_value bigint, - id integer NOT NULL, - load_date timestamp without time zone -); - - --- --- Name: lot_id_seq; Type: SEQUENCE; Schema: public; Owner: - --- - -CREATE SEQUENCE lot_id_seq - START WITH 1 - INCREMENT BY 1 - NO MINVALUE - NO MAXVALUE - CACHE 1; - - --- --- Name: lot; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE lot ( - administrative_deivision_id integer, - appointment_bbl bigint, - appointment_date timestamp without time zone, - areasource smallint, - bbl bigint, - block smallint, - boro_code character(2), - boro_id smallint, - city_service_id integer, - commerical_overlay_code_1 character varying(9), - commerical_overlay_code_2 character varying(10), - dof_tax_map_number integer, - e_designation_number character varying(11), - extenstion_code character varying(3), - finance_id integer, - historical_district character varying(40), - id integer DEFAULT nextval('lot_id_seq'::regclass) NOT NULL, - irregular_shape_flag boolean, - landmark_name character varying(35), - land_use_category_code smallint, - limited_height_district_code character varying(5), - load_date timestamp without time zone, - lot_dimentions_id integer, - lot_number smallint, - lot_owner_id integer, - number_of_buildings smallint, - number_of_easements smallint, - pluto_id integer, - split_zone_flag boolean, - type smallint, - zone_map_border_flag character varying(5) -); - - --- --- Name: lot_address; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE lot_address ( - lot_id integer NOT NULL, - address_id integer NOT NULL -); - - --- --- Name: lot_dimentions; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE lot_dimentions ( - allocated_factory_area integer, - allocated_garage_area integer, - allocated_office_area integer, - allocated_other_area integer, - allocated_residential_area integer, - allocated_retail_use integer, - allocated_storage_area integer, - area bigint, - area_commercial_use integer, - depth numeric(6,2), - far_commerical numeric(4,2), - far_facility numeric(4,2), - far_residential numeric(4,2), - frotage numeric(6,2), - id integer NOT NULL, - load_date timestamp without time zone -); - - --- --- Name: lot_owner; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE lot_owner ( - id integer NOT NULL, - load_date timestamp without time zone, - owner_name character varying(21), - owner_type character(1) -); - - --- --- Name: pluto; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE pluto ( - dcp_map_number character varying(3), - id integer NOT NULL, - load_date timestamp without time zone, - pluto_map_id smallint, - pluto_version character varying(4), - sanborn_map_number character varying(8), - x_coord bigint, - y_coord bigint -); - - --- --- Name: vw_building_address; Type: MATERIALIZED VIEW; Schema: public; Owner: - --- - -CREATE MATERIALIZED VIEW vw_building_address AS - WITH address_mapping AS ( - SELECT DISTINCT b.bbl, - b.bin, - b.id AS building_id, - b.lot_id, - b.targeting_score, - 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)))) - ) - SELECT DISTINCT aa.building_id, - aa.lot_id, - aa.bbl, - aa.bin, - aa.targeting_score, - (( - 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, - lh.borough_id, - lh.zipcode, - lh.state, - aa.pad_range_id, - lh.id AS min_address_id, - hh.id AS max_address_id, - la.address_id AS lot_address_id - FROM (((address_mapping aa - JOIN address lh ON (((aa.pad_range_id = lh.pad_range_id) AND (aa.min_sequence = lh.house_number_sequence)))) - JOIN address hh ON (((aa.pad_range_id = hh.pad_range_id) AND (aa.max_sequence = hh.house_number_sequence)))) - LEFT JOIN lot_address la ON ((la.lot_id = aa.lot_id))) - ORDER BY aa.lot_id, aa.building_id - WITH NO DATA; - - --- --- Name: address id; Type: DEFAULT; Schema: public; Owner: - --- - -ALTER TABLE ONLY address ALTER COLUMN id SET DEFAULT nextval('address_id_seq'::regclass); - - --- --- Name: address address_pkey1; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY address - ADD CONSTRAINT address_pkey1 PRIMARY KEY (id); - - --- --- Name: administrative_division administrative_division_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY administrative_division - ADD CONSTRAINT administrative_division_pkey PRIMARY KEY (id); - - --- --- Name: borough borough_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY borough - ADD CONSTRAINT borough_pkey PRIMARY KEY (id); - - --- --- Name: building_address building_address_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY building_address - ADD CONSTRAINT building_address_pkey PRIMARY KEY (building_id, address_id); - - --- --- Name: building_dimentions building_dimentions_pk; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY building_dimentions - ADD CONSTRAINT building_dimentions_pk PRIMARY KEY (id); - - --- --- Name: building building_pk; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY building - ADD CONSTRAINT building_pk PRIMARY KEY (id); - - --- --- Name: city_service city_service_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY city_service - ADD CONSTRAINT city_service_pkey PRIMARY KEY (id); - - --- --- Name: finance finance_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY finance - ADD CONSTRAINT finance_pkey PRIMARY KEY (id); - - --- --- Name: lot_address lot_address_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot_address - ADD CONSTRAINT lot_address_pkey PRIMARY KEY (lot_id, address_id); - - --- --- Name: lot_dimentions lot_dimentions_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot_dimentions - ADD CONSTRAINT lot_dimentions_pkey PRIMARY KEY (id); - - --- --- Name: lot_owner lot_owner_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot_owner - ADD CONSTRAINT lot_owner_pkey PRIMARY KEY (id); - - --- --- Name: lot lot_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot - ADD CONSTRAINT lot_pkey PRIMARY KEY (id); - - --- --- Name: pluto pluto_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY pluto - ADD CONSTRAINT pluto_pkey PRIMARY KEY (id); - - --- --- Name: address__borough_id_IDX; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX "address__borough_id_IDX" ON address USING btree (borough_id); - - --- --- Name: address__bourough_id_idx; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX address__bourough_id_idx ON address USING btree (borough_id); - - --- --- Name: address__full_address_IDX; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX "address__full_address_IDX" ON address USING btree (btrim((((house_number)::text || ' '::text) || (street_name)::text))); - - --- --- Name: address__range_seq_IDX; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX "address__range_seq_IDX" ON address USING btree (pad_range_id, house_number_sequence); - - --- --- Name: address__street_address_idx; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX address__street_address_idx ON address USING gin (street_address); - - --- --- Name: address__zip_idx; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX address__zip_idx ON address USING btree (zipcode); - - --- --- Name: address__zipcode_IDX; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX "address__zipcode_IDX" ON address USING btree (zipcode); - - --- --- Name: building_bbl_bin_unique_IDX; Type: INDEX; Schema: public; Owner: - --- - -CREATE UNIQUE INDEX "building_bbl_bin_unique_IDX" ON building USING btree (bbl, bin); - - --- --- Name: lot__bbl_IDX; Type: INDEX; Schema: public; Owner: - --- - -CREATE UNIQUE INDEX "lot__bbl_IDX" ON lot USING btree (bbl); - - --- --- Name: lot__bbl_idx; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX lot__bbl_idx ON lot USING btree (bbl); - - --- --- Name: vw_building_address__bbl_idx; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX vw_building_address__bbl_idx ON vw_building_address USING btree (bbl); - - --- --- Name: vw_building_address__building_id_idx; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX vw_building_address__building_id_idx ON vw_building_address USING btree (building_id); - - --- --- Name: vw_building_address__pad_range_id_idx; Type: INDEX; Schema: public; Owner: - --- - -CREATE UNIQUE INDEX vw_building_address__pad_range_id_idx ON vw_building_address USING btree (pad_range_id); - - --- --- Name: building_address address__building_address_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY building_address - ADD CONSTRAINT address__building_address_fk FOREIGN KEY (address_id) REFERENCES address(id) ON DELETE SET NULL; - - --- --- Name: lot_address address__lot_address_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot_address - ADD CONSTRAINT address__lot_address_fk FOREIGN KEY (address_id) REFERENCES address(id); - - --- --- Name: lot administrative_division_lot_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot - ADD CONSTRAINT administrative_division_lot_fk FOREIGN KEY (administrative_deivision_id) REFERENCES administrative_division(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- Name: address borough__address_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY address - ADD CONSTRAINT borough__address_fk FOREIGN KEY (borough_id) REFERENCES borough(id); - - --- --- Name: building_address building__building_address_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY building_address - ADD CONSTRAINT building__building_address_fk FOREIGN KEY (building_id) REFERENCES building(id); - - --- --- Name: building building_dimentions_building_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY building - ADD CONSTRAINT building_dimentions_building_fk FOREIGN KEY (building_dimentions_id) REFERENCES building_dimentions(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- Name: building building_lot_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY building - ADD CONSTRAINT building_lot_fk FOREIGN KEY (lot_id) REFERENCES lot(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- Name: lot city_service_lot_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot - ADD CONSTRAINT city_service_lot_fk FOREIGN KEY (city_service_id) REFERENCES city_service(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- Name: lot finance_lot_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot - ADD CONSTRAINT finance_lot_fk FOREIGN KEY (finance_id) REFERENCES finance(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- Name: lot_address lot__lot_address_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot_address - ADD CONSTRAINT lot__lot_address_fk FOREIGN KEY (lot_id) REFERENCES lot(id); - - --- --- Name: lot lot_dimentions_lot_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot - ADD CONSTRAINT lot_dimentions_lot_fk FOREIGN KEY (lot_dimentions_id) REFERENCES lot_dimentions(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- Name: lot lot_owner_lot_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot - ADD CONSTRAINT lot_owner_lot_fk FOREIGN KEY (lot_owner_id) REFERENCES lot_owner(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- Name: lot pluto_lot_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot - ADD CONSTRAINT pluto_lot_fk FOREIGN KEY (pluto_id) REFERENCES pluto(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- PostgreSQL database dump complete --- - -- GitLab From a587360adc4937c85171e8bf79a816face3c18c7 Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Wed, 25 Mar 2020 11:38:16 -0700 Subject: [PATCH 09/24] Remove more redundant files. --- ..._building__public_pre_phase_3_snapshot.sql | 872 ------------------ 1 file changed, 872 deletions(-) delete mode 100644 Building/ddl/stg_building__public_pre_phase_3_snapshot.sql diff --git a/Building/ddl/stg_building__public_pre_phase_3_snapshot.sql b/Building/ddl/stg_building__public_pre_phase_3_snapshot.sql deleted file mode 100644 index 5fdd1c1..0000000 --- a/Building/ddl/stg_building__public_pre_phase_3_snapshot.sql +++ /dev/null @@ -1,872 +0,0 @@ --- --- PostgreSQL database dump --- - --- Dumped from database version 9.5.4 --- Dumped by pg_dump version 9.6.2 - -SET statement_timeout = 0; -SET lock_timeout = 0; -SET idle_in_transaction_session_timeout = 0; -SET client_encoding = 'UTF8'; -SET standard_conforming_strings = on; -SET check_function_bodies = false; -SET client_min_messages = warning; -SET row_security = off; - -SET search_path = public, pg_catalog; - --- --- Name: building_search(bigint, integer, character varying, numeric, integer); Type: FUNCTION; Schema: public; Owner: - --- - -CREATE FUNCTION building_search(in_bbl bigint DEFAULT NULL::bigint, in_building_id integer DEFAULT NULL::integer, in_address character varying DEFAULT NULL::character varying, in_zip numeric DEFAULT NULL::numeric, in_boro integer DEFAULT NULL::integer) RETURNS TABLE(building_id integer, lot_id integer, bbl bigint, bin bigint, street_address text, borough character varying, zipcode numeric, state character, targeting_score float) - LANGUAGE plpgsql - AS $_$ - - DECLARE - in_bbl ALIAS FOR $1; - in_building_id ALIAS for $2; - in_address ALIAS for $3; - in_zip ALIAS for $4; - in_boro ALIAS for $5; - - found_address_id integer default NULL; - found_lot_id integer default null; - - BEGIN - if ( (in_bbl is not null or in_building_id is not null) and in_address is null and in_zip is null and in_boro is null )then - 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 - from vw_building_address v - join borough b - on b.id = v.borough_id - where v.building_id = in_building_id - or v.bbl = in_bbl - ; - ELSIF (in_bbl is null and in_building_id is null and (in_address is not null or in_zip is not null or in_boro is not null) )THEN - select id - into found_address_id - from get_address(in_address, in_zip, in_boro) - ; - --raise notice 'outer else'; - Return QUERY --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 - from vw_building_address v - join borough b - on b.id = v.borough_id - WHERE v.pad_range_id in (select pad_range_id from get_address(in_address, in_zip, in_boro)) - or v.lot_address_id in (select id from get_address(in_address, in_zip, in_boro)) - ; - - END IF; - - - END; -$_$; - - --- --- Name: get_address(character varying, numeric, integer); Type: FUNCTION; Schema: public; Owner: - --- - -CREATE FUNCTION get_address(in_address character varying DEFAULT NULL::character varying, in_zip numeric DEFAULT NULL::numeric, in_boro integer DEFAULT NULL::integer) RETURNS TABLE(borough_id smallint, house_number character varying, house_number_sequence integer, id integer, pad_range_id integer, street_name character varying, state character, zipcode numeric) - LANGUAGE plpgsql - AS $_$ - - DECLARE - in_addres ALIAS FOR $1; - in_zip ALIAS for $2; - in_boro ALIAS FOR $3; - address_id integer; - - - BEGIN - SELECT replace(in_address,' ','&') into in_address; - - - return QUERY - select a.borough_id, - a.house_number, - a.house_number_sequence, - a.id, - a.pad_range_id, - a.street_name, - a.state, - a.zipcode - from address a - where street_address @@ to_tsquery(in_address) - and (a.borough_id = in_boro or in_boro is null) - and (a.zipcode = in_zip or in_zip is null) - - ; - - END; - -$_$; - - --- --- Name: get_building(character varying, bigint, integer, integer); Type: FUNCTION; Schema: public; Owner: - --- - -CREATE FUNCTION get_building(in_address character varying DEFAULT NULL::character varying, in_bbl bigint DEFAULT NULL::bigint, in_lot_id integer DEFAULT NULL::integer, in_building_id integer DEFAULT NULL::integer) RETURNS TABLE(building_id integer, address character varying, bbl bigint, borough character, zipcode numeric, lot_id integer, bin_id bigint) - LANGUAGE plpgsql - AS $_$ - - DECLARE - address_input ALIAS FOR $1; - bbl_input ALIAS FOR $2; - lot_id_input ALIAS FOR $3; - building_id_input ALIAS for $4; - - - BEGIN - SELECT replace(address_input,' ','&') into address_input; - - if ( address_input is null and bbl_input is null and lot_id_input is null and building_id_input is null) then - return QUERY - SELECT p.id as building_id, - a.street_line_1, - p.bbl, - l.boro_code, - a.zip::numeric(5,0), - p.lot_id, - p.bin - FROM public.building p - join public.lot l - on p.lot_id = l.id - join public.address a - on l.address_id = a.id - join kf_bbl_list k - on p.bbl = k.bbl -; - else - RETURN QUERY - SELECT p.id as building_id, - a.street_line_1, - p.bbl, - l.boro_code, - a.zip::numeric(5,0), - p.lot_id, - p.bin - FROM public.building p - join public.lot l - on p.lot_id = l.id - join public.address a - on l.address_id = a.id - WHERE (address_input is NULL - or textsearchable_address @@ to_tsquery(address_input) ) - AND (bbl_input is null - or p.bbl = bbl_input) - AND (lot_id_input is NULL - or p.lot_id = lot_id_input) - AND (building_id_input is NULL - or p.id = building_id_input) - ; - -END IF; - - END; -$_$; - - -SET default_tablespace = ''; - -SET default_with_oids = false; - --- --- Name: address; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE address ( - borough_id smallint, - house_number character varying(20), - house_number_sequence integer, - id integer NOT NULL, - load_date date DEFAULT now(), - pad_range_id integer, - street_name character varying(50), - state character(2), - zipcode numeric(5,0), - street_address tsvector -); - - --- --- Name: address_id_seq; Type: SEQUENCE; Schema: public; Owner: - --- - -CREATE SEQUENCE address_id_seq - START WITH 1 - INCREMENT BY 1 - NO MINVALUE - NO MAXVALUE - CACHE 1; - - --- --- Name: address_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - --- - -ALTER SEQUENCE address_id_seq OWNED BY address.id; - - --- --- Name: administrative_division; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE administrative_division ( - census_block_2010 smallint, - census_track_2010 numeric(6,2), - census_tract_2_2010 integer, - city_council_district smallint, - community_district smallint, - health_area integer, - id integer NOT NULL, - load_date timestamp without time zone, - school_district smallint, - special_purpose_district_1 character varying(10), - special_purpose_district_2 character varying(10), - zone_class_1 character varying(17), - zone_class_2 character varying(15), - zone_district_clssification_1 character varying(9), - zone_district_clssification_2 character varying(9), - zone_district_clssification_3 character varying(9), - zone_district_clssification_4 character varying(9) -); - - --- --- Name: borough; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE borough ( - id smallint NOT NULL, - description character varying(15) -); - - --- --- Name: building_id_seq; Type: SEQUENCE; Schema: public; Owner: - --- - -CREATE SEQUENCE building_id_seq - START WITH 1 - INCREMENT BY 1 - NO MINVALUE - NO MAXVALUE - CACHE 1; - - --- --- Name: building; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE building ( - basement_description_code smallint, - bbl bigint, - bin bigint, - building_dimentions_id integer, - class character(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, - load_date timestamp without time zone, - lot_id integer, - number_of_floors numeric(5,2), - proximity_code smallint, - year_altered smallint, - year_altered_previous smallint, - year_built smallint, - year_built_estimate character varying(5), - targeting_score float -); - - --- --- Name: building_address; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE building_address ( - building_id integer NOT NULL, - address_id integer NOT NULL, - pad_range_id integer, - house_number_sequence integer -); - - --- --- Name: building_address_bkup; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE building_address_bkup ( - building_id integer, - address_id integer, - pad_range_id integer, - house_number_sequence integer -); - - --- --- Name: building_dimentions; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE building_dimentions ( - area integer, - id integer NOT NULL, - load_date timestamp without time zone, - primary_structure_depth numeric(6,2), - primary_structure_frontage numeric(6,2) -); - - --- --- Name: city_service; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE city_service ( - fire_company character varying(4), - id integer NOT NULL, - load_date timestamp without time zone, - police_precinct smallint, - sanitation_boro smallint, - sanitation_district smallint, - sanitation_subsection character varying(2) -); - - --- --- Name: document; Type: FOREIGN TABLE; Schema: public; Owner: - --- - -CREATE FOREIGN TABLE document ( - id integer NOT NULL, - created timestamp without time zone, - updated timestamp without time zone, - content_type character varying(255), - key uuid NOT NULL, - name character varying(255) NOT NULL, - tags character varying(255), - building_id integer -) -SERVER fdw_documents -OPTIONS ( - schema_name 'public', - table_name 'document' -); -ALTER FOREIGN TABLE document ALTER COLUMN id OPTIONS ( - column_name 'id' -); -ALTER FOREIGN TABLE document ALTER COLUMN created OPTIONS ( - column_name 'created' -); -ALTER FOREIGN TABLE document ALTER COLUMN updated OPTIONS ( - column_name 'updated' -); -ALTER FOREIGN TABLE document ALTER COLUMN content_type OPTIONS ( - column_name 'content_type' -); -ALTER FOREIGN TABLE document ALTER COLUMN key OPTIONS ( - column_name 'key' -); -ALTER FOREIGN TABLE document ALTER COLUMN name OPTIONS ( - column_name 'name' -); -ALTER FOREIGN TABLE document ALTER COLUMN tags OPTIONS ( - column_name 'tags' -); -ALTER FOREIGN TABLE document ALTER COLUMN building_id OPTIONS ( - column_name 'building_id' -); - - --- --- Name: finance; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE finance ( - assessed_land_value bigint, - assessed_lot_value bigint, - exempt_land_value bigint, - exempt_total_value bigint, - id integer NOT NULL, - load_date timestamp without time zone -); - - --- --- Name: lot_id_seq; Type: SEQUENCE; Schema: public; Owner: - --- - -CREATE SEQUENCE lot_id_seq - START WITH 1 - INCREMENT BY 1 - NO MINVALUE - NO MAXVALUE - CACHE 1; - - --- --- Name: lot; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE lot ( - administrative_deivision_id integer, - appointment_bbl bigint, - appointment_date timestamp without time zone, - areasource smallint, - bbl bigint, - block smallint, - boro_code character(2), - boro_id smallint, - city_service_id integer, - commerical_overlay_code_1 character varying(9), - commerical_overlay_code_2 character varying(10), - dof_tax_map_number integer, - e_designation_number character varying(11), - extenstion_code character varying(3), - finance_id integer, - historical_district character varying(40), - id integer DEFAULT nextval('lot_id_seq'::regclass) NOT NULL, - irregular_shape_flag boolean, - landmark_name character varying(35), - land_use_category_code smallint, - limited_height_district_code character varying(5), - load_date timestamp without time zone, - lot_dimentions_id integer, - lot_number smallint, - lot_owner_id integer, - number_of_buildings smallint, - number_of_easements smallint, - pluto_id integer, - split_zone_flag boolean, - type smallint, - zone_map_border_flag character varying(5) -); - - --- --- Name: lot_address; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE lot_address ( - lot_id integer NOT NULL, - address_id integer NOT NULL -); - - --- --- Name: lot_dimentions; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE lot_dimentions ( - allocated_factory_area integer, - allocated_garage_area integer, - allocated_office_area integer, - allocated_other_area integer, - allocated_residential_area integer, - allocated_retail_use integer, - allocated_storage_area integer, - area bigint, - area_commercial_use integer, - depth numeric(6,2), - far_commerical numeric(4,2), - far_facility numeric(4,2), - far_residential numeric(4,2), - frotage numeric(6,2), - id integer NOT NULL, - load_date timestamp without time zone -); - - --- --- Name: lot_owner; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE lot_owner ( - id integer NOT NULL, - load_date timestamp without time zone, - owner_name character varying(21), - owner_type character(1) -); - - --- --- Name: pluto; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE pluto ( - dcp_map_number character varying(3), - id integer NOT NULL, - load_date timestamp without time zone, - pluto_map_id smallint, - pluto_version character varying(4), - sanborn_map_number character varying(8), - x_coord bigint, - y_coord bigint -); - - --- --- Name: vw_building_address; Type: MATERIALIZED VIEW; Schema: public; Owner: - --- - -CREATE MATERIALIZED VIEW vw_building_address AS - WITH address_mapping AS ( - SELECT DISTINCT b.bbl, - b.bin, - b.id AS building_id, - b.lot_id, - b.targeting_score, - 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)))) - ) - SELECT DISTINCT aa.building_id, - aa.lot_id, - aa.bbl, - aa.bin, - aa.targeting_score, - (( - 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, - lh.borough_id, - lh.zipcode, - lh.state, - aa.pad_range_id, - lh.id AS min_address_id, - hh.id AS max_address_id, - la.address_id AS lot_address_id - FROM (((address_mapping aa - JOIN address lh ON (((aa.pad_range_id = lh.pad_range_id) AND (aa.min_sequence = lh.house_number_sequence)))) - JOIN address hh ON (((aa.pad_range_id = hh.pad_range_id) AND (aa.max_sequence = hh.house_number_sequence)))) - LEFT JOIN lot_address la ON ((la.lot_id = aa.lot_id))) - ORDER BY aa.lot_id, aa.building_id - WITH NO DATA; - - --- --- Name: address id; Type: DEFAULT; Schema: public; Owner: - --- - -ALTER TABLE ONLY address ALTER COLUMN id SET DEFAULT nextval('address_id_seq'::regclass); - - --- --- Name: address address_pkey1; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY address - ADD CONSTRAINT address_pkey1 PRIMARY KEY (id); - - --- --- Name: administrative_division administrative_division_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY administrative_division - ADD CONSTRAINT administrative_division_pkey PRIMARY KEY (id); - - --- --- Name: borough borough_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY borough - ADD CONSTRAINT borough_pkey PRIMARY KEY (id); - - --- --- Name: building_address building_address_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY building_address - ADD CONSTRAINT building_address_pkey PRIMARY KEY (building_id, address_id); - - --- --- Name: building_dimentions building_dimentions_pk; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY building_dimentions - ADD CONSTRAINT building_dimentions_pk PRIMARY KEY (id); - - --- --- Name: building building_pk; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY building - ADD CONSTRAINT building_pk PRIMARY KEY (id); - - --- --- Name: city_service city_service_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY city_service - ADD CONSTRAINT city_service_pkey PRIMARY KEY (id); - - --- --- Name: finance finance_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY finance - ADD CONSTRAINT finance_pkey PRIMARY KEY (id); - - --- --- Name: lot_address lot_address_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot_address - ADD CONSTRAINT lot_address_pkey PRIMARY KEY (lot_id, address_id); - - --- --- Name: lot_dimentions lot_dimentions_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot_dimentions - ADD CONSTRAINT lot_dimentions_pkey PRIMARY KEY (id); - - --- --- Name: lot_owner lot_owner_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot_owner - ADD CONSTRAINT lot_owner_pkey PRIMARY KEY (id); - - --- --- Name: lot lot_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot - ADD CONSTRAINT lot_pkey PRIMARY KEY (id); - - --- --- Name: pluto pluto_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY pluto - ADD CONSTRAINT pluto_pkey PRIMARY KEY (id); - - --- --- Name: address__borough_id_IDX; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX "address__borough_id_IDX" ON address USING btree (borough_id); - - --- --- Name: address__full_address_IDX; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX "address__full_address_IDX" ON address USING btree (btrim((((house_number)::text || ' '::text) || (street_name)::text))); - - --- --- Name: address__range_seq_IDX; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX "address__range_seq_IDX" ON address USING btree (pad_range_id, house_number_sequence); - - --- --- Name: address__street_address_vector_idx; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX address__street_address_vector_idx ON address USING gin (street_address); - - --- --- Name: address__zipcode_IDX; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX "address__zipcode_IDX" ON address USING btree (zipcode); - - --- --- Name: building_bbl_bin_unique_IDX; Type: INDEX; Schema: public; Owner: - --- - -CREATE UNIQUE INDEX "building_bbl_bin_unique_IDX" ON building USING btree (bbl, bin); - - --- --- Name: lot__bbl_IDX; Type: INDEX; Schema: public; Owner: - --- - -CREATE UNIQUE INDEX "lot__bbl_IDX" ON lot USING btree (bbl); - - --- --- Name: lot__bbl_idx; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX lot__bbl_idx ON lot USING btree (bbl); - - --- --- Name: vw_building_address__bbl_idx; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX vw_building_address__bbl_idx ON vw_building_address USING btree (bbl); - - --- --- Name: vw_building_address__building_id_idx; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX vw_building_address__building_id_idx ON vw_building_address USING btree (building_id); - - --- --- Name: vw_building_address__pad_range_id_idx; Type: INDEX; Schema: public; Owner: - --- - -CREATE UNIQUE INDEX vw_building_address__pad_range_id_idx ON vw_building_address USING btree (pad_range_id); - - --- --- Name: building_address address__building_address_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY building_address - ADD CONSTRAINT address__building_address_fk FOREIGN KEY (address_id) REFERENCES address(id) ON DELETE SET NULL; - - --- --- Name: lot_address address__lot_address_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot_address - ADD CONSTRAINT address__lot_address_fk FOREIGN KEY (address_id) REFERENCES address(id); - - --- --- Name: lot administrative_division_lot_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot - ADD CONSTRAINT administrative_division_lot_fk FOREIGN KEY (administrative_deivision_id) REFERENCES administrative_division(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- Name: address borough__address_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY address - ADD CONSTRAINT borough__address_fk FOREIGN KEY (borough_id) REFERENCES borough(id); - - --- --- Name: building_address building__building_address_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY building_address - ADD CONSTRAINT building__building_address_fk FOREIGN KEY (building_id) REFERENCES building(id); - - --- --- Name: building building_dimentions_building_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY building - ADD CONSTRAINT building_dimentions_building_fk FOREIGN KEY (building_dimentions_id) REFERENCES building_dimentions(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- Name: building building_lot_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY building - ADD CONSTRAINT building_lot_fk FOREIGN KEY (lot_id) REFERENCES lot(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- Name: lot city_service_lot_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot - ADD CONSTRAINT city_service_lot_fk FOREIGN KEY (city_service_id) REFERENCES city_service(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- Name: lot finance_lot_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot - ADD CONSTRAINT finance_lot_fk FOREIGN KEY (finance_id) REFERENCES finance(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- Name: lot_address lot__lot_address_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot_address - ADD CONSTRAINT lot__lot_address_fk FOREIGN KEY (lot_id) REFERENCES lot(id); - - --- --- Name: lot lot_dimentions_lot_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot - ADD CONSTRAINT lot_dimentions_lot_fk FOREIGN KEY (lot_dimentions_id) REFERENCES lot_dimentions(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- Name: lot lot_owner_lot_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot - ADD CONSTRAINT lot_owner_lot_fk FOREIGN KEY (lot_owner_id) REFERENCES lot_owner(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- Name: lot pluto_lot_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot - ADD CONSTRAINT pluto_lot_fk FOREIGN KEY (pluto_id) REFERENCES pluto(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- PostgreSQL database dump complete --- - -- GitLab From 1881e53c68517dd8dda25d7f2bdccb74a62a1356 Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Wed, 25 Mar 2020 11:40:15 -0700 Subject: [PATCH 10/24] Add notes to foreign data wrapper. --- Building/ddl/fdw_building.sql | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Building/ddl/fdw_building.sql b/Building/ddl/fdw_building.sql index d3d45b7..dafb449 100644 --- a/Building/ddl/fdw_building.sql +++ b/Building/ddl/fdw_building.sql @@ -1,4 +1,6 @@ -CREATE SERVER fdw_builging +-- Create Foreign Data Wrapper to City Data Research DB +-- TODO: How are are we using fdw building? +CREATE SERVER fdw_builging FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'citydataresearch.czgvwxaefxfj.us-east-1.rds.amazonaws.com', port '5432', dbname 'building'); @@ -7,4 +9,4 @@ CREATE USER MAPPING FOR blocpower OPTIONS (user 'blocpower', password '8tQzq97N6Qxkqt5Q5DoC'); --replace with proper password IMPORT FOREIGN SCHEMA oltp - FROM SERVER fdw_builging INTO salesforce_cleanlead; \ No newline at end of file + FROM SERVER fdw_builging INTO salesforce_cleanlead; -- GitLab From debe91466f206f363f6625199f1e24148c562cd7 Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Thu, 26 Mar 2020 06:20:53 -0700 Subject: [PATCH 11/24] Remove architect file. --- Building/erd/OLTP Building ERD.architect | 1271 ---------------------- 1 file changed, 1271 deletions(-) delete mode 100644 Building/erd/OLTP Building ERD.architect diff --git a/Building/erd/OLTP Building ERD.architect b/Building/erd/OLTP Building ERD.architect deleted file mode 100644 index e07025d..0000000 --- a/Building/erd/OLTP Building ERD.architect +++ /dev/null @@ -1,1271 +0,0 @@ - - - OLTP BUuilding ERD - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-- GitLab From 30e236f1a5adc1f42379994513b17a7da1ac8ae2 Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Thu, 26 Mar 2020 12:40:46 -0700 Subject: [PATCH 12/24] Alter table in buildings. --- Building/ddl/new_building.sql | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Building/ddl/new_building.sql diff --git a/Building/ddl/new_building.sql b/Building/ddl/new_building.sql new file mode 100644 index 0000000..346f5e4 --- /dev/null +++ b/Building/ddl/new_building.sql @@ -0,0 +1,3 @@ +-- Updates to building to building table to add place name +ALTER TABLE public.building +ADD place_name varchar(255); -- GitLab From 7c177cfa2b25f5b20139045352ece11af5775a02 Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Thu, 26 Mar 2020 13:31:27 -0700 Subject: [PATCH 13/24] Add place_name to building address view(vw_building_address). --- Building/ddl/new_building.sql | 9 +++++++++ Building/ddl/vw_building_address.sql | 1 + 2 files changed, 10 insertions(+) diff --git a/Building/ddl/new_building.sql b/Building/ddl/new_building.sql index 346f5e4..2bc5769 100644 --- a/Building/ddl/new_building.sql +++ b/Building/ddl/new_building.sql @@ -1,3 +1,12 @@ -- Updates to building to building table to add place name ALTER TABLE public.building ADD place_name varchar(255); + +-- Update vw_building_address +-- making changes in vw_building_address.sql + +-- Insert place_name from BlocLink +-- TODO: Read from Bloclink and update value by BuildingID + +-- Create new functions +-- new functions in building_functions.sql diff --git a/Building/ddl/vw_building_address.sql b/Building/ddl/vw_building_address.sql index 59b303f..0f4d55a 100644 --- a/Building/ddl/vw_building_address.sql +++ b/Building/ddl/vw_building_address.sql @@ -5,6 +5,7 @@ create materialized view vw_building_address as b.id AS building_id, b.lot_id, b.targeting_score, + b.place_name, ba.pad_range_id, lh_1.min_sequence, lh_1.max_sequence -- GitLab From 55584e7aa503d446918d3b3c02fa1d83d0d1c7ef Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Thu, 26 Mar 2020 13:41:48 -0700 Subject: [PATCH 14/24] Add read building function that filters by building_id or place_name --- Building/ddl/building_functions.sql | 77 +++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 Building/ddl/building_functions.sql diff --git a/Building/ddl/building_functions.sql b/Building/ddl/building_functions.sql new file mode 100644 index 0000000..35217b8 --- /dev/null +++ b/Building/ddl/building_functions.sql @@ -0,0 +1,77 @@ +create function read_building( + in_building_id integer DEFAULT NULL::integer, + in_address 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_building_id ALIAS FOR $1; + in_address ALIAS for $2; + found_address_id integer default NULL; + found_lot_id integer default null; + + BEGIN + if ( (in_building_id is not null) and 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; +$$; + +alter function read_building(integer, varchar) + owner to blocpower; + +/* + +TEST CASES: read building + +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() limit 100 --selcting all buildings; + +*/ -- GitLab From 8d673a4f4ebf1b46d963ef0bfff5dcbff9ed21af Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Mon, 30 Mar 2020 13:04:55 -0700 Subject: [PATCH 15/24] Remove extra lines in get_address. --- Building/DAL/get_address.sql | 2 -- Building/ddl/new_building.sql | 5 ++++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Building/DAL/get_address.sql b/Building/DAL/get_address.sql index 679496a..b0c93fc 100644 --- a/Building/DAL/get_address.sql +++ b/Building/DAL/get_address.sql @@ -7,8 +7,6 @@ DECLARE in_zip ALIAS for $2; in_boro ALIAS FOR $3; address_id integer; - - BEGIN SELECT replace(in_address,' ','&') into in_address; diff --git a/Building/ddl/new_building.sql b/Building/ddl/new_building.sql index 2bc5769..ab4d9d8 100644 --- a/Building/ddl/new_building.sql +++ b/Building/ddl/new_building.sql @@ -9,4 +9,7 @@ ADD place_name varchar(255); -- TODO: Read from Bloclink and update value by BuildingID -- Create new functions --- new functions in building_functions.sql +-- new functions in building_functions.sql +-- read_building() +-- TODO: create_building() +-- TODO: update_building() -- GitLab From c1909d5e25be7de62e992e2819f4164ef1f3defa Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Mon, 30 Mar 2020 13:05:57 -0700 Subject: [PATCH 16/24] Change function to make is more readable. --- Building/ddl/vw_building_address.sql | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Building/ddl/vw_building_address.sql b/Building/ddl/vw_building_address.sql index 0f4d55a..424b57e 100644 --- a/Building/ddl/vw_building_address.sql +++ b/Building/ddl/vw_building_address.sql @@ -36,11 +36,11 @@ create materialized view vw_building_address as lh.id AS min_address_id, hh.id AS max_address_id, la.address_id AS lot_address_id - FROM (((address_mapping aa - JOIN address lh ON (((aa.pad_range_id = lh.pad_range_id) AND (aa.min_sequence = lh.house_number_sequence)))) - JOIN address hh ON (((aa.pad_range_id = hh.pad_range_id) AND (aa.max_sequence = hh.house_number_sequence)))) - LEFT JOIN lot_address la ON ((la.lot_id = aa.lot_id))) - ORDER BY aa.lot_id, aa.building_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))) + ORDER BY am.lot_id, am.building_id; alter materialized view vw_building_address owner to blocpower; -- GitLab From 4525066d18836a9a86bec09ae2fa3e407458f06c Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Mon, 30 Mar 2020 13:53:40 -0700 Subject: [PATCH 17/24] Add new read function. --- Building/ddl/building_functions.sql | 86 ++++++++++++++++++++++++++--- Building/ddl/new_building.sql | 19 ++++++- 2 files changed, 93 insertions(+), 12 deletions(-) diff --git a/Building/ddl/building_functions.sql b/Building/ddl/building_functions.sql index 35217b8..015a300 100644 --- a/Building/ddl/building_functions.sql +++ b/Building/ddl/building_functions.sql @@ -1,6 +1,6 @@ -create function read_building( - in_building_id integer DEFAULT NULL::integer, - in_address character varying DEFAULT NULL::character varying +-- TODO: create_building() +create function create_building( + in_place_name character varying DEFAULT NULL::character varying returns TABLE( building_id integer, lot_id integer, @@ -15,11 +15,7 @@ create function read_building( language plpgsql as $$ DECLARE - in_building_id ALIAS FOR $1; in_address ALIAS for $2; - found_address_id integer default NULL; - found_lot_id integer default null; - BEGIN if ( (in_building_id is not null) and in_place_name is null)then return QUERY -- Building ID or BBL Look up @@ -30,7 +26,7 @@ DECLARE v.street_address, b.description as borough, v.zipcode, - v.state, + v.state, v.targeting_score v.place_name from vw_building_address v -- from vw_building_address join on Borough to Borough @@ -55,7 +51,77 @@ DECLARE 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 IF; + END; +$$; + +alter function read_building(integer, varchar) + owner to blocpower; +/* + +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'); +*/ +-- read_building() +create function read_building( + in_building_id integer DEFAULT NULL::integer, + in_address 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_building_id ALIAS FOR $1; + 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 + 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; $$; @@ -75,3 +141,5 @@ select * from read_building(in_place_name := '105 CENTRAL AVENUE'); --using name select * from read_building() limit 100 --selcting all buildings; */ + +-- TODO: update_building() diff --git a/Building/ddl/new_building.sql b/Building/ddl/new_building.sql index ab4d9d8..ebd1744 100644 --- a/Building/ddl/new_building.sql +++ b/Building/ddl/new_building.sql @@ -1,12 +1,25 @@ -- Updates to building to building table to add place name -ALTER TABLE public.building +ALTER TABLE public.building_address ADD place_name varchar(255); --- Update vw_building_address +-- Update vw_building_address to use new column in building_address -- making changes in vw_building_address.sql +-- Copy Mapbox_building table from BlocLink +-- pg_dump -t table_to_copy source_db | psql target_db + -- Insert place_name from BlocLink --- TODO: Read from Bloclink and update value by BuildingID +-- Read from Bloclink MapBox Building and update value by BuildingID +-- Cases: +/* + +*/ +UPDATE building_address +SET place_name = mapbox_buildings.place_name, +FROM + mapbox_buildings +WHERE + building_address.building_id = mapbox_buildings.building_id; -- Create new functions -- new functions in building_functions.sql -- GitLab From 0ad9cfdfdef2605dfef52d112d92609f03fd82ef Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Mon, 30 Mar 2020 14:13:29 -0700 Subject: [PATCH 18/24] Update vw_building_address to use fetch place name. --- Building/ddl/vw_building_address.sql | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Building/ddl/vw_building_address.sql b/Building/ddl/vw_building_address.sql index 424b57e..822383c 100644 --- a/Building/ddl/vw_building_address.sql +++ b/Building/ddl/vw_building_address.sql @@ -5,7 +5,7 @@ create materialized view vw_building_address as b.id AS building_id, b.lot_id, b.targeting_score, - b.place_name, + ba.place_name, ba.pad_range_id, lh_1.min_sequence, lh_1.max_sequence @@ -19,20 +19,21 @@ create materialized view vw_building_address as 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 aa.building_id, - aa.lot_id, - aa.bbl, - aa.bin, - aa.targeting_score, + SELECT DISTINCT am.building_id, + am.lot_id, + am.bbl, + am.bin, + am.targeting_score, + am.place_name, (( 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, + END || ' ' :: text) || (lh.street_name) :: text) AS street_address, -- Generate Address range from sequence lh.borough_id, lh.zipcode, lh.state, - aa.pad_range_id, + am.pad_range_id, lh.id AS min_address_id, hh.id AS max_address_id, la.address_id AS lot_address_id -- GitLab From 671971b649fe8dd51477857f1440703d7c9c6abc Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Mon, 30 Mar 2020 16:08:09 -0700 Subject: [PATCH 19/24] Update read function. --- 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 015a300..5ccde4d 100644 --- a/Building/ddl/building_functions.sql +++ b/Building/ddl/building_functions.sql @@ -69,7 +69,7 @@ select * from create_building(in_place_name := '936 East 17th Street, New York, -- read_building() create function read_building( in_building_id integer DEFAULT NULL::integer, - in_address character varying DEFAULT NULL::character varying) + in_place_name character varying DEFAULT NULL::character varying) returns TABLE( building_id integer, lot_id integer, -- GitLab From 40b94c1b7744b2fc9e538e3ce03166a22bc8c3ec Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Mon, 30 Mar 2020 16:09:03 -0700 Subject: [PATCH 20/24] Add additional intructions for new building model.: --- Building/ddl/new_building.sql | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/Building/ddl/new_building.sql b/Building/ddl/new_building.sql index ebd1744..874b71e 100644 --- a/Building/ddl/new_building.sql +++ b/Building/ddl/new_building.sql @@ -2,24 +2,22 @@ ALTER TABLE public.building_address ADD place_name varchar(255); --- Update vw_building_address to use new column in building_address --- making changes in vw_building_address.sql +-- Update vw_building_address to read buildings using new column in building_address +-- changes in vw_building_address.sql --- Copy Mapbox_building table from BlocLink --- pg_dump -t table_to_copy source_db | psql target_db +-- copy Mapbox_building table from BlocLink and put in bis.mapbox_building -- Insert place_name from BlocLink -- Read from Bloclink MapBox Building and update value by BuildingID --- Cases: -/* - -*/ -UPDATE building_address -SET place_name = mapbox_buildings.place_name, +UPDATE public.building_address +SET place_name = mb.mapbox_address FROM - mapbox_buildings + bis.mapbox_building mb WHERE - building_address.building_id = mapbox_buildings.building_id; + building_address.building_id = mb.building_id; + +-- refresh materialized view +REFRESH MATERIALIZED VIEW vw_building_address; -- Create new functions -- new functions in building_functions.sql -- GitLab From 610294050fd40fe5a077f1d079cec4bcf69a9089 Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Mon, 30 Mar 2020 16:37:00 -0700 Subject: [PATCH 21/24] Add commmented section for unique index. --- Building/ddl/vw_building_address.sql | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Building/ddl/vw_building_address.sql b/Building/ddl/vw_building_address.sql index 822383c..5b0a087 100644 --- a/Building/ddl/vw_building_address.sql +++ b/Building/ddl/vw_building_address.sql @@ -43,6 +43,9 @@ create materialized view vw_building_address as LEFT JOIN lot_address la ON ((la.lot_id = am.lot_id))) ORDER BY am.lot_id, am.building_id; +-- CREATE UNIQUE INDEX sales_summary_seller -- Add index for concurrent refresh view +-- ON vw_building_address (lot_id, building_id, place_name); + alter materialized view vw_building_address owner to blocpower; -- GitLab From 0d0d664764a954a544f65a2111e1ba8c18697554 Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Mon, 30 Mar 2020 16:39:01 -0700 Subject: [PATCH 22/24] Change parameter for insert. --- Building/ddl/building_functions.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Building/ddl/building_functions.sql b/Building/ddl/building_functions.sql index 5ccde4d..56fc908 100644 --- a/Building/ddl/building_functions.sql +++ b/Building/ddl/building_functions.sql @@ -15,9 +15,9 @@ create function create_building( language plpgsql as $$ DECLARE - in_address ALIAS for $2; + in_place_name ALIAS for $1; BEGIN - if ( (in_building_id is not null) and in_place_name is null)then + if (in_place_name is null)then return QUERY -- Building ID or BBL Look up select v.building_id, v.lot_id, -- GitLab From e9606b930e1d172dc43779202a177169681c752d Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Mon, 30 Mar 2020 16:39:56 -0700 Subject: [PATCH 23/24] Comment out inserting into building_address since it's not working yet. --- Building/ddl/building_functions.sql | 114 ++++++++++++++-------------- 1 file changed, 57 insertions(+), 57 deletions(-) diff --git a/Building/ddl/building_functions.sql b/Building/ddl/building_functions.sql index 56fc908..cc04947 100644 --- a/Building/ddl/building_functions.sql +++ b/Building/ddl/building_functions.sql @@ -1,62 +1,62 @@ -- 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, +-- 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; +-- $$; -alter function read_building(integer, varchar) - owner to blocpower; +-- alter function read_building(integer, varchar) +-- owner to blocpower; /* TEST CASES: create building -- GitLab From d872c0dd1fe78a6260663965af566186d995e47b Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Wed, 1 Apr 2020 11:52:00 -0700 Subject: [PATCH 24/24] Add comments for copying Mapbox address and building mapping from Bloclink.: --- Building/ddl/new_building.sql | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Building/ddl/new_building.sql b/Building/ddl/new_building.sql index 874b71e..86ee69b 100644 --- a/Building/ddl/new_building.sql +++ b/Building/ddl/new_building.sql @@ -2,11 +2,12 @@ ALTER TABLE public.building_address ADD place_name varchar(255); +-- copy Mapbox_building table from BlocLink and put in bis.mapbox_building +-- dump mapbox_building + -- Update vw_building_address to read buildings using new column in building_address -- changes in vw_building_address.sql --- copy Mapbox_building table from BlocLink and put in bis.mapbox_building - -- Insert place_name from BlocLink -- Read from Bloclink MapBox Building and update value by BuildingID UPDATE public.building_address -- GitLab