forked from derpibooru/philomena
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcreate_notable_table.sql
More file actions
executable file
·36 lines (30 loc) · 1.12 KB
/
create_notable_table.sql
File metadata and controls
executable file
·36 lines (30 loc) · 1.12 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
CREATE TABLE IF NOT EXISTS notable_names (
name varchar PRIMARY KEY,
source text NOT NULL,
created_at timestamp DEFAULT current_timestamp
);
DROP TRIGGER IF EXISTS notable_name_insert_trigger on public.notable_names;
CREATE OR REPLACE FUNCTION notable_name_processing(name text)
RETURNS text AS '
BEGIN
name := regexp_replace(name,''[|/\.,?><:~!@#$%^&*_=+\011\042\047\050\051\073\133\135\140\173\175-]+'','''',''g'');
name := regexp_replace(name,''\s+'','''',''g'');
name := LOWER(name);
RETURN name;
END' LANGUAGE 'plpgsql';
CREATE OR REPLACE FUNCTION notable_name_query(query_name text)
RETURNS TABLE (name varchar, source text, created_at timestamp) AS '
BEGIN
query_name := notable_name_processing(query_name);
RETURN QUERY SELECT * FROM notable_names WHERE notable_names.name=query_name;
END' LANGUAGE 'plpgsql';
CREATE OR REPLACE FUNCTION notable_name_insert_processing()
RETURNS trigger AS '
BEGIN
NEW.name := notable_name_processing(NEW.name);
RETURN NEW;
END' LANGUAGE 'plpgsql';
CREATE TRIGGER notable_name_insert_trigger
BEFORE INSERT ON notable_names
FOR EACH ROW
EXECUTE PROCEDURE notable_name_insert_processing();