[Schmitzm-commits] r1831 - trunk/schmitzm-db/src/main/java/de/schmitzm/postgres

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Sat Jan 21 21:19:23 CET 2012


Author: alfonx
Date: 2012-01-21 21:19:22 +0100 (Sat, 21 Jan 2012)
New Revision: 1831

Modified:
   trunk/schmitzm-db/src/main/java/de/schmitzm/postgres/PGUtil.java
Log:
PGUtil, neue Methode:
	
public static boolean existsTable(Connection c, String tableName) throws SQLException 
	
		/**
	 * Existiert ein relationsname als Tabelle?
	 * 
	 * @param tableName
	 *            Darf eine Schemaangabe mit . Trenner enthalten.
	 * @throws SQLException
	 */


Modified: trunk/schmitzm-db/src/main/java/de/schmitzm/postgres/PGUtil.java
===================================================================
--- trunk/schmitzm-db/src/main/java/de/schmitzm/postgres/PGUtil.java	2012-01-18 11:42:51 UTC (rev 1830)
+++ trunk/schmitzm-db/src/main/java/de/schmitzm/postgres/PGUtil.java	2012-01-21 20:19:22 UTC (rev 1831)
@@ -701,6 +701,34 @@
 		}
 	}
 
+	/**
+	 * Existiert ein relationsname als Tabelle?
+	 * 
+	 * @param tableName
+	 *            Darf eine Schemaangabe mit . Trenner enthalten.
+	 * @throws SQLException
+	 */
+	public static boolean existsTable(Connection c, String tableName) throws SQLException {
+
+		String schema = "public";
+
+		if (tableName.contains(".")) {
+			final String[] split = tableName.split("\\.");
+			schema = split[0];
+			tableName = split[1];
+		}
+
+		final String checkTableExists = "SELECT * FROM pg_tables where schemaname = '" + schema + "' and tablename = '"
+				+ tableName + "' LIMIT 1";
+		ResultSet rs = c.createStatement().executeQuery(checkTableExists);
+
+		try {
+			return rs.next();
+		} finally {
+			rs.close();
+		}
+	}
+
 	public static void dropViewIfExists(Connection c, String viewName) throws SQLException {
 		final String dropView = "DROP VIEW IF EXISTS " + viewName;
 		c.createStatement().executeUpdate(dropView);



More information about the Schmitzm-commits mailing list