Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.57 KB | None | 0 0
  1. /****************************************************************
  2.  
  3. Wutirkai Pornchai
  4. 14-09-2014
  5.  
  6. *****************************************************************/
  7. /******************************************
  8. View Procedures
  9. *******************************************/
  10. -- * Data selected for underlying user
  11. drop function if exists "dbLegal".tbLGStepProItemVW_UserData ( in UserID int , in AccID int
  12. , in _pid int
  13. ) cascade;
  14. create or replace FUNCTION "dbLegal".tbLGStepProItemVW_UserData ( in UserID int , in AccID int
  15. , in _pid int = null )
  16. returns table
  17. ( id int
  18. ,Title varchar -- (50) not null, -- comment 'ขั้นตอนดำเนินคดี',
  19. ,Description text --null, -- comment 'รายละเอียด',
  20. ,Duration smallint -- null, -- comment 'ระยะเวลา- วัน',
  21. ,SuccStat varchar-- (20) null, -- comment 'สถานะของงานหรือคดีหากขั้นตอนนี้สำเร็จ',
  22. ,FailStat varchar-- (20) null, -- comment 'สถานะของงานหรือคดีหากขั้นตอนนี้ไม่สำเร็จ',
  23. ,CaseStat text
  24. ,PID int -- not null references "dbLegal".tblgsteppro(ID) on delete cascade on update cascade
  25.  
  26. ,dis smallint
  27. ,fqty smallint
  28. ,idgrp int
  29. ,grp text
  30. ,path int[]
  31. ,orderno smallint
  32. ,Selectable boolean
  33. ,StartActive boolean
  34. ,processid int
  35. ,process_name text
  36. ,Description_require boolean
  37. ,uid int
  38. ,SysStep text
  39. )
  40. AS $$
  41.  
  42. select
  43. a.id
  44. --,a.Title -- 'ขั้นตอนดำเนินคดี',
  45. ,case
  46. when a.processid is null then a.title
  47. else format('%s %s',c.no, c.title)
  48. end
  49. --,a.Description
  50. ,case
  51. when a.description is null then c.description
  52. else a.description
  53. end
  54. ,a.Duration --smallint -- null, -- comment 'ระยะเวลา- วัน',
  55. ,a.SuccStat --varchar-- (20) null, -- comment 'สถานะของงานหรือคดีหากขั้นตอนนี้สำเร็จ',
  56. ,a.FailStat --varchar-- (20) null, -- comment 'สถานะของงานหรือคดีหากขั้นตอนนี้ไม่สำเร็จ',
  57. ,a.CaseStat
  58. ,a.PID --int -- not null references "dbLegal".tblgsteppro(ID) on delete cascade on update cascade
  59. ,a.dis
  60. ,a.fqty
  61. ,a.idgrp
  62. ,b.title
  63. ,z.path
  64. ,a.orderno
  65. ,a.Selectable
  66. ,a.StartActive
  67. ,a.processid
  68. ,c.title
  69. ,a.Description_require
  70. ,a.uid
  71. ,a.SysStep
  72.  
  73. from "dbLegal".tbLGStepProItem a
  74. -- right join "dbSys".grpGetData( userid, accid , '"dbLegal".tbLGStepProItem',path_only:=true, uid_child:=true ) z on (z.id = a.id)
  75. -- join "dbSys".grpGetData( userid, accid , '"dbLegal".tbLGStepProItem',path_only:=true, _pid := pid ) z on (z.id = a.id)
  76. right join "dbSys".grpGetData( userid, accid , '"dbLegal".tbLGStepProItem'
  77. , path_only:=true
  78. , uid_child :=true
  79. , _pid := _pid -- case when pid is not null then pid else null end
  80. ) z on (z.id = a.id)
  81.  
  82. left join "dbLegal".tbLGStepProItem b on (b.id = a.idgrp )
  83. left join "dbLegal".tblgsteppro c on (c.id = a.processid )
  84. where
  85. case
  86. when _pid is null then true
  87. else a.pid = _pid
  88. end
  89. order by z.path
  90. ;
  91.  
  92. $$ LANGUAGE sql;
  93.  
  94. -- * How to update underlying tables whose data is projected to this view
  95. drop function if exists "dbLegal".tbLGStepProItemVW_UpdData() cascade;
  96. create or replace function "dbLegal".tbLGStepProItemVW_UpdData()
  97. returns trigger as $$
  98. declare Grp varchar; GrpID int; Change boolean; sql text; lastid int;
  99. rec record;
  100. begin
  101. if (TG_OP = 'INSERT') then
  102. insert into "dbLegal".tbLGStepProItem (
  103. Title -- varchar -- (50) not null, -- comment 'ขั้นตอนดำเนินคดี',
  104. ,Description --text --null, -- comment 'รายละเอียด',
  105. ,Duration -- smallint -- null, -- comment 'ระยะเวลา- วัน',
  106. ,SuccStat -- varchar-- (20) null, -- comment 'สถานะของงานหรือคดีหากขั้นตอนนี้สำเร็จ',
  107. ,FailStat -- varchar-- (20) null, -- comment 'สถานะของงานหรือคดีหากขั้นตอนนี้ไม่สำเร็จ',
  108. ,CaseStat
  109. ,PID -- int -- not null references "dbLegal".tblgsteppro(ID) on delete cascade on update cascade
  110. ,idgrp
  111. ,Selectable
  112. ,StartActive
  113. ,processid
  114. ,Description_require
  115. ,uid
  116. ) select
  117. new.Title -- varchar -- (50) not null, -- comment 'ขั้นตอนดำเนินคดี',
  118. ,new.Description --text --null, -- comment 'รายละเอียด',
  119. ,new.Duration -- smallint -- null, -- comment 'ระยะเวลา- วัน',
  120. ,new.SuccStat -- varchar-- (20) null, -- comment 'สถานะของงานหรือคดีหากขั้นตอนนี้สำเร็จ',
  121. ,new.FailStat -- varchar-- (20) null, -- comment 'สถานะของงานหรือคดีหากขั้นตอนนี้ไม่สำเร็จ',
  122. ,new.CaseStat
  123. ,new.PID -- int -- not null references "dbLegal".tblgsteppro(ID) on delete cascade on update cascade
  124. ,new.idgrp
  125. ,new.Selectable
  126. ,new.StartActive
  127. ,new.processid
  128. ,new.Description_require
  129. ,new.uid
  130. returning id into new.id;
  131. return NEW ;
  132. elseif (TG_OP = 'UPDATE') then
  133. sql := null;
  134. sql := sqlSetValue( OLD.Title, NEW.Title, sql, 'Title' );
  135. sql := sqlSetValue( OLD.Description, NEW.Description, sql, 'Description' );
  136.  
  137. sql := sqlSetValue( OLD.Duration, NEW.Duration, sql, 'Duration' );
  138. sql := sqlSetValue( OLD.SuccStat, NEW.SuccStat, sql, 'SuccStat' );
  139. sql := sqlSetValue( OLD.FailStat, NEW.FailStat, sql, 'FailStat' );
  140. sql := sqlSetValue( OLD.CaseStat, NEW.CaseStat, sql, 'CaseStat' );
  141. sql := sqlSetValue( OLD.PID, NEW.PID, sql, 'PID' );
  142. sql := sqlSetValue( OLD.orderno, NEW.orderno, sql, 'orderno' );
  143. sql := sqlSetValue( OLD.Selectable, NEW.Selectable, sql, 'Selectable' );
  144. sql := sqlSetValue( OLD.idgrp, NEW.idgrp, sql, 'idgrp' );
  145. sql := sqlSetValue( OLD.processid, NEW.processid, sql, 'processid' );
  146. sql := sqlSetValue( OLD.StartActive, NEW.StartActive, sql, 'StartActive' );
  147.  
  148.  
  149. sql := sqlSetValue( OLD.Description_require, NEW.Description_require, sql, 'Description_require' );
  150. sql := "dbSys".sqlSetValue( OLD.uid, NEW.uid, sql, 'uid' );
  151.  
  152.  
  153. if sql is not null then
  154. sql := 'update "dbLegal".tbLGStepProItem ' || sql || ' where id = ' || new.id;
  155. execute sql ;
  156. end if;
  157.  
  158. return NEW;
  159. elseif TG_OP = 'DELETE' then
  160. -- delete from "dbLegal".tbLGStepProItem where id = old.id; -- record id of this decendant
  161. perform "dbSys".grpdelete( old.id , '"dbLegal".tbLGStepProItem');
  162. return OLD;
  163.  
  164. end if;
  165. end ; $$ LANGUAGE plpgsql;
  166.  
  167. /*****************************
  168. View registration
  169. ******************************/
  170. select "dbSys".viewRegistration( '"dbLegal".tbLGStepProItemVW'
  171. , _delete_system_data := true
  172. ,_init_data := true, _init_data_uid := 1 ,_init_data_acc := null , _init_sysorg := true , _init_count := 'count_step'
  173.  
  174. );
  175.  
  176.  
  177. /************************************************************
  178. Registration : inheritance
  179. *************************************************************/
  180. -- select "dbSys".tblRegistration_Inherit( '"dbLegal".tbLGStepProItemVW','"dbBase".tbLGStepProItemVW', false, true, true );
  181.  
  182.  
  183. --* create view
  184. select * from "dbSys".tvcreateview(varint('userid'),varint('accid'),'"dbLegal".tblgstepproitem', dropit:=true );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement