public inbox for [email protected]  
help / color / mirror / Atom feed
Subject: [Pljava-dev] Rules
Date: Fri, 04 Sep 2009 07:49:25 +0200
Message-ID: <[email protected]> (raw)

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








reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Subject: Re: [Pljava-dev] Rules
  In-Reply-To: <[email protected]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox