Guest User

Untitled

a guest
Feb 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. create or replace function array_push(text[], text) returns text[] as $$
  2. select coalesce($1 || $2, array[$2]);
  3. $$ language sql;
  4.  
  5. create or replace function array_push(int[], int) returns int[] as $$
  6. select coalesce($1 || $2, array[$2]);
  7. $$ language sql;
  8.  
  9. orkut=# select array_push(array[1,2,3], 4);
  10. array_push
  11. ------------
  12. {1,2,3,4}
  13. (1 registro)
  14.  
  15. orkut=# select array_push(null::int[], 2);
  16. array_push
  17. ------------
  18. {2}
  19. (1 registro)
  20.  
  21. ---------------------------------------------------------------------------
  22.  
  23. create or replace function array_pop(int[]) returns int[] as $$
  24. select ($1)[array_lower($1, 1) + 1 : array_upper($1, 1)];
  25. $$ language sql;
  26.  
  27. orkut=# select array_pop(array[1,2,3,4]);
  28. array_pop
  29. -----------
  30. {2,3,4}
  31. (1 registro)
  32.  
  33. orkut=# select array_pop(string_to_array('4,54,56,43', ',')::int[]);
  34. array_pop
  35. ------------
  36. {54,56,43}
  37. (1 registro)
  38.  
  39. ---------------------------------------------------------------------------
  40.  
  41. create or replace function array_first(int[]) returns int as $$
  42. select ($1)[array_lower($1, 1)];
  43. $$ language sql;
  44.  
  45. orkut=# select array_first(array[2,3,5]);
  46. array_first
  47. -------------
  48. 2
  49. (1 registro)
  50.  
  51. ---------------------------------------------------------------------------
  52.  
  53. create or replace function array_last(int[]) returns int as $$
  54. select ($1)[array_upper($1, 1)];
  55. $$ language sql;
  56.  
  57. orkut=# select array_last(array[1,3,5]);
  58. array_last
  59. ------------
  60. 5
  61. (1 registro)
  62.  
  63. ---------------------------------------------------------------------------
  64.  
  65. create or replace function array_contains(int[], int) returns boolean as $$
  66. select $2 = any($1);
  67. $$ language sql;
  68.  
  69. orkut=# select array_contains(array[1,3,5], 4);
  70. array_contains
  71. ----------------
  72. f
  73. (1 registro)
  74.  
  75. orkut=# select array_contains(array[1,3,5], 5);
  76. array_contains
  77. ----------------
  78. t
  79. (1 registro)
  80.  
  81. ---------------------------------------------------------------------------
  82.  
  83. create or replace function array_position(int[], int) returns int as $$
  84. declare
  85. ii int;
  86. begin
  87. if not $2 = any($1) then
  88. return null;
  89. end if;
  90.  
  91. for ii in array_lower($1, 1)..array_upper($1, 1)
  92. loop
  93. if ($1)[ii] = $2 then
  94. return ii;
  95. end if;
  96. end loop;
  97.  
  98. return null;
  99. end
  100. $$ language plpgsql;
  101.  
  102. orkut=# select array_position(array[1,3,5], 3);
  103. array_position
  104. ----------------
  105. 2
  106. (1 registro)
  107.  
  108. orkut=# select array_position(array[1,3,5], 5);
  109. array_position
  110. ----------------
  111. 3
  112. (1 registro)
  113.  
  114. orkut=# select array_position(array[1,3,5], 4);
  115. array_position
  116. ----------------
  117.  
  118. (1 registro)
Add Comment
Please, Sign In to add comment