Skip to content

Commit

Permalink
Merge pull request emilybache#4 from codecop/master
Browse files Browse the repository at this point in the history
Ported GildedRose to Oracle PL/SQL
  • Loading branch information
Emily Bache committed Apr 13, 2014
2 parents 4891dcd + a5a06c7 commit 4967a56
Show file tree
Hide file tree
Showing 11 changed files with 233 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Java/com/gildedrose/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public Item(String name, int sellIn, int quality) {
this.quality = quality;
}

public String toString() {
@Override
public String toString() {
return this.name + ", " + this.sellIn + ", " + this.quality;
}
}
35 changes: 35 additions & 0 deletions php/src/texttest_fixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

require_once 'gilded_rose.php';

echo "OMGHAI!\n";

$items = array(
new Item('+5 Dexterity Vest', 10, 20),
new Item('Aged Brie', 2, 0),
new Item('Elixir of the Mongoose', 5, 7),
new Item('Sulfuras, Hand of Ragnaros', 0, 80),
new Item('Sulfuras, Hand of Ragnaros', -1, 80),
new Item('Backstage passes to a TAFKAL80ETC concert', 15, 20),
new Item('Backstage passes to a TAFKAL80ETC concert', 10, 49),
new Item('Backstage passes to a TAFKAL80ETC concert', 5, 49),
// this conjured item does not work properly yet
new Item('Conjured Mana Cake', 3, 6)
);

$app = new GildedRose($items);

$days = 2;
if (count($argv) > 1) {
$days = (int) $argv[1];
}

for ($i = 0; $i < $days; $i++) {
echo("-------- day $i --------\n");
echo("name, sellIn, quality\n");
foreach ($items as $item) {
echo $item . PHP_EOL;
}
echo PHP_EOL;
$app->update_quality();
}
7 changes: 7 additions & 0 deletions plsql/create_user_if_needed.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
PROMPT Creating User 'DOJO'
DROP USER dojo;
CREATE USER dojo IDENTIFIED BY pass
DEFAULT TABLESPACE USERS TEMPORARY TABLESPACE temp;
GRANT RESOURCE TO dojo;
GRANT CONNECT TO dojo;
/
9 changes: 9 additions & 0 deletions plsql/item.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
PROMPT Creating Table 'ITEM'
DROP TABLE item;
CREATE TABLE item
(
name VARCHAR2(100) NOT NULL,
sell_in NUMBER(6) NOT NULL,
quality NUMBER(6) NOT NULL
);
/
31 changes: 31 additions & 0 deletions plsql/item_with_id.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
PROMPT Creating Table 'ITEM' with auto-increment primary key 'ID'
DROP TABLE item;
CREATE TABLE item
(
id NUMBER(6) NOT NULL,
name VARCHAR2(100) NOT NULL,
sell_in NUMBER(6) NOT NULL,
quality NUMBER(6) NOT NULL
);
/

ALTER TABLE item ADD (
CONSTRAINT item_pk PRIMARY KEY (ID));
/

DROP SEQUENCE item_id_seq;
CREATE SEQUENCE item_id_seq
INCREMENT BY 1
START WITH 1
MAXVALUE 999999
MINVALUE 1
NOCYCLE;
/

CREATE OR REPLACE TRIGGER item_bis_trg
BEFORE INSERT ON item
FOR EACH ROW
BEGIN
SELECT item_id_seq.NEXTVAL INTO :new.id FROM dual;
END;
/
9 changes: 9 additions & 0 deletions plsql/new_item.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE OR REPLACE PROCEDURE new_item(
i_name item.name%TYPE,
i_sell_in item.sell_in%TYPE,
i_quality item.quality%TYPE)
IS
BEGIN
INSERT INTO item (name, sell_in, quality) VALUES (i_name, i_sell_in, i_quality);
END new_item;
/
34 changes: 34 additions & 0 deletions plsql/texttest_fixture.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
DELETE FROM item;

DECLARE
l_days NUMBER(3);
CURSOR c_items IS
SELECT name, sell_in, quality FROM item;
l_item c_items%ROWTYPE;
BEGIN
DBMS_OUTPUT.PUT_LINE('OMGHAI!');

new_item('+5 Dexterity Vest', 10, 20);
new_item('Aged Brie', 2, 0);
new_item('Elixir of the Mongoose', 5, 7);
new_item('Sulfuras, Hand of Ragnaros', 0, 80);
new_item('Sulfuras, Hand of Ragnaros', -1, 80);
new_item('Backstage passes to a TAFKAL80ETC concert', 15, 20);
new_item('Backstage passes to a TAFKAL80ETC concert', 10, 49);
new_item('Backstage passes to a TAFKAL80ETC concert', 5, 49);
-- this conjured item does not work properly yet ;
new_item('Conjured Mana Cake', 3, 6);

l_days := 2;

FOR i IN 0 .. l_days - 1
LOOP
DBMS_OUTPUT.PUT_LINE('-------- day ' || TO_CHAR(i) || ' --------');
DBMS_OUTPUT.PUT_LINE('name, sellIn, quality');
FOR l_item IN c_items LOOP
DBMS_OUTPUT.PUT_LINE(l_item.name || ', ' || l_item.sell_in || ', ' || l_item.quality);
END LOOP;
DBMS_OUTPUT.PUT_LINE('');
update_quality();
END LOOP;
END;
66 changes: 66 additions & 0 deletions plsql/update_quality.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
CREATE OR REPLACE PROCEDURE update_quality
IS
CURSOR c_items IS
SELECT name, sell_in, quality FROM item FOR UPDATE;
l_item c_items%ROWTYPE;
l_name item.name%TYPE;
l_sell_in item.sell_in%TYPE;
l_quality item.quality%TYPE;
BEGIN
FOR l_item IN c_items
LOOP
l_name := l_item.name;
l_sell_in := l_item.sell_in;
l_quality := l_item.quality;

IF l_name != 'Aged Brie' AND l_name != 'Backstage passes to a TAFKAL80ETC concert' THEN
IF l_quality > 0 THEN
IF l_name != 'Sulfuras, Hand of Ragnaros' THEN
l_quality := l_quality - 1;
END IF;
END IF;
ELSE
IF (l_quality < 50) THEN
l_quality := l_quality + 1;
IF l_name = 'Backstage passes to a TAFKAL80ETC concert' THEN
IF l_sell_in < 11 THEN
IF l_quality < 50 THEN
l_quality := l_quality + 1;
END IF;
END IF;
IF l_sell_in < 6 THEN
IF l_quality < 50 THEN
l_quality := l_quality + 1;
END IF;
END IF;
END IF;
END IF;
END IF;

IF l_name != 'Sulfuras, Hand of Ragnaros' THEN
l_sell_in := l_sell_in - 1;
END IF;

IF l_sell_in < 0 THEN
IF l_name != 'Aged Brie' THEN
IF l_name != 'Backstage passes to a TAFKAL80ETC concert' THEN
IF l_quality > 0 THEN
IF l_name != 'Sulfuras, Hand of Ragnaros' THEN
l_quality := l_quality - 1;
END IF;
END IF;
ELSE
l_quality := l_quality - l_quality;
END IF;
ELSE
IF l_quality < 50 THEN
l_quality := l_quality + 1;
END IF;
END IF;
END IF;

UPDATE item
SET name = l_name, sell_in = l_sell_in, quality = l_quality WHERE CURRENT OF c_items;
END LOOP;
END update_quality;
/
27 changes: 27 additions & 0 deletions plsql/ut_update_quality.pkb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
CREATE OR REPLACE PACKAGE BODY ut_update_quality
IS

PROCEDURE ut_setup IS
BEGIN
DELETE FROM item;
END;

PROCEDURE ut_teardown IS
BEGIN
NULL;
END;

PROCEDURE ut_foo
IS
l_name item.name%TYPE;
BEGIN
new_item('foo', 0, 0);

update_quality();

SELECT name INTO l_name FROM item;
utAssert.eq('name did change', l_name, 'fixme');
END ut_foo;

END ut_update_quality;
/
8 changes: 8 additions & 0 deletions plsql/ut_update_quality.pks
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE OR REPLACE PACKAGE ut_update_quality
IS
PROCEDURE ut_setup;
PROCEDURE ut_teardown;

PROCEDURE ut_foo;
END ut_update_quality;
/
5 changes: 5 additions & 0 deletions plsql/ut_update_quality.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- unit test using utPLSQL 2.2.1, see http://utplsql.sourceforge.net/ ;
-- test package must be named like the procedure we want to test ;

EXEC utplsql.test ('update_quality', recompile_in => FALSE);
-- check DBMS_OUTPUT for output ;

0 comments on commit 4967a56

Please sign in to comment.