Guest User

Untitled

a guest
Jan 18th, 2018
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. -- Has the player been banned?
  2. select true from pref_ban where id=?
  3.  
  4. -- What is the reputation of this player?
  5. select
  6. count(nullif(nice, false)) -
  7. count(nullif(nice, true)) as rep
  8. from pref_rep where id=?
  9.  
  10. -- Is he or she a special VIP player?
  11. select vip > now() as vip from pref_users where id=?
  12.  
  13. -- How many games has the player played to the end?
  14. select completed from pref_match where id=?
  15.  
  16. create or replace function get_user_info(_id varchar) returns XXX as $BODY$
  17. declare
  18. is_banned boolean;
  19. reputation integer;
  20. is_vip boolean;
  21. completed_games integer;
  22. begin
  23.  
  24. select 1 into is_banned from pref_ban where id=_id;
  25.  
  26. select
  27. count(nullif(nice, false)) -
  28. count(nullif(nice, true))
  29. into reputation
  30. from pref_rep where id=_id;
  31.  
  32. select vip > now() into is_vip from pref_users where id=_id;
  33.  
  34. select completed into completed_games from pref_match where id=_id;
  35.  
  36. return XXX; /* How to return 4 values here? */
  37.  
  38. end;
  39. $BODY$ language plpgsql;
  40.  
  41. create or replace function get_user_info(
  42. IN _id varchar,
  43. OUT is_banned boolean,
  44. OUT reputation integer,
  45. OUT is_vip boolean,
  46. OUT completed_games integer
  47. )
  48. -- no returns clause necessary, output structure controlled by OUT parameters
  49. -- returns XXX
  50. as $BODY$
  51. begin
  52. select true into is_banned from pref_ban where id=_id;
  53.  
  54. select
  55. count(nullif(nice, false)) -
  56. count(nullif(nice, true))
  57. into reputation
  58. from pref_rep where id=_id;
  59.  
  60. select vip > now() into is_vip from pref_users where id=_id;
  61.  
  62. select completed into completed_games from pref_match where id=_id;
  63.  
  64. -- no return statement necessary, output values already stored in OUT parameters
  65. -- return XXX;
  66. end
  67. $BODY$ language plpgsql;
  68.  
  69. -- this will return all properties (columns) from your function:
  70. select * from get_user_info();
  71.  
  72. -- these will return one property (column) from your function:
  73. select is_banned from get_user_info();
  74. select (get_user_info()).is_banned;
  75.  
  76. create type user_type as (
  77. is_banned boolean,
  78. reputation integer,
  79. is_vip boolean,
  80. completed_games integer);
  81.  
  82. create or replace function check_user_type ()
  83. returns user_type language plpgsql as $$
  84. declare
  85. rec user_type;
  86. begin
  87. select true into rec.is_banned;
  88. select 100 into rec.reputation;
  89. select false into rec.is_vip;
  90. select 22 into rec.completed_games;
  91. -- you can do the same in a little bit nicer way:
  92. -- select true, 100, false, 22 into rec
  93. return rec;
  94. end $$;
  95.  
  96. select * from check_user_type();
  97.  
  98. create or replace function check_set_of_user_type ()
  99. returns setof user_type language plpgsql as $$
  100. declare
  101. rec user_type;
  102. begin
  103. for rec in
  104. select i/2*2 = i, i, i < 3, i+ 20
  105. from generate_series(1, 4) i
  106. loop
  107. return next rec;
  108. end loop;
  109.  
  110. return query
  111. select true, 100+ i, true, 100+ i
  112. from generate_series(1, 2) i;
  113. end $$;
  114.  
  115. select * from check_set_of_user_type();
  116.  
  117. is_banned | reputation | is_vip | completed_games
  118. -----------+------------+--------+-----------------
  119. f | 1 | t | 21
  120. t | 2 | t | 22
  121. f | 3 | f | 23
  122. t | 4 | f | 24
  123. t | 101 | t | 101
  124. t | 102 | t | 102
Add Comment
Please, Sign In to add comment