Skip to content
This repository has been archived by the owner on Jan 4, 2021. It is now read-only.

exception? #44

Open
bjne opened this issue May 17, 2017 · 5 comments
Open

exception? #44

bjne opened this issue May 17, 2017 · 5 comments
Labels

Comments

@bjne
Copy link

bjne commented May 17, 2017

Is it possible to raise an sql exception from lua without calling function?
error() does not seem to be the proper solution here..

@eugwne
Copy link
Member

eugwne commented May 19, 2017

That can brake lua vm internal state. Lua catches only lua errors, and postgres - pg errors. Inside there is conversion lua<->pg errors

@smarwei
Copy link

smarwei commented Jul 2, 2017

So if my insert query I called with server.execute fails because there is a unique constraint violation, I can not catch the error inside of my pllua function?

@eugwne
Copy link
Member

eugwne commented Jul 2, 2017

@smarwei , as I mentioned, there is conversion lua->pg and pg->lua errors

@smarwei
Copy link

smarwei commented Jul 2, 2017

Is there a special way to catch these errors? I tried pcall(server.execute(throws unique violation)) but the error got propagated anyway.

@eugwne
Copy link
Member

eugwne commented Jul 5, 2017

part of example from subtransaction.sql:

CREATE TABLE accounts
(
  id bigserial NOT NULL,
  account_name text,
  balance integer,
  CONSTRAINT accounts_pkey PRIMARY KEY (id),
  CONSTRAINT no_minus CHECK (balance >= 0)
);
insert into accounts(account_name, balance) values('joe', 200);
insert into accounts(account_name, balance) values('mary', 50);
CREATE OR REPLACE FUNCTION pg_temp.sub_test2()
RETURNS bool AS $$

local f = function(data) 
    local p = server.prepare(" insert into accounts(id , account_name, balance) values($1, $2, $3)", {"int4", "text","int4"})
    p:execute (data)
    return true
  end 
  local status, err = subtransaction(f, {1,'joe 2', 100}) 
  if not status then
	print(err)
  end
  local status, err = subtransaction(f, {10,'joe 2', 100}) 
  return status
  $$ LANGUAGE pllua;
select pg_temp.sub_test2();

info message here: INFO: duplicate key value violates unique constraint "accounts_pkey"

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

No branches or pull requests

3 participants