Unlike an array that saves a built-in type of values, a struct saves multiple types of values. tbJDBC provides the com.tmax.tibero.jdbc.TbStruct class which implements the java.sql.Struct interface.
Declaring Struct Data Type
Declare a record (struct) by declaring a package as follows:
create or replace type t_obj is object (obj_id number, name varchar(128), d date);
Using Structs as Input Arguments
A struct object can be sent to a PSM procedure as an input argument as follows:
Create a struct object by using the Connection object.
public Struct Connection.createStruct(String typeName, Object[] attributes)
Bind the struct object to a procedure.
A struct object can be sent to a PSM procedure by using CallableStatement.setObject().
The following example passes a struct object as an input argument. The print_obj procedure receives and processes a struct object.
A struct object can be received from a PSM procedure as an output argument as follows:
Call a procedure through out binding. Then, the procedure returns a struct object.
Receive the struct object by using CallableStatement.getStruct().
Receive the values from the struct object.
The following example receives a struct object from a procedure as an output argument.
The p_out_t_rec procedure returns NUMBER, VARCHAR, and DATE types of record objects.
The following is sample procedure code to create arrays and structs.
create or replace type t_obj is object (obj_id number, name varchar(128), d date);
/
create or replace type t_varr is varray(16) of varchar2(128);
/
create or replace type t_varr_varr is varray(8) of t_varr;
/
create or replace type t_tbl is table of varchar2(128);
/
create or replace type t_tbl_tbl is table of t_tbl;
/
create or replace type t_varr_obj is varray(4) of t_obj;
/
create or replace type t_tbl_obj is table of t_obj;
/
create or replace procedure p_out_t_obj(o out t_obj)
is
begin
o := t_obj(1,'abc',to_date('1900-01-01', 'YYYY-MM-DD')); end;
/
create or replace procedure p_out_t_varr(o out t_varr, str varchar2)
as
o_val t_varr;
begin
o_val := t_varr();
for i in 1 .. 16 loop
o_val.extend;
o_val(i) := str || to_char(i);
end loop;
o := o_val;
end;
/
create or replace procedure p_out_t_varr_varr(o out t_varr_varr)
as
o_val t_varr_varr;
begin
o_val := t_varr_varr();
for i in 1 .. 8 loop
o_val.extend;
p_out_t_varr(o_val(i), 'elem' || to_char(i));
end loop;
o := o_val;
end;
/
create or replace procedure p_out_t_tbl(o out t_tbl, str varchar2)
as
o_val t_tbl;
begin
o_val := t_tbl();
for i in 1 .. 4 loop
o_val.extend;
o_val(i) := str || to_char(i);
end loop;
o := o_val;
end;
/
create or replace procedure p_out_t_tbl_tbl(o out t_tbl_tbl)
as
o_val t_tbl_tbl;
begin
o_val := t_tbl_tbl();
for i in 1 .. 2 loop
o_val.extend;
p_out_t_tbl(o_val(i), 'elem' || to_char(i));
end loop;
o := o_val;
end;
/
create or replace procedure p_out_t_varr_obj(o out t_varr_obj)
as
o_val t_varr_obj;
begin
o_val := t_varr_obj();
for i in 1 .. 4 loop
o_val.extend;
p_out_t_obj(o_val(i));
end loop;
o := o_val;
end;
/
create or replace procedure p_out_t_tbl_obj(o out t_tbl_obj)
as
o_val t_tbl_obj;
begin
o_val := t_tbl_obj();
for i in 1 .. 6 loop
o_val.extend;
p_out_t_obj(o_val(i));
end loop;
o := o_val;
end;
/
create or replace procedure print_obj(p in t_obj)
as
begin
dbms_output.put_line('obj_id:' || p.obj_id);
dbms_output.put_line('name :' || p.name);
dbms_output.put_line('d :' || p.d);
end;
/
create or replace procedure print_varr(p in t_varr)
as
begin
for i in 1 .. 16 loop
dbms_output.put_line(to_char(i) || ':' || p(i));
end loop;
end;
/
create or replace procedure print_varr_varr(p in t_varr_varr)
as
begin
for i in 1 .. 8 loop
print_varr(p(i));
end loop;
end;
/
create or replace procedure print_tbl(p in t_tbl)
as
begin
for i in 1 .. 4 loop
dbms_output.put_line(to_char(i) || ':' || p(i));
end loop;
end;
/
create or replace procedure print_tbl_tbl(p in t_tbl_tbl)
as
begin
for i in 1 .. 2 loop
print_tbl(p(i));
end loop;
end;
/
create or replace procedure print_varr_obj(p in t_varr_obj)
as
begin
for i in 1 .. 4 loop
print_obj(p(i));
end loop;
end;
/
create or replace procedure print_tbl_obj(p in t_tbl_obj)
as
begin
for i in 1 .. 6 loop
print_obj(p(i));
end loop;
end;
/