Advertisement
alexarcan

lab11_sab

Dec 12th, 2016
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. --b)
  2. --LS / DIR - Displays all the sub-folders / files of a certain folder
  3. SELECT name
  4. FROM(
  5. SELECT directoryid, name, parentid FROM DIRECTORIES
  6. UNION
  7. SELECT 0, name, directoryid FROM FILES
  8. )
  9. WHERE parentid=7;
  10.  
  11. --TREE - Displays all the folders / files in a "tree-like" view (Connect By)
  12.  
  13. SELECT level, LPAD (' ', 2 * (LEVEL - 1)) || name AS padname
  14. FROM (select directoryid, name, parentid FROM directories
  15. UNION
  16. SELECT 0, name, directoryid from files) res
  17. START WITH directoryid=1
  18. CONNECT BY PRIOR directoryid=parentid
  19. ORDER siblings BY name;
  20.  
  21. -- MKDIR - Creates a folder (complying with the initial constraints)
  22. INSERT INTO directories values(2,'genres',1);--already exists=>error
  23. INSERT INTO directories values(10,'documentaries',3);
  24.  
  25. -- MKFILE - Creates a file (complying with the initial constraints)
  26. INSERT INTO files values(7,'xyz',1,'b');--already exists=>error
  27. INSERT INTO files values(8,'best_jazz',8,'m');
  28.  
  29. -- (CAT)- adds data to it
  30. UPDATE files SET content=CONCAT(content,'nop') WHERE directoryid = 8;
  31.  
  32. -- values to be deleted:
  33. INSERT INTO directories values (11,'A',1);
  34. INSERT INTO directories values (12,'B',7);
  35. INSERT INTO directories values (13,'C',7);
  36.  
  37. --RMDIR - Deletes a folder and all the corresponding files / sub-folders
  38. --make files table fk to be on cascade delete:
  39. ALTER TABLE FILES
  40. ADD FOREIGN KEY (DIRECTORYID)
  41. REFERENCES DIRECTORIES(DIRECTORYID)
  42. ON DELETE CASCADE;
  43.  
  44. DELETE FROM directories
  45. WHERE directoryid = 13;
  46.  
  47. --DEL - Deletes a file
  48. DELETE FROM files
  49. WHERE name = 'abc' AND directoryid = 7;
  50.  
  51.  
  52. -- c) PL/SQL - Create PL/SQL stored procedures / functions corresponding to the
  53. -- above mentioned commands + CD (change directory)
  54. -- LS PROCEDURE:
  55. set define off;
  56.  
  57. CREATE OR REPLACE PROCEDURE "LS"
  58. (
  59. dirid IN directories.directoryid%TYPE
  60. ) AS
  61. BEGIN
  62. dbms_output.put_line('Directories and files found in the directory with directoryid = '||dirid||':');
  63. FOR rec in (select name
  64. from (select directoryid, name, parentid from directories
  65. union
  66. select 0, name, directoryid from files) res
  67. where parentid=dirid) LOOP
  68. dbms_output.put_line(rec.name);
  69. END LOOP;
  70. EXCEPTION
  71. WHEN NO_DATA_FOUND THEN
  72. dbms_output.put_line('No such directory!');
  73. WHEN others THEN
  74. dbms_output.put_line('Error!');
  75. END LS;
  76.  
  77. SET serveroutput ON
  78. begin
  79. ls(999);
  80. end;
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87. --DIR PROCEDURE:
  88. set define off;
  89. CREATE OR REPLACE PROCEDURE "DIR"
  90. (
  91. P1 IN VARCHAR2
  92. )AS
  93. BEGIN
  94. dbms_output.put_line('abc ' || P1);
  95. for i in (SELECT * FROM directories) loop
  96. dbms_output.put_line('->'||i.name);
  97. end loop;
  98. end DIR;
  99.  
  100. SET serveroutput ON
  101. begin
  102. dir(1);
  103. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement