Pipelined table function で ascii art Tweet
クリスマスも過ぎ、新年へのカウントダウンも始まっていますが、
Pipelined table functionを使って簡単なASCIIアートをJPOUG Advent Calendar 2012描画してましたが中身を公開してなかったな〜
ということで、少々手直しして公開!
公開しておくとPipelined table functionとかtable functionを書かなきゃなった時に思い出しやすいのでw 自分向けなんですけどね。 (^^;;
参考:
Oracle Database Online Documentation 12c Release 1 (12.1)
Unstructured Data and Content Management
13 Using Pipelined and Parallel Table Functions
create or replace type messageTblType as table of varchar2(300)
/
注)
以下関数ではdbms_lock.sleep()を利用しているため、事前にexecute権限を付与しておく必要があります。
また、エラー処理は端折ってます. (^^;;
create or replace function tree(lheight pls_integer, linterval_change_color float)
return messageTblType
pipelined
as
height_of_tree pls_integer := lheight;
center pls_integer;
i pls_integer := 1;
siz_of_trunk pls_integer := 3;
pos_of_trunk pls_integer;
interval_change_color float := linterval_change_color;
begin
center := ceil((1 + height_of_tree + ( height_of_tree - 2)) / 2);
pos_of_trunk := case when ceil(height_of_tree / 5) > 10 then 10 else ceil(height_of_tree / 5) end;
for l in 1..75 loop
pipe row(chr(27)||'[2J'||chr(27)||'[1;'||to_char(ceil(dbms_random.value(1,7))+30)||'m');
i := 1;
--draw leaves
while (i < height_of_tree) loop
pipe row(
chr(27)||'['||to_char(i,'FM0999')||';'||to_char(center - i,'FM0999')||'H'||lpad('*', i + (i - 2) + 1, '*')
);
i := i + 1;
end loop;
--draw trunk
for j in -1..pos_of_trunk loop
pipe row(
chr(27)||'['||to_char(j + height_of_tree,'FM0999')||';'||to_char(center - 2,'FM0999')||'H'||lpad('*', siz_of_trunk, '*')
);
end loop;
dbms_lock.sleep(interval_change_color);
end loop;
pipe row(chr(27)||'[0m');
return;
end;
/
show errors
以下、実行コードの例
Treeのサイズを10/20/50、色の変更感覚を0.2秒として実行してます。
set linesize 300
set pagesize 0
set array 1
select * from table(tree(10, 0.2));
select * from table(tree(20, 0.2));
select * from table(tree(50, 0.2));
実行結果はYoutubeで:)
| 固定リンク | 0
コメント