Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. CREATE FUNCTION public.list_container_by_parent(in_parent_id uuid, in_locale text, in_sort_key text, in_custom_init_key text) RETURNS SETOF public.node
  2. LANGUAGE sql STABLE COST 30 ROWS 20
  3. AS $$
  4. -- Returns the child containers of the specified parent container in the
  5. -- sort order specified by the sort key and locale.
  6. -- If sort key is not specified, the sort key of the parent container,
  7. -- or default for parent container type, is used.
  8. -- If custom_init key is specified, containers that have this key set are
  9. -- excluded from output. The value of the custom_init key doesn't matter.
  10. WITH
  11. container_default_sort_key(container_type, default_sort_key) AS (
  12. VALUES
  13. ('folder', 'index_asc'),
  14. ('album', 'index_asc'),
  15. ('blog', 'updated_desc')
  16. ),
  17. parent_container AS (
  18. SELECT
  19. c.container_id,
  20. COALESCE(
  21. in_sort_key,
  22. c.sort_key,
  23. cdsk.default_sort_key
  24. ) AS parent_sort_key
  25. FROM container c
  26. JOIN container_default_sort_key cdsk USING(container_type)
  27. WHERE c.container_id = in_parent_id
  28. ),
  29. child_containers AS (
  30. SELECT
  31. ct.child_container_id AS container_id,
  32. -- Only extract custom_init value if key was specified
  33. CASE
  34. WHEN in_custom_init_key IS NOT NULL
  35. THEN extract_custom_init_value(c.custom_init, in_custom_init_key)
  36. ELSE NULL
  37. END AS _extracted_custom_init_value
  38. FROM container_tree ct
  39. JOIN container c ON ct.child_container_id = c.container_id
  40. JOIN parent_container pc ON ct.parent_container_id = pc.container_id
  41. -- Sort based on specified or calculated sort key
  42. -- Using COALESCE(in_sort_key, ...) here ensures constant folding
  43. -- of the entire sort expression
  44. ORDER BY
  45. CASE COALESCE(in_sort_key, pc.parent_sort_key)
  46. WHEN 'index_asc' THEN c.sort_number
  47. WHEN 'size_asc' THEN c.sort_number
  48. END ASC NULLS LAST,
  49. CASE COALESCE(in_sort_key, pc.parent_sort_key)
  50. WHEN 'updated_asc' THEN c.updated
  51. WHEN 'created_asc' THEN c.created
  52. END ASC NULLS FIRST,
  53. CASE COALESCE(in_sort_key, pc.parent_sort_key)
  54. WHEN 'index_desc' THEN c.sort_number
  55. WHEN 'size_desc' THEN c.sort_number
  56. END DESC NULLS FIRST,
  57. CASE COALESCE(in_sort_key, pc.parent_sort_key)
  58. WHEN 'updated_desc' THEN c.updated
  59. WHEN 'created_desc' THEN c.created
  60. END DESC NULLS LAST,
  61. CASE COALESCE(in_sort_key, pc.parent_sort_key)
  62. WHEN 'index_asc' THEN collkey(c.title, in_locale)
  63. WHEN 'title_asc' THEN collkey(c.title, in_locale)
  64. WHEN 'size_asc' THEN collkey(c.title, in_locale)
  65. WHEN 'updated_asc' THEN collkey(c.title, in_locale)
  66. WHEN 'created_asc' THEN collkey(c.title, in_locale)
  67. WHEN 'updated_desc' THEN collkey(c.title, in_locale)
  68. WHEN 'created_desc' THEN collkey(c.title, in_locale)
  69. END ASC,
  70. CASE COALESCE(in_sort_key, pc.parent_sort_key)
  71. WHEN 'index_desc' THEN collkey(c.title, in_locale)
  72. WHEN 'title_desc' THEN collkey(c.title, in_locale)
  73. WHEN 'size_desc' THEN collkey(c.title, in_locale)
  74. END DESC
  75. )
  76. SELECT
  77. cc.container_id,
  78. 'container'::text,
  79. row_number() OVER ()::integer,
  80. NULL::integer
  81. FROM child_containers cc
  82. -- Exclude containers where custom_init value is present
  83. WHERE cc._extracted_custom_init_value IS NULL
  84. $$;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement