|
| 1 | +CREATE OR REPLACE FUNCTION public.rid_to_space_id_and_local_id(rid VARCHAR) |
| 2 | +RETURNS public.accessible_resource STRICT STABLE |
| 3 | +SET search_path = '' |
| 4 | +LANGUAGE plpgsql AS $$ |
| 5 | +DECLARE |
| 6 | + uri VARCHAR; |
| 7 | + source_local_id VARCHAR; |
| 8 | + source_id BIGINT; |
| 9 | +BEGIN |
| 10 | +source_local_id := split_part(rid, '/', -1); |
| 11 | +IF length(source_local_id) = length(rid) THEN |
| 12 | + RETURN (null, 'Not a Rid')::public.accessible_resource; |
| 13 | +END IF; |
| 14 | +uri := substr(rid, 1, length(rid) - length(source_local_id) - 1); |
| 15 | +IF rid ~ '^orn:\w+\.\w+:.*$' THEN |
| 16 | + uri := concat(split_part(split_part(uri, ':', 2), '.', 1), ':', split_part(uri, ':', 3)); |
| 17 | +ELSE |
| 18 | + IF rid ~ '^orn:\w+:.*$' THEN |
| 19 | + uri := substr(uri, 5); |
| 20 | + END IF; |
| 21 | +END IF; |
| 22 | +SELECT id INTO source_id FROM public."Space" where url=uri; |
| 23 | +IF source_id IS NULL THEN |
| 24 | + RETURN (null, concat('Cannot find ', uri))::public.accessible_resource; |
| 25 | +END IF; |
| 26 | +RETURN (source_id, source_local_id); |
| 27 | +END; |
| 28 | +$$; |
| 29 | + |
| 30 | +CREATE OR REPLACE FUNCTION public.rid_or_local_id_to_concept_db_id(rid VARCHAR, default_space_id BIGINT) |
| 31 | +RETURNS BIGINT STRICT STABLE |
| 32 | +SET search_path = '' |
| 33 | +LANGUAGE plpgsql AS $$ |
| 34 | +DECLARE r public.accessible_resource; |
| 35 | +BEGIN |
| 36 | + r := (SELECT public.rid_to_space_id_and_local_id(rid)); |
| 37 | + IF r.space_id IS NULL THEN |
| 38 | + RETURN (SELECT id FROM public."Concept" WHERE space_id = default_space_id AND source_local_id = rid); |
| 39 | + ELSE |
| 40 | + RETURN (SELECT id FROM public."Concept" WHERE space_id = r.space_id AND source_local_id = r.source_local_id); |
| 41 | + END IF; |
| 42 | +END; |
| 43 | +$$; |
| 44 | + |
| 45 | +CREATE OR REPLACE FUNCTION public._local_concept_to_db_concept(data public.concept_local_input) |
| 46 | +RETURNS public."Concept" STABLE |
| 47 | +SET search_path = '' |
| 48 | +LANGUAGE plpgsql |
| 49 | +AS $$ |
| 50 | +DECLARE |
| 51 | + concept public."Concept"%ROWTYPE; |
| 52 | + reference_content JSONB := jsonb_build_object(); |
| 53 | + key varchar; |
| 54 | + value JSONB; |
| 55 | + ref_single_val BIGINT; |
| 56 | + ref_array_val BIGINT[]; |
| 57 | +BEGIN |
| 58 | + -- not fan of going through json, but not finding how to populate a record by a different shape record |
| 59 | + concept := jsonb_populate_record(NULL::public."Concept", to_jsonb(data)); |
| 60 | + IF data.author_local_id IS NOT NULL THEN |
| 61 | + SELECT id FROM public."PlatformAccount" |
| 62 | + WHERE account_local_id = data.author_local_id INTO concept.author_id; |
| 63 | + END IF; |
| 64 | + IF data.represented_by_id IS NOT NULL THEN |
| 65 | + SELECT space_id, source_local_id FROM public."Content" |
| 66 | + WHERE id = data.represented_by_id INTO concept.space_id, concept.source_local_id; |
| 67 | + END IF; |
| 68 | + IF data.space_url IS NOT NULL THEN |
| 69 | + SELECT id FROM public."Space" |
| 70 | + WHERE url = data.space_url INTO concept.space_id; |
| 71 | + END IF; |
| 72 | + IF concept.source_local_id = '' THEN |
| 73 | + concept.source_local_id := NULL; |
| 74 | + END IF; |
| 75 | + IF data.represented_by_local_id = '' THEN |
| 76 | + data.represented_by_local_id := NULL; |
| 77 | + END IF; |
| 78 | + IF data.schema_represented_by_local_id IS NOT NULL THEN |
| 79 | + SELECT public.rid_or_local_id_to_concept_db_id( |
| 80 | + data.schema_represented_by_local_id, concept.space_id) INTO concept.schema_id; |
| 81 | + END IF; |
| 82 | + concept.source_local_id = COALESCE(concept.source_local_id, data.represented_by_local_id); -- legacy input field |
| 83 | + concept.reference_content := coalesce(data.reference_content, '{}'::jsonb); |
| 84 | + IF data.local_reference_content IS NOT NULL THEN |
| 85 | + FOR key, value IN SELECT * FROM jsonb_each(data.local_reference_content) LOOP |
| 86 | + IF jsonb_typeof(value) = 'array' THEN |
| 87 | + WITH el AS (SELECT jsonb_array_elements_text(value) as x), |
| 88 | + el2 AS (SELECT public.rid_or_local_id_to_concept_db_id(x, concept.space_id) AS id FROM el) |
| 89 | + SELECT array_agg(DISTINCT el2.id) INTO STRICT ref_array_val |
| 90 | + FROM el2 WHERE el2.id IS NOT NULL; |
| 91 | + reference_content := jsonb_set(reference_content, ARRAY[key], to_jsonb(ref_array_val)); |
| 92 | + ELSIF jsonb_typeof(value) = 'string' THEN |
| 93 | + SELECT public.rid_or_local_id_to_concept_db_id(value #>> '{}', concept.space_id) INTO STRICT ref_single_val; |
| 94 | + reference_content := jsonb_set(reference_content, ARRAY[key], to_jsonb(ref_single_val)); |
| 95 | + ELSE |
| 96 | + RAISE EXCEPTION 'Invalid value in local_reference_content % %', value, jsonb_typeof(value); |
| 97 | + END IF; |
| 98 | + END LOOP; |
| 99 | + concept.reference_content := concept.reference_content || reference_content; |
| 100 | + END IF; |
| 101 | + RETURN concept; |
| 102 | +END; |
| 103 | +$$; |
| 104 | + |
| 105 | +CREATE OR REPLACE FUNCTION public.upsert_concepts(v_space_id bigint, data jsonb) |
| 106 | +RETURNS SETOF BIGINT |
| 107 | +SET search_path = '' |
| 108 | +LANGUAGE plpgsql |
| 109 | +AS $$ |
| 110 | +DECLARE |
| 111 | + v_platform public."Platform"; |
| 112 | + local_concept public.concept_local_input; |
| 113 | + db_concept public."Concept"%ROWTYPE; |
| 114 | + concept_row JSONB; |
| 115 | + concept_id BIGINT; |
| 116 | +BEGIN |
| 117 | + SELECT platform INTO STRICT v_platform FROM public."Space" WHERE id=v_space_id; |
| 118 | + FOR concept_row IN SELECT * FROM jsonb_array_elements(data) |
| 119 | + LOOP |
| 120 | + -- first set defaults |
| 121 | + local_concept := jsonb_populate_record(NULL::public.concept_local_input, '{"epistemic_status": "unknown", "literal_content":{},"reference_content":{},"is_schema":false}'); |
| 122 | + -- then input values |
| 123 | + local_concept := jsonb_populate_record(local_concept, concept_row); |
| 124 | + local_concept.space_id := v_space_id; |
| 125 | + BEGIN |
| 126 | + db_concept := public._local_concept_to_db_concept(local_concept); |
| 127 | + -- cannot use db_concept.* because of refs. |
| 128 | + INSERT INTO public."Concept" ( |
| 129 | + epistemic_status, name, description, author_id, created, last_modified, space_id, schema_id, literal_content, is_schema, source_local_id, reference_content |
| 130 | + ) VALUES ( |
| 131 | + db_concept.epistemic_status, db_concept.name, db_concept.description, db_concept.author_id, db_concept.created, db_concept.last_modified, db_concept.space_id, db_concept.schema_id, db_concept.literal_content, db_concept.is_schema, db_concept.source_local_id, db_concept.reference_content |
| 132 | + ) |
| 133 | + ON CONFLICT (space_id, source_local_id) DO UPDATE SET |
| 134 | + epistemic_status = db_concept.epistemic_status, |
| 135 | + name = db_concept.name, |
| 136 | + description = db_concept.description, |
| 137 | + author_id = db_concept.author_id, |
| 138 | + created = db_concept.created, |
| 139 | + last_modified = db_concept.last_modified, |
| 140 | + schema_id = db_concept.schema_id, |
| 141 | + literal_content = db_concept.literal_content, |
| 142 | + is_schema = db_concept.is_schema, |
| 143 | + reference_content = db_concept.reference_content |
| 144 | + -- If the syntax allowed two conflict clauses, I would add |
| 145 | + -- ON CONFLICT (space_id, name) DO NOTHING |
| 146 | + -- but since not, I have to handle it as an exception. |
| 147 | + RETURNING id INTO concept_id; |
| 148 | + RETURN NEXT concept_id; |
| 149 | + EXCEPTION |
| 150 | + WHEN unique_violation THEN |
| 151 | + -- a distinct unique constraint failed |
| 152 | + RAISE WARNING 'Concept with space_id: % and name % already exists', v_space_id, local_concept.name; |
| 153 | + RETURN NEXT -1; -- Return a special value to indicate conflict |
| 154 | + WHEN OTHERS THEN |
| 155 | + -- Null value; probably due to a missing concept. |
| 156 | + RAISE WARNING 'Error in concept upsert: (%) %', SQLSTATE, SQLERRM; |
| 157 | + RETURN NEXT -2; -- Return a special value to indicate error |
| 158 | + END; |
| 159 | + END LOOP; |
| 160 | + RAISE DEBUG 'Completed upsert_concepts successfully'; |
| 161 | +END; |
| 162 | +$$; |
0 commit comments