PL/SQL Server Pages
OracleのPL/SQL Server Pagesを使ってみた
※環境はOracleXE for Linuxで行っています
Database Access Descriptorを作成
begin
DBMS_EPG.create_dad(
dad_name => "test",
path => "/test/*"
);
end;
これでhttp://localhost:8080/test/(PL/SQLプロシージャ名)でアクセスできるようになる
PL/SQLの作成
SQLPlus上から作成するのではなく、以下をどっかにtest.pspとして作成する
<%@ page language="PL/SQL" %>
<%@ plsql procedure="sample_proc" %>
<%@ plsql parameter="id" type="number" default="0" %>
<html>
<head>
<title>test as ID=<%= id %></title>
</head>
<body>
<table border="1" cellpadding="5">
<tr>
<th>ID</th>
<th>NAME</th>
</tr>
<%! CURSOR smp_csr IS SELECT ID,NAME FROM sample; %>
<% FOR smp IN smp_csr LOOP %>
<tr>
<td><%= smp.ID %></td>
<td><%= smp.NAME %></td>
</tr>
<% END LOOP; %>
</table>
</body>
</html>
FORの部分は
<% FOR smp IN (SELECT ID,NAME FROM sample) %>
にしても大丈夫っぽい
デプロイ
loadpsp -replace -user hoge/hoge@xe test.psp
を実行するとユーザー内にsample_procっていうプロシージャが作成される。
あとはhttp://localhost:8080/test/sample_procにアクセスする
備考
ちなみに以下のように作成も可能
create or replace procedure sample is
HTP.print("hogehoge");
end;
あとloadpspユーティリティを利用する場合はORACLEHOMEとORACLESIDが設定されてないと実行できない
備考
http://localhost:8080/test/sample_procにアクセスせずにhttp://localhost:8080/testにアクセスするとsample_procを利用するようにしたい場合は以下のように設定する
begin
dbms_epg.set_dad_attribute("test", "default-page", "sample_proc");
end;
testの部分はDatabase Access Descriptor Nameを指定し、default-page属性値に実行されるPL/SQLプロシージャを指定すればいいっぽい