2

Say, I have the following in MyBatis mapper for a postgres table:

    <select id="selectValues" parameterType='int' resultType="SomeType">
        select foo from bar where baz=#{qux}
    </select>

A list of SomeType values can be returned from the table. But I'd like to check with a help of trigger, if the returned list is empty and, if so, give it a null value. It would probably look like:

CREATE TRIGGER mytrigger AFTER select ON bar FOR EACH STATEMENT EXECUTE PROCEDURE trigger_after_select ();

CREATE FUNCTION trigger_after_select () RETURNS trigger AS ' 
BEGIN 
if (select count(*) from bar)=0
then return NULL; 
...

I wonder if I'm moving in the right direction and if someone could tell how the rest of the trigger would look like (if the beginning looks suitable). An advice would be helpful, thanks in advice.

1 Answer 1

3

There is no "SELECT" trigger in Postgres.

If you change your SQL to:

select foo from bar where baz=#{qux}
union all
select null where not exists (select 1 from bar where baz=#{qux})

then you'll get the same effect. It will return a NULL value if there is no row with that value for the column baz.

(Disclaimer: I don't know MyBatis, so I don't know if it's possible to specify such a statement)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.