Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Replaced overloaded functions with functions using DEFAULT values for…

… input parameters.
  • Loading branch information...
commit 4b4e6db51e9b22c763e4b63dba8e21aa6de67c2e 1 parent 9a1906e
@leehach authored
View
8 README.md
@@ -110,9 +110,9 @@ The data scripts from the last section were created programmatically using sever
The meta-scripts are set up to run in the public schema, so that they can be called without qualification. If you prefer to keep these functions segregated in their own schema (e.g., census), you will later have to set the appropriate search_path or call them using a schema-qualified name (e.g. census.sql_import_geoheader()).
-In general, the order in which these scripts are run is not that important. Some of the user functions do refer to other user functions, but Postgres will create the function regardless and won't throw an error until the function with the dependecy is called. They are separated into these six files of related functions primarily for bookkeeping purposes. Only one of the last three (Data Store) scripts needs to be run. Running all three is harmless, as they merely create the functions which will create the data stores.
+In general, the order in which these scripts are run is not that important. Some of the user functions do refer to other user functions, but Postgres will create the function regardless and won't throw an error until the function with the dependecy is called. They are separated into these six files of related functions primarily for bookkeeping purposes. Only one of the Data Store scripts needs to be run (i.e., one of the last three scripts). Running all three is harmless, as they merely create the functions which will create the data stores.
-The actual functions and support tables are documented in this secion's subsections. In the function definition, brackets ([]) indicate optional parameters. For most of the following scripts, all parameters are optional. However, in order to specify a parameter, all earlier parameters must be explicitly given, even if the default value is desired.
+The actual functions and support tables are documented in this section's subsections. In the function definition, brackets ([]) indicate optional parameters. For most of the following scripts, all parameters are optional. However, in order to specify a parameter, all earlier parameters must be explicitly given, even if the default value is desired.
## Support Functions and Tables.sql
@@ -293,8 +293,8 @@ SELECT sql_geoheader_comments(TRUE);
SELECT sql_store_by_tables(TRUE);
SELECT sql_view_estimate_stored_by_tables(TRUE);
SELECT sql_view_moe_stored_by_tables(TRUE);
-SELECT sql_parse_tmp_geoheader(TRUE); Copies all data from tmp_geoheader to geoheader
-SELECT sql_insert_into_tables(TRUE); Copies all estimates and margins of error to sequence tables
+SELECT sql_parse_tmp_geoheader(TRUE); --Copies all data from tmp_geoheader to geoheader
+SELECT sql_insert_into_tables(TRUE); --Copies all estimates and margins of error to sequence tables
```
If using array-based or hstore-based data store, the last five functions will be different.
View
21,846 acs2010_5yr/insert_into_tables.sql
21,845 additions, 1 deletion not shown
View
26 meta-scripts/Data Store Array-Based.sql
@@ -5,7 +5,7 @@ SET search_path = public;
--CREATE DDL FOR STORAGE BY ARRAY COLUMNS
DROP FUNCTION IF EXISTS sql_store_by_array_columns(boolean);
-CREATE FUNCTION sql_store_by_array_columns(exec boolean) RETURNS text AS $function$
+CREATE FUNCTION sql_store_by_array_columns(exec boolean = FALSE) RETURNS text AS $function$
DECLARE
sql TEXT := '';
BEGIN
@@ -28,17 +28,6 @@ BEGIN
END;
$function$ LANGUAGE plpgsql;
-DROP FUNCTION IF EXISTS sql_store_by_array_columns();
-CREATE FUNCTION sql_store_by_array_columns() RETURNS text AS $function$
-DECLARE
- sql TEXT := '';
-BEGIN
- SELECT sql_store_by_array_columns(FALSE) INTO sql;
- RETURN sql;
-END;
-$function$ LANGUAGE plpgsql;
-
-
/********************************************************************
Currently editing this function
*********************************************************************/
@@ -50,7 +39,7 @@ SELECT sql_parse_tmp_geoheader(ARRAY[TRUE], 'by_arrays');
This function adds data to columns with UPDATE statements.*/
DROP FUNCTION IF EXISTS sql_insert_into_array_columns(boolean);
-CREATE FUNCTION sql_insert_into_array_columns(exec boolean) RETURNS text AS $function$
+CREATE FUNCTION sql_insert_into_array_columns(exec boolean = FALSE) RETURNS text AS $function$
DECLARE
sql TEXT := '';
BEGIN
@@ -76,14 +65,3 @@ BEGIN
RETURN sql;
END;
$function$ LANGUAGE plpgsql;
-
-DROP FUNCTION IF EXISTS sql_insert_into_array_columns();
-CREATE FUNCTION sql_insert_into_array_columns() RETURNS text AS $function$
-DECLARE
- sql TEXT := '';
-BEGIN
- SELECT sql_insert_into_array_columns(FALSE) INTO sql;
- RETURN sql;
-END;
-$function$ LANGUAGE plpgsql;
-
View
58 meta-scripts/Data Store Table-Based.sql
@@ -2,7 +2,7 @@
--CREATE DDL FOR STORAGE BY SEQUENCE
DROP FUNCTION IF EXISTS sql_store_by_tables(boolean);
-CREATE FUNCTION sql_store_by_tables(exec boolean) RETURNS text AS $function$
+CREATE FUNCTION sql_store_by_tables(exec boolean = FALSE) RETURNS text AS $function$
DECLARE
sql TEXT := '';
sql_estimate text;
@@ -44,18 +44,8 @@ BEGIN
END;
$function$ LANGUAGE plpgsql;
-DROP FUNCTION IF EXISTS sql_store_by_tables();
-CREATE FUNCTION sql_store_by_tables() RETURNS text AS $function$
-DECLARE
- sql TEXT := '';
-BEGIN
- SELECT sql_store_by_tables(FALSE) INTO sql;
- RETURN sql;
-END;
-$function$ LANGUAGE plpgsql;
-
DROP FUNCTION IF EXISTS sql_view_estimate_stored_by_tables(boolean);
-CREATE FUNCTION sql_view_estimate_stored_by_tables(exec boolean) RETURNS text AS $function$
+CREATE FUNCTION sql_view_estimate_stored_by_tables(exec boolean = FALSE) RETURNS text AS $function$
DECLARE
sql TEXT := '';
BEGIN
@@ -89,22 +79,12 @@ BEGIN
END;
$function$ LANGUAGE plpgsql;
-DROP FUNCTION IF EXISTS sql_view_estimate_stored_by_tables();
-CREATE FUNCTION sql_view_estimate_stored_by_tables() RETURNS text AS $function$
-DECLARE
- sql TEXT := '';
-BEGIN
- SELECT sql_view_estimate_stored_by_tables(FALSE) INTO sql;
- RETURN sql;
-END;
-$function$ LANGUAGE plpgsql;
-
/*Margin of error will rarely be used without estimate, so even though
they are stored in independent sequences, subject table views return estimates
as well as margins of error
*/
DROP FUNCTION IF EXISTS sql_view_moe_stored_by_tables(boolean);
-CREATE FUNCTION sql_view_moe_stored_by_tables(exec boolean) RETURNS text AS $function$
+CREATE FUNCTION sql_view_moe_stored_by_tables(exec boolean = FALSE) RETURNS text AS $function$
DECLARE
sql TEXT := '';
BEGIN
@@ -138,18 +118,8 @@ BEGIN
END;
$function$ LANGUAGE plpgsql;
-DROP FUNCTION IF EXISTS sql_view_moe_stored_by_tables();
-CREATE FUNCTION sql_view_moe_stored_by_tables() RETURNS text AS $function$
-DECLARE
- sql TEXT := '';
-BEGIN
- SELECT sql_view_moe_stored_by_tables(FALSE) INTO sql;
- RETURN sql;
-END;
-$function$ LANGUAGE plpgsql;
-
DROP FUNCTION IF EXISTS sql_insert_into_tables(boolean, text);
-CREATE FUNCTION sql_insert_into_tables(exec boolean, actions text) RETURNS text AS $function$
+CREATE FUNCTION sql_insert_into_tables(exec boolean = FALSE, actions text = 'em') RETURNS text AS $function$
DECLARE
sql TEXT := '';
sql_estimate TEXT;
@@ -201,24 +171,4 @@ BEGIN
END;
$function$ LANGUAGE plpgsql;
-DROP FUNCTION IF EXISTS sql_insert_into_tables(boolean);
-CREATE FUNCTION sql_insert_into_tables(exec boolean) RETURNS text AS $function$
-DECLARE
- sql TEXT := '';
-BEGIN
- SELECT sql_insert_into_tables(exec, 'em') INTO sql;
- RETURN sql;
-END;
-$function$ LANGUAGE plpgsql;
-
-DROP FUNCTION IF EXISTS sql_insert_into_tables();
-CREATE FUNCTION sql_insert_into_tables() RETURNS text AS $function$
-DECLARE
- sql TEXT := '';
-BEGIN
- SELECT sql_insert_into_tables(FALSE, 'em') INTO sql;
- RETURN sql;
-END;
-$function$ LANGUAGE plpgsql;
-
View
47 meta-scripts/Geoheader.sql
@@ -2,7 +2,7 @@
--CREATE SQL STATEMENT TO CREATE geoheader
DROP FUNCTION IF EXISTS sql_create_geoheader(boolean);
-CREATE FUNCTION sql_create_geoheader(exec boolean) RETURNS text AS $function$
+CREATE FUNCTION sql_create_geoheader(exec boolean = FALSE) RETURNS text AS $function$
DECLARE
sql TEXT;
BEGIN
@@ -31,19 +31,9 @@ BEGIN
END;
$function$ LANGUAGE plpgsql;
-DROP FUNCTION IF EXISTS sql_create_geoheader();
-CREATE FUNCTION sql_create_geoheader() RETURNS text AS $function$
-DECLARE
- sql TEXT := '';
-BEGIN
- SELECT sql_create_geoheader(FALSE) INTO sql;
- RETURN sql;
-END;
-$function$ LANGUAGE plpgsql;
-
--ADD COMMENTS TO COLUMNS OF geoheader
DROP FUNCTION IF EXISTS sql_geoheader_comments(boolean);
-CREATE FUNCTION sql_geoheader_comments(exec boolean) RETURNS text AS $function$
+CREATE FUNCTION sql_geoheader_comments(exec boolean = FALSE) RETURNS text AS $function$
DECLARE
sql TEXT := '';
row RECORD;
@@ -58,20 +48,10 @@ BEGIN
END;
$function$ LANGUAGE plpgsql;
-DROP FUNCTION IF EXISTS sql_geoheader_comments();
-CREATE FUNCTION sql_geoheader_comments() RETURNS text AS $function$
-DECLARE
- sql TEXT := '';
-BEGIN
- SELECT sql_geoheader_comments(FALSE) INTO sql;
- RETURN sql;
-END;
-$function$ LANGUAGE plpgsql;
-
--CREATE SQL STATEMENT TO PARSE tmp_geoheader INTO PERMANENT TABLE
--To Do: Allow target table to depend upon store_by method
DROP FUNCTION IF EXISTS sql_parse_tmp_geoheader(boolean, text);
-CREATE FUNCTION sql_parse_tmp_geoheader(exec boolean, target text) RETURNS text AS $function$
+CREATE FUNCTION sql_parse_tmp_geoheader(exec boolean = FALSE, target text = 'geoheader') RETURNS text AS $function$
DECLARE
sql TEXT := '';
row RECORD;
@@ -93,24 +73,3 @@ BEGIN
RETURN sql;
END;
$function$ LANGUAGE plpgsql;
-
-DROP FUNCTION IF EXISTS sql_parse_tmp_geoheader(boolean);
-CREATE FUNCTION sql_parse_tmp_geoheader(exec boolean) RETURNS text AS $function$
-DECLARE
- sql TEXT := '';
-BEGIN
- SELECT sql_parse_tmp_geoheader(exec, 'geoheader') INTO sql;
- RETURN sql;
-END;
-$function$ LANGUAGE plpgsql;
-
-DROP FUNCTION IF EXISTS sql_parse_tmp_geoheader();
-CREATE FUNCTION sql_parse_tmp_geoheader() RETURNS text AS $function$
-DECLARE
- sql TEXT := '';
-BEGIN
- SELECT sql_parse_tmp_geoheader(FALSE, 'geoheader') INTO sql;
- RETURN sql;
-END;
-$function$ LANGUAGE plpgsql;
-
View
109 meta-scripts/Staging Tables and Data Import Functions.sql
@@ -2,7 +2,7 @@
--CREATE DDL FOR GEOHEADER IMPORT TABLE
DROP FUNCTION IF EXISTS sql_create_tmp_geoheader(boolean);
-CREATE FUNCTION sql_create_tmp_geoheader(exec boolean) RETURNS text AS $function$
+CREATE FUNCTION sql_create_tmp_geoheader(exec boolean = FALSE) RETURNS text AS $function$
DECLARE
sql TEXT := '';
BEGIN
@@ -18,19 +18,9 @@ WITH (autovacuum_enabled = FALSE, toast.autovacuum_enabled = FALSE)
END;
$function$ LANGUAGE plpgsql;
-DROP FUNCTION IF EXISTS sql_create_tmp_geoheader();
-CREATE FUNCTION sql_create_tmp_geoheader() RETURNS text AS $function$
-DECLARE
- sql TEXT := '';
-BEGIN
- SELECT sql_create_tmp_geoheader(FALSE) INTO sql;
- RETURN sql;
-END;
-$function$ LANGUAGE plpgsql;
-
--CREATE SQL STATEMENT TO IMPORT GEOHEADER TO STAGING TABLE
DROP FUNCTION IF EXISTS sql_import_geoheader(boolean, text[]);
-CREATE FUNCTION sql_import_geoheader(exec boolean, stusab_criteria text[]) RETURNS text AS $function$
+CREATE FUNCTION sql_import_geoheader(exec boolean = FALSE, stusab_criteria text[] = ARRAY['%']) RETURNS text AS $function$
DECLARE
sql TEXT := '';
row RECORD;
@@ -46,29 +36,9 @@ BEGIN
END;
$function$ LANGUAGE plpgsql;
-DROP FUNCTION IF EXISTS sql_import_geoheader(boolean);
-CREATE FUNCTION sql_import_geoheader(exec boolean) RETURNS text AS $function$
-DECLARE
- sql TEXT := '';
-BEGIN
- SELECT sql_import_geoheader(exec, ARRAY['%']) INTO sql;
- RETURN sql;
-END;
-$function$ LANGUAGE plpgsql;
-
-DROP FUNCTION IF EXISTS sql_import_geoheader();
-CREATE FUNCTION sql_import_geoheader() RETURNS text AS $function$
-DECLARE
- sql TEXT := '';
-BEGIN
- SELECT sql_import_geoheader(FALSE, ARRAY['%']) INTO sql;
- RETURN sql;
-END;
-$function$ LANGUAGE plpgsql;
-
--CREATE DDL FOR SEQUENCE IMPORT TABLES
DROP FUNCTION IF EXISTS sql_drop_import_tables(boolean);
-CREATE FUNCTION sql_drop_import_tables(exec boolean) RETURNS text AS $function$
+CREATE FUNCTION sql_drop_import_tables(exec boolean = FALSE) RETURNS text AS $function$
DECLARE
sql TEXT := '';
sql_estimate text;
@@ -92,18 +62,8 @@ BEGIN
END;
$function$ LANGUAGE plpgsql;
-DROP FUNCTION IF EXISTS sql_drop_import_tables();
-CREATE FUNCTION sql_drop_import_tables() RETURNS text AS $function$
-DECLARE
- sql TEXT := '';
-BEGIN
- SELECT sql_drop_import_tables(FALSE) INTO sql;
- RETURN sql;
-END;
-$function$ LANGUAGE plpgsql;
-
DROP FUNCTION IF EXISTS sql_create_import_tables(boolean);
-CREATE FUNCTION sql_create_import_tables(exec boolean) RETURNS text AS $function$
+CREATE FUNCTION sql_create_import_tables(exec boolean = FALSE) RETURNS text AS $function$
DECLARE
sql TEXT := '';
sql_estimate text;
@@ -147,16 +107,6 @@ BEGIN
END;
$function$ LANGUAGE plpgsql;
-DROP FUNCTION IF EXISTS sql_create_import_tables();
-CREATE FUNCTION sql_create_import_tables() RETURNS text AS $function$
-DECLARE
- sql TEXT := '';
-BEGIN
- SELECT sql_drop_import_tables(FALSE) INTO sql;
- RETURN sql;
-END;
-$function$ LANGUAGE plpgsql;
-
--CREATE DDL FOR IMPORT (COPY) STATEMENTS
/*
--COPY can be executed with e.g.
@@ -177,7 +127,8 @@ SELECT sql_import_sequences(TRUE, array['ny', 'w%'], (SELECT array_agg(seq) FROM
--More examples (BETWEEN 'a' AND 'm', BETWEEN 1 AND 50, using generate_series()) in import file.
*/
DROP FUNCTION IF EXISTS sql_import_sequences(boolean, text[], int[], text);
-CREATE FUNCTION sql_import_sequences(exec boolean, stusab_criteria text[], seq_criteria int[], actions text
+CREATE FUNCTION sql_import_sequences(exec boolean = FALSE, stusab_criteria text[] = ARRAY['%'],
+ seq_criteria int[] = ARRAY[-1], actions text = 'atem'
) RETURNS text AS $sql_import_sequences$
DECLARE
sql TEXT := '';
@@ -189,7 +140,13 @@ DECLARE
sql_small_geo TEXT;
sql_large_geo_moe TEXT;
sql_small_geo_moe TEXT;
+ seq_criteria2 int[];
BEGIN
+ IF seq_criteria = ARRAY[-1] THEN
+ seq_criteria2 := (SELECT array_agg(seq) FROM vw_sequence);
+ ELSE
+ seq_criteria2 := seq_criteria;
+ END IF;
SELECT
array_to_string(array_agg(sql1), E'\n'),
array_to_string(array_agg(sql2), E'\n'),
@@ -215,7 +172,7 @@ BEGIN
|| stusab || lpad(seq::varchar, 4, '0') || E'000.txt\' WITH CSV;'
AS sql2_moe
FROM stusab, vw_sequence
- WHERE stusab ILIKE ANY (stusab_criteria) AND seq = ANY (seq_criteria)
+ WHERE stusab ILIKE ANY (stusab_criteria) AND seq = ANY (seq_criteria2)
) s
;
@@ -242,43 +199,3 @@ BEGIN
RETURN sql;
END;
$sql_import_sequences$ LANGUAGE plpgsql;
-
-DROP FUNCTION IF EXISTS sql_import_sequences(boolean, text[], int[]);
-CREATE FUNCTION sql_import_sequences(exec boolean, stusab_criteria text[], seq_criteria int[]) RETURNS text AS $function$
-DECLARE
- sql TEXT := '';
-BEGIN
- SELECT sql_import_sequences(exec, stusab_criteria, seq_criteria, 'atem') INTO sql;
- RETURN sql;
-END;
-$function$ LANGUAGE plpgsql;
-
-DROP FUNCTION IF EXISTS sql_import_sequences(boolean, text[]);
-CREATE FUNCTION sql_import_sequences(exec boolean, stusab_criteria text[]) RETURNS text AS $function$
-DECLARE
- sql TEXT := '';
-BEGIN
- SELECT sql_import_sequences(exec, stusab_criteria, (SELECT array_agg(seq) FROM vw_sequence), 'atem') INTO sql;
- RETURN sql;
-END;
-$function$ LANGUAGE plpgsql;
-
-DROP FUNCTION IF EXISTS sql_import_sequences(boolean);
-CREATE FUNCTION sql_import_sequences(exec boolean) RETURNS text AS $function$
-DECLARE
- sql TEXT := '';
-BEGIN
- SELECT sql_import_sequences(exec, ARRAY['%'], (SELECT array_agg(seq) FROM vw_sequence), 'atem') INTO sql;
- RETURN sql;
-END;
-$function$ LANGUAGE plpgsql;
-
-DROP FUNCTION IF EXISTS sql_import_sequences();
-CREATE FUNCTION sql_import_sequences() RETURNS text AS $function$
-DECLARE
- sql TEXT := '';
-BEGIN
- SELECT sql_import_sequences(FALSE, ARRAY['%'], (SELECT array_agg(seq) FROM vw_sequence), 'atem') INTO sql;
- RETURN sql;
-END;
-$function$ LANGUAGE plpgsql;
View
13 meta-scripts/Support Functions and Tables.sql
@@ -32,26 +32,17 @@ END;
$function$ LANGUAGE plpgsql;
DROP FUNCTION IF EXISTS sql_import_data_dictionary(text);
-CREATE FUNCTION sql_import_data_dictionary(filename text) RETURNS text AS $function$
+CREATE FUNCTION sql_import_data_dictionary(filename text = 'Sequence_Number_and_Table_Number_Lookup.txt') RETURNS text AS $function$
DECLARE
sql TEXT := '';
BEGIN
- sql := E'COPY data_dictionary FROM \'' || get_census_upload_root() || '/' || current_schema || '/' || filename || E'\' WITH CSV HEADER NULL;';
+ sql := E'COPY data_dictionary FROM \'' || get_census_upload_root() || '/' || current_schema || '/' || filename || E'\' WITH CSV HEADER NULL \'\';';
EXECUTE sql;
RETURN sql;
END;
$function$ LANGUAGE plpgsql;
-DROP FUNCTION IF EXISTS sql_import_data_dictionary();
-CREATE FUNCTION sql_import_data_dictionary() RETURNS text AS $function$
-DECLARE
- sql TEXT := '';
-BEGIN
- RETURN sql_import_data_dictionary('Sequence_Number_and_Table_Number_Lookup.txt');
-END;
-$function$ LANGUAGE plpgsql;
-
--CREATE LIST OF STATES/GEOGRAPHIC ENTITIES FOR PURPOSES OF ITERATING FILES FOR IMPORT
DROP TABLE IF EXISTS stusab;
CREATE TABLE stusab (
Please sign in to comment.
Something went wrong with that request. Please try again.