Creating onBeforeCreate dynamic hook using SQL language - orientdb

I want to create a dynamic hook for the onBeforeCreate event using SQL to validate a record. How do you access the properties of the new record within the function?
I'm using OrientDB 3.0.4. From the studio, I enter these commands into a new database to set it up:
create class Kid extends V,OTriggered;
create property Kid.FirstName String;
create property Kid.LastName String;
create property Kid.Age Integer;
create property Kid.Grade Integer;
create function CreatingKid 'begin; console.log "Creating Kid"; commit; return "ok";' language SQL;
alter class Kid custom onBeforeCreate=CreatingKid;
I use the following to test it out and it doesn't seem to work. Nothing gets printed in the log:
insert into Kid(FirstName, LastName, Age, Grade)values('Karen', 'Tooyoung', 8, 10);
I have two questions: 1) how do I know if the CreatingKid function is being called (I'm not seeing the "Creating Kid" message in the console), and 2) how should I change the CreatingKid function to compare the age and grade and reject the record if the age minus grade is less than 5?

Related

Obtain primary key of newly inserted row in PostgreSQL trigger

In PostgreSQL I'm trying to make a database trigger on an INSERT that passes the primary key of the new row as the payload of a channel notification using NOTIFY
I'm having trouble wrapping my head around the correct usage age of the NEW variable in this context. No matter what I try seems to generate a syntax error.
Below is a simplified example of my use case.
CREATE OR REPLACE FUNCTION table_event_notify_function()
RETURNS trigger AS $$
BEGIN
NOTIFY table_event_notify, NEW.id;
return NULL;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER table_events_insert_trigger AFTER INSERT ON table_events EXECUTE PROCEDURE table_event_notify_function();
psycopg2.errors.SyntaxError: syntax error at or near "NEW"
The payload parameter for NOTIFY must be a constant. To provide a dynamic parameter, you need to use pg_notify()
perform pg_notifiy('table_event_notify', new.id::txt);

DB2oC (DB2 on Cloud) : Facing "attempted to modify data but was not defined as MODIFIES SQL DATA" error

I have created very simple function in DB2oC as below, which has one UPDATE sql statement and one SELECT sql statement along with MODIFIES SQL DATA. But still I get the below error, though I have specified MODIFIES SQL DATA. I did GRANT ALL on that TEST table to my user id and also did GRANT EXECUTE ON FUNCTION to my user id on safe side. Can you please help to explain on what could be the issue?
I have simply invoked the function using SELECT statement like below:
SELECT TESTSCHEMA.MAIN_FUNCTION() FROM TABLE(VALUES(1));
SQL Error [38002]: User defined routine "TESTSCHEMA.MAIN_FUNCTION"
(specific name "SQL201211013006981") attempted to modify data but was
not defined as MODIFIES SQL DATA.. SQLCODE=-577, SQLSTATE=38002,
DRIVER=4.27.25
CREATE OR REPLACE FUNCTION MAIN_FUNCTION()
RETURNS VARCHAR(20)
LANGUAGE SQL
MODIFIES SQL DATA
BEGIN
DECLARE val VARCHAR(20);
UPDATE TEST t SET t.CONTENT_TEXT = 'test value' WHERE t.ID = 1;
select CONTENT_TEXT into val from TEST where ID = 1;
return val;
end;
Appreciate your help.
For the modifies SQL data clause , the usage of the function is restricted on Db2-LUW.
These restrictions do not apply for user defined functions that do not modify data.
For your specific example, that UDF will operate when used as the sole expression on the right hand side of an assignment statement in a compound-SQL compiled statemnent.
For example:
create or replace variable my_result varchar(20) default null;
begin
set my_result = main_function();
end#
Consider using stored procedures to modify table contents, instead of user defined functions.
You could avoid using a function, and just use a single "change data statement"
SELECT CONTENT_TEXT
FROM NEW TABLE(
UPDATE TEST t
SET t.CONTENT_TEXT = 'test value'
WHERE t.ID = 1
)

Hibernate: Pass array of Entities to PostgreSql function

Hibernate: 5.4.0.Final
PostgreSql: 9.6.6 (It would be an option to upgrade to latest version)
I have tables A, B and C. And the following postgresql function:
CREATE OR REPLACE FUNCTION myFunction(param1 "A"[], param2 "B") RETURNS SETOF "C" AS $$
BEGIN
......
END;
$$ LANGUAGE plpgsql;
So basicly, I want to pass an array of records from table A and a single record of B. As result, I want a list of records from table C. I want to call this function from hibernate. Note that the passed entities from table A are not persistent so it is not an option to pass an array of ids. Instead I want to pass the whole (not persistent) entities.
My current hibernate code looks like this:
ProcedureCall call = session.createStoredProcedureCall("myFunction");
call.registerParameter(1, <Type?>, ParameterMode.IN).bindValue(listOfEntities);
call.registerParameter(2, EntityForTableB.class, ParameterMode.IN).bindValue(entityForTableB);
((ResultSetOutput)call.getOutputs().getCurrent()).getResultList();
I cannot identify the right way to pass the function parameters. Currently, I get this error:
org.postgresql.util.PSQLException: ERROR: Function myFunction(bytea, bytea) does not exist
What would be the correct way to handle this? I found solutions where the entities are transformed to json format but it looked complicated. Would appreciate any advice.

Errors: check compiler log

I am learning how to create and call a standalone function. For this reason I use sql developer and create the function there. I wrote the following code:
CREATE FUNCTION get_bal(acc_no IN NUMBER)
RETURN NUMBER
IS acc_bal NUMBER(11,2);
BEGIN
SELECT order_total
INTO acc_bal
FROM orders
WHERE customer_id = acc_no;
RETURN(acc_bal);
END;
/
compiler protocoll displays the following error
what is wrong with the sql developer?
table or view does not exist. run a select query in the same connection to check if the table orders exists or not. else you create the table. or check the connection, whether you are using the correct one.

Execute triggers function of another schema on the actual chema

my problem is easy to explain with an example: I have a 'common' schema (the public one?) where I store common data between a clustered application.
For every instance of my application, I have a role (used as the application user).
And i have a common role, app_users, with read-only privileges on the common schema, and every application role is a member of app_users.
Now my problem is: how can i set a trigger on the app_a scheme that execute a function (procedure) in the common scheme, but affect the (and only the) app_a tables?
I mean:
// common_scheme, dummy function to emulate the mysql on update = now()
CREATE OR REPLACEFUNCTION update_etime() RETURNS TRIGGER AS $$
BEGIN
NEW.etime = date_part('epoch'::text, now())::int;
RETURN NEW;
END;
$$ language plpgsql;
// now, in the app_foo scheme, i have the table:
CREATE TABLE foo_table (fid serial not null primary key unique, label char(25));
// and the trigger:
CREATE TRIGGER foo_table_update_etime BEFORE UPDATE ON foo_talbe FOR EACH ROW EXECUTE PROCEDURE update_etime();
// ERROR: function update_etime() does not exist
CREATE TRIGGER foo_table_update_etime BEFORE UPDATE ON foo_talbe FOR EACH ROW EXECUTE PROCEDURE common_scheme.update_etime();
// ERROR: function common_scheme.update_etime() does not exist
The user that will access app_foo has the execute privilege on update_etime() function in common_schema.
Any idea?
I've googled around but the only solution I fount to call functions from other schemas is something like execute 'select * from ' || schema_name || '.table_name'; but i dont think this will do the trick in my case, becose the function must work with the 'local' scheme.
Your second set of syntax should work... the one with "EXECUTE PROCEDURE common_scheme.update_etime();"
If it isn't finding the function, I'd guess that you either have created it in a different schema than you think it is in, or you haven't created it at all (and note, your example create syntax has a bug, no space between "replace" and "function", which would cause an error when trying to create the function. Try doing a:
\df *.update_etime
As superuser to verify the function exists and is in the location you think it is in. HTH.