Search code examples
oracleobjectplsqlconcatenationsql-types

PL/SQL for implode function on custom types


Is there any way to create an implode routine in PL/SQL that takes any custom datatype as a parameter and concatenates its members, delimited by some specified string?

For example, say I've got the following type:

CREATE TYPE myPerson AS OBJECT(
  forename VARCHAR2(50),
  surname  VARCHAR2(50),
  age      NUMBER
);

Then, say a function returns an object of type myPerson, but I want the columns concatenated together:

SELECT implode(getPerson(1234),'$$') from dual;

to return (supposing the data in this contrived example is set up):

John$$Doe$$55

Where the delimiter can be specified as an optional parameter, but the type of the first parameter could be anything (not necessarily myPerson).


Solution

  • Your custom datatype can support methods and methods can have parameters.

    CREATE TYPE myPerson AS OBJECT(   
      forename VARCHAR2(50),   
      surname  VARCHAR2(50),   
      age      NUMBER,
    
      MEMBER FUNCTION
      get_record(pGlue IN varchar2)   RETURN VARCHAR2 );
    
    CREATE TYPE BODY myPerson 
    AS 
       MEMBER FUNCTION get_record(pGlue varchar2) RETURN VARCHAR2
    
     IS
    BEGIN
     RETURN forename || pGlue  || surname  || pGlue || age ;
    END get_record;
    
    END;