Created
June 20, 2015 15:52
-
-
Save xivSolutions/ba20d517dc10b6699752 to your computer and use it in GitHub Desktop.
MassiveJS Table With Keys as JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DEALLOCATE tables; | |
PREPARE tables(text, text, text) AS | |
SELECT row_to_json (tbls) AS tables FROM ( | |
SELECT t.table_schema, t.table_name, | |
(SELECT json_agg(keys) from ( | |
SELECT c.COLUMN_NAME, c.DATA_TYPE, c.CHARACTER_MAXIMUM_LENGTH AS char_length, | |
c.column_default IS NOT NULL AS is_auto | |
FROM information_schema.columns c | |
LEFT OUTER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu | |
ON c.TABLE_SCHEMA = kcu.CONSTRAINT_SCHEMA AND c.TABLE_NAME = kcu.TABLE_NAME AND c.COLUMN_NAME = kcu.COLUMN_NAME | |
LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc | |
ON kcu.CONSTRAINT_SCHEMA = tc.CONSTRAINT_SCHEMA AND kcu.CONSTRAINT_NAME = tc.CONSTRAINT_NAME | |
WHERE | |
tc.constraint_type = 'PRIMARY KEY' AND c.TABLE_NAME = t.table_name AND c.table_schema = t.table_schema | |
) AS keys | |
) AS pk_columns | |
FROM information_schema.tables AS t | |
WHERE t.table_schema NOT IN('information_schema', 'pg_catalog') AND | |
((CASE -- allow specific schemas (none or '' assumes all): | |
WHEN $1 ='' THEN 1=1 | |
ELSE t.table_schema = ANY(string_to_array(REPLACE($1, ' ', ''), ',')) END) | |
AND | |
(CASE -- blacklist tables using LIKE by fully-qualified name (no schema assumes public): | |
WHEN $2 = '' THEN 1=1 | |
ELSE REPLACE((t.table_schema || '.'|| t.table_name), 'public.', '') NOT LIKE ALL(string_to_array(REPLACE($2, ' ', ''), ',')) END)) | |
OR | |
(CASE -- make exceptions for specific tables, with fully-qualified name or wildcard pattern (no schema assumes public). | |
WHEN $3 = '' THEN 1=0 | |
-- Below can use '%' as wildcard. Change 'like' to '=' to require exact names: | |
ELSE REPLACE((t.table_schema || '.'|| t.table_name), 'public.', '') LIKE ANY(string_to_array(REPLACE($3, ' ', ''), ',')) END) | |
ORDER BY t.table_schema, | |
t.table_name | |
) tbls; | |
EXECUTE tables('', '', ''); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment