【PB续命03】借用Oracle的编码解码续命

【PB续命02】感觉比较乱,整理一下,封装一个Oracle的包,把编码和解码放在一起,包名为BLM_ENCODE

1 包头

create or replace package BLM_ENCODE is
/*
  -- Author  : GROMS
  -- Created : 2023-10-23 9:18:12
  -- Purpose : 编码与解码
  
  -- Public type declarations
  type  is ;
  
  -- Public constant declarations
   constant  := ;

  -- Public variable declarations
   ;

  -- Public function and procedure declarations
  function ( ) return ;
 */
 
    --Base64 编码
    function base64_encode(plaintext varchar2 ) return varchar2;
 
    --Base64 解码
    function base64_decode(base64Encode varchar2 ) return varchar2;
    
    /* 
    --base64编码与解码 应用示例
    select blm_encode.base64_encode('blma5217') from dual
    --编码后结果:YmxtYTUyMTc=
    select blm_encode.base64_decode('YmxtYTUyMTc=') from dual
    --解码后结果:blma5217
    */
    
    
    --UrlEncode编码
    FUNCTION url_encode(urlString IN VARCHAR2) RETURN VARCHAR2;
    
    --UrlDecode解码
    FUNCTION url_decode(urlencode IN VARCHAR2) RETURN VARCHAR2;
    
    /* 
    --URL编码与解码 应用示例
    select blm_encode.url_encode('白龙马5217') from dual
    --编码后结果:%E7%99%BD%E9%BE%99%E9%A9%AC5217
    select blm_encode.url_decode('%E7%99%BD%E9%BE%99%E9%A9%AC5217') from dual
    --解码后结果:blma5217
    */

end BLM_ENCODE;

2 包体

create or replace package body BLM_ENCODE is
/*
  -- Private type declarations
  type  is ;
  
  -- Private constant declarations
   constant  := ;

  -- Private variable declarations
   ;

  -- Function and procedure implementations
  function ( ) return  is
     ;
  begin
    ;
    return();
  end;


begin
  -- Initialization
  ;
*/

    --Base64 编码
    function base64_encode(plaintext varchar2 ) return varchar2
      as 
    begin
      return utl_raw.cast_to_varchar2(utl_encode.base64_encode(utl_raw.cast_to_raw(plaintext)))  ;
    end;
    
    --Base64 解码
    function base64_decode(base64Encode varchar2 ) return varchar2
      as 
    begin
      return (utl_raw.cast_to_varchar2(utl_encode.base64_decode(utl_raw.cast_to_raw(base64Encode))))  ;
    end;
    
    --UrlEncode编码
    FUNCTION url_encode(urlString IN VARCHAR2) RETURN VARCHAR2 
       AS
    BEGIN--utl_url.escape()该方法只能在函数中调用
       RETURN utl_url.escape(urlString, TRUE, 'utf-8');
    END;
    
    --UrlDecode解码
    FUNCTION url_decode(urlencode IN VARCHAR2) RETURN VARCHAR2 
       AS
    BEGIN--utl_url.escape()该方法只能在函数中调用
       RETURN utl_url.unescape(urlencode, 'utf-8');
    END;
    
end BLM_ENCODE;

你可能感兴趣的:(oracle,数据库)