Skip to content

Commit

Permalink
Merge pull request #26 from MikeNeilson/locations_view_table
Browse files Browse the repository at this point in the history
Locations view table
  • Loading branch information
MikeNeilson authored May 26, 2021
2 parents 96f4e18 + 284cdfb commit 19b2b2f
Show file tree
Hide file tree
Showing 23 changed files with 491 additions and 101 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ allprojects {
apply plugin: 'java'


version = '0.1.2'
version = '0.1.3'
group = 'net.hobbyscience.housedb'

repositories {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package db.migration.units;
package db.migration.java.units;

import java.io.BufferedReader;
import java.io.InputStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,19 @@ BEGIN
foreach cur_level in ARRAY parts
loop
-- search for existing object at this level
--raise info 'Search for (%,%)', cur_level,the_parent_id;
select into the_id,found_parent_id id,parent_id from locations where name=cur_level and (parent_id = the_parent_id); -- or parent_id is null);
raise info 'Search for (%,%)', cur_level,the_parent_id;
select
into the_id,found_parent_id id,parent_id
from locations
where
lower(name)=lower(cur_level)
and (
(parent_id = the_parent_id)
or
(parent_id is null and the_parent_id is null)
);
if the_id is NULL THEN
--raise notice 'insert new value';
raise notice 'insert new value';
--raise notice '%',found_parent_id ;
insert into locations(name,parent_id) values (cur_level,the_parent_id) returning id into the_id;
the_parent_id = the_id;
Expand All @@ -65,4 +74,51 @@ BEGIN
end loop;
RETURN the_id;
END;
$$ LANGUAGE plpgsql;
$$ LANGUAGE plpgsql;


create or replace function housedb_timeseries.insert_location()
returns trigger
as $$
declare
loc_info housedb.timeseries%rowtype;
l_loc_id bigint;
l_parent_id bigint;
loc_name text;
begin
set search_path to housedb_locations,housedb_units,housedb,public;
if TG_OP = 'DELETE' then
raise notice 'deleting %', OLD;
return OLD;
else
--raise notice 'Inserting or updating value %', NEW;
if NEW.id is not null and NEW.name is not null THEN
raise exception 'Specify only timeseries_id or name, not both' using ERRCODE = 'ZX082';
end if;

if TG_OP = 'UPDATE' then
perform 'housedb_security.can_perform(housedb_security.get_session_user(),''UPDATE'',''timeseries'',ts_name)';
raise exception 'Update not supported at this time';
else -- INSERT
perform 'housedb_security.can_perform(housedb_security.get_session_user(),''CREATE'',''timeseries'',ts_name)';
select housedb_locations.create_location(NEW.name) into l_loc_id;
select into l_parent_id parent_id from housedb.view_locations where lower(name) = lower(NEW.name);
NEW.id = l_loc_id;
NEW.parent_id = l_parent_id;
update locations
set latitude = NEW.latitude,
longitude = NEW.longitude,
horizontal_datum = NEW.horizontal_datum,
elevation = NEW.elevation,
vertical_datum = NEW.vertical_datum
where id = NEW.id;
end if ;


end if;

return new;
end;
$$ language plpgsql;


Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
drop index housedb.location_names_lower;

create unique index location_names_lower_no_parent on housedb.locations( lower(name) ) where parent_id is null;
create unique index location_names_lower_has_parent on housedb.locations( lower(name), parent_id ) where parent_id is not null;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
alter table housedb.locations add column latitude double precision;
alter table housedb.locations add column longitude double precision;
alter table housedb.locations add column horizontal_datum varchar(50);
alter table housedb.locations add column elevation double precision;
alter table housedb.locations add column vertical_datum varchar(50);
20 changes: 20 additions & 0 deletions database/src/main/resources/db/migration/Views/R__locations.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
create or replace view housedb.view_locations as
select id,
housedb_locations.expand_location_name(
id
) AS name,
parent_id,
housedb_locations.expand_location_name(
parent_id
) AS parent,
latitude,
longitude,
horizontal_datum,
elevation,
vertical_datum
from housedb.locations
;

drop trigger if exists insert_location_trigger on housedb.view_locations;
create trigger insert_location_trigger instead of insert or update or delete on housedb.view_locations
for each row execute procedure housedb_timeseries.insert_location();
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.junit.jupiter.api.Test;

import db.migration.units.R__unit_conversions;
import db.migration.java.units.R__unit_conversions;
import net.hobbyscience.SimpleInfixCalculator;
import net.hobbyscience.database.Conversion;
import net.hobbyscience.database.Unit;
Expand Down
37 changes: 37 additions & 0 deletions database/src/test/sql/location_test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,43 @@ END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION housedb_tests.test_create_with_sub_doesnt_create_duplicate()
RETURNS SETOF TEXT AS $$
DECLARE
base_name text = 'Test';
full_loc text = 'Test-Sub Location';
n_rows int;
BEGIN
perform housedb_security.add_permission('guest', 'CREATE', 'locations','.*');

perform housedb_locations.create_location(base_name);
select into n_rows count(*) from housedb.locations;
RETURN NEXT ok( n_rows = 1, 'Should only have 1 row');

perform housedb_locations.create_location(full_loc);
select into n_rows count(*) from housedb.locations;
RETURN NEXT ok( n_rows = 2, 'Should only have 2 rows, duplicate row created');

END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION housedb_tests.test_insert_new_locations_into_view()
RETURNS SETOF TEXT AS $$
DECLARE
n_rows int;
BEGIN
perform housedb_security.add_permission('guest', 'CREATE', 'locations','.*');

insert into
housedb.view_locations(name,latitude,longitude,horizontal_datum)
values ('Test',0,3,'RASTER');
select into n_rows count(*) from housedb.locations;
RETURN NEXT ok( n_rows = 1, 'There should be one row');

END;
$$ LANGUAGE plpgsql;





Expand Down
25 changes: 0 additions & 25 deletions housedb-dao/.classpath

This file was deleted.

23 changes: 23 additions & 0 deletions housedb-dao/src/main/java/net/hobbyscience/housedb/dao/Dao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package net.hobbyscience.housedb.dao;

import java.util.List;
import java.util.Optional;

import org.jooq.DSLContext;

public abstract class Dao<T> {

@SuppressWarnings("unused")
protected DSLContext dsl = null;

public Dao(DSLContext dsl){
this.dsl = dsl;
}

public abstract List<T> getAll();
public abstract Optional<T> getByUniqueName(String uniqueName);

public abstract void update(T modified);
public abstract void save(T newObj);

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import java.util.ArrayList;

import static net.hobbyscience.housedb.housedb.Routines.*;

import net.hobbyscience.housedb.dto.Location;
import net.hobbyscience.housedb.dto.TimeSeries;
import net.hobbyscience.housedb.housedb.tables.*;

import static net.hobbyscience.housedb.housedb.tables.TimeseriesValues.*;
Expand All @@ -17,6 +20,7 @@
import static net.hobbyscience.housedb.housedb.tables.Parameters.*;
import static net.hobbyscience.housedb.housedb.tables.Intervals.*;
import net.hobbyscience.housedb.housedb_security.Routines;
import net.hobbyscience.housedb.types.DataTriple;

import static org.jooq.impl.DSL.*;

Expand Down Expand Up @@ -53,6 +57,10 @@ public HouseDb setUsername(String username){
return this;
}

public LocationsDao locationDao(){
return new LocationsDao(dsl);
}

public List<String> getAllLocations() throws Exception{
ArrayList<String> locations = new ArrayList<>();

Expand Down

This file was deleted.

Loading

0 comments on commit 19b2b2f

Please sign in to comment.