-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c6d60c1
commit b29424a
Showing
38 changed files
with
580 additions
and
669 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ ml_structure.json | |
.DS_Store | ||
.venv | ||
venv | ||
.vscode/extensions.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
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
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
40 changes: 22 additions & 18 deletions
40
fertiscan/db/bytebase/get_inspection/get_organizations_json.sql
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,58 @@ | ||
|
||
--Unverified organization data | ||
DROP IF EXISTS FUNCTION "fertiscan_0.0.18".get_organizations_information_json(label_id uuid); | ||
DROP FUNCTION IF EXISTS "fertiscan_0.0.18".get_organizations_information_json(label_id_value uuid); | ||
CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".get_organizations_information_json( | ||
label_id uuid) | ||
label_id_value uuid) | ||
RETURNS jsonb | ||
LANGUAGE plpgsql | ||
AS $function$ | ||
DECLARE | ||
result_json jsonb; | ||
BEGIN | ||
SELECT jsonb_agg(jsonb_build_object( | ||
SELECT jsonb_build_object( | ||
'organizations', | ||
jsonb_build_object( | ||
'id', COALESCE(org.id, Null), | ||
'name', COALESCE(org.name, Null), | ||
'address', COALESCE(org.address, Null), | ||
'phone_number', COALESCE(org.phone_number, Null), | ||
'website', COALESCE(org.website, Null), | ||
'edited', COALESCE(org.edited, Null), | ||
'is_main_contact', COALESCE(org.is_main_contact, Null) | ||
) | ||
)) | ||
COALESCE(jsonb_agg( | ||
jsonb_build_object( | ||
'id', COALESCE(org.id, Null), | ||
'name', COALESCE(org.name, Null), | ||
'address', COALESCE(org.address, Null), | ||
'phone_number', COALESCE(org.phone_number, Null), | ||
'website', COALESCE(org.website, Null), | ||
'edited', COALESCE(org.edited, Null), | ||
'is_main_contact', COALESCE(org.is_main_contact, Null) | ||
) | ||
), '[]'::jsonb) | ||
) | ||
INTO result_json | ||
FROM organization_information as org | ||
WHERE org.label_id = label_id; | ||
WHERE org.label_id = label_id_value; | ||
RETURN result_json; | ||
END; | ||
$function$; | ||
|
||
-- verified organization | ||
DROP IF EXISTS FUNCTION "fertiscan_0.0.18".get_organizations_json(); | ||
DROP FUNCTION IF EXISTS "fertiscan_0.0.18".get_organizations_json(); | ||
CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".get_organizations_json() | ||
RETURNS jsonb | ||
LANGUAGE plpgsql | ||
AS $function$ | ||
DECLARE | ||
result_json jsonb; | ||
BEGIN | ||
SELECT jsonb_agg(jsonb_build_object( | ||
SELECT jsonb_build_object( | ||
'organizations', | ||
jsonb_build_object( | ||
jsonb_agg(jsonb_build_object( | ||
'id', COALESCE(org.id, Null), | ||
'name', COALESCE(org.name, Null), | ||
'address', COALESCE(org.address, Null), | ||
'phone_number', COALESCE(org.phone_number, Null), | ||
'website', COALESCE(org.website, Null), | ||
'updated_at', COALESCE(org.updated_at, Null), | ||
'updated_at', COALESCE(org.updated_at, Null) | ||
) | ||
)) | ||
INTO result_json | ||
FROM organization_information as org; | ||
RETURN result_json; | ||
END; | ||
$function$; | ||
|
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
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
61 changes: 61 additions & 0 deletions
61
fertiscan/db/bytebase/update_inspection/update_sub_label.sql
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
|
||
-- Function to update sub labels: delete old and insert new | ||
Drop FUNCTION IF EXISTS "fertiscan_0.0.18".update_sub_labels(uuid, jsonb); | ||
CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".update_sub_labels( | ||
p_label_id uuid, | ||
new_sub_labels jsonb | ||
) | ||
RETURNS void AS $$ | ||
DECLARE | ||
sub_type_rec RECORD; | ||
fr_values jsonb; | ||
en_values jsonb; | ||
i int; | ||
max_length int; | ||
fr_value text; | ||
en_value text; | ||
BEGIN | ||
-- Delete existing sub labels for the given label_id | ||
DELETE FROM sub_label WHERE label_id = p_label_id; | ||
|
||
-- Loop through each sub_type | ||
FOR sub_type_rec IN SELECT id, type_en FROM sub_type | ||
LOOP | ||
-- Extract the French and English arrays for the current sub_type | ||
fr_values := COALESCE(new_sub_labels->sub_type_rec.type_en->'fr', '[]'::jsonb); | ||
en_values := COALESCE(new_sub_labels->sub_type_rec.type_en->'en', '[]'::jsonb); | ||
|
||
-- Determine the maximum length of the arrays | ||
max_length := GREATEST( | ||
jsonb_array_length(fr_values), | ||
jsonb_array_length(en_values) | ||
); | ||
|
||
-- Check if lengths are not equal, and raise a notice | ||
IF jsonb_array_length(en_values) != jsonb_array_length(fr_values) THEN | ||
RAISE NOTICE 'Array length mismatch for sub_type: %, EN length: %, FR length: %', | ||
sub_type_rec.type_en, jsonb_array_length(en_values), jsonb_array_length(fr_values); | ||
END IF; | ||
|
||
-- Loop through the indices up to the maximum length | ||
FOR i IN 0..(max_length - 1) | ||
LOOP | ||
-- Extract values or set to empty string if not present | ||
fr_value := fr_values->>i; | ||
en_value := en_values->>i; | ||
|
||
-- Insert sub label record | ||
INSERT INTO sub_label ( | ||
text_content_fr, text_content_en, label_id, edited, sub_type_id | ||
) | ||
VALUES ( | ||
fr_value, | ||
en_value, | ||
p_label_id, | ||
NULL, -- not handled | ||
sub_type_rec.id | ||
); | ||
END LOOP; | ||
END LOOP; | ||
END; | ||
$$ LANGUAGE plpgsql; |
20 changes: 12 additions & 8 deletions
20
fertiscan/db/bytebase/update_inspection/upsert_organization.sql
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,40 @@ | ||
|
||
DROP IF EXISTS FUNCTION "fertiscan_0.0.18".upsert_organization(); | ||
DROP FUNCTION IF EXISTS "fertiscan_0.0.18".upsert_organization(); | ||
CREATE OR REPLACE FUNCTION "fertiscan_0.0.18".upsert_organization(org_info_id uuid) | ||
RETURNS id AS $$ | ||
RETURNS uuid | ||
LANGUAGE plpgsql | ||
AS $function$ | ||
DECLARE | ||
record record; | ||
address_str TEXT; | ||
org_id uuid; | ||
BEGIN | ||
-- Select the row from the organization_information table | ||
SELECT * INTO record FROM organization_information WHERE id = org_info_id; | ||
SELECT * INTO record FROM organization_information WHERE "id" = org_info_id; | ||
|
||
-- Check if the organization exists | ||
Select id INTO org_id FROM organization WHERE name ILIKE record.name; | ||
Select "id" INTO org_id FROM organization WHERE name ILIKE record.name; | ||
|
||
-- UPSERT DATA INTO THE ORGANIZATION TABLE | ||
IF org_id IS NULL THEN | ||
INSERT INTO organization ("website","phone_number","address","main_location_id") | ||
INSERT INTO organization ("name","website","phone_number","address","main_location_id") | ||
VALUES ( | ||
record.name, | ||
record.website, | ||
record.phone_number, | ||
record.address, | ||
Null -- Not re-implemented yet | ||
) | ||
RETURNING id INTO org_id; | ||
RETURNING "id" INTO org_id; | ||
ELSE | ||
UPDATE organization SET | ||
"website" = record.website, | ||
"phone_number" = record.phone_number, | ||
"address" = record.address, | ||
"main_location_id" = Null -- Not re-implemented yet | ||
WHERE id = org_id | ||
RETURNING id INTO org_id; | ||
WHERE id = org_id; | ||
END IF; | ||
RETURN org_id; | ||
END; | ||
$function$; | ||
|
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
Oops, something went wrong.