Advertisement
alexarcan

test2a

Dec 19th, 2016
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. create table "Usrs"
  2. ( "usr_id" int primary key , "nume" varchar2(32) not null , "type" int not null);
  3. -- 0 for "normal user" 1 for "superuser"
  4.  
  5. create table "BugState"
  6. ( "state_id" int primary key , "state_name" varchar2(32));
  7.  
  8. create table "Bugs"
  9. ( "bug_id" int primary key , "title" varchar2(32) not null , "description" varchar2(32) not null , "state_id" int , "usr_id" int,
  10. constraint fk_state_id foreign key ( "state_id" )
  11. references "BugState" ("state_id"),
  12. CONSTRAINT fk_usr_bug_id FOREIGN KEY ("usr_id")
  13. REFERENCES "Usrs" ("usr_id"));
  14.  
  15. create table "Annotations"
  16. ("annotation_id" int primary key , "bug_id" int , "content" varchar2(32) , "usr_id" int,
  17. constraint fk_bugs_id foreign key ( "bug_id" )
  18. references "Bugs" ("bug_id"),
  19. CONSTRAINT fk_usr_anno_id FOREIGN KEY ("usr_id")
  20. REFERENCES "Usrs" ("usr_id"));
  21.  
  22. INSERT INTO "Usrs" VALUES (1,'Ionut' ,0);
  23. insert into "Usrs" values (2,'Dorel' ,1);
  24.  
  25. insert into "BugState" values (1,'Open');
  26. insert into "BugState" values (2,'Closed');
  27.  
  28. insert into "Bugs" values (1,'Bug1','TestBug' , 1,1);
  29. insert into "Bugs" values (2,'Bug2','TestBug' , 2,1);
  30. INSERT INTO "Bugs" VALUES (3,'Bug3','TestBug' , 1,2);
  31.  
  32. insert into "Annotations" values (1,1,'Anno1',1);
  33. INSERT INTO "Annotations" VALUES (2,1,'Anno2',2);
  34. insert into "Annotations" values (3,2,'Anno3',2);
  35. insert into "Annotations" values (4,1,'Anno4',1);
  36.  
  37. CREATE PROCEDURE "NextAnnoId"
  38. (
  39. annoID OUT int
  40. ) AS
  41. BEGIN
  42. SELECT COUNT(*) INTO annoID FROM "Annotations";
  43. end "NextAnnoId";
  44. /
  45.  
  46. CREATE or replace PROCEDURE "Annotate"
  47. (
  48. description in varchar2,
  49. local_bug_id in varchar2,
  50. local_usr_id in int,
  51. annoid in out int,
  52. bug_title in out varchar2
  53. ) AS
  54. begin
  55. "NextAnnoId"(annoID);
  56. annoid := annoid+1;
  57. select "title" into bug_title from "Bugs" where "state_id"=1 and "usr_id" = local_usr_id and "bug_id" = local_bug_id;
  58. INSERT INTO "Annotations" VALUES ( annoID , local_bug_id , description , local_usr_id);
  59. exception when no_data_found then
  60. dbms_output.put_line('Can not insert annotation');
  61. end "Annotate";
  62. /
  63.  
  64. create or replace procedure "ListBugs" (
  65. "nousrs" in out int,
  66. "local_usr_id" in out int
  67. )AS
  68. begin
  69. for arow in (select "bug_id","title" from "Bugs"
  70. WHERE "state_id" = 2 AND "usr_id" = "local_usr_id" )
  71. LOOP
  72. dbms_output.put_line(arow."title");
  73. for brow in (select "content" from "Annotations"
  74. WHERE "bug_id" = aRow."bug_id")
  75. LOOP
  76. dbms_output.put_line(brow."content");
  77. end loop;
  78. select count(distinct "usr_id") into "nousrs" from "Annotations"
  79. where "bug_id"= aRow."bug_id";
  80. dbms_output.put_line("nousrs");
  81. dbms_output.put_line('');
  82. END LOOP;
  83.  
  84. end "ListBugs";
  85. /
  86.  
  87. SET serveroutput ON size 20000
  88. declare
  89. description varchar2(32) :='TestAnnoAnBlock';
  90. local_bug_id int :=1;
  91. local_usr_id int :=1;
  92. annoid int :=0;
  93. bug_title varchar2(32) :=null;
  94. nousrs int :=0;
  95.  
  96. begin
  97. "Annotate"(description,local_bug_id,local_usr_id,annoid,bug_title);
  98. "ListBugs"(nousrs,local_usr_id);
  99. end;
  100. /
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement