[Schmitzm-commits] r1803 - trunk/schmitzm-core/src/main/java/de/schmitzm/postgres
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Mon Dec 5 22:58:10 CET 2011
Author: alfonx
Date: 2011-12-05 22:58:10 +0100 (Mon, 05 Dec 2011)
New Revision: 1803
Modified:
trunk/schmitzm-core/src/main/java/de/schmitzm/postgres/PGUtil.java
Log:
Verbesserungen bei der Trigger- und Functionserstellung.
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/postgres/PGUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/postgres/PGUtil.java 2011-12-02 22:00:38 UTC (rev 1802)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/postgres/PGUtil.java 2011-12-05 21:58:10 UTC (rev 1803)
@@ -314,29 +314,80 @@
* @param name
* z.B.: keckformel wird dann zu keckformel()
* @param plCommands
- * z.B. NEW.kpk_kreis = NEW.risiko_soz+NEW.risiko_emo+NEW.risiko_moti +NEW.risiko_spr+NEW.risiko_wohl;
+ * z.B.
+ * <code>NEW.kpk_kreis = NEW.risiko_soz+NEW.risiko_emo+NEW.risiko_moti +NEW.risiko_spr+NEW.risiko_wohl;</code>
+ * OHNE <code>RETURN NEW</code>, das wird automatisch angehangen.
* @throws SQLException
*/
- public static void createOrReplaceFunction(Connection c, String name, String plCommands) throws SQLException {
+ public static void createOrReplaceTriggerFunction(Connection c, String name, String plCommands) throws SQLException {
// System.err.println(plCommands);
- c.createStatement().executeUpdate(
- "CREATE OR REPLACE FUNCTION " + name + "()" + "RETURNS trigger AS 'BEGIN " + plCommands
- + "RETURN NEW; END;' LANGUAGE 'plpgsql'");
+
+ // String sql = "CREATE OR REPLACE FUNCTION " + name + "()" + " RETURNS trigger AS $" + name
+ // + "$\nBEGIN\n?\nRETURN NEW;" + "\nEND;\n$" + name + "$ LANGUAGE 'plpgsql'";
+ // PreparedStatement ps = c.prepareStatement(sql);
+ // ps.setString(0, plCommands);
+ // ps.executeUpdate();
+
+ // String sql = "CREATE OR REPLACE FUNCTION " + name + "()" + " RETURNS trigger AS $" + name + "$\nBEGIN\n "
+ // + plCommands + " \nRETURN NEW;" + "\nEND;\n$" + name + "$ LANGUAGE 'plpgsql'";
+ // c.createStatement().executeUpdate(sql);
+
+ createOrReplaceFunction(c, name, null, "TRIGGER", null, plCommands + "\nRETURN NEW");
}
/**
+ *
* @param c
+ * @param name
+ * z.B.: keckformel wird dann zu keckformel()
+ * @param parameters
+ * Parameter der Funcktion, kann <code>null</code> sein.
+ * @param returns
+ * z.B. <code>TRIGGER</code> oder <code>SETOF bigint</code>
+ * @param declare
+ * Declare block oder <code>null</code>
+ * @param plCommands
+ * z.B. NEW.kpk_kreis = NEW.risiko_soz+NEW.risiko_emo+NEW.risiko_moti +NEW.risiko_spr+NEW.risiko_wohl;
+ */
+ public static void createOrReplaceFunction(Connection c, String name, String parameters, String returns,
+ String declare, String plCommands) throws SQLException {
+ // System.err.println(plCommands);
+
+ // String sql = "CREATE OR REPLACE FUNCTION " + name + "()" + " RETURNS trigger AS $" + name
+ // + "$\nBEGIN\n?\nRETURN NEW;" + "\nEND;\n$" + name + "$ LANGUAGE 'plpgsql'";
+ // PreparedStatement ps = c.prepareStatement(sql);
+ // ps.setString(0, plCommands);
+ // ps.executeUpdate();
+
+ parameters = LangUtil.removeWhitespacesToEmpty(parameters);
+ declare = LangUtil.removeWhitespacesToEmpty(declare);
+
+ String sql = "CREATE OR REPLACE FUNCTION " + name + "(" + parameters + ")" + " RETURNS " + returns + " AS $$\n"
+ + "DECLARE\n" + declare + "\n" + "BEGIN\n " + plCommands + ";" + "\nEND;\n$$ LANGUAGE 'plpgsql'";
+ c.createStatement().executeUpdate(sql);
+ }
+
+ /**
+ * Erstellt einen <code>FOR EACH ROW EXECUTE PROCEDURE</code> trigger für eine Tabelle
+ *
+ * @param c
* @param triggerName
* @param type
* z.B. "BEFORE INSERT OR UPDATE"
* @param tableName
* @param plCommands
- * Simple PSQL commands
+ * PSQL commands, e.g.<br/>
+ * <code>
+ IF ((TG_OP = 'UPDATE') or (TG_OP = 'INSERT') ) THEN
+ INSERT INTO workqueue values (NEW.id);
+ RETURN NEW;
+ END IF;
+ </code>
*/
public static void createOrRecreateTrigger(Connection c, String triggerName, String type, String tableName,
String plCommands) throws SQLException {
dropTriggerIfExists(c, triggerName, tableName);
- createOrReplaceFunction(c, triggerName + "_fn", plCommands);
+ createOrReplaceTriggerFunction(c, triggerName + "_fn", plCommands);
c.createStatement().executeUpdate(
"CREATE TRIGGER " + triggerName + " " + type + " ON " + tableName + " FOR EACH ROW EXECUTE PROCEDURE "
+ triggerName + "_fn" + "()");
@@ -464,7 +515,8 @@
if (fillFactor != null)
sqlFillFactor = " WITH (FILLFACTOR=" + (int) (fillFactor * 100) + ") ";
- String sqlTableSpace = (tablespace == null || tablespace.equalsIgnoreCase("null")) ? "" : " TABLESPACE " + tablespace;
+ String sqlTableSpace = (tablespace == null || tablespace.equalsIgnoreCase("null")) ? "" : " TABLESPACE "
+ + tablespace;
String sqlUsing = type == null ? "" : " USING " + type;
More information about the Schmitzm-commits
mailing list