create table props(id number, name varchar2(50), ord number)
Table created.
alter table props add constraint props_pk primary key (id)
Table altered.
insert into props
select level, 'prop' || level, level
from dual
connect by level < 10
9 row(s) inserted.
create table vectors(id number, notes varchar2(50))
Table created.
alter table vectors add constraint vectors_pk primary key (id)
Table altered.
create table vectors_details(vector_id number, prop_id number, val varchar2(50))
Table created.
alter table vectors_details add constraint vectors_details_pk primary key (vector_id, prop_id)
Table altered.
alter table vectors_details add constraint vectors_details_vector_id_fk foreign key (vector_id) references vectors(id)
Table altered.
alter table vectors_details add constraint vectors_details_prop_fk foreign key (prop_id) references props(id)
Table altered.
insert into vectors(id)
select level
from dual
connect by level <= 3
3 row(s) inserted.
insert into vectors_details(vector_id, prop_id, val)
select
v.id, -- vector_id
x.id, -- prop_id
'val ' || v.id || x.id -- val
from
vectors v cross join (
select level id
from dual
connect by level < 10) x
27 row(s) inserted.
create or replace package pkg_ptf is
function describe(tab in out dbms_tf.table_t) return dbms_tf.describe_t;
procedure fetch_rows;
function fn_vectors_list (tab table) return table pipelined row polymorphic using pkg_ptf;
end;
Package created.
create or replace package body pkg_ptf is
function fn_val(p_vector_id number, p_prop_id number) return varchar2
as
l_result vectors_details.val%type;
begin
/*
this function:
. returs dat.val of given p_vector_id and p_prop.
*/
select val
into l_result
from vectors_details
where
vector_id = p_vector_id
and prop_id = p_prop_id;
return l_result;
end;
function describe(tab in out dbms_tf.table_t) return dbms_tf.describe_t
as
cols dbms_tf.columns_new_t;
i pls_integer := 1;
begin
dbms_output.put_line('pkg_ptf.describe');
-- hide notes, just for the sake of it:
for i in 1 .. tab.column.count loop
tab.column(i).pass_through := tab.column(i).description.name != '"NOTES"';
tab.column(i).for_read := tab.column(i).description.name != '"NOTES"';
end loop;
-- add props (pivot):
for l_rec in (select name from props order by ord)
loop
cols(i) := dbms_tf.column_metadata_t(name => l_rec.name, type => dbms_tf.type_varchar2);
i := i + 1;
end loop;
return dbms_tf.describe_t(new_columns => cols);
end;
procedure fetch_rows
as
l_rowset dbms_tf.row_set_t;
l_vals_rowset dbms_tf.row_set_t;
l_row_count binary_integer;
l_col_count binary_integer;
l_vector_id_col binary_integer := 1;
l_col binary_integer;
begin
-- fetch rowset:
dbms_tf.get_row_set(l_rowset, l_row_count, l_col_count);
for l_props_rec in (select id from props order by ord)
loop
for l_row in 1 .. l_row_count
loop
l_col := l_props_rec.id;
l_vals_rowset(l_col).tab_varchar2(l_row) := fn_val(l_rowset(1).tab_number(l_row), l_props_rec.id);
end loop;
end loop;
dbms_tf.put_row_set(l_vals_rowset);
end;
end;
Package Body created.
select * from pkg_ptf.fn_vectors_list(vectors)
ID | PROP1 | PROP2 | PROP3 | PROP4 | PROP5 | PROP6 | PROP7 | PROP8 | PROP9 | 1 | val 11 | val 12 | val 13 | val 14 | val 15 | val 16 | val 17 | val 18 | val 19 | 2 | val 21 | val 22 | val 23 | val 24 | val 25 | val 26 | val 27 | val 28 | val 29 | 3 | val 31 | val 32 | val 33 | val 34 | val 35 | val 36 | val 37 | val 38 | val 39 |
---|
insert into props values (10, 'props10', 10)
1 row(s) inserted.
select * from pkg_ptf.fn_vectors_list(vectors)
ID | PROP1 | PROP2 | PROP3 | PROP4 | PROP5 | PROP6 | PROP7 | PROP8 | PROP9 | PROPS10 | 1 | - | - | - | - | - | - | - | - | - | - | 2 | - | - | - | - | - | - | - | - | - | - | 3 | - | - | - | - | - | - | - | - | - | - |
---|
select * from pkg_ptf.fn_vectors_list(vectors)
ID | PROP1 | PROP2 | PROP3 | PROP4 | PROP5 | PROP6 | PROP7 | PROP8 | PROP9 | PROPS10 | 1 | - | - | - | - | - | - | - | - | - | - | 2 | - | - | - | - | - | - | - | - | - | - | 3 | - | - | - | - | - | - | - | - | - | - |
---|
select * from pkg_ptf.fn_vectors_list(vectors)
ID | PROP1 | PROP2 | PROP3 | PROP4 | PROP5 | PROP6 | PROP7 | PROP8 | PROP9 | 1 | - | - | - | - | - | - | - | - | - | 2 | - | - | - | - | - | - | - | - | - | 3 | - | - | - | - | - | - | - | - | - |
---|
select * from pkg_ptf.fn_vectors_list(vectors)
ID | PROP1 | PROP2 | PROP3 | PROP4 | PROP5 | PROP6 | PROP7 | PROP8 | PROP9 | PROPS10 | 1 | - | - | - | - | - | - | - | - | - | - | 2 | - | - | - | - | - | - | - | - | - | - | 3 | - | - | - | - | - | - | - | - | - | - |
---|
select * from pkg_ptf.fn_vectors_list(vectors)
ID | PROP1 | PROP2 | PROP3 | PROP4 | PROP5 | PROP6 | PROP7 | PROP8 | PROP9 | 1 | - | - | - | - | - | - | - | - | - | 2 | - | - | - | - | - | - | - | - | - | 3 | - | - | - | - | - | - | - | - | - |
---|
select * from pkg_ptf.fn_vectors_list(vectors)
ID | PROP1 | PROP2 | PROP3 | PROP4 | PROP5 | PROP6 | PROP7 | PROP8 | PROP9 | 1 | - | - | - | - | - | - | - | - | - | 2 | - | - | - | - | - | - | - | - | - | 3 | - | - | - | - | - | - | - | - | - |
---|
select * from pkg_ptf.fn_vectors_list(vectors)
ID | PROP1 | PROP2 | PROP3 | PROP4 | PROP5 | PROP6 | PROP7 | PROP8 | PROP9 | PROPS10 | 1 | - | - | - | - | - | - | - | - | - | - | 2 | - | - | - | - | - | - | - | - | - | - | 3 | - | - | - | - | - | - | - | - | - | - |
---|
select * from pkg_ptf.fn_vectors_list(vectors)
ID | PROP1 | PROP2 | PROP3 | PROP4 | PROP5 | PROP6 | PROP7 | PROP8 | PROP9 | PROPS10 | 1 | - | - | - | - | - | - | - | - | - | - | 2 | - | - | - | - | - | - | - | - | - | - | 3 | - | - | - | - | - | - | - | - | - | - |
---|
select * from pkg_ptf.fn_vectors_list(vectors)
ID | PROP1 | PROP2 | PROP3 | PROP4 | PROP5 | PROP6 | PROP7 | PROP8 | PROP9 | PROPS10 | 1 | - | - | - | - | - | - | - | - | - | - | 2 | - | - | - | - | - | - | - | - | - | - | 3 | - | - | - | - | - | - | - | - | - | - |
---|
select * from pkg_ptf.fn_vectors_list(vectors)
ID | PROP1 | PROP2 | PROP3 | PROP4 | PROP5 | PROP6 | PROP7 | PROP8 | PROP9 | 1 | - | - | - | - | - | - | - | - | - | 2 | - | - | - | - | - | - | - | - | - | 3 | - | - | - | - | - | - | - | - | - |
---|