Porting Oracle procedure to PostgreSQL (exception codes from orafce for utl_file module)

若如初见. 提交于 2019-12-08 12:22:50

问题


I am in the middle of a database migration from Oracle to PostgreSQL. We are using ora2pg to make the conversion the plus automatic possible and the orafce plugin for PostgreSQL for functions compatibility.

We have just started and a lot of work is to be done.

Just now I am with stored procedures (output from ora2pg script) and after resolving the different syntax errors I does not know how to solve the last one.

In particular the problematic code is the following:

 select utl_file.put_line(vIdLog,'******************************** COMPTE RENDU PURGE_DOUBLONS_FS **************************');
 select utl_file.put_line(vIdLog,'*      - Debut du traitement le          : '||vDateDebut);
 select utl_file.put_line(vIdLog,'*');
 select utl_file.put_line(vIdLog,'*      - Nb de lun appairées à plusieurs ZV  : ' || vNbLunMultiZV);
 select utl_file.put_line(vIdLog,'*      - Nb FS appairée à plusieurs ZV       : ' || vNbUpdateFsMultiZV);
 select utl_file.put_line(vIdLog,'*      - Nb Liens Lun/FS en suppression logique    : ' || vNbUpdateLunMultiZV);
 select utl_file.put_line(vIdLog,'*      - Nb Liens FS en suppression logique    : ' || vNbUpdateFsMultiZV);
 select utl_file.put_line(vIdLog,'*      - Nb Liens FS(ZG mono ZV)en suppression logique    : ' || vNbUpdateLunFSZVseule);
 select utl_file.put_line(vIdLog,'*      - Nb Liens FS(ZG mono ZV)en suppression logique 2  : ' || vNbUpdateLunFSZVseule2);
 select utl_file.put_line(vIdLog,'*      - Nb Liens LUN/FS ZV obsolètes             : ' || vNbOldLunZV);
 select utl_file.put_line(vIdLog,'*      - Nb Liens LUN/FS ZG obsolètes             : ' || vNbOldLunZG);
 select utl_file.put_line(vIdLog,'*      - Temps de traitement de calcul   : ' || OUTILS.time_to_char(tTotal));
 select utl_file.put_line(vIdLog,'*');
 select utl_file.put_line(vIdLog,'*      - Fin du calcul HOST_LUN le       : ' || to_char(clock_timestamp(), 'DD/MM/YYYY HH24:MI:SS'));
 select utl_file.put_line(vIdLog,'************************** FIN COMPTE RENDU PURGE_DOUBLONS_FS ****************************');
 select utl_file.fclose(vIdLog);
EXCEPTION
 when UTL_FILE.INVALID_PATH then
   select Fin_traitement_fichier('Erreur E/S');
   RAISE EXCEPTION '%', 'File location or filename was invalid.';
 when UTL_FILE.INVALID_MODE then
   select Fin_traitement_fichier('Erreur E/S');
   RAISE EXCEPTION '%', 'The open_mode parameter in FOPEN was invalid.';
 when others then
   select Fin_traitement_fichier(SQLERRM);
   RAISE NOTICE 'ERR005 : ERREUR TRAITEMENT PURGE_DOUBLONS_FS : %', SQLERRM;
   IF cFs%ISOPEN THEN
     CLOSE cFs;
   END IF;

The error produced is the following

ERROR:  syntax error at or near "UTL_FILE"
LINE 341:    RAISE EXCEPTION '%', 'File location or filename was inval...
                     ^
********** Error **********

If I use only the "when others" part of the exception treatment it works ok, so the problem comes from the constants UTL_FILE.INVALID_PATH and UTL_FILE.INVALID_MODE that are not recognised by PostgreSQL.

Any idea if how to treat exception codes from orafce for utl_file module?


回答1:


PLpgSQL doesn't allow to define own exceptions - so Orafce cannot to define UTL_FILE.* exceptions. You should to look to orafce source code file.c to list of used exceptions:

The code uses macro CUSTOM_EXCEPTION

#define CUSTOM_EXCEPTION(msg, detail) \
    ereport(ERROR, \
        (errcode(ERRCODE_RAISE_EXCEPTION), \
         errmsg("%s", msg), \
         errdetail("%s", detail)))

In this list you can see all PostgreSQL exceptions. So the name of used exception is raise_exception and the reason of exception is in SQLERRM variable. So your code should to look like:

WHEN RAISE_EXCEPTION THEN
  CASE SQLERRM
   WHEN 'UTL_FILE_INVALID_PATH' THEN
     PERFORM ...
   WHEN 'UTL_FILE_INVALID_MODE' THEN
     PERFORM ...
   ELSE 
     PERFORM ...
   END;


来源:https://stackoverflow.com/questions/43617176/porting-oracle-procedure-to-postgresql-exception-codes-from-orafce-for-utl-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!