Ticket #1769: cruizch_2012_04_10.patch

File cruizch_2012_04_10.patch, 1.3 KB (added by cruizch, 4 years ago)
  • long_xact.sql.in.c

     
    159159        AS $$
    160160DECLARE
    161161        schema text;
     162        exist boolean;
    162163BEGIN
    163164        IF NOT LongTransactionsEnabled() THEN
    164165                RAISE EXCEPTION 'Long transaction support disabled, use EnableLongTransaction() to enable.';
     
    170171                SELECT current_schema() into schema;
    171172        END IF;
    172173
    173         -- TODO: check for an already existing trigger ?
     174        EXECUTE 'SELECT EXISTS(SELECT * FROM pg_namespace, pg_class, pg_trigger WHERE' ||
     175                ' pg_namespace.nspname = ' || quote_literal(schema) ||
     176                ' AND pg_class.relname = ' || quote_literal($2) ||
     177                ' AND pg_class.oid = pg_trigger.tgrelid' ||
     178                ' AND pg_trigger.tgname = ' || quote_literal('check_auth') || ')' INTO exist;
     179       
     180        IF NOT exist THEN
     181                EXECUTE 'CREATE TRIGGER check_auth BEFORE UPDATE OR DELETE ON '
     182                        || quote_ident(schema) || '.' || quote_ident($2)
     183                        ||' FOR EACH ROW EXECUTE PROCEDURE CheckAuthTrigger('
     184                        || quote_literal($3) || ')';
     185        END IF;
    174186
    175         EXECUTE 'CREATE TRIGGER check_auth BEFORE UPDATE OR DELETE ON '
    176                 || quote_ident(schema) || '.' || quote_ident($2)
    177                 ||' FOR EACH ROW EXECUTE PROCEDURE CheckAuthTrigger('
    178                 || quote_literal($3) || ')';
    179 
    180187        RETURN 0;
    181188END;
    182189$$