[Schmitzm-commits] r1671 - trunk/schmitzm-core/src/main/java/de/schmitzm/geotools/postgres

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed Aug 10 01:10:20 CEST 2011


Author: alfonx
Date: 2011-08-10 01:10:19 +0200 (Wed, 10 Aug 2011)
New Revision: 1671

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/geotools/postgres/PGUtil.java
Log:


Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/geotools/postgres/PGUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/geotools/postgres/PGUtil.java	2011-08-09 23:06:26 UTC (rev 1670)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/geotools/postgres/PGUtil.java	2011-08-09 23:10:19 UTC (rev 1671)
@@ -35,10 +35,10 @@
 		Privileges(String sql) {
 			this.sql = sql;
 		}
-		
+
 		@Override
 		public String toString() {
-		  return this.sql;
+			return this.sql;
 		}
 	}
 
@@ -112,6 +112,8 @@
 	 * @param geoColumnName
 	 * @param srid
 	 */
+	@Deprecated
+	// tut noch nicht
 	public static void createOrUpdateGeometrsColumnsEntry(String tableName, String geoColumnName, int srid) {
 
 		String createGeometryEntrySQL = "INSERT INTO geometry_columns (f_table_catalog, f_table_schema, f_table_name, f_geometry_column, coord_dimension, srid, \"type\") VALUES ( '', 'public', '"
@@ -222,10 +224,94 @@
 	}
 
 	public static void removeAllPrivilegesFrom(Connection c, String rolename, String tablename) throws SQLException {
-		c.createStatement().executeUpdate("revoke all on TABLE " + tablename.toLowerCase() + " from " + rolename.toLowerCase() + " cascade");
+		c.createStatement().executeUpdate(
+				"revoke all on TABLE " + tablename.toLowerCase() + " from " + rolename.toLowerCase() + " cascade");
 	}
 
 	public static void dropRoleIfExists(Connection c, String rolename) throws SQLException {
 		c.createStatement().executeUpdate("drop role if exists " + rolename.toLowerCase());
 	}
+
+	public static void dropTriggerIfExists(Connection c, String triggerName, String tableName) throws SQLException {
+		c.createStatement().executeUpdate("drop TRIGGER if exists " + triggerName.toLowerCase() + " ON " + tableName);
+	}
+
+	/**
+	 * 
+	 * @param c
+	 * @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;
+	 * @throws SQLException
+	 */
+	public static void createOrReplaceFunction(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'");
+	}
+
+	/**
+	 * @param c
+	 * @param triggerName
+	 * @param type
+	 *            z.B. "BEFORE INSERT OR UPDATE"
+	 * @param tableName
+	 * @param plCommands
+	 *            Simple PSQL commands
+	 */
+	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);
+		c.createStatement().executeUpdate(
+				"CREATE TRIGGER " + triggerName + " " + type + " ON " + tableName + " FOR EACH ROW EXECUTE PROCEDURE "
+						+ triggerName + "_fn" + "()");
+	}
+
+	/**
+	 * @param tableName
+	 *            e.g. 'inseratabfragen_kvps'
+	 * @param columns
+	 *            e.g. 'inseratabfrage_id'
+	 * @param idxNamePostfix
+	 *            e.g. 'idx1'
+	 * @param unique
+	 *            <code>true</code> if the columns are unique. A UNIQUE INDEX will be created.
+	 */
+	public static void addIndex(Connection c, final String tableName, final String columns, String idxPostfix,
+			final boolean unique) throws SQLException {
+		final String idxName = (tableName + "_" + idxPostfix).replace(".", "_");
+
+		Statement s = c.createStatement();
+		// TODO DROP FIRST?
+		final String queryString = "CREATE " + (unique ? "UNIQUE " : "") + "INDEX " + idxName + " ON " + tableName
+				+ " (" + columns + ");";
+		try {
+			s.execute(queryString);
+			c.commit();
+			log.info("Neuen Index erstellt: " + queryString);
+		} catch (SQLException e) {
+			c.rollback();
+			Throwable cause = e;
+			if (e.getCause() != null)
+				cause = e.getCause();
+
+			// keine Ausgabe bei: already EXISTs, EXISTiert
+			// bereits,
+			// ...
+			// Ex abfangen, wenn IDX schon existiert
+
+			String msg = cause.getMessage();
+			if (msg.endsWith("already exists") || msg.endsWith("existiert bereits")) {
+				// TO NOTHING, IDX already exists
+				log.info("Index existierte bereits, nicht neuerstellt: " + queryString);
+			} else
+				log.error(e.getLocalizedMessage(), e);
+
+		}
+
+	}
+
 }



More information about the Schmitzm-commits mailing list