support_functions
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
support_functions [2012/02/27 20:52] – daniel | support_functions [2017/05/26 11:46] – [get_header_line()] daniel | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== tag_path(): extract the tag name with its hierarchy | + | ====== tag_path(int) ====== |
+ | **Extract the tag name with its hierarchy.** \\ | ||
This recursive function takes the ID of a tag and returns its full name, including its hierarchy. | This recursive function takes the ID of a tag and returns its full name, including its hierarchy. | ||
- | <code sql> | + | <file sql tag_path.sql> |
CREATE OR REPLACE FUNCTION tag_path(in_tag_id INTEGER) RETURNS text AS | CREATE OR REPLACE FUNCTION tag_path(in_tag_id INTEGER) RETURNS text AS | ||
$$ | $$ | ||
Line 21: | Line 22: | ||
$$ LANGUAGE plpgsql STABLE; | $$ LANGUAGE plpgsql STABLE; | ||
- | </code> | + | </file> |
Example: | Example: | ||
Line 27: | Line 28: | ||
will retrieve all tags sorted and formatted as in the " | will retrieve all tags sorted and formatted as in the " | ||
- | ====== tag_depth(): returns | + | |
+ | ====== tag_depth(int) | ||
+ | **Returns | ||
This recursive function takes the ID of a tag and returns its depth inside its hierarchy, starting by 1 for top-level tags. | This recursive function takes the ID of a tag and returns its depth inside its hierarchy, starting by 1 for top-level tags. | ||
- | <code sql> | + | <file sql tag_depth.sql> |
CREATE OR REPLACE FUNCTION tag_depth(in_tag_id INTEGER) RETURNS INT AS | CREATE OR REPLACE FUNCTION tag_depth(in_tag_id INTEGER) RETURNS INT AS | ||
$$ | $$ | ||
Line 47: | Line 50: | ||
END; | END; | ||
$$ LANGUAGE plpgsql STABLE STRICT; | $$ LANGUAGE plpgsql STABLE STRICT; | ||
- | </code> | + | </file> |
+ | |||
+ | ====== tag_id(text) ====== | ||
+ | **Returns the ID of a tag from its full hierarchical name (case insensitive).** \\ | ||
+ | This is the inverse function of tag_path(int). | ||
+ | If the name is not found, it returns null. | ||
+ | <file sql tag_id.sql> | ||
+ | CREATE OR REPLACE FUNCTION tag_id(in_tag_path text) RETURNS integer AS | ||
+ | $$ | ||
+ | DECLARE | ||
+ | id INTEGER: | ||
+ | | ||
+ | BEGIN | ||
+ | FOR component IN SELECT regexp_split_to_table(in_tag_path, | ||
+ | LOOP | ||
+ | SELECT tag_id FROM tags WHERE upper(NAME)=upper(component) AND parent_id is not distinct from id | ||
+ | INTO id; | ||
+ | END LOOP; | ||
+ | RETURN id; | ||
+ | END; | ||
+ | $$ LANGUAGE plpgsql STABLE STRICT; | ||
+ | </ | ||
+ | |||
+ | ====== get_header_line() ====== | ||
+ | **Extract entries from a mail header.** \\ | ||
+ | This function takes a mail_id and the name of a header field and returns all corresponding header entries for this message. | ||
+ | |||
+ | <file sql get_header_line.sql> | ||
+ | CREATE FUNCTION get_header_line(int, | ||
+ | AS $$ | ||
+ | | ||
+ | FROM header WHERE mail_id=$1; | ||
+ | $$ LANGUAGE sql; | ||
+ | |||
+ | </file> |
support_functions.txt · Last modified: 2018/04/20 11:17 by daniel