-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
supabase.sql
108 lines (96 loc) · 1.85 KB
/
supabase.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
create or replace index on "public"."images" using btree (category);
create or replace index on "public"."quotes" using btree (language);
create or replace function get_image_categories()
returns table (
name text,
count bigint
)
language plpgsql
as $$
begin
return query
select
category as name,
count(category)
from images
group by category
order by count(category) desc;
end;$$;
create or replace function get_image_photographers()
returns table (
name text,
count bigint
)
language plpgsql
as $$
begin
return query
select
photographer as name,
count(photographer)
from images
group by photographer
order by count(photographer) desc;
end;$$;
create or replace function get_quote_languages()
returns table (
name text,
count bigint
)
language plpgsql
as $$
begin
return query
select
language as name,
count(language)
from quotes
group by language
order by count(language) desc;
end;$$;
create or replace function get_random_image(
_category text default 'landscapes',
_exclude text default ''
)
returns setof images
language plpgsql
as $$
begin
return query
select *
from images
where
category = _category
and
cast(pun as text) not in (select * from unnest(string_to_array(_exclude, ',')))
order by random()
limit 1;
end;$$;
create or replace function get_random_old_quote(
_language text default 'English'
)
returns setof old_quotes
language plpgsql
as $$
begin
return query
select *
from old_quotes
where language = _language
order by random()
limit 1;
end;$$;
create or replace function get_random_quote(
_language text default 'en'
)
returns setof quotes
language plpgsql
as $$
begin
return query
select *
from quotes
where language = _language
order by random()
limit 1;
end;$$;