{"id":403817,"date":"2024-06-29T17:20:46","date_gmt":"2024-06-29T17:20:46","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=403817"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=403817","title":{"rendered":"<span>\u0420\u0435\u0446\u0435\u043f\u0442\u044b PostgreSQL: \u0432\u0438\u0434\u0436\u0435\u0442 \u0413\u043e\u0441\u0443\u0434\u0430\u0440\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u0410\u0434\u0440\u0435\u0441\u043d\u043e\u0433\u043e \u0420\u0435\u0435\u0441\u0442\u0440\u0430<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0414\u043b\u044f \u043f\u0440\u0438\u0433\u043e\u0442\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0432\u0438\u0434\u0436\u0435\u0442\u0430 \u0413\u043e\u0441\u0443\u0434\u0430\u0440\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u0410\u0434\u0440\u0435\u0441\u043d\u043e\u0433\u043e \u0420\u0435\u0435\u0441\u0442\u0440\u0430 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0443\u0436\u043d\u043e \u0435\u0433\u043e (\u0413\u0410\u0420) <a href=\"https:\/\/habr.com\/ru\/post\/584724\/\" rel=\"noopener noreferrer nofollow\">\u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c<\/a>. \u041f\u0440\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0431\u0430\u0437\u044b \u0431\u044b\u043b\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u044b \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0434\u043b\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0432 \u043d\u0438\u0445 \u0413\u0410\u0420, \u043d\u043e \u0442\u0430\u043a\u0436\u0435 \u0438 \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u0432\u0438\u0434\u0436\u0435\u0442\u0430. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c\u0441\u044f \u043d\u0430 \u043d\u0438\u0445 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435.<\/p>\n<p>\u0418\u0442\u0430\u043a, \u0434\u043b\u044f \u0432\u0438\u0434\u0436\u0435\u0442\u0430 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443 (\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b), \u0432 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u043c \u0432\u0441\u0435 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e \u0432\u0441\u0435\u043c \u0440\u0435\u0433\u0438\u043e\u043d\u0430\u043c:<\/p>\n<pre><code class=\"sql\">CREATE TABLE IF NOT EXISTS gar ( -- \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0435\u0441\u043b\u0438 \u0435\u0451 \u0435\u0449\u0451 \u043d\u0435\u0442     -- \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u044b\u0439 \u043a\u043b\u044e\u0447     id uuid NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,     parent uuid, -- \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c     name text NOT NULL, -- \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0435     short text NOT NULL, -- \u043a\u0440\u0430\u0442\u043a\u0438\u0439 \u0442\u0438\u043f     type text NOT NULL, -- \u043f\u043e\u043b\u043d\u044b\u0439 \u0442\u0438\u043f     post text, -- \u043f\u043e\u0447\u0442\u043e\u0432\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441     region smallint NOT NULL -- \u043a\u043e\u0434 \u0440\u0435\u0433\u0438\u043e\u043d\u0430 ); CREATE INDEX IF NOT EXISTS gar_parent_idx ON gar USING btree (parent); CREATE INDEX IF NOT EXISTS gar_name_idx ON gar USING btree (name); CREATE INDEX IF NOT EXISTS gar_short_idx ON gar USING btree (short); CREATE INDEX IF NOT EXISTS gar_type_idx ON gar USING btree (type); CREATE INDEX IF NOT EXISTS gar_region_idx ON gar USING btree (region);<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u044f<\/p>\n<pre><code class=\"sql\">-- \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0438\u043b\u0438 \u043c\u0435\u043d\u044f\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043e\u0442 \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u044f, \u043a\u0440\u0430\u0442\u043a\u043e\u0433\u043e \u0438 \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430 CREATE OR REPLACE FUNCTION gar_text(name text, short text, type text) RETURNS text -- \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0443\u044e \u043f\u043e\u043b\u043d\u043e\u0435 \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0435 LANGUAGE sql -- \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0443\u044e \u044f\u0437\u044b\u043a sql IMMUTABLE AS $body$ -- \u0437\u0430\u0432\u0438\u0441\u044f\u0449\u0443\u044e \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0442 \u0441\u0432\u043e\u0438\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432     select case -- \u0432 \u0441\u043b\u0443\u0447\u0430\u0435     -- \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u043b\u043d\u044b\u0439 \u0442\u0438\u043f \u043d\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d, \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0438\u0442\u044c \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0435         when gar_text.type in ('\u041d\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043e') then gar_text.name         -- \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u043b\u043d\u044b\u0439 \u0442\u0438\u043f - \u0427\u0443\u0432\u0430\u0448\u0438\u044f, \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0438\u0442\u044c \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u043f\u043e\u043b\u043d\u044b\u0439 \u0442\u0438\u043f         when gar_text.type in ('\u0427\u0443\u0432\u0430\u0448\u0438\u044f') then gar_text.name||' '||gar_text.type         -- \u043a\u043e\u0433\u0434\u0430 \u0432 \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442\u0441\u044f \u043f\u043e\u043b\u043d\u044b\u0439 \u0442\u0438\u043f, \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0438\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0435         when gar_text.name ilike '%'||gar_text.type||'%' then gar_text.name         -- \u0438\u043d\u0430\u0447\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0438\u0442\u044c \u043a\u0440\u0430\u0442\u043a\u0438\u0439 \u0442\u0438\u043f \u0442\u043e\u0447\u043a\u0430 \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0435         else gar_text.short||'.'||gar_text.name     end; $body$;<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435\u043c \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043a\u043e\u043b\u043e\u043d\u043a\u0438<\/p>\n<pre><code class=\"sql\">CREATE OR REPLACE VIEW gar_view AS -- \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0438\u043b\u0438 \u043c\u0435\u043d\u044f\u0435\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 SELECT gar.*, -- \u0432\u044b\u0431\u0438\u0440\u0430\u044f \u0432\u0441\u0451 gar_text(name, short, type) AS text -- \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044f \u043f\u043e\u043b\u043d\u043e\u0435 \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0435 from gar; -- \u0438\u0437 \u0413\u0410\u0420\u0430<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u043f\u043e\u0434\u0441\u0447\u0451\u0442\u0430 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432<\/p>\n<pre><code class=\"sql\">-- \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0438\u043b\u0438 \u043c\u0435\u043d\u044f\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043e\u0442 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430 CREATE OR REPLACE FUNCTION gar_child(id uuid) -- \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0443\u044e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0432\u0442\u0432\u043e \u0435\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 RETURNS bigint LANGUAGE sql STABLE AS $body$     select count(1) from gar where parent = gar_child.id; $body$;<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0436\u0435, \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u0432\u044b\u0431\u043e\u0440\u0430, \u0435\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u043d \u043c\u0430\u0441\u0441\u0438\u0432 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u0432<\/p>\n<pre><code class=\"sql\">-- \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0438\u043b\u0438 \u043c\u0435\u043d\u044f\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043e\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u0432 CREATE OR REPLACE FUNCTION gar_select(id uuid[]) RETURNS SETOF gar_view -- \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0443\u044e \u043d\u0430\u0431\u043e\u0440 \u0441\u0442\u0440\u043e\u043a \u0438\u0437 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f LANGUAGE sql STABLE AS $body$ -- \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u0432\u0441\u0451, \u0432 \u0442.\u0447. \u0438 \u043f\u043e\u043b\u043d\u043e\u0435 \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0435     select gar.*, gar_text(name, short, type) AS text from gar     -- \u0432 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u043c \u0430\u0433\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u043e\u0440\u044f\u0434\u043a\u0435     inner join (select unnest(gar_select.id) as id,                 generate_subscripts(gar_select.id, 1) as i) as _ on _.id = gar.id     -- \u0434\u043b\u044f \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0445 \u0432 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u0432     where gar.id = any(gar_select.id) order by i; $body$;<\/code><\/pre>\n<p>\u0418 \u0435\u0449\u0451 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0433\u043e \u0432\u044b\u0431\u043e\u0440\u0430 \u0432\u0432\u0435\u0440\u0445 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u0439 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0433\u043e \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430, \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u044f\u0441\u044c \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u043c \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u043c \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u043c<\/p>\n<pre><code class=\"sql\">CREATE OR REPLACE FUNCTION gar_select( -- \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0438\u043b\u0438 \u043c\u0435\u043d\u044f\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043e\u0442 id uuid, -- \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430 parent uuid DEFAULT NULL -- \u0438 \u043d\u0435\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430 ) RETURNS SETOF gar_view -- \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0443\u044e \u043d\u0430\u0431\u043e\u0440 \u0441\u0442\u0440\u043e\u043a \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f LANGUAGE sql STABLE AS $body$     with recursive _ as ( -- \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e       -- \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u0431\u0438\u0440\u0430\u044f \u043f\u043e \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u043c\u0443 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0443         select gar.*, 0 as i from gar where id = gar_select.id         union       -- \u0430 \u043f\u043e\u0442\u043e\u043c \u0432\u044b\u0431\u0438\u0440\u0430\u044f \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u0439 \u0432\u0432\u0435\u0440\u0445 \u0434\u043e \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0433\u043e \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f \u0438\u043b\u0438 \u0434\u043e \u0441\u0430\u043c\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u0430         select gar.*, _.i + 1 as i from gar inner join _ on (_.parent = gar.id)         where gar_select.parent is null or _.parent != gar_select.parent     ) select id, parent, name, short, type, post, region, gar_text(name, short, type) AS text      from _ order by i desc; $body$;<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430<\/p>\n<pre><code class=\"sql\">CREATE OR REPLACE FUNCTION gar_select( -- \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0438\u043b\u0438 \u043c\u0435\u043d\u044f\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e parent uuid, -- \u043e\u0442 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f   name text, -- \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u044f   short text, -- \u043a\u0440\u0430\u0442\u043a\u043e\u0433\u043e \u0442\u0438\u043f\u0430   type text, -- \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430   post text, -- \u043f\u043e\u0447\u0442\u043e\u0432\u043e\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430   region text -- \u043a\u043e\u0434\u0430 \u0440\u0435\u0433\u0438\u043e\u043d\u0430 ) RETURNS SETOF gar_view -- \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0443\u044e \u043d\u0430\u0431\u043e\u0440 \u0441\u0442\u0440\u043e\u043a \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f LANGUAGE sql STABLE AS $body$ -- \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u0432\u0441\u0451, \u0433\u0434\u0435     select gar.*, gar_text(gar.name, gar.short, gar.type) AS text from gar where true     -- \u0435\u0441\u043b\u0438 \u043d\u0435 \u0437\u0430\u0434\u0430\u043d \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c \u0442\u043e \u0431\u0435\u0437 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f, \u0430 \u0435\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u043d - \u0442\u043e \u043f\u043e \u043d\u0435\u043c\u0443     and ((gar_select.parent is null and parent is null) or parent = gar_select.parent) -- \u0435\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u043d\u043e \u043d\u0430\u0438\u043c\u043d\u043e\u0432\u0430\u043d\u0438\u0435, \u0442\u043e \u0438\u0449\u0435\u043c \u043f\u043e \u043d\u0435\u043c\u0443 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u043b\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0431\u0435\u043b\u0430 \u0438\u043b\u0438 \u043f\u043e\u0441\u043b\u0435 \u0434\u0435\u0444\u0438\u0441\u0430 \u0438\u043b\u0438 \u043f\u043e\u0441\u043b\u0435 \u0442\u043e\u0447\u043a\u0438         and (gar_select.name is null or name ilike gar_select.name||'%' or name ilike '% '||gar_select.name||'%' or name ilike '%-'||gar_select.name||'%' or name ilike '%.'||gar_select.name||'%')     -- \u0435\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u043d \u043a\u0440\u0430\u0442\u043a\u0438 \u0442\u0438\u043f, \u0442\u043e \u0438\u0449\u0435\u043c \u043f\u043e \u043d\u0435\u043c\u0443     and (gar_select.short is null or short ilike gar_select.short)     -- \u0435\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u043d \u043f\u043e\u043b\u043d\u044b\u0439 \u0442\u0438\u043f \u0438 \u044d\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 - \u0442\u043e \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u043c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0430 \u0435\u0441\u043b\u0438 \u043d\u0435 \u043c\u0430\u0441\u0441\u0438\u0432, \u0442\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430     and (gar_select.type is null or case when gar_select.type ilike '{%}' then type = any(gar_select.type::text[]) else type ilike gar_select.type||'%' end)     -- \u0435\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u043d \u043f\u043e\u0447\u0442\u043e\u0432\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441, \u0442\u043e \u0438\u0449\u0435\u043c \u043f\u043e \u043d\u0435\u043c\u0443 \u0441\u043d\u0430\u0447\u0430\u043b\u0430     and (gar_select.post is null or post ilike gar_select.post||'%')     -- \u0435\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u043d \u043a\u043e\u0434 \u0440\u0435\u0433\u0438\u043e\u043d\u0430 \u0438 \u044d\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 - \u0442\u043e \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u043c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0430 \u0435\u0441\u043b\u0438 \u043d\u0435 \u043c\u0430\u0441\u0441\u0438\u0432 - \u0442\u043e \u0438\u0449\u0435\u043c \u043f\u043e \u043d\u0435\u043c\u0443      and (gar_select.region is null or case when gar_select.region ilike '{%}' then region = any(gar_select.region::smallint[]) else region = gar_select.region::smallint end)     -- \u0441\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e \u0447\u0438\u0441\u043b\u0435\u043d\u043d\u043e\u043c\u0443 \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u044e, \u0430 \u043f\u043e\u0442\u043e\u043c \u043f\u043e \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u044e     order by to_number('0'||name, '999999999'), name; $body$;<\/code><\/pre>\n<p>\u0418 \u0435\u0449\u0451 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u0439<\/p>\n<pre><code class=\"sql\">-- \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0438\u043b\u0438 \u043c\u0435\u043d\u044f\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043e\u0442 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f, \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u044f, ... CREATE OR REPLACE FUNCTION gar_select_parent(parent uuid, name text, short text, type text, post text, region text) RETURNS SETOF gar_view LANGUAGE sql STABLE AS $body$ -- \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u0432\u0441\u0451     select gar.*, gar_text(gar.name, gar.short, gar.type) AS text from gar     -- \u0433\u0434\u0435 \u043f\u043e\u043b\u043d\u044b\u0439 \u0442\u0438\u043f \u0438\u0437 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430     where type = any(gar_select_parent.type::text[])     -- \u0438 \u0435\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u043d\u043d\u043e \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0435 , \u0442\u043e \u0438\u0449\u0435\u043c \u043f\u043e \u043d\u0435\u043c\u0443 \u0438\u043b\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0431\u0435\u043b\u0430 \u0438\u043b\u0438 \u043f\u043e\u0441\u043b\u0435 \u0434\u0435\u0444\u0438\u0441\u0430 \u0438\u043b\u0438 \u043f\u043e\u0441\u043b\u0435 \u0442\u043e\u0447\u043a\u0438     and (gar_select_parent.name is null or name ilike gar_select_parent.name||'%' or name ilike '% '||gar_select_parent.name||'%' or name ilike '%-'||gar_select_parent.name||'%' or name ilike '%.'||gar_select_parent.name||'%') -- \u0441\u043e\u0440\u0442\u0438\u0440\u0443\u044f \u043f\u043e \u0433\u043b\u0443\u0431\u0438\u043d\u0435 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u0438         order by (select count(id) from gar_select(id, gar_select_parent.parent)), to_number('0'||name, '999999999'), name; $body$;<\/code><\/pre>\n<p>\u041d\u0443 \u0438 \u0442\u0435\u043f\u0435\u0440\u044c, \u0433\u043b\u0430\u0432\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u0432\u0438\u0434\u0436\u0435\u0442\u0430<\/p>\n<pre><code class=\"sql\">-- \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0438\u043b\u0438 \u043c\u0435\u043d\u044f\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043e\u0442 json, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0443\u044e json CREATE OR REPLACE FUNCTION gar_select(INOUT json json) RETURNS json LANGUAGE plpgsql STABLE AS $body$ &lt;&lt;local>> declare     id text default nullif(trim(gar_select.json->>'id'), ''); -- \u0443\u0438\u0434     parent text default nullif(trim(gar_select.json->>'parent'), ''); -- \u0443\u0438\u0434 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f     name text default nullif(trim(gar_select.json->>'name'), ''); -- \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0435     short text default nullif(trim(gar_select.json->>'short'), ''); -- \u043a\u0440\u0430\u0442\u043a\u043e     type text default nullif(trim(gar_select.json->>'type'), ''); -- \u0442\u0438\u043f     post text default nullif(trim(gar_select.json->>'port'), ''); -- \u0438\u043d\u0434\u0435\u043a\u0441     region text default nullif(trim(gar_select.json->>'region'), ''); -- \u0440\u0435\u0433\u0438\u043e\u043d     text text default nullif(trim(gar_select.json->>'text'), ''); -- \u0441\u0442\u0440\u043e\u043a\u0430 \u043f\u043e\u0438\u0441\u043a\u0430     offset int default coalesce(nullif(trim(gar_select.json->>'offset'), '')::int, 0); -- \u043e\u0444\u0441\u0435\u0442     limit int default coalesce(nullif(trim(gar_select.json->>'limit'), '')::int, 10); -- \u043b\u0438\u043c\u0438\u0442     \"full\" boolean default coalesce(nullif(trim(gar_select.json->>'full'), '')::boolean, false); -- \u0432\u0441\u0435?     child boolean default coalesce(nullif(trim(gar_select.json->>'child'), '')::boolean, false); -- \u0434\u0435\u0442\u0438? begin     if local.id is not null then -- \u0435\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u043d id         local.id = translate(local.id, '[]','{}');         if local.id ilike '{%}' then -- \u0435\u0441\u043b\u0438 id - \u043c\u0430\u0441\u0441\u0438\u0432             if local.full then -- \u0435\u0441\u043b\u0438 \u0432\u0441\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b                 with _ as (                     with _ as (                         select * from gar_select(local.id::uuid[])                     ) select count(1), gar_select.json as query, local.offset, local.limit, (                         with _ as (                             select * from _ offset local.offset limit local.limit                         ) select coalesce(json_agg((select json_agg(_) from (                             select *, case when local.child then gar_child(_.id) end as child from gar_select(_.id::uuid)                         ) as _)), '[]'::json) from _                     ) as data from _                 ) select to_json(_) from _ into strict gar_select.json;             else -- \u0438\u043d\u0430\u0447\u0435 - \u043d\u0435 \u0432\u0441\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b                 with _ as (                     with _ as (                         select * from gar_select(local.id::uuid[])                     ) select count(1), gar_select.json as query, local.offset, local.limit, (                         with _ as (                             select *, case when local.child then gar_child(_.id) end as child from _ offset local.offset limit local.limit                         ) select coalesce(json_agg(_), '[]'::json) from _                     ) as data from _                 ) select to_json(_) from _ into strict gar_select.json;             end if;         else  -- \u0438\u043d\u0430\u0447\u0435 id - \u043d\u0435 \u043c\u0430\u0441\u0441\u0438\u0432             if local.full then -- \u0435\u0441\u043b\u0438 \u0432\u0441\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b                 with _ as (                     with _ as (                         select * from gar_select(local.id::uuid)                     ) select count(1), gar_select.json as query, local.offset, local.limit, (                         with _ as (                             select * from _ offset local.offset limit local.limit                         ) select coalesce(json_agg((select json_agg(_) from (                             select *, case when local.child then gar_child(_.id) end as child from gar_select(_.id::uuid)                         ) as _)), '[]'::json) from _                     ) as data from _                 ) select to_json(_) from _ into strict gar_select.json;             else -- \u0438\u043d\u0430\u0447\u0435 - \u043d\u0435 \u0432\u0441\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b                 with _ as (                     with _ as (                         select * from gar_select(local.id::uuid)                     ) select count(1), gar_select.json as query, local.offset, local.limit, (                         with _ as (                             select *, case when local.child then gar_child(_.id) end as child from _ offset local.offset limit local.limit                         ) select coalesce(json_agg(_), '[]'::json) from _                     ) as data from _                 ) select to_json(_) from _ into strict gar_select.json;             end if;         end if;     else -- \u0438\u043d\u0430\u0447\u0435 - \u043d\u0435 \u0437\u0430\u0434\u0430\u043d id         if local.text is not null then -- \u0435\u0441\u043b\u0438 \u0438\u0441\u043a\u0430\u0442\u044c \u0447\u0442\u043e-\u0442\u043e             local.name = local.text;             local.short = split_part(local.name, '.', 1);             if local.short = local.name or position(' ' in local.short) > 0 or position(',' in local.short) > 0 then                 local.short = null;             else                 local.name = split_part(local.name, '.', 2);             end if;             local.name = ltrim(local.name, ' ');         end if;         if local.text is not null and local.parent is null then -- \u0435\u0441\u043b\u0438 \u0438\u0441\u043a\u0430\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u0438 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c \u043d\u0435 \u0437\u0430\u0434\u0430\u043d             with _ as (                 with _ as (                     select * from gar_select_parent(local.parent::uuid, local.name, local.short, array['\u0413\u043e\u0440\u043e\u0434', '\u041f\u043e\u0441\u0435\u043b\u043e\u043a', '\u041f\u043e\u0441\u0435\u043b\u0435\u043d\u0438\u0435', '\u0414\u0435\u0440\u0435\u0432\u043d\u044f', '\u041d\u0430\u0441\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u043f\u0443\u043d\u043a\u0442', '\u0421\u0435\u043b\u043e', '\u0420\u0430\u0431\u043e\u0447\u0438\u0439 \u043f\u043e\u0441\u0435\u043b\u043e\u043a', '\u041f\u043e\u0441\u0435\u043b\u043e\u043a \u0433\u043e\u0440\u043e\u0434\u0441\u043a\u043e\u0433\u043e \u0442\u0438\u043f\u0430']::text, local.post, local.region)                 ) select count(1), gar_select.json as query, local.offset, local.limit, (                     with _ as (                         select * from _ offset local.offset limit local.limit                     ) select coalesce(json_agg((select json_agg(_) from (                         select * from gar_select(_.id, local.parent::uuid)                     ) as _)), '[]'::json) from _                 ) as data from _             ) select to_json(_) from _ into strict json;         else             local.type = translate(local.type, '[]','{}');             if local.full then -- \u0435\u0441\u043b\u0438 \u0432\u0441\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b                 with _ as (                     with _ as (                         select * from gar_select(local.parent::uuid, local.name, local.short, local.type, local.post, local.region)                     ) select count(1), gar_select.json as query, local.offset, local.limit, (                         with _ as (                             select * from _ offset local.offset limit local.limit                         ) select coalesce(json_agg((select json_agg(_) from (                             select *, case when local.child then gar_child(_.id) end as child from gar_select(_.id::uuid)                         ) as _)), '[]'::json) from _                     ) as data from _                 ) select to_json(_) from _ into strict gar_select.json;             else -- \u0438\u043d\u0430\u0447\u0435 - \u043d\u0435 \u0432\u0441\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b                 with _ as (                     with _ as (                         select * from gar_select(local.parent::uuid, local.name, local.short, local.type, local.post, local.region)                     ) select count(1), gar_select.json as query, local.offset, local.limit, (                         with _ as (                             select *, case when local.child then gar_child(_.id) end as child from _ offset local.offset limit local.limit                         ) select coalesce(json_agg(_), '[]'::json) from _                     ) as data from _                 ) select to_json(_) from _ into strict gar_select.json;             end if;         end if;     end if; end;$body$;<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/585244\/\"> https:\/\/habr.com\/ru\/articles\/585244\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0414\u043b\u044f \u043f\u0440\u0438\u0433\u043e\u0442\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0432\u0438\u0434\u0436\u0435\u0442\u0430 \u0413\u043e\u0441\u0443\u0434\u0430\u0440\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u0410\u0434\u0440\u0435\u0441\u043d\u043e\u0433\u043e \u0420\u0435\u0435\u0441\u0442\u0440\u0430 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0443\u0436\u043d\u043e \u0435\u0433\u043e (\u0413\u0410\u0420) <a href=\"https:\/\/habr.com\/ru\/post\/584724\/\" rel=\"noopener noreferrer nofollow\">\u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c<\/a>. \u041f\u0440\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0431\u0430\u0437\u044b \u0431\u044b\u043b\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u044b \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0434\u043b\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0432 \u043d\u0438\u0445 \u0413\u0410\u0420, \u043d\u043e \u0442\u0430\u043a\u0436\u0435 \u0438 \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u0432\u0438\u0434\u0436\u0435\u0442\u0430. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c\u0441\u044f \u043d\u0430 \u043d\u0438\u0445 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435.<\/p>\n<p>\u0418\u0442\u0430\u043a, \u0434\u043b\u044f \u0432\u0438\u0434\u0436\u0435\u0442\u0430 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443 (\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b), \u0432 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u043c \u0432\u0441\u0435 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e \u0432\u0441\u0435\u043c \u0440\u0435\u0433\u0438\u043e\u043d\u0430\u043c:<\/p>\n<pre><code class=\"sql\">CREATE TABLE IF NOT EXISTS gar ( -- \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0435\u0441\u043b\u0438 \u0435\u0451 \u0435\u0449\u0451 \u043d\u0435\u0442     -- \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u044b\u0439 \u043a\u043b\u044e\u0447     id uuid NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,     parent uuid, -- \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c     name text NOT NULL, -- \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0435     short text NOT NULL, -- \u043a\u0440\u0430\u0442\u043a\u0438\u0439 \u0442\u0438\u043f     type text NOT NULL, -- \u043f\u043e\u043b\u043d\u044b\u0439 \u0442\u0438\u043f     post text, -- \u043f\u043e\u0447\u0442\u043e\u0432\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441     region smallint NOT NULL -- \u043a\u043e\u0434 \u0440\u0435\u0433\u0438\u043e\u043d\u0430 ); CREATE INDEX IF NOT EXISTS gar_parent_idx ON gar USING btree (parent); CREATE INDEX IF NOT EXISTS gar_name_idx ON gar USING btree (name); CREATE INDEX IF NOT EXISTS gar_short_idx ON gar USING btree (short); CREATE INDEX IF NOT EXISTS gar_type_idx ON gar USING btree (type); CREATE INDEX IF NOT EXISTS gar_region_idx ON gar USING btree (region);<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u044f<\/p>\n<pre><code class=\"sql\">-- \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0438\u043b\u0438 \u043c\u0435\u043d\u044f\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043e\u0442 \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u044f, \u043a\u0440\u0430\u0442\u043a\u043e\u0433\u043e \u0438 \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430 CREATE OR REPLACE FUNCTION gar_text(name text, short text, type text) RETURNS text -- \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0443\u044e \u043f\u043e\u043b\u043d\u043e\u0435 \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0435 LANGUAGE sql -- \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0443\u044e \u044f\u0437\u044b\u043a sql IMMUTABLE AS $body$ -- \u0437\u0430\u0432\u0438\u0441\u044f\u0449\u0443\u044e \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0442 \u0441\u0432\u043e\u0438\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432     select case -- \u0432 \u0441\u043b\u0443\u0447\u0430\u0435     -- \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u043b\u043d\u044b\u0439 \u0442\u0438\u043f \u043d\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d, \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0438\u0442\u044c \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0435         when gar_text.type in ('\u041d\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043e') then gar_text.name         -- \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u043b\u043d\u044b\u0439 \u0442\u0438\u043f - \u0427\u0443\u0432\u0430\u0448\u0438\u044f, \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0438\u0442\u044c \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u043f\u043e\u043b\u043d\u044b\u0439 \u0442\u0438\u043f         when gar_text.type in ('\u0427\u0443\u0432\u0430\u0448\u0438\u044f') then gar_text.name||' '||gar_text.type         -- \u043a\u043e\u0433\u0434\u0430 \u0432 \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442\u0441\u044f \u043f\u043e\u043b\u043d\u044b\u0439 \u0442\u0438\u043f, \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0438\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0435         when gar_text.name ilike '%'||gar_text.type||'%' then gar_text.name         -- \u0438\u043d\u0430\u0447\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0438\u0442\u044c \u043a\u0440\u0430\u0442\u043a\u0438\u0439 \u0442\u0438\u043f \u0442\u043e\u0447\u043a\u0430 \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0435         else gar_text.short||'.'||gar_text.name     end; $body$;<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435\u043c \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043a\u043e\u043b\u043e\u043d\u043a\u0438<\/p>\n<pre><code class=\"sql\">CREATE OR REPLACE VIEW gar_view AS -- \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0438\u043b\u0438 \u043c\u0435\u043d\u044f\u0435\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 SELECT gar.*, -- \u0432\u044b\u0431\u0438\u0440\u0430\u044f \u0432\u0441\u0451 gar_text(name, short, type) AS text -- \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044f \u043f\u043e\u043b\u043d\u043e\u0435 \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0435 from gar; -- \u0438\u0437 \u0413\u0410\u0420\u0430<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u043f\u043e\u0434\u0441\u0447\u0451\u0442\u0430 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432<\/p>\n<pre><code class=\"sql\">-- \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0438\u043b\u0438 \u043c\u0435\u043d\u044f\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043e\u0442 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430 CREATE OR REPLACE FUNCTION gar_child(id uuid) -- \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0443\u044e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0432\u0442\u0432\u043e \u0435\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 RETURNS bigint LANGUAGE sql STABLE AS $body$     select count(1) from gar where parent = gar_child.id; $body$;<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0436\u0435, \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u0432\u044b\u0431\u043e\u0440\u0430, \u0435\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u043d \u043c\u0430\u0441\u0441\u0438\u0432 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u0432<\/p>\n<pre><code class=\"sql\">-- \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0438\u043b\u0438 \u043c\u0435\u043d\u044f\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043e\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u0432 CREATE OR REPLACE FUNCTION gar_select(id uuid[]) RETURNS SETOF gar_view -- \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0443\u044e \u043d\u0430\u0431\u043e\u0440 \u0441\u0442\u0440\u043e\u043a \u0438\u0437 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f LANGUAGE sql STABLE AS $body$ -- \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u0432\u0441\u0451, \u0432 \u0442.\u0447. \u0438 \u043f\u043e\u043b\u043d\u043e\u0435 \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0435     select gar.*, gar_text(name, short, type) AS text from gar     -- \u0432 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u043c \u0430\u0433\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u043e\u0440\u044f\u0434\u043a\u0435     inner join (select unnest(gar_select.id) as id,                 generate_subscripts(gar_select.id, 1) as i) as _ on _.id = gar.id     -- \u0434\u043b\u044f \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0445 \u0432 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u0432     where gar.id = any(gar_select.id) order by i; $body$;<\/code><\/pre>\n<p>\u0418 \u0435\u0449\u0451 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0433\u043e \u0432\u044b\u0431\u043e\u0440\u0430 \u0432\u0432\u0435\u0440\u0445 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u0439 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0433\u043e \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430, \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u044f\u0441\u044c \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u043c \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u043c \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u043c<\/p>\n<pre><code class=\"sql\">CREATE OR REPLACE FUNCTION gar_select( -- \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0438\u043b\u0438 \u043c\u0435\u043d\u044f\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043e\u0442 id uuid, -- \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430 parent uuid DEFAULT NULL -- \u0438 \u043d\u0435\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430 ) RETURNS SETOF gar_view -- \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0443\u044e \u043d\u0430\u0431\u043e\u0440 \u0441\u0442\u0440\u043e\u043a \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f LANGUAGE sql STABLE AS $body$     with recursive _ as ( -- \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e       -- \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u0431\u0438\u0440\u0430\u044f \u043f\u043e \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u043c\u0443 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0443         select gar.*, 0 as i from gar where id = gar_select.id         union       -- \u0430 \u043f\u043e\u0442\u043e\u043c \u0432\u044b\u0431\u0438\u0440\u0430\u044f \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u0439 \u0432\u0432\u0435\u0440\u0445 \u0434\u043e \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0433\u043e \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f \u0438\u043b\u0438 \u0434\u043e \u0441\u0430\u043c\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u0430         select gar.*, _.i + 1 as i from gar inner join _ on (_.parent = gar.id)         where gar_select.parent is null or _.parent != gar_select.parent     ) select id, parent, name, short, type, post, region, gar_text(name, short, type) AS text      from _ order by i desc; $body$;<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430<\/p>\n<pre><code class=\"sql\">CREATE OR REPLACE FUNCTION gar_select( -- \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0438\u043b\u0438 \u043c\u0435\u043d\u044f\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e parent uuid, -- \u043e\u0442 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f   name text, -- \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u044f   short text, -- \u043a\u0440\u0430\u0442\u043a\u043e\u0433\u043e \u0442\u0438\u043f\u0430   type text, -- \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430   post text, -- \u043f\u043e\u0447\u0442\u043e\u0432\u043e\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430   region text -- \u043a\u043e\u0434\u0430 \u0440\u0435\u0433\u0438\u043e\u043d\u0430 ) RETURNS SETOF gar_view -- \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0443\u044e \u043d\u0430\u0431\u043e\u0440 \u0441\u0442\u0440\u043e\u043a \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f LANGUAGE sql STABLE AS $body$ -- \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u0432\u0441\u0451, \u0433\u0434\u0435     select gar.*, gar_text(gar.name, gar.short, gar.type) AS text from gar where true     -- \u0435\u0441\u043b\u0438 \u043d\u0435 \u0437\u0430\u0434\u0430\u043d \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c \u0442\u043e \u0431\u0435\u0437 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f, \u0430 \u0435\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u043d - \u0442\u043e \u043f\u043e \u043d\u0435\u043c\u0443     and ((gar_select.parent is null and parent is null) or parent = gar_select.parent) -- \u0435\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u043d\u043e \u043d\u0430\u0438\u043c\u043d\u043e\u0432\u0430\u043d\u0438\u0435, \u0442\u043e \u0438\u0449\u0435\u043c \u043f\u043e \u043d\u0435\u043c\u0443 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u043b\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0431\u0435\u043b\u0430 \u0438\u043b\u0438 \u043f\u043e\u0441\u043b\u0435 \u0434\u0435\u0444\u0438\u0441\u0430 \u0438\u043b\u0438 \u043f\u043e\u0441\u043b\u0435 \u0442\u043e\u0447\u043a\u0438         and (gar_select.name is null or name ilike gar_select.name||'%' or name ilike '% '||gar_select.name||'%' or name ilike '%-'||gar_select.name||'%' or name ilike '%.'||gar_select.name||'%')     -- \u0435\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u043d \u043a\u0440\u0430\u0442\u043a\u0438 \u0442\u0438\u043f, \u0442\u043e \u0438\u0449\u0435\u043c \u043f\u043e \u043d\u0435\u043c\u0443     and (gar_select.short is null or short ilike gar_select.short)     -- \u0435\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u043d \u043f\u043e\u043b\u043d\u044b\u0439 \u0442\u0438\u043f \u0438 \u044d\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 - \u0442\u043e \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u043c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0430 \u0435\u0441\u043b\u0438 \u043d\u0435 \u043c\u0430\u0441\u0441\u0438\u0432, \u0442\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430     and (gar_select.type is null or case when gar_select.type ilike '{%}' then type = any(gar_select.type::text[]) else type ilike gar_select.type||'%' end)     -- \u0435\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u043d \u043f\u043e\u0447\u0442\u043e\u0432\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441, \u0442\u043e \u0438\u0449\u0435\u043c \u043f\u043e \u043d\u0435\u043c\u0443 \u0441\u043d\u0430\u0447\u0430\u043b\u0430     and (gar_select.post is null or post ilike gar_select.post||'%')     -- \u0435\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u043d \u043a\u043e\u0434 \u0440\u0435\u0433\u0438\u043e\u043d\u0430 \u0438 \u044d\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 - \u0442\u043e \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u043c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0430 \u0435\u0441\u043b\u0438 \u043d\u0435 \u043c\u0430\u0441\u0441\u0438\u0432 - \u0442\u043e \u0438\u0449\u0435\u043c \u043f\u043e \u043d\u0435\u043c\u0443      and (gar_select.region is null or case when gar_select.region ilike '{%}' then region = any(gar_select.region::smallint[]) else region = gar_select.region::smallint end)     -- \u0441\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e \u0447\u0438\u0441\u043b\u0435\u043d\u043d\u043e\u043c\u0443 \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u044e, \u0430 \u043f\u043e\u0442\u043e\u043c \u043f\u043e \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u044e     order by to_number('0'||name, '999999999'), name; $body$;<\/code><\/pre>\n<p>\u0418 \u0435\u0449\u0451 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u0439<\/p>\n<pre><code class=\"sql\">-- \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0438\u043b\u0438 \u043c\u0435\u043d\u044f\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043e\u0442 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f, \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u044f, ... CREATE OR REPLACE FUNCTION gar_select_parent(parent uuid, name text, short text, type text, post text, region text) RETURNS SETOF gar_view LANGUAGE sql STABLE AS $body$ -- \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u0432\u0441\u0451     select gar.*, gar_text(gar.name, gar.short, gar.type) AS text from gar     -- \u0433\u0434\u0435 \u043f\u043e\u043b\u043d\u044b\u0439 \u0442\u0438\u043f \u0438\u0437 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430     where type = any(gar_select_parent.type::text[])     -- \u0438 \u0435\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u043d\u043d\u043e \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0435 , \u0442\u043e \u0438\u0449\u0435\u043c \u043f\u043e \u043d\u0435\u043c\u0443 \u0438\u043b\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0431\u0435\u043b\u0430 \u0438\u043b\u0438 \u043f\u043e\u0441\u043b\u0435 \u0434\u0435\u0444\u0438\u0441\u0430 \u0438\u043b\u0438 \u043f\u043e\u0441\u043b\u0435 \u0442\u043e\u0447\u043a\u0438     and (gar_select_parent.name is null or name ilike gar_select_parent.name||'%' or name ilike '% '||gar_select_parent.name||'%' or name ilike '%-'||gar_select_parent.name||'%' or name ilike '%.'||gar_select_parent.name||'%') -- \u0441\u043e\u0440\u0442\u0438\u0440\u0443\u044f \u043f\u043e \u0433\u043b\u0443\u0431\u0438\u043d\u0435 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u0438         order by (select count(id) from gar_select(id, gar_select_parent.parent)), to_number('0'||name, '999999999'), name; $body$;<\/code><\/pre>\n<p>\u041d\u0443 \u0438 \u0442\u0435\u043f\u0435\u0440\u044c, \u0433\u043b\u0430\u0432\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u0432\u0438\u0434\u0436\u0435\u0442\u0430<\/p>\n<pre><code class=\"sql\">-- \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0438\u043b\u0438 \u043c\u0435\u043d\u044f\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043e\u0442 json, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0443\u044e json CREATE OR REPLACE FUNCTION gar_select(INOUT json json) RETURNS json LANGUAGE plpgsql STABLE AS $body$ &lt;&lt;local>> declare     id text default nullif(trim(gar_select.json->>'id'), ''); -- \u0443\u0438\u0434     parent text default nullif(trim(gar_select.json->>'parent'), ''); -- \u0443\u0438\u0434 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f     name text default nullif(trim(gar_select.json->>'name'), ''); -- \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0435     short text default nullif(trim(gar_select.json->>'short'), ''); -- \u043a\u0440\u0430\u0442\u043a\u043e     type text default nullif(trim(gar_select.json->>'type'), ''); -- \u0442\u0438\u043f     post text default nullif(trim(gar_select.json->>'port'), ''); -- \u0438\u043d\u0434\u0435\u043a\u0441     region text default nullif(trim(gar_select.json->>'region'), ''); -- \u0440\u0435\u0433\u0438\u043e\u043d     text text default nullif(trim(gar_select.json->>'text'), ''); -- \u0441\u0442\u0440\u043e\u043a\u0430 \u043f\u043e\u0438\u0441\u043a\u0430     offset int default coalesce(nullif(trim(gar_select.json->>'offset'), '')::int, 0); -- \u043e\u0444\u0441\u0435\u0442     limit int default coalesce(nullif(trim(gar_select.json->>'limit'), '')::int, 10); -- \u043b\u0438\u043c\u0438\u0442     \"full\" boolean default coalesce(nullif(trim(gar_select.json->>'full'), '')::boolean, false); -- \u0432\u0441\u0435?     child boolean default coalesce(nullif(trim(gar_select.json->>'child'), '')::boolean, false); -- \u0434\u0435\u0442\u0438? begin     if local.id is not null then -- \u0435\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u043d id         local.id = translate(local.id, '[]','{}');         if local.id ilike '{%}' then -- \u0435\u0441\u043b\u0438 id - \u043c\u0430\u0441\u0441\u0438\u0432             if local.full then -- \u0435\u0441\u043b\u0438 \u0432\u0441\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b                 with _ as (                     with _ as (                         select * from gar_select(local.id::uuid[])                     ) select count(1), gar_select.json as query, local.offset, local.limit, (                         with _ as (                             select * from _ offset local.offset limit local.limit                         ) select coalesce(json_agg((select json_agg(_) from (                             select *, case when local.child then gar_child(_.id) end as child from gar_select(_.id::uuid)                         ) as _)), '[]'::json) from _                     ) as data from _                 ) select to_json(_) from _ into strict gar_select.json;             else -- \u0438\u043d\u0430\u0447\u0435 - \u043d\u0435 \u0432\u0441\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b                 with _ as (                     with _ as (                         select * from gar_select(local.id::uuid[])                     ) select count(1), gar_select.json as query, local.offset, local.limit, (                         with _ as (                             select *, case when local.child then gar_child(_.id) end as child from _ offset local.offset limit local.limit                         ) select<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-403817","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/403817","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=403817"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/403817\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=403817"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=403817"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=403817"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}