Advertisement
jelledebock

Oefening6Package

Dec 21st, 2014
569
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PL/SQL 5.99 KB | None | 0 0
  1. CREATE OR REPLACE
  2. PACKAGE OEFENING6PACKAGE AS
  3.   -- %%%%%%%%%%%%%%%Types%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4.   -- Step 6.1
  5.   TYPE product_rec_type IS RECORD (
  6.     product_id      OE.product_information.product_id%TYPE,
  7.     product_name    OE.product_information.product_name%TYPE,
  8.     new_price       OE.product_information.list_price%TYPE
  9.   );
  10.   -- Step 6.2
  11.   TYPE product_tab_type IS TABLE OF product_rec_type;
  12.   -- %%%%%%%%%%%%%%%Procedures%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  13.   PROCEDURE productenUitDeInventaris;
  14.   PROCEDURE updatePrijzen(p_category_id IN OE.categories.category_id%TYPE, p_number IN NUMBER);
  15.   PROCEDURE printproductabtype(tab IN product_tab_type);
  16.   -- %%%%%%%%%%%%%%%Functies%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  17.   FUNCTION checkCategory(category_p_id OE.categories.category_id%TYPE) RETURN BOOLEAN;
  18.   FUNCTION checkEnough(
  19.       category_p_id   OE.categories.category_id%TYPE,
  20.       p_number      NUMBER    
  21.   ) RETURN BOOLEAN;
  22.  
  23.   FUNCTION getProductDiscounts
  24.   (
  25.       category_p_id   OE.categories.category_id%TYPE,
  26.       p_number      NUMBER    
  27.   ) RETURN product_tab_type;
  28.   -- %%%%%%%%%%%%%%%Exceptions%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  29.   category_not_found EXCEPTION;
  30.   not_enough_products EXCEPTION;
  31.  
  32. END OEFENING6PACKAGE;
  33. /
  34. CREATE OR REPLACE
  35. PACKAGE BODY OEFENING6PACKAGE AS
  36.  
  37.   PROCEDURE productenUitDeInventaris AS
  38.       CURSOR warehouse_cursor IS SELECT * FROM warehouses;
  39.      
  40.       CURSOR product_pw_cursor(wrh warehouse_cursor%ROWTYPE) IS
  41.       SELECT pi.product_id, pi.product_name, inv.quantity_on_hand FROM inventories inv
  42.       inner join product_information pi ON pi.product_id = inv.product_id
  43.       WHERE inv.warehouse_id = wrh.warehouse_id;
  44.   BEGIN
  45.       -- Loop through every warehouse
  46.       FOR warehouse IN warehouse_cursor LOOP
  47.         DBMS_OUTPUT.put_line('Warehouse '||warehouse.warehouse_id||', '||warehouse.warehouse_name);
  48.         -- Loop through every product in a warehouse
  49.         FOR product IN product_pw_cursor(warehouse) LOOP
  50.           DBMS_OUTPUT.put_line(product.product_id||' '||product.product_name||' Stockaantal = '||product.quantity_on_hand);
  51.         END LOOP;
  52.       END LOOP;
  53.   END productenUitDeInventaris;
  54.  
  55.   PROCEDURE updatePrijzen(p_category_id IN OE.categories.category_id%TYPE, p_number IN NUMBER) AS
  56.       producten product_tab_type;
  57.   BEGIN
  58.       producten := getproductdiscounts(p_category_id, p_number);
  59.       FORALL indx IN 1..producten.COUNT
  60.         UPDATE product_information i SET i.list_price = producten(indx).new_price
  61.         WHERE i.product_id = producten(indx).product_id;
  62.       EXCEPTION
  63.         WHEN category_not_found THEN
  64.           DBMS_OUTPUT.put_line('Category with id ' || p_category_id || ' not found');
  65.         WHEN not_enough_products THEN
  66.           DBMS_OUTPUT.put_line('Category with id ' || p_category_id || ' not enough products');
  67.   END updatePrijzen;
  68.  
  69.   PROCEDURE printproductabtype(tab IN product_tab_type) AS
  70.     counter INTEGER:=0;
  71.     rowt product_rec_type;
  72.   BEGIN
  73.     DBMS_OUTPUT.put_line('Lijst van de '|| tab.COUNT || ' duurste producten');
  74.     FOR i IN 1..tab.COUNT LOOP
  75.       rowt := tab(i);
  76.       counter := counter +1;
  77.       DBMS_OUTPUT.put_line(counter||'    '||rowt.product_id||'    '||rowt.product_name||'    '||rowt.new_price);
  78.     END LOOP;
  79.   END;
  80.  
  81.   FUNCTION checkCategory(category_p_id OE.categories.category_id%TYPE) RETURN BOOLEAN AS
  82.   occurences NUMBER;
  83.   BEGIN
  84.     SELECT COUNT(*) INTO occurences FROM categories WHERE category_id = category_p_id;
  85.     IF occurences = 0 THEN
  86.       RETURN FALSE;
  87.     ELSE
  88.       RETURN TRUE;
  89.     END IF;
  90.   END checkCategory;
  91.  
  92.   FUNCTION checkEnough(
  93.                         category_p_id   OE.categories.category_id%TYPE,
  94.                         p_number      NUMBER    
  95.                       )
  96.   RETURN BOOLEAN AS
  97.   occurences NUMBER;
  98.   BEGIN
  99.     SELECT COUNT(*) INTO occurences FROM product_information pi
  100.     inner join categories cat ON cat.category_id = pi.category_id
  101.     WHERE pi.category_id = category_p_id;
  102.      
  103.     IF occurences >= p_number THEN
  104.       RETURN TRUE;
  105.     ELSE
  106.       RETURN FALSE;
  107.     END IF;
  108.   END checkEnough;
  109.  
  110.   FUNCTION getProductDiscounts
  111.   (
  112.       category_p_id   OE.categories.category_id%TYPE,
  113.       p_number      NUMBER    
  114.   )
  115.   RETURN product_tab_type AS
  116.     -- %%%%%Extra vars %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  117.    
  118.     v_product_tab product_tab_type;
  119.     counter INTEGER:=0;
  120.      -- %%%%%Extra cursor %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  121.     CURSOR pduurste
  122.           (
  123.             p_category_id OE.categories.category_id%TYPE,
  124.             p_number NUMBER
  125.           )
  126.   IS
  127.     SELECT product_id, product_name, (list_price * 0.9) AS NEW_PRICE FROM OE.product_information
  128.     WHERE (ROWNUM <= p_number) AND (category_id = p_category_id)
  129.     ORDER BY list_price DESC;
  130.      -- %%%%%Implementation %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  131.     BEGIN
  132.     IF checkcategory(category_p_id) != TRUE THEN
  133.       RAISE category_not_found;
  134.     END IF;
  135.     IF checkenough(category_p_id, p_number) != TRUE THEN
  136.       RAISE not_enough_products;
  137.     END IF;
  138.     v_product_tab := product_tab_type();
  139.     FOR n IN pduurste(category_p_id, p_number) LOOP
  140.       counter := counter + 1;
  141.       v_product_tab.extend;
  142.       v_product_tab(counter).product_id := n.product_id;
  143.       v_product_tab(counter).product_name := n.product_name;
  144.       v_product_tab(counter).new_price := n.new_price;
  145.     END LOOP;
  146.     RETURN v_product_tab;
  147.   END getProductDiscounts;
  148.  
  149. END OEFENING6PACKAGE;
  150. /
  151. /***Testapplication ****/
  152. BEGIN
  153. --products per warehouse
  154. -- OE.oefening6package.productenuitdeinventaris();
  155. --geef 5 duurste met id 11
  156. OE.oefening6package.printproductabtype(oefening6package.getproductdiscounts(11,5));
  157. -- Update them
  158. OE.oefening6package.updateprijzen(11,5);
  159. OE.oefening6package.printproductabtype(oefening6package.getproductdiscounts(11,5));
  160. END;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement