
-- Copyright (C) 2011-2013, 2024, 2025 The Meme Factory, Inc.,
--           http://www.karlpinc.com/
-- Copyright (C) 2004-2011, Karl O. Pinc <kop@karlpinc.com>
--
--  This program is free software: you can redistribute it and/or modify
--  it under the terms of the GNU Affero General Public License as published
--  by the Free Software Foundation, either version 3 of the License, or
--  (at your option) any later version.
--
--  This program is distributed in the hope that it will be useful,
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--  GNU Affero General Public License for more details.
--
--  You should have received a copy of the GNU Affero General Public License
--  along with this program.  If not, see <https://www.gnu.org/licenses/>.
--
-- Karl O. Pinc <kop@karlpinc.com>

-- DO NOT EDIT THIS FILE.  It was automatically generated.  Edit
-- the *.m4 files instead.  (Files _should_ be re-created by
-- typing 'make', with the appropriate target, at the command line.)

--  
-- isok_queries
--

CREATE FUNCTION isok_queries_update_func ()
  RETURNS trigger
  LANGUAGE plpgsql
  AS $$
  BEGIN
  -- Function for isok_queries update triggers
  --
  -- Copyright (C) 2015, 2025 The Meme Factory, Inc., http://www.karlpinc.com/
  -- Distributed under the GNU Affero General Public License, version 3 or later.

  
  IF NEW.IQName <> OLD.IQName THEN
    -- IQName has changed
    RAISE EXCEPTION integrity_constraint_violation USING
          MESSAGE = 'Error on UPDATE of @extschema@.ISOK_QUERIES'
        , DETAIL =  'Value (IQName) = (' || OLD.IQName
                    || '): @extschema@.ISOK_QUERIES.IQName cannot be changed';
    RETURN NULL;
  END IF;


  IF OLD.error <> NEW.error AND NEW.error THEN
    PERFORM 1
      FROM @extschema@.isok_results
      WHERE @extschema@.isok_results.iqname = NEW.iqname
            AND @extschema@.isok_results.resolved IS NOT NULL;
    IF FOUND THEN
      RAISE EXCEPTION integrity_constraint_violation USING
            MESSAGE = 'Error on UPDATE of ISOK_QUERIES'
          , DETAIL = 'Key (IQName) = (' || NEW.iqname
                     || '): Value (Error) = (' || NEW.error
                     || '): Cannot make into an error when there are already'
                     || ' ISOK_RESULTS rows with a TRUE Resolved value';
      RETURN NULL;
    END IF;
  END IF;

  RETURN NULL;
  END;
$$;


CREATE TRIGGER isok_queries_update_trigger
  AFTER UPDATE
  ON isok_queries FOR EACH ROW
  EXECUTE PROCEDURE isok_queries_update_func();
