[Pljava-dev] Rules

From: john(dot)bester at softco(dot)co(dot)za (John Bester)
To:
Subject: [Pljava-dev] Rules
Date: 2009-09-04 05:49:25
Message-ID: 4AA0AA65.3020104@softco.co.za
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pljava-dev

Hi,

I have searched quite a bit, but it seems there is no information on how
to use PL/Java to create a rule. Is it at all possible to write a rule
in the following way to let a function handle the events on a table:

CREATE OR REPLACE RULE someRule AS ON INSERT
TO someTable
DO INSTEAD SELECT someJavaFunction(NEW);

The reason for my asking is that I am trying to see if I can develop an
easy way to deploy a PL/Java jar by using Java annotations. For example,
if I write a Java function as follows:

package some.package;

public class TriggerFunctions {
@PGTriggerAfter("someTrigger")
@PGOperation({PGOperation.INSERT,PGOperation.UPDATE})
@PGTable({"someTable","anotherTable"})
public static void handlerForSomeTrigger(TriggerData data)
throws SQLException {
// Some logic
}
}

I run my deployment script generator and it spits out SQL that looks
like this:

CREATE OR REPLACE FUNCTION someTrigger
RETURNS trigger
AS 'some.package.TriggerFunctions.handlerForSomeTrigger'
LANGUAGE java;

DROP TRIGGER IF EXISTS someTrigger_someTable ON entity;
CREATE TRIGGER someTrigger_someTable AFTER INSERT OR UPDATE
ON someTable FOR EACH ROW
EXECUTE PROCEDURE syncFromDevice;

DROP TRIGGER IF EXISTS someTrigger_anotherTable ON address;
CREATE TRIGGER someTrigger_anotherTable AFTER INSERT OR UPDATE
ON anotherTable FOR EACH ROW
EXECUTE PROCEDURE someTrigger;

Now when it comes to rules, you could do the following:

CREATE OR REPLACE FUNCTION someRule(Field1 as text, Field2 as text)
RETURNS void
AS 'some.package.RuleFunctions.handlerForSomeRule'
LANGUAGE java;

CREATE OR REPLACE RULE someRule_someTable AS ON INSERT
TO someTable
DO INSTEAD SELECT someRule(NEW.Field1, NEW.Field2);

The problem with this is that you have to know how to match the fields
in the table to the parameters in the Java method. At runtime (when I
generate the script), you can only determine the types of the parameter,
not their names, so doing some kind of automatic field assignments is
not an easy task. Therefore, if you could write the function with some
other parameter such as "RuleData" (as in TriggerData for triggers) or
ResultSet as a single parameter, it would make this possible.

Any ideas would be appreciated.

Kind regards
John Bester

Browse pljava-dev by date

  From Date Subject
Next Message iMDT - Tiago Jacobs 2009-10-08 04:16:46 [Pljava-dev] Compiling with JDK6
Previous Message Jatinder Sangha 2009-09-03 15:23:23 [Pljava-dev] PLJava SIGSEGV on Linux