DROP FUNCTION QUEBRA_TEXTO
/
DROP TYPE TEXTOQUEBRADO_TABLE
/
DROP TYPE TEXTOQUEBRADO_TYPE
/

CREATE TYPE TEXTOQUEBRADO_TYPE AS
OBJECT (DESCTEXTO1      VARCHAR2(100),
        DESCTEXTO2      VARCHAR2(100),
        DESCTEXTO3      VARCHAR2(100),
        DESCTEXTO4      VARCHAR2(100),
        DESCTEXTO5      VARCHAR2(100));
/

CREATE TYPE TEXTOQUEBRADO_TABLE AS
TABLE OF TEXTOQUEBRADO_TYPE
/

CREATE OR REPLACE FUNCTION QUEBRA_TEXTO(
   P_TEXTO     IN VARCHAR2,
   P_QUEBRA    IN INTEGER) RETURN TEXTOQUEBRADO_TABLE PIPELINED
IS

   V_POSESPACO   INTEGER;
   V_TXTQUEBRA   VARCHAR2(5000);
   V_TXTRESTO    VARCHAR2(5000);
   V_TXTRETORNO1 VARCHAR2(5000);
   V_TXTRETORNO2 VARCHAR2(5000);
   V_TXTRETORNO3 VARCHAR2(5000);
   V_TXTRETORNO4 VARCHAR2(5000);
   V_TXTRETORNO5 VARCHAR2(5000);
   V_COUNT       INTEGER;

OUT_RECORD TEXTOQUEBRADO_TYPE := TEXTOQUEBRADO_TYPE (NULL, NULL, NULL,
                                                     NULL, NULL);

BEGIN

   V_TXTQUEBRA  := '';
   V_TXTRESTO   := P_TEXTO;

   V_POSESPACO := INSTR(P_TEXTO, ' ');
   IF V_POSESPACO = 0 THEN
      V_POSESPACO := LENGTH(P_TEXTO);
   END IF;
   V_TXTQUEBRA := V_TXTQUEBRA||SUBSTR(P_TEXTO, 1, V_POSESPACO);
   V_TXTRESTO  := SUBSTR(P_TEXTO, (V_POSESPACO+1), LENGTH(P_TEXTO));

   V_TXTRETORNO1 := V_TXTQUEBRA;
   V_COUNT := 0;

   WHILE ((((LENGTH(V_TXTQUEBRA)) <= 15)) AND
          (LENGTH(V_TXTRESTO) > 0)) LOOP
      V_POSESPACO := INSTR(V_TXTRESTO, ' ');
      IF V_POSESPACO = 0 THEN
         V_POSESPACO := LENGTH(V_TXTRESTO);
      END IF;

      V_TXTRETORNO1 := V_TXTQUEBRA;
      V_TXTQUEBRA := V_TXTQUEBRA||SUBSTR(V_TXTRESTO, 1, V_POSESPACO);
      V_TXTRESTO  := SUBSTR(V_TXTRESTO, (V_POSESPACO+1), LENGTH(V_TXTRESTO));
      IF (LENGTH(V_TXTQUEBRA) <= 15) THEN
         V_TXTRETORNO1 := V_TXTQUEBRA;
      END IF;

      --V_COUNT := V_COUNT +1;
      --IF V_COUNT = 2 THEN
        --RAISE_APPLICATION_ERROR(-20405,LENGTH(V_TXTQUEBRA)||'***'||V_TXTQUEBRA||'***'||V_TXTRESTO||'***'||V_TXTRETORNO, TRUE);
      --END IF;
   END LOOP;

   V_TXTQUEBRA  := '';
   V_TXTRESTO := LTRIM(RTRIM(SUBSTR(P_TEXTO,LENGTH(V_TXTRETORNO1)+1, LENGTH(P_TEXTO))));
   V_POSESPACO := INSTR(V_TXTRESTO, ' ');
   IF V_POSESPACO = 0 THEN
      V_POSESPACO := LENGTH(V_TXTRESTO);
   END IF;
   V_TXTQUEBRA := V_TXTQUEBRA||SUBSTR(V_TXTRESTO, 1, V_POSESPACO);
   V_TXTRESTO  := SUBSTR(V_TXTRESTO, (V_POSESPACO+1), LENGTH(V_TXTRESTO));
   V_TXTRETORNO2 := V_TXTQUEBRA;
   V_COUNT := 0;

   WHILE ((((LENGTH(V_TXTQUEBRA)) <= 15)) AND
          (LENGTH(V_TXTRESTO) > 0)) LOOP

      V_POSESPACO := INSTR(V_TXTRESTO, ' ');
      --IF V_COUNT = 2 THEN
--        RAISE_APPLICATION_ERROR(-20405,V_TXTRESTO||'***'||V_POSESPACO, TRUE);
      --END IF;
      IF V_POSESPACO = 0 THEN
         V_POSESPACO := LENGTH(V_TXTRESTO);
      END IF;

      V_TXTRETORNO2 := V_TXTQUEBRA;
      V_TXTQUEBRA := V_TXTQUEBRA||SUBSTR(V_TXTRESTO, 1, V_POSESPACO);
      
      V_TXTRESTO  := SUBSTR(V_TXTRESTO, (V_POSESPACO+1), LENGTH(V_TXTRESTO));

      IF (LENGTH(V_TXTQUEBRA) <= 15) THEN
         V_TXTRETORNO2 := V_TXTQUEBRA;
      END IF;

      V_COUNT := V_COUNT +1;
      --IF V_COUNT = 2 THEN
        --RAISE_APPLICATION_ERROR(-20405,LENGTH(V_TXTQUEBRA)||'***'||V_TXTQUEBRA||'***'||V_TXTRESTO, TRUE);
      --END IF;
   END LOOP;

   V_TXTQUEBRA  := '';
   V_TXTRESTO := SUBSTR(P_TEXTO,LENGTH(V_TXTRETORNO1||V_TXTRETORNO2)+1, LENGTH(P_TEXTO));
   --RAISE_APPLICATION_ERROR(-20405,V_TXTRESTO, TRUE);
   V_POSESPACO := INSTR(V_TXTRESTO, ' ');
   IF V_POSESPACO = 0 THEN
      V_POSESPACO := LENGTH(V_TXTRESTO);
   END IF;
   V_TXTQUEBRA := V_TXTQUEBRA||SUBSTR(V_TXTRESTO, 1, V_POSESPACO);
   V_TXTRESTO  := SUBSTR(V_TXTRESTO, (V_POSESPACO+1), LENGTH(V_TXTRESTO));
   V_TXTRETORNO3 := V_TXTQUEBRA;
   V_COUNT := 0;

   WHILE ((((LENGTH(V_TXTQUEBRA)) <= 15)) AND
          (LENGTH(V_TXTRESTO) > 0)) LOOP
      V_POSESPACO := INSTR(V_TXTRESTO, ' ');
      IF V_POSESPACO = 0 THEN
         V_POSESPACO := LENGTH(V_TXTRESTO);
      END IF;

      V_TXTRETORNO3 := V_TXTQUEBRA;
      V_TXTQUEBRA := V_TXTQUEBRA||SUBSTR(V_TXTRESTO, 1, V_POSESPACO);
      V_TXTRESTO  := SUBSTR(V_TXTRESTO, (V_POSESPACO+1), LENGTH(V_TXTQUEBRA));
      IF (LENGTH(V_TXTQUEBRA) <= 15) THEN
         V_TXTRETORNO3 := V_TXTQUEBRA;
      END IF;

      --V_COUNT := V_COUNT +1;
      --IF V_COUNT = 2 THEN
        --RAISE_APPLICATION_ERROR(-20405,LENGTH(V_TXTQUEBRA)||'***'||V_TXTQUEBRA||'***'||V_TXTRESTO||'***'||V_TXTRETORNO, TRUE);
      --END IF;
   END LOOP;

   V_TXTQUEBRA  := '';
   V_TXTRESTO := SUBSTR(P_TEXTO,LENGTH(V_TXTRETORNO1||V_TXTRETORNO2||V_TXTRETORNO3)+1, LENGTH(P_TEXTO));
   V_POSESPACO := INSTR(V_TXTRESTO, ' ');
   IF V_POSESPACO = 0 THEN
      V_POSESPACO := LENGTH(V_TXTRESTO);
   END IF;
   V_TXTQUEBRA := V_TXTQUEBRA||SUBSTR(V_TXTRESTO, 1, V_POSESPACO);
   V_TXTRESTO  := SUBSTR(V_TXTRESTO, (V_POSESPACO+1), LENGTH(V_TXTRESTO));
   V_TXTRETORNO4 := V_TXTQUEBRA;
   V_COUNT := 0;

   WHILE ((((LENGTH(V_TXTQUEBRA)) <= 15)) AND
          (LENGTH(V_TXTRESTO) > 0)) LOOP
      V_POSESPACO := INSTR(V_TXTRESTO, ' ');
      IF V_POSESPACO = 0 THEN
         V_POSESPACO := LENGTH(V_TXTRESTO);
      END IF;

      V_TXTRETORNO4 := V_TXTQUEBRA;
      V_TXTQUEBRA := V_TXTQUEBRA||SUBSTR(V_TXTRESTO, 1, V_POSESPACO);
      V_TXTRESTO  := SUBSTR(V_TXTRESTO, (V_POSESPACO+1), LENGTH(V_TXTQUEBRA));
      IF (LENGTH(V_TXTQUEBRA) <= 15) THEN
         V_TXTRETORNO4 := V_TXTQUEBRA;
      END IF;

      --V_COUNT := V_COUNT +1;
      --IF V_COUNT = 2 THEN
        --RAISE_APPLICATION_ERROR(-20405,LENGTH(V_TXTQUEBRA)||'***'||V_TXTQUEBRA||'***'||V_TXTRESTO||'***'||V_TXTRETORNO, TRUE);
      --END IF;
   END LOOP;

   V_TXTQUEBRA  := '';
   V_TXTRESTO := SUBSTR(P_TEXTO,LENGTH(V_TXTRETORNO1||V_TXTRETORNO2||V_TXTRETORNO3||V_TXTRETORNO4)+1, LENGTH(P_TEXTO));
   V_POSESPACO := INSTR(V_TXTRESTO, ' ');
   IF V_POSESPACO = 0 THEN
      V_POSESPACO := LENGTH(V_TXTRESTO);
   END IF;
   V_TXTQUEBRA := V_TXTQUEBRA||SUBSTR(V_TXTRESTO, 1, V_POSESPACO);
   V_TXTRESTO  := SUBSTR(V_TXTRESTO, (V_POSESPACO+1), LENGTH(V_TXTRESTO));
   V_TXTRETORNO5 := V_TXTQUEBRA;
   V_COUNT := 0;

   WHILE ((((LENGTH(V_TXTQUEBRA)) <= 15)) AND
          (LENGTH(V_TXTRESTO) > 0)) LOOP
      V_POSESPACO := INSTR(V_TXTRESTO, ' ');
      IF V_POSESPACO = 0 THEN
         V_POSESPACO := LENGTH(V_TXTRESTO);
      END IF;

      V_TXTRETORNO5 := V_TXTQUEBRA;
      V_TXTQUEBRA := V_TXTQUEBRA||SUBSTR(V_TXTRESTO, 1, V_POSESPACO);
      V_TXTRESTO  := SUBSTR(V_TXTRESTO, (V_POSESPACO+1), LENGTH(V_TXTQUEBRA));
      IF (LENGTH(V_TXTQUEBRA) <= 15) THEN
         V_TXTRETORNO5 := V_TXTQUEBRA;
      END IF;

      --V_COUNT := V_COUNT +1;
      --IF V_COUNT = 2 THEN
        --RAISE_APPLICATION_ERROR(-20405,LENGTH(V_TXTQUEBRA)||'***'||V_TXTQUEBRA||'***'||V_TXTRESTO||'***'||V_TXTRETORNO, TRUE);
      --END IF;
   END LOOP;

   IF V_TXTRETORNO1 IS NOT NULL OR
      V_TXTRETORNO2 IS NOT NULL OR
      V_TXTRETORNO3 IS NOT NULL OR
      V_TXTRETORNO4 IS NOT NULL OR
      V_TXTRETORNO5 IS NOT NULL THEN
      OUT_RECORD.DESCTEXTO1 := V_TXTRETORNO1;
      OUT_RECORD.DESCTEXTO2 := V_TXTRETORNO2;
      OUT_RECORD.DESCTEXTO3 := V_TXTRETORNO3;
      OUT_RECORD.DESCTEXTO4 := V_TXTRETORNO4;
      OUT_RECORD.DESCTEXTO5 := V_TXTRETORNO5;
      PIPE ROW (OUT_RECORD);
   END IF;

--   RETURN(V_TXTRETORNO);
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('QUEBRA_TEXTO','6.05.xxx'); END;
/
