본문 바로가기
프로그래밍/PHP

[함수] 데이터 베이스간 통신에 쓰이는 PLSQL

by 백룡화검 2010. 4. 23.

 제가 한건 아니고,제 사수가 했는데,너무 유용한것 같아서,
많은 이와 공유하고 싶어서 이렇게 올립니다.
어디에 쓰이냐 하면,스트리밍 서비스를 제공 하는 사이트와,인증만을
제공하는 회사와 통신하여,각회원의 유효기간,그리고,사용 횟수,현제 사용 횟수
아이디,페스웨드등등..인증회사와 데이터 베이스통신을 한후
동영상을 보여줄것인가?아니면,보여주지 않을 것인가 하는 겁니다.
각 콘텐츠영상은 인증이 필요 하므로,재생은 불가능하고요.오직 데이터 베이스만이
통신 가능하므로,어쩔수 없이 PLSQL을 사용합니다.환경은 PostgreSQL입니다.

지난번에 답변주신,송효진님과 그네님 감사합니다.


CREATE OR REPLACE FUNCTION licence_chk(text, text)
  RETURNS text AS
$BODY$
----------------------------------------------------------------
/***************************************
* arg:
*      email    text
*      password  text
*
* return: text
*      1:존재 하지않는 회원
*      2:패스웨드가 일치하지 않음.
*      3:유효기간이 끝났슴.
*      9:그밖에 에러.
*      일자.:라이센스 기한일
*
****************************************/

/***************************************
宣言
****************************************/
declare
    p_email alias for $1;
    p_pass  alias for $2;

    ret    text;
    cnt    int4;
    rec    record;
    id      int8;
    pass    text;
    licence_expire  timestamp;
/***************************************
처리
****************************************/
begin
    ret := '9';
    if trim(p_email) = '' then
        --메일 지정이 없을 경우
        ret := 1;
    else
        select count(*) into cnt from m_kaiin where kaiin_mail = p_email;
        if cnt = 0 then
            --메일 주소가 없을 경우
            ret := '1';
        else
            select kaiin_id ,kaiin_pass,kaiin_licence_limit,kaiin_licence_count into rec  from m_kaiin  where kaiin_mail = p_email;
            if rec.kaiin_pass != p_pass then
                --패스웨드가 일치하지 않을 때.
                ret := '2';
            elsif rec.kaiin_licence_limit <= rec.kaiin_licence_count then
                ret := '4';
            else
                select test_end_ymd into licence_expire from m_test
                  where test_kaiin_id = rec.kaiin_id
                    and test_start_ymd::timestamp <= Now()::timestamp
                    and test_end_ymd::timestamp >= Now()::timestamp;
                if licence_expire is null then
                    --사용기한이 끝났을 경우.
                    ret := '3';
                else
                    ret := substr(licence_expire::text,1,10);
                   
                    --라이센스 카운트를 up
                    update m_kaiin set kaiin_licence_count = kaiin_licence_count+1 where kaiin_id = rec.kaiin_id;
                   
                   
                end if;
            end if;
        end if;

    end if;

    return ret;

end;
----------------------------------------------------------------
$BODY$
  LANGUAGE 'plpgsql' VOLATILE;

 

출처 : http://www.phpschool.com/gnuboard4/bbs/board.php?bo_table=tipntech&wr_id=57624&page=12