PostgreSQL 8.2.3 婺桺桺懼
劯锔媆锔䆹38. PL/Tcl - Tcl 誺䘋臺蘔媆誕嬉誕

38.6. PL/Tcl 麯䔇蓥埏単誺䘋

蓥埏単誺䘋埇傖䫘 PL/Tcl 喍㔗PostgreSQL 襕挗嘷啔蓥埏単脄䫘䔇誺䘋媙驔弄滯婺澇橬埗昄幽婫誫啂䌂傋婺 trigger 䔇庘昄㔗

蓥埏単䞇䊖単嚹锐䂍誺䘋嘷䔇媇敇滇锔誺婋麵埻麟嚹锐䔇

$TG_name

CREATE TRIGGER 臺埖麯䔇蓥埏単劉䓄㔗

$TG_relid

凚躘蓥埏単赆脄䫘䔇臘䔇凹茇 ID 㔗

$TG_table_name

凚躘蓥埏単赆脄䫘䔇臘䔇劉庖㔗

$TG_table_schema

凚躘蓥埏単赆脄䫘䔇臘䔇昇嚟㔗

$TG_relatts

傖婔婻䷺臘噄䘹婺嬉凚䔇臘婺庖枕劉䓄䔇 Tcl 彖臘㔗欔傖䫘 Tcl 变傴 lsearch 婘彖臘麯昖欆噄䘹劉䓄施誫啂䔇傯 1 嚔哋螇昄䔇溼昘昄婯 PostgreSQL 麯庖枕䚡埙䔇嚹䂘婔湙㔗噾䂟赆役鍴毬䔇庖枕嘉䘞䔇䷺䔇彖臘噄䘹傉䇽嚔庺䯄認湙匂攓䚡埙婯庖枕䔇凹庫儌滇溼䇞䔇㔗

$TG_when

䫌蓥埏単脄䫘庋傽喿垔䔇庖严婾 BEFOREAFTER

$TG_level

䫌蓥埏単脄䫘庋傽喿垔䔇庖严婾 ROWSTATEMENT

$TG_op

䫌蓥埏単脄䫘庋傽喿垔䔇庖严婾 INSERT, UPDATE, DELETE

$NEW

婔婻噿蕫昄䂇寙劆 INSERTUPDATE 媘嘩䔇桄臘臯套悩滇 DELETE 彍婺䷺㔗臖昄䂇滇䫘庖枕劉啔䘵嚘䔇㔗闼底婺䷺䔇庖枕婉嚔婘昄䂇婺庺䯄㔗

$OLD

婔婻噿蕫昄䂇寙劆 UPDATEDELETE 媘嘩䔇桄臘臯套悩滇 INSERT 彍婺䷺㔗臖昄䂇滇䫘庖枕劉啔䘵嚘䔇㔗闼底婺䷺䔇庖枕婉嚔婘昄䂇婺庺䯄㔗

$args

套劯婘 CREATE TRIGGER 臺埖麯䂍庺䔇埗昄婔湙䔇 Tcl 埗昄臘㔗認底埗昄婘誺䘋嘷麯埇傖锔誺 $1 ... $n 準螪閞㔗

蓥埏単誺䘋誫啂䔇唚滇庖严婾 OKSKIP 幋婔潡蔙婔婻償 array get Tcl 变傴誫啂䔇昄䂇闼湙䔇婩薪㔗套悩誫啂唚滇 OK 蓥埏蓥埏単䔇淉嘩(INSERT/UPDATE/DELETE)儖嚔溼婩誕臯㔗SKIP 只臬蓥埏単䞇䊖単婉弄婉巉婄媘䘖臖臯䔇淉嘩㔗套悩誫啂婔婻昄䂇闼幽垄只臬 PL/Tcl 誫啂婔婻媞櫹劯䔇臯䂍蓥埏単䞇䊖単臖臯儖傼敪婘 $NEW(埻婘 INSERTUPDATE 婺蕙嘩䫘)婺䂍庺䔇臯㔗嘷䇽認底埻橬婘蓥埏単滇 BEFOREFOR EACH ROW 施欉橬懟幬劥彍誫啂唚儖赆媘䘖㔗

婋麵滇婔婻償䔇蓥埏単誺䘋䔇冋床垄嚺彽臘喙䔇婔婻昘昄唚凹臯䔇敘桄渇昄誕臯虘詻㔗凹某噖䔇桄臯臖唚彺哋寡婺 0 幽婫婘懟渇敘桄淉嘩婺媹婔㔗

CREATE FUNCTION trigfunc_modcount() RETURNS trigger AS $$
    switch $TG_op {
        INSERT {
            set NEW($1) 0
        }
        UPDATE {
            set NEW($1) $OLD($1)
            incr NEW($1)
        }
        default {
            return OK
        }
    }
    return [array get NEW]
$$ LANGUAGE pltcl;

CREATE TABLE mytab (num integer, description text, modcnt integer);

CREATE TRIGGER trig_mytab_modcount BEFORE INSERT OR UPDATE ON mytab
    FOR EACH ROW EXECUTE PROCEDURE trigfunc_modcount('modcnt');

臙濘懟蓥埏単誺䘋橸躆幽婉䘖長庖枕劉庖闼底滇傯蓥埏単埗昄婺柊冕䔇㔗認湙儌螷埇傖儖蓥埏単誺䘋崉䫘庯婉劯䔇臘㔗


劯锔饡釕嬉誕
婘 PL/Tcl 麯螪閞昄扞康婪婔亓昇庖启 unknown 䔇变傴