Wednesday, June 11, 2008

An aggregate function for collection types

Today I stumbled upon a blog entry where the poster needs something like Tom Kyte's stragg function, only with dynamic delimiters. Here's my solution, using a variation of stragg that accumulates data in a collection instead of appending it to a string.

First of all, this is how you use it:
SQL> r
1 with data as (
2 select lpad('x',level,'x') data
3 from dual
4 connect by level <= 5)
5 select v2_pkg.join(v2_agg(data),'-')
6* from data

V2_PKG.JOIN(V2_AGG(DATA),'-')
--------------------------------------------------------------------------------
x-xx-xxx-xxxx-xxxxx

1 Zeile wurde ausgewählt.


The v2_agg function (and it's underlying object type) append strings to a collection. The v2_pkg.join concatenates the elements of it's first argument (the collection) with the second argument in between. See below for the source code. It's part of my everyday toolkit, v2_pkg contains lots more, for example.

The collection type
create type v2_tbl as table of varchar2(4000);

The aggregate object type spec
create or replace type v2_agg_type as object (                                  
elements v2_tbl,
static function
ODCIAggregateInitialize(sctx IN OUT v2_agg_type )
return number,
member function
ODCIAggregateIterate(self IN OUT v2_agg_type ,
value IN varchar2 )
return number,
member function
ODCIAggregateTerminate(self IN v2_agg_type,
returnValue OUT v2_tbl,
flags IN number)
return number,
member function
ODCIAggregateMerge(self IN OUT v2_agg_type,
ctx2 IN v2_agg_type)
return number
);
/

The aggregate function
create or replace function v2_agg(input varchar2)
return v2_tbl
parallel_enable aggregate using v2_agg_type;
/

The utility package spec
create or replace package v2_pkg as
function join(i_tbl in v2_tbl,
i_glue in varchar2 := ',')
return varchar2;
end;
/

The object type body
create or replace type body v2_agg_type  is
static function ODCIAggregateInitialize(sctx IN OUT v2_agg_type)
return number
is
begin
sctx := v2_agg_type( null );
return ODCIConst.Success;
end;
member function ODCIAggregateIterate(self IN OUT v2_agg_type,
value IN varchar2 )
return number
is
begin
if self.elements is null then
self.elements := v2_tbl();
end if;
self.elements.extend;
self.elements(self.elements.count) := value;
return ODCIConst.Success;
end;
member function ODCIAggregateTerminate(self IN v2_agg_type,
returnValue OUT v2_tbl,
flags IN number)
return number
is
begin
returnValue := self.elements;
return ODCIConst.Success;
end;
member function ODCIAggregateMerge(self IN OUT v2_agg_type,
ctx2 IN v2_agg_type)
return number
is
begin
if ctx2.elements is not null then
if self.elements is null then
self.elements := v2_tbl();
end if;
for i in 1 .. ctx2.elements.count loop
self.elements.extend;
self.elements(self.elements.count) := ctx2.elements(i);
end loop;
end if;
return ODCIConst.Success;
end;
end;
/

The utility package body
create or replace package body v2_pkg as
function join(i_tbl in v2_tbl,
i_glue in varchar2 := ',')
return varchar2
is
v_str varchar2(32767);
begin
IF i_tbl is not null THEN
FOR i in 1 .. i_tbl.count LOOP
v_str := v_str || i_glue || i_tbl(i);
END LOOP;
END IF;
return substr(v_str,length(i_glue)+1);
end;
end;
/


By the way, Oracle's COLLECT function might be more appropriate than v2_agg for large datasets. This post explains why.

Friday, March 28, 2008

Forms 6i certified with Oracle 11g?

Today I found this entry on Steven Chan's Oracle E-Business Suite Technology blog. It's titled "Oracle Database 11g Release 1 (11.1.0.6.0) Certified With E-Business Suite Release 11i".

EBS 11i uses the Developer 6i techstack, for which extended support ended last january. I think it's great (and highly amusing) that Oracle now blessed this configuration. There was probably a lot of customer pressure, so Oracle realized that reality does not conform with their product support plans ;-).

I also wonder if this certification only applies to EBS 11i or Dev6i in general (i.e. also good old client/server deployment). Probably not, but one can hope.

Friday, February 22, 2008

Comfortable javascript editing in APEX



This week, I built a small extension to Patrick Wolf's APEX Builder Plugin. It generates separate text fields for javascript events. Makes writing event code quite a bit easier.

Tuesday, January 29, 2008

Maybe it's time to re-think what web development should be about

I just came about this link and am currently half-way through Dan Ingall's Google TechTalk. Wow!

Btw, there is also Smalltalk in Flash.

Saturday, January 19, 2008

Object oriented database management systems

Recently, there were several blog posts about ODBMSs. It all started with this interview of Michael Stonebraker.
Dan Weinreb (ex ObjectStore) responded and today I read a blog post of Oracle's John Russell.

In the early/mid 90ies, when I was still on university and hooked on OO, ODBMSs had a big fascination for me. I read every book and paper about them I could get and had uni accounts on machines to play with Gemstone and ObjectStore. Hell, I even wrote one myself, in c++ and tcl (chaos - chris' alternative object store;-)).

Accidently, my first job was with Oracle 7 and Forms/Reports, "boring" technology or so I thought. This changed quickly, when I realized how efficient development in this environment was. It turned out that I didn't need all that fancy OO stuff I thought indispensable to get things done and have fun working. In some way, programming pl/sql even reminds me of the Gemstone/Smalltalk days. My interface seems minimalistic in comparison (sql*plus and vi), but it's only the interface to that powerful, persistent multiuser sql and pl/sql machine.