[postgis-commits] svn - r2695 - branches/gSoC2007/lwgeom

postgis-commits at postgis.refractions.net postgis-commits at postgis.refractions.net
Fri Aug 17 11:34:00 PDT 2007


Author: yecarrillo
Date: 2007-08-17 11:33:58 -0700 (Fri, 17 Aug 2007)
New Revision: 2695

Modified:
   branches/gSoC2007/lwgeom/lwpostgis.sql.in
Log:
DropGeometryView and CreateGeometryView finished

Modified: branches/gSoC2007/lwgeom/lwpostgis.sql.in
===================================================================
--- branches/gSoC2007/lwgeom/lwpostgis.sql.in	2007-08-15 14:39:04 UTC (rev 2694)
+++ branches/gSoC2007/lwgeom/lwpostgis.sql.in	2007-08-17 18:33:58 UTC (rev 2695)
@@ -2286,18 +2286,21 @@
 -- Creates the view without explicit columns names using SQL CREATE VIEW
 -- Types, srids, dims are queried from geometry_column table
 -- Adds a row to geometry_columns for each geometry column found
--- Creates spatial GIST indexes 
 --
 -----------------------------------------------------------------------
-CREATEFUNCTION CreateGeometryView(varchar, varchar, varchar, varchar) RETURNS text AS '
+CREATE FUNCTION CreateGeometryView(varchar, varchar, varchar, varchar) RETURNS text AS '
 DECLARE
 	catalog_name alias for $1;
 	schema_name alias for $2;
 	view_name alias for $3;
 	query alias for $4;
 	has_geom bool;
+	rec RECORD;
+	rec2 RECORD;
+	f_dim integer;
+	f_srid integer;
+	f_type text;
 #if USE_VERSION >= 73
-	rec RECORD;
 	schema_ok bool;
 	real_schema name;
 #endif
@@ -2323,45 +2326,106 @@
 	END IF;
 #endif
 
-  -- Create view
-  EXECUTE ''CREATE OR REPLACE VIEW '' || quote_literal(view_name) || ''
-    AS '' || quote_literal(query);
-  
-  -- Query all fields for geometric columns
-  
+	-- Create view
+	EXECUTE ''CREATE OR REPLACE VIEW '' 
+#if USE_VERSION >= 73
+			|| quote_ident(real_schema) || ''.'' ||
+#endif
+		quote_ident(view_name) || ''
+		AS '' || query;
+	
+	-- Query all fields for geometric columns
 	has_geom = ''f'';
-	FOR rec IN SELECT attlen FROM pg_attribute WHERE  LOOP
+	FOR rec IN SELECT t.typname FROM pg_attribute a, pg_class c, pg_type t WHERE a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid AND t.typname = ''geometry'' AND c.relname = view_name LOOP
 		has_geom := ''t'';
+	END LOOP; 
 
-    -- if yes, add records in geometry_columns
-    EXECUTE ''INSERT INTO geometry_columns VALUES 
-    -- add indexes if they exist in original fields
-    
-	END LOOP;
-	
  	IF ( has_geom <> ''t'' ) THEN
-
-	  -- if not, remove view
-	  EXECUTE ''DROP VIEW IF EXISTS ''
+		-- if not, remove view
+		EXECUTE ''DROP VIEW IF EXISTS ''
 #if USE_VERSION >= 73
 			|| quote_ident(real_schema) || ''.'' ||
-#endif   
-	  quote_ident(view_name) || '' CASCADE'';
+#endif
+		quote_ident(view_name) || '' CASCADE'';
 
-		RAISE NOTICE ''SQL statement has not geometric columns'';
-	  return ''fail'';
+		RAISE EXCEPTION ''SELECT or VALUES command do not provide geometric columns'';
+		return ''fail'';
 	END IF;
+
+	-- Add records in geometry_column 
+	FOR rec IN SELECT a.attname FROM pg_attribute a, pg_class c, pg_type t WHERE a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid AND t.typname = ''geometry'' AND c.relname = view_name LOOP
+		FOR rec2 IN SELECT c2.relname, n.nspname FROM pg_class c, pg_class c1, pg_class c2, pg_depend d, pg_rewrite r, pg_attribute a, pg_namespace n WHERE a.attnum > 0 AND a.attrelid = c2.oid AND d.classid = c1.oid AND r.ev_class = c.oid AND d.objid = r.oid AND c2.oid = d.refobjid AND c2.relname <> c.relname AND n.oid = c.relnamespace AND c.relname = view_name AND a.attname = rec.attname LIMIT 1  LOOP
+			SELECT coord_dimension FROM geometry_columns WHERE f_table_name = rec2.relname AND f_table_schema = rec2.nspname AND f_geometry_column = rec.attname INTO f_dim;
+			SELECT srid FROM geometry_columns WHERE f_table_name = rec2.relname AND f_table_schema = rec2.nspname AND f_geometry_column = rec.attname INTO f_srid;
+			SELECT type FROM geometry_columns WHERE f_table_name = rec2.relname AND f_table_schema = rec2.nspname AND f_geometry_column = rec.attname INTO f_type;
+			EXECUTE ''INSERT INTO geometry_columns VALUES ('' ||
+				quote_literal('''') || '','' ||
+#if USE_VERSION >= 73
+				quote_literal(real_schema) || '','' ||
+#else
+				quote_literal('''') || '','' ||
+#endif
+				quote_literal(view_name) || '','' ||
+				quote_literal(rec.attname) || '','' ||
+				f_dim || '','' || f_srid || '','' ||
+				quote_literal(f_type) || '')'';
+		END LOOP;
+	END LOOP;
 	
-	-- 
-	
-  
-  
+	RETURN
+#if USE_VERSION >= 73
+		real_schema || ''.'' ||
+#endif
+	view_name ||'' effectively created.'';
+END;
+'
+LANGUAGE 'plpgsql' _VOLATILE_STRICT; -- WITH (isstrict);
 
+-----------------------------------------------------------------------
+-- CREATEGEOMETRYVIEW
+--   <schema>, <view>, <query>
+-----------------------------------------------------------------------
+--
+-- This is a wrapper to the real CreateGeometryView, for use
+-- when catalogue is undefined
+--
+-----------------------------------------------------------------------
+CREATEFUNCTION CreateGeometryView(varchar, varchar,varchar)
+	RETURNS text
+	AS 
+'
+DECLARE
+	ret text;
+BEGIN
+	SELECT CreateGeometryView('''',$1,$2,$3) into ret;
+	RETURN ret;
 END;
 '
 LANGUAGE 'plpgsql' _VOLATILE_STRICT; -- WITH (isstrict);
 
 -----------------------------------------------------------------------
+-- CREATEGEOMETRYVIEW
+--   <view>, <query>
+-----------------------------------------------------------------------
+--
+-- This is a wrapper to the real CreateGeometryView, for use
+-- when catalogue and schema is undefined. 
+--
+-----------------------------------------------------------------------
+CREATEFUNCTION CreateGeometryView(varchar, varchar)
+	RETURNS text
+	AS 
+'
+DECLARE
+	ret text;
+BEGIN
+	SELECT CreateGeometryView('''','''',$1,$2) into ret;
+	RETURN ret;
+END;
+'
+LANGUAGE 'plpgsql' _VOLATILE_STRICT; -- WITH (isstrict);
+
+-----------------------------------------------------------------------
 -- DROPGEOMETRYVIEW
 --   <catalogue>, <schema>, <view>
 -----------------------------------------------------------------------
@@ -2409,6 +2473,8 @@
 		'' AND '' ||
 #endif
 		'' f_table_name = '' || quote_literal(view_name);
+
+	-- Check if exists
 	
   -- Remove view
   EXECUTE ''DROP VIEW IF EXISTS ''
@@ -2421,13 +2487,57 @@
 #if USE_VERSION >= 73
 		real_schema || ''.'' ||
 #endif 
-		view_name ||'' dropped.'';
+		view_name ||'' view dropped.'';
 		
 END;
 '
 LANGUAGE 'plpgsql' _VOLATILE_STRICT; -- WITH (isstrict);
 
 -----------------------------------------------------------------------
+-- DROPGEOMETRYVIEW
+--   <schema>, <view>
+-----------------------------------------------------------------------
+--
+-- This is a wrapper to the real DropGeometryView, for use
+-- when catalogue is undefined
+--
+-----------------------------------------------------------------------
+CREATEFUNCTION DropGeometryView(varchar,varchar)
+	RETURNS text
+	AS 
+'
+DECLARE
+	ret text;
+BEGIN
+	SELECT DropGeometryView('''',$1,$2) into ret;
+	RETURN ret;
+END;
+'
+LANGUAGE 'plpgsql' _VOLATILE_STRICT; -- WITH (isstrict);
+
+-----------------------------------------------------------------------
+-- DROPGEOMETRYVIEW
+--   <view>
+-----------------------------------------------------------------------
+--
+-- This is a wrapper to the real DropGeometryView, for use
+-- when catalogue and schema is undefined. 
+--
+-----------------------------------------------------------------------
+CREATEFUNCTION DropGeometryView(varchar)
+	RETURNS text
+	AS 
+'
+DECLARE
+	ret text;
+BEGIN
+	SELECT DropGeometryView('''','''',$1) into ret;
+	RETURN ret;
+END;
+'
+LANGUAGE 'plpgsql' _VOLATILE_STRICT; -- WITH (isstrict);
+
+-----------------------------------------------------------------------
 -- UPDATEGEOMETRYSRID
 --   <catalogue>, <schema>, <table>, <column>, <srid>
 -----------------------------------------------------------------------



More information about the postgis-commits mailing list