Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get SQL 'RAISE EXCEPTION' functionality from DB to Crystal #200

Open
ArtLinkov opened this issue Jan 15, 2020 · 7 comments
Open

Get SQL 'RAISE EXCEPTION' functionality from DB to Crystal #200

ArtLinkov opened this issue Jan 15, 2020 · 7 comments

Comments

@ArtLinkov
Copy link
Contributor

It is possible to write exceptions and notices in SQL functions (like in any programming language), for example:

CREATE OR REPLACE FUNCTION foo(IN str TEXT)
  RETURNS VOID
  LANGUAGE 'plpgsql'
  AS $$
     BEGIN
	IF str = 'yes' THEN
          RAISE NOTICE 'Glad we agree!';
	ELSE
	  RAISE EXCEPTION 'You know nothing John Snow!';
        END IF;
     END;
  $$;

I did not find any method of querying that also captures such exceptions and returns them to crystal.
Does this functionality exist or is it a missing feature?

@straight-shoota
Copy link
Contributor

straight-shoota commented Jan 15, 2020

There's Connection#on_notice for receiving notices.

@ArtLinkov
Copy link
Contributor Author

Ah, would you demonstrate how it can be used?

@straight-shoota
Copy link
Contributor

That's how I use it in specs to just print the notice to STDOUT:

db.connection.on_notice do |notice|
  puts
  print "NOTICE from PG: "
  puts notice
end

@ArtLinkov
Copy link
Contributor Author

I see!
Thanks @straight-shoota! 😄
It may be a good idea to add this to the docs somewhere.

@will
Copy link
Owner

will commented Jan 15, 2020

It may be a good idea to add this to the docs somewhere.

@ArtLinkov Would you be able to open a PR for the readme? There is a related part here https://github.com/will/crystal-pg#listennotify where it could go. Since you most recently knew what you were looking for, you'd be in the best spot to write it in such a way that would be easy for the next person having the same issue.

@ArtLinkov
Copy link
Contributor Author

@will PR sent

@jwoertink
Copy link
Contributor

I think this is closed by #201

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants