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

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Thu Jul 21 14:27:29 CEST 2011


Author: alfonx
Date: 2011-07-21 14:27:28 +0200 (Thu, 21 Jul 2011)
New Revision: 1638

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-07-21 10:41:29 UTC (rev 1637)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/geotools/postgres/PGUtil.java	2011-07-21 12:27:28 UTC (rev 1638)
@@ -4,6 +4,8 @@
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.List;
 
 import org.apache.log4j.Logger;
 
@@ -46,8 +48,8 @@
 	 *            List der zuzuordnenden Privilegien
 	 * @throws SQLException
 	 */
-	public static void grantPrivilegesOnTable(Connection c, String targetRole, String tablename, Privileges... privileges)
-			throws SQLException {
+	public static void grantPrivilegesOnTable(Connection c, String targetRole, String tablename,
+			Privileges... privileges) throws SQLException {
 		// GRANT SELECT, UPDATE, INSERT ON mytable TO admin;
 		String sql = "GRANT " + LangUtil.stringConcatWithSep(",", (Object[]) privileges) + " ON " + tablename + " TO "
 				+ targetRole;
@@ -63,14 +65,14 @@
 	 *            List der zuzuordnenden Privilegien
 	 * @throws SQLException
 	 */
-	public static void revokePrivilegesOnTable(Connection c, String tablename, String targetRole, Privileges... privileges)
-			throws SQLException {
+	public static void revokePrivilegesOnTable(Connection c, String tablename, String targetRole,
+			Privileges... privileges) throws SQLException {
 		// GRANT SELECT, UPDATE, INSERT ON mytable TO admin;
 		String sql = "REVOKE " + LangUtil.stringConcatWithSep(",", (Object[]) privileges) + " ON " + tablename
 				+ " FROM " + targetRole;
 		c.createStatement().executeUpdate(sql);
 	}
-	
+
 	/**
 	 * Erstellt eine neue Rolle ohne besondere Starteigenschaften.
 	 */
@@ -155,4 +157,42 @@
 				"select 1 from pg_catalog.pg_namespace where nspname = '" + schemaname + "'");
 		return rs.next();
 	}
+
+	/**
+	 * Liefert eine iste der Rollen in denen der Übergebenen USER (Login role) ist. <br/>
+	 * Tested with PG 8.4
+	 * 
+	 * @throws SQLException
+	 */
+	public static List<String> listRolesForUser(Connection c, String username) throws SQLException {
+		List<String> roles = new ArrayList<String>();
+		ResultSet rs = c
+				.createStatement()
+				.executeQuery(
+						"select rolname from pg_user join pg_auth_members on (pg_user.usesysid=pg_auth_members.member) join pg_roles on (pg_roles.oid=pg_auth_members.roleid) where pg_user.usename='"
+								+ username + "'");
+		while (rs.next()) {
+			roles.add(rs.getString(1));
+		}
+		return roles;
+	}
+
+	/**
+	 * Liste aller Tabellennamen in diesem Schema, ohne die Schemaangabe im Namen. (Also 'anreden' und nicht
+	 * 'public.anreden') <br/>
+	 * Tested with PG 8.4
+	 * 
+	 * @param schemaname
+	 *            if <code>null</code>, <code>public</code> is used.
+	 */
+	public static List<String> listTablesInSchema(Connection c, String schemaname) throws SQLException {
+		List<String> tables = new ArrayList<String>();
+
+		String sql = "select tablename from pg_tables where schemaname = '" + schemaname + "'";
+		ResultSet rs = c.createStatement().executeQuery(sql);
+		while (rs.next()) {
+			tables.add(rs.getString(1));
+		}
+		return tables;
+	}
 }



More information about the Schmitzm-commits mailing list