-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
161 lines (144 loc) · 4 KB
/
schema.sql
File metadata and controls
161 lines (144 loc) · 4 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
160
161
CREATE TABLE modreports (
id SERIAL PRIMARY KEY,
reporter BIGINT NOT NULL,
target BIGINT NOT NULL, -- the user in question
reportRemarks TEXT, -- the remarks sent with the report. could be None if there were no remarks
channel BIGINT, -- when targeting a message, these two will be populated
message BIGINT,
reportMessage BIGINT, -- the message in the report channel, for persistent views
mod BIGINT, -- the mod who responded to the report
modAction TEXT, -- the action taken. Automatically generated
modRemarks TEXT,
modResponse TEXT
);
CREATE TABLE tags_new (
id SERIAL UNIQUE,
name TEXT NOT NULL CHECK (char_length(name) <= 32),
PRIMARY KEY (name),
content TEXT NOT NULL CHECK (char_length(content) <= 2000),
owner BIGINT NOT NULL,
uses INT DEFAULT 0,
created TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT (NOW() AT TIME ZONE 'utc')
);
CREATE TABLE tag_lookup (
name TEXT PRIMARY KEY CHECK (char_length(name) <= 32),
tagId INT NOT NULL REFERENCES tags_new(id),
isAlias BOOLEAN NOT NULL
);
CREATE FUNCTION isTagOwner(tagID_ INTEGER, requester BIGINT)
RETURNS BOOLEAN
LANGUAGE plpgsql
AS
$$
BEGIN
IF ((
SELECT 1
FROM tags_new
WHERE
owner = requester AND id = tagID_
) IS NULL
)
THEN RETURN FALSE;
END IF;
RETURN TRUE;
END
$$;
CREATE FUNCTION findTag(givenName TEXT)
RETURNS TEXT[] -- [name, content]
LANGUAGE plpgsql
AS
$$
DECLARE
tagID_ INT;
tagName TEXT;
tagContent TEXT;
BEGIN
SELECT tagId
INTO tagID_
FROM tag_lookup
WHERE name = givenName;
IF tagID_ IS NULL
THEN RETURN NULL;
END IF;
UPDATE tags_new
SET uses = uses + 1
WHERE id = tagID_
RETURNING name, content
INTO tagName, tagContent;
RETURN ARRAY[tagName, tagContent];
END
$$;
CREATE FUNCTION createTag (tag_name TEXT, tag_content TEXT, tag_owner BIGINT)
RETURNS INT
LANGUAGE plpgsql
AS
$$
DECLARE
tagID_ INT;
BEGIN
INSERT INTO
tags_new (name, content, owner)
VALUES (tag_name, tag_content, tag_owner)
RETURNING id INTO tagID_;
INSERT INTO
tag_lookup (name, tagId, isAlias)
VALUES (tag_name, tagID_, FALSE);
RETURN tagID_;
END
$$;
CREATE FUNCTION createAlias (originalTag TEXT, aliasName TEXT)
RETURNS TEXT
LANGUAGE plpgsql
AS
$$
DECLARE
tagID_ INT;
tagName TEXT;
BEGIN
SELECT
tagId, name
INTO tagID_, tagName
FROM tag_lookup
WHERE name = originalTag;
IF (tagID_ IS NULL)
THEN RETURN 'Tag does not exist';
END IF;
INSERT INTO
tag_lookup (name, tagId, isAlias)
VALUES (aliasName, tagID_, TRUE);
RETURN concat('Created alias ', aliasName, ' that points to ', tagName);
END
$$;
CREATE FUNCTION deleteTag (tagName TEXT, requester BIGINT)
RETURNS TEXT
LANGUAGE plpgsql
AS
$$
DECLARE
tagID_ INT;
isAlias_ BOOLEAN;
BEGIN
SELECT
tagId, isAlias
INTO tagID_, isAlias_
FROM tag_lookup
WHERE name = tagName;
IF (tagID_ IS NULL)
THEN RETURN 'This tag does not exist';
END IF;
IF (isTagOwner(tagID_, requester) IS FALSE)
THEN RETURN 'You do not own this tag';
END IF;
IF (isAlias_ IS FALSE)
THEN
DELETE FROM tag_lookup WHERE tagId = tagID_;
DELETE FROM tags_new WHERE id = tagID_;
RETURN 'Tag deleted';
END IF;
IF (isAlias_ IS TRUE)
THEN
DELETE FROM tag_lookup WHERE name = tagName;
RETURN 'Tag alias deleted';
END IF;
END
$$;