<?php
global $config, $bd;


$bd->Execute("DROP FUNCTION IF EXISTS adiciona_data;");

$bd->Execute("CREATE FUNCTION adiciona_data(t1 datetime, t2 float(100,3), t3 varchar(50))
RETURNS datetime
DETERMINISTIC
BEGIN
IF (t3='SECOND') THEN RETURN TIMESTAMPADD(SECOND,t2,t1);
ELSEIF (t3='MINUTE') THEN RETURN TIMESTAMPADD(MINUTE,t2,t1);
ELSEIF (t3='HOUR') THEN RETURN TIMESTAMPADD(HOUR,t2,t1);
ELSEIF (t3='DAY') THEN RETURN TIMESTAMPADD(DAY,t2,t1);
ELSEIF (t3='WEEK') THEN RETURN TIMESTAMPADD(WEEK,t2,t1);
ELSEIF (t3='MONTH') THEN RETURN TIMESTAMPADD(MONTH,t2,t1);
ELSEIF (t3='QUARTER') THEN RETURN TIMESTAMPADD(QUARTER,t2,t1);
ELSEIF (t3='YEAR') THEN RETURN TIMESTAMPADD(YEAR,t2,t1);
END IF;
END;");

$bd->Execute("DROP FUNCTION IF EXISTS extrair;");

$bd->Execute("CREATE FUNCTION extrair(t1 varchar(50), t2 datetime)
RETURNS integer(100)
DETERMINISTIC
BEGIN
IF (t1='SECOND') THEN RETURN EXTRACT(SECOND FROM t2);
ELSEIF (t1='MINUTE') THEN RETURN EXTRACT(MINUTE FROM t2);
ELSEIF (t1='HOUR') THEN RETURN EXTRACT(HOUR FROM t2);
ELSEIF (t1='DAY') THEN RETURN EXTRACT(DAY FROM t2);
ELSEIF (t1='WEEK') THEN RETURN EXTRACT(WEEK FROM t2);
ELSEIF (t1='MONTH') THEN RETURN EXTRACT(MONTH FROM t2);
ELSEIF (t1='QUARTER') THEN RETURN EXTRACT(QUARTER FROM t2);
ELSEIF (t1='YEAR') THEN RETURN EXTRACT(YEAR FROM t2);
ELSEIF (t1='HOUR_MINUTE') THEN RETURN EXTRACT(HOUR_MINUTE FROM t2);
END IF;
END;");
?>
