Permalink
Browse files

Fix for Postgres 10

  • Loading branch information...
myfreeweb committed Nov 1, 2017
1 parent 5f70f85 commit ddb9383fc2fa67661b6a6ec43d4bae9335794dfc
Showing with 14 additions and 6 deletions.
  1. +1 −1 README.md
  2. +13 −5 migrations/0001_init.up.sql
View
@@ -1,6 +1,6 @@
# mf2sql [![unlicense](https://img.shields.io/badge/un-license-green.svg?style=flat)](http://unlicense.org)
A PostgreSQL (9.6) schema for storing [microformats2](http://microformats.org/wiki/microformats2) objects (as the canonical JSON representation), featuring:
A PostgreSQL (10) schema for storing [microformats2](http://microformats.org/wiki/microformats2) objects (as the canonical JSON representation), featuring:
- Fast storage and querying ([jsonb](https://www.postgresql.org/docs/9.6/static/datatype-json.html) + [GIN](https://www.postgresql.org/docs/9.6/static/indexes-types.html))
- Uniqueness constraint on the first `url` value
@@ -28,17 +28,25 @@ CREATE INDEX properties_idx ON mf2.objects USING GIN(properties jsonb_path_ops);
-------------------------------------------------------------------------------------------- Full Text Search
CREATE INDEX tsv_idx ON mf2.objects USING GIST(tsv);
CREATE FUNCTION mf2.jsonb_values(data jsonb) RETURNS SETOF jsonb AS $$
BEGIN
IF jsonb_typeof(jsonlevel) = 'object' THEN
RETURN QUERY SELECT (jsonb_each(jsonlevel)).value;
ELSIF jsonb_typeof(jsonlevel) = 'array' THEN
RETURN QUERY SELECT jsonb_array_elements(jsonlevel);
ELSE
RETURN QUERY SELECT true WHERE false;
END IF;
END
$$ LANGUAGE plpgsql IMMUTABLE;
CREATE FUNCTION mf2.flatten_jsonb(data jsonb) RETURNS SETOF jsonb AS $$
-- based on https://stackoverflow.com/a/27742278/239140
-- modified to use jsonb_typeof
WITH RECURSIVE deconstruct (jsonlevel) AS (
VALUES (data)
UNION ALL
SELECT
CASE jsonb_typeof(jsonlevel)
WHEN 'object' THEN (jsonb_each(jsonlevel)).value
WHEN 'array' THEN jsonb_array_elements(jsonlevel)
END AS jsonlevel
SELECT mf2.jsonb_values(jsonlevel) AS jsonlevel
FROM deconstruct
WHERE jsonb_typeof(jsonlevel) IN ('object', 'array')
) SELECT * FROM deconstruct;

0 comments on commit ddb9383

Please sign in to comment.