terça-feira, 1 de outubro de 2019

Habilitar o Python no PostgreSQL


  • Cenário

Windows 10
PostgreSQL 9.5 (instalado em C:\Program Files\PostgreSQL\9.5)
Python 3.7.4 - Windows x86-64 executable installer (python-3.7.4-amd64.exe - instalado em C:\Python\Python37)

  • Problema 1

Ao executar o comando:

create extension plpython3u;

Ocorre o erro:

ERROR:  could not load library "C:/Program Files/PostgreSQL/9.5/lib/plpython3.dll": unknown error 126

segunda-feira, 4 de março de 2019

Função para validação do CNS (Cartão Nacional de Saúde)


drop function if exists fnc_valida_cns_aux(pNumero text);

create or replace function fnc_valida_cns_aux(pNumero text) returns int as 
$body$
declare
  vArray char[];
  vSoma int;
  vNum int;
  vNum2 int;
begin

  vArray := regexp_split_to_array(pNumero, '');
  vSoma := 0;
  
  for i in array_lower(vArray, 1) .. array_length(vArray, 1) loop  
    vNum := vArray[i];
    vNum2 := 15 - i + 1;
    vSoma := vSoma + (vNum * vNum2);    
  end loop;        

  return vSoma;

end;
$body$
language 'plpgsql';

drop function if exists fnc_valida_cns_def(pCNS text); 

create or replace function fnc_valida_cns_def(pCNS text) returns boolean as 
$body$
declare
  vPIS text;
  vSoma int;
  vResto int;
  vDV int;
  vResultado text;
  vRetorno boolean;  
begin

  vPIS := substring(pCNS, 1, 11);
  vSoma := fnc_valida_cns_aux(vPIS);
  vResto := mod(vSoma, 11);  
  vDV := 11 - vResto;
  
  if vDV = 11 then
    vDV := 0;
  end if;
  
  if vDV = 10 then
    vSoma := fnc_valida_cns_aux(vPIS) + 2;
    vResto := mod(vSoma, 11);  
    vDV := 11 - vResto;  
    vResultado := vPIS || '001' || cast(vDV as text);
  else
    vResultado := vPIS || '000' || cast(vDV as text);
  end if;
  
  vRetorno := pCNS = vResultado; 
  return vRetorno;

end;
$body$
language 'plpgsql';

drop function if exists fnc_valida_cns_prov(pCNS text); 

create or replace function fnc_valida_cns_prov(pCNS text) returns boolean as 
$body$
declare
  vSoma int;
  vResto int;
  vRetorno boolean;  
begin

  vSoma := fnc_valida_cns_aux(pCNS);
  vResto := mod(vSoma, 11);
  
  vRetorno := vResto = 0; 
  return vRetorno;

end;
$body$
language 'plpgsql';

drop function if exists fnc_valida_cns(pCNS text); 

create or replace function fnc_valida_cns(pCNS text) returns boolean as 
$body$
declare
  vRetorno boolean;
  vCNS text;
begin

  vRetorno := false;
  
  vCNS := substring(regexp_replace(pCNS, '[^0-9]+', '', 'g'), 1, 15);
 
  if length(vCNS) = 15 then
  
    if substring(vCNS, 1, 1) in ('1', '2') then
      vRetorno := fnc_valida_cns_def(vCNS);
    elsif substring(vCNS, 1, 1) in ('7', '8', '9') then
      vRetorno := fnc_valida_cns_prov(vCNS);
    end if;
  
  end if;

  return vRetorno;

end;
$body$
language 'plpgsql';


Referências:

http://esusab.github.io/integracao/ledi/regras/algoritmo_CNS.html
http://www.yanaga.com.br/2012/06/validacao-do-cns-cartao-nacional-de.html