-- DROP FUNCTION g_master_stamp();

CREATE OR REPLACE FUNCTION g_master_stamp()
  RETURNS "trigger" AS
$$
        BEGIN
    IF(TG_OP = 'INSERT') THEN
        IF (NEW.upddate IS NULL) THEN
            NEW.upddate := current_timestamp;
        END IF;
        IF (NEW.adddate IS NULL) THEN
            NEW.adddate := current_timestamp;
        END IF;
        IF (NEW.delflg IS NULL) THEN
            NEW.delflg := false;
        END IF;
        IF (NEW.updcnt IS NULL) THEN
            NEW.updcnt := 0;
        END IF;
        return new;
    ELSIF(TG_OP = 'UPDATE') THEN
        NEW.upddate := current_timestamp;
        NEW.updcnt := OLD.updcnt + 1;
        return new;
    END IF;
    RETURN NULL;
END;
$$
  LANGUAGE 'plpgsql' VOLATILE;

