Commit 3d2b7d43 authored by Eduardo L. Buratti's avatar Eduardo L. Buratti

database: Add load function for search_school table

Signed-off-by: Eduardo L. Buratti's avatarEduardo L. Buratti <elb09@c3sl.ufpr.br>
parent 45653fbf
CREATE OR REPLACE FUNCTION populate_search_school() returns void as $$
DECLARE
start_ts TIMESTAMP;
total_rows INTEGER;
cur_rows INTEGER;
BEGIN
-- getting initial timestamp
start_ts = CLOCK_TIMESTAMP();
total_rows := 0;
-- remove old data
TRUNCATE TABLE search_school;
INSERT INTO search_school (sch_id, cit_id, ts_vector)
SELECT
sch.id,
cit.id,
setweight(to_tsvector('portuguese', COALESCE(sch.name, '')), 'A')
|| setweight(to_tsvector('portuguese', COALESCE(cit.name, '')), 'B')
FROM dim_school sch, dim_city cit
WHERE cit.id = sch.cit_id;
-- accumulate total rows affected
GET DIAGNOSTICS cur_rows := ROW_COUNT;
total_rows := total_rows + cur_rows;
UPDATE search_school SET contacted_once = true
FROM (SELECT DISTINCT(sch_id) FROM fact_contact) contact
WHERE search_school.sch_id = contact.sch_id;
GET DIAGNOSTICS cur_rows := ROW_COUNT;
total_rows := total_rows + cur_rows;
-- logging
INSERT INTO control (name, start_time, end_time, total)
VALUES ('populate search_school', start_ts, CLOCK_TIMESTAMP(), total_rows);
END;
$$ language plpgsql;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment