pllua / pllua-deprecated

[DEPRECATED] This repository is no longer maintained. Please follow https://github.com/pllua/pllua
197 stars 16 forks source link

exception? #44

Open bjne opened 7 years ago

bjne commented 7 years ago

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

eugwne commented 7 years ago

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

smarwei commented 7 years ago

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 commented 7 years ago

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

smarwei commented 7 years ago

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

eugwne commented 7 years ago

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"