Advertisement
pveselov

agrio merge_all test

Jan 7th, 2015
598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. CREATE OR REPLACE FUNCTION public.merge_all_test2()
  2.  RETURNS void
  3.  LANGUAGE plpgsql
  4. AS $function$
  5.   declare
  6.     agrow RECORD;
  7.     agrid numeric;
  8.     ver numeric;
  9.  
  10.     missing_agrio CURSOR FOR select * from R_AGRIO_test
  11.       where main_id is null
  12.       order by tagid, blockid, rowdate, device_type, placement;
  13.  
  14.     missing_brkiofaults CURSOR FOR select * from R_BRKIOFAULTS_test
  15.       where main_brk_id is null;
  16.  
  17. BEGIN
  18.  
  19.   with pivot as (
  20.     select
  21.       agr.id as node_id,
  22.       make_r_code(agr.tagid, agr.blockid) as code,
  23.       get_r_lock(make_r_code(agr.tagid, agr.blockid), false) as locked_ver,
  24.       main_agr.id as main_id
  25.     from R_AGRIO_test agr
  26.     left join R_AGRIO main_agr
  27.       -- all of the key columns must be here!
  28.       using ( tagid, blockid, rowdate, device_type, placement )
  29.   )
  30.   update R_AGRIO_test
  31.   set
  32.     locked_version = pivot.locked_ver,
  33.     main_id = pivot.main_id,
  34.     lock_code = pivot.code
  35.   from pivot
  36.   where
  37.     R_AGRIO_test.id = pivot.node_id;
  38.  
  39.   select count(id) as c from R_AGRIO_test where locked_version <= 0 into agrow;
  40.   if agrow.c > 0 then
  41.     -- $TODO: we used to be able to respond with the code, and ver that
  42.     -- lead to a failure, but now it's more complicated to, but possible,
  43.     -- and should be done if this becomes a problem.
  44.     raise exception USING ERRCODE = 'check_violation',
  45.       MESSAGE = 'Can not obtain lock for ' || agrow.c || ' entries from r_agrio';
  46.   end if;
  47.  
  48.   -- all entries that have version less than the locked_version can be
  49.   -- removed now (we used to ignore them, but now it's just easier to delete)
  50.   -- note that the expectation that all dependent brk entries will be deleted
  51.   -- by cascade
  52.   delete from R_AGRIO_test where version < locked_version;
  53.  
  54.   -- now, for all the entries that weren't found in the join with main r_agrio
  55.   -- table, we need to create a new entry there.
  56.   for agrow in missing_agrio loop
  57.  
  58.     -- this is a bit tricky. We want to just establish the new main R_AGRIO
  59.     -- entries, and then merge into them later, for all of the data set, so
  60.     -- we use proper key values, but 0 data values.
  61.     select merge_agrio(
  62.       null::numeric,
  63.       agrow.tagid,
  64.       agrow.blockid,
  65.       agrow.rowdate,
  66.       0, 0, 0, 0,
  67.       agrow.device_type,
  68.       agrow.placement,
  69.       0, 0) into agrid;
  70.  
  71.     update R_AGRIO_test set main_id = agrid where current of missing_agrio;
  72.  
  73.   end loop;
  74.  
  75.   -- now, let's merge all r_agrio data in bulk!
  76.  
  77.   with n as (
  78.     select * from R_AGRIO_test
  79.     order by tagid, blockid, rowdate, device_type, placement
  80.   )
  81.   update r_agrio a set
  82.     count = a.count + n.count,
  83.     events = a.events + n.events,
  84.     devents = a.devents + n.devents,
  85.     duration = a.duration + n.duration,
  86.     unserved = a.unserved + n.unserved,
  87.     unconfirmed = a.unconfirmed + n.unconfirmed
  88.   from n
  89.   where a.id = n.main_id;
  90.  
  91.   -- r_agrio data merged, move to sattelite tables now.
  92.   -- it's the same deal now:
  93.   -- 1. Merge the node table with main table to find which
  94.   --    primary keys to update.
  95.   -- 2. Create missing records, if any
  96.   -- 3. do the actual merge.
  97.   -- Note, that there is no order-by for sattelite tables,
  98.   -- we already would have locked R_AGRIO entry if there is any update.
  99.  
  100.   -- R_BRKIOFAULTS
  101.  
  102.   -- main_id : main R_AGRIO.id
  103.   -- sat_id : main R_BRKxxx.id
  104.   -- node_id : node R_BRKxxx.id
  105.   with pivot as (
  106.     select
  107.       brk.agrioid as main_id,
  108.       brk.id as sat_id,
  109.       refed.node_id as node_id
  110.     from (
  111.       select
  112.         b.id as node_id,
  113.         a.main_id as agrioid,
  114.         b.blockid as blockid,
  115.         b.name as name,
  116.         b.type as type,
  117.         coalesce(b.cname,'') as cname,
  118.         b.typeid as typeid
  119.       from R_BRKIOFAULTS_test b
  120.       join R_AGRIO_test a
  121.         on a.id = b.agrioid
  122.     ) refed
  123.     left join R_BRKIOFAULTS brk
  124.       on
  125.         refed.agrioid = brk.agrioid and
  126.         refed.blockid = brk.blockid and
  127.         refed.name = brk.name and
  128.         refed.type = brk.type and
  129.         refed.cname = coalesce(brk.cname, '') and
  130.         refed.typeid = brk.typeid
  131.   )
  132.   update R_BRKIOFAULTS_test
  133.   set
  134.     main_brk_id = pivot.sat_id,
  135.     main_id = pivot.main_id
  136.   from pivot
  137.   where
  138.     R_BRKIOFAULTS_test.id = pivot.node_id;
  139.  
  140.   for agrow in missing_brkiofaults loop
  141.     select merge_brkiofaults(
  142.         agrow.main_id,
  143.         agrow.blockid,
  144.         agrow.name,
  145.         0,
  146.         agrow.cname,
  147.         agrow.typeid,
  148.         0, 0,
  149.         agrow.auto
  150.       ) into agrid;
  151.  
  152.     update R_BRKIOFAULTS_test set main_brk_id = agrid
  153.       where current of missing_brkiofaults;
  154.  
  155.   end loop;
  156.  
  157.   with n as (
  158.     select * from R_BRKIOFAULTS_test
  159.   )
  160.   update R_BRKIOFAULTS a set
  161.     count = a.count + n.count,
  162.     dcount = a.dcount + n.dcount,
  163.     type = n.type
  164.   from n
  165.   where a.id = n.main_brk_id;
  166.  
  167.   delete from R_BRKIOFAULTS_test;
  168.   delete from R_AGRIO_test;
  169.  
  170. END;
  171. $function$
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement