-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsupabase.sql
More file actions
159 lines (140 loc) · 3.31 KB
/
supabase.sql
File metadata and controls
159 lines (140 loc) · 3.31 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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;$$;
-- Marketplace analytics table
create table if not exists marketplace_analytics (
item_id text not null,
category text not null,
views bigint default 0,
downloads bigint default 0,
updated_at timestamp with time zone default now(),
constraint marketplace_analytics_pkey primary key (item_id, category)
);
create index if not exists idx_marketplace_analytics_category on marketplace_analytics(category);
create index if not exists idx_marketplace_analytics_views on marketplace_analytics(views desc);
create index if not exists idx_marketplace_analytics_downloads on marketplace_analytics(downloads desc);
-- Function to increment marketplace item views
create or replace function increment_marketplace_views(
_item_id text,
_category text
)
returns void
language plpgsql
as $$
begin
insert into marketplace_analytics (item_id, category, views, updated_at)
values (_item_id, _category, 1, now())
on conflict (item_id, category)
do update set
views = marketplace_analytics.views + 1,
updated_at = now();
end;$$;
-- Function to increment marketplace item downloads
create or replace function increment_marketplace_downloads(
_item_id text,
_category text
)
returns void
language plpgsql
as $$
begin
insert into marketplace_analytics (item_id, category, downloads, updated_at)
values (_item_id, _category, 1, now())
on conflict (item_id, category)
do update set
downloads = marketplace_analytics.downloads + 1,
updated_at = now();
end;$$;