viernes, octubre 14, 2005

Postgresql

PostgreSQL es la base de datos Open Source más poderosa. Eso es lo que se dice por todas partes. Quienes la usamos todos los días podemos asegurar que es un pedazo de software extraordinario.

PostgreSQL tiene todas las características necesaria para desarrollar aplicaciones empresariales complejas y distribuidas: Transacciones aisladas, un extenso lenguaje sql, procedimientos en una variedad extensas de lenguajes.

Nosotros utilizamos PostgreSQL, preferiblemente, en servidores Linux. Pero para aquellos que aún están amarrados al pasado existe la versión de PostgreSQL nativa para Windows.

También existe versión para windows del  pgadmin  nuestra herramienta favorita para trabajar con PostgreSQL.

Por último compartimos unos ejemplos de funciones en postgreSQL que permiten ver la estructura general de las mismas: (alldates muy conveniente la encontré usando google ;-) )


create or replace function alldates(date,date) returns setof date
as
'
declare
        s alias for $1;
        e alias for $2;
        d date;
begin
        d := s;

        while d <= e
        LOOP
                return next d;
                select d + \'1 day\'::interval into d;
        END LOOP;

        return null;
end;
'
LANGUAGE 'plpgsql'



CREATE TYPE returnhorario AS
   (
    rempid int4,
    rfecha date,
    rhorai time,
    rhoraf time,
    rstatus int2);



CREATE OR REPLACE FUNCTION horarioempleado(int4, date)
  RETURNS returnhorario AS
'
declare
        empleadoid alias for $1;
        fecha alias for $2;
        r returnhorario;
begin
        select empleadoid,hd.fecha,hd.horai,hd.horaf,hd.status
from horario h,horariodet hd
where h.horarioid=hd.horarioid

and h.empleadoid=empleadoid and
(h.fechai<=fecha::date and h.fechaf>=fecha::date and
hd.fecha=fecha::date) into r;

if found then
return r;
exit;
else
select empleadoid,fecha,h.horai,h.horaf,0
from horario h
where h.empleadoid=empleadoid and
(h.fechai<=fecha::date and h.fechaf>=fecha::date) into r;
if found then
return r;
exit;
else
select empleadoid,fecha,null,null into r;
return r;
exit;
end if;

end if;

       return r;
end;
'
  LANGUAGE 'plpgsql';


CREATE OR REPLACE FUNCTION horarioempleado(int4, date,date)
  RETURNS setof returnhorario AS
'
declare
        empleadoid alias for $1;
        fechai alias for $2;
fechaf alias for $3;
r returnhorario;
f record;

begin
       

for f in select alldates as fecha from alldates(fechai,fechaf) loop

select * from horarioempleado(empleadoid,f.fecha) into r;
return next r;
end loop;
return null;
      
end;
'
  LANGUAGE 'plpgsql';


---------------------------------------------


CREATE OR REPLACE FUNCTION sumatardanzast(int4, date,date)
  RETURNS setof record as
'select sum(case
when rstatus is null then ''0''::interval
when entrada is null and rstatus=1 then ''0''::interval
when entrada is null then rhoraf-rhorai
when entrada-(rhorai+interval ''1 minute'')>=0 and rstatus=0 then entrada-rhorai
else ''0''::interval end) as sumatardanza,

sum(case
when rstatus is null then ''0''::interval
when salida is null and rstatus=1 then ''0''::interval
when salida is null then rhorai-rhoraf
else salida-rhoraf
end) as sumast from horarioempleado($1,$2,$3) he left join

(select * from vista_emp_es where empleadoid=$1 and f>=$2
and f<=$3) ves on he.rfecha=ves.f;    

'
  LANGUAGE 'sql';