Rules

View: New views
1 Messages — Rating Filter:   Alert me  

Rules

by Stellenbosser :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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



_______________________________________________
Pljava-dev mailing list
Pljava-dev@...
http://pgfoundry.org/mailman/listinfo/pljava-dev