Skip to content

Instantly share code, notes, and snippets.

@kaumac
Created January 2, 2024 15:27
Show Gist options
  • Save kaumac/9706d44a45997ff149eb6a0e569c960a to your computer and use it in GitHub Desktop.
Save kaumac/9706d44a45997ff149eb6a0e569c960a to your computer and use it in GitHub Desktop.
create or replace function create_property (
property_name text,
property_description text default null,
property_organization bigint default null,
address_location geography default null,
address_city text default null,
address_country text default null,
address_district text default null,
address_formatted text default null,
address_housenumber text default null,
address_lat numeric default null,
address_lon numeric default null,
address_organization bigint default null,
address_postcode text default null,
address_state text default null,
address_street text default null,
address_suburb text default null
) returns properties as $BODY$
DECLARE
created_property properties;
address_id bigint;
BEGIN
INSERT INTO properties (name, description, organization, created_at)
VALUES (property_name, property_description, property_organization, now())
RETURNING * INTO created_property;
INSERT INTO property_addresses (location, property, city, country, district, formatted, housenumber, lat, lon, organization, postcode, state, street, suburb)
VALUES (address_location, created_property.id, address_city, address_country, address_district, address_formatted, address_housenumber, address_lat, address_lon, address_organization, address_postcode, address_state, address_street, address_suburb)
RETURNING id INTO address_id;
RETURN created_property;
EXCEPTION
WHEN others THEN
INSERT INTO error_log (error_timestamp, error_message)
VALUES (NOW(), 'Error creating property: ' || SQLERRM);
RAISE EXCEPTION 'Error creating property. See error log for details.';
END;
$BODY$ language plpgsql security definer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment