-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathschema_postgres.sql
More file actions
48 lines (45 loc) · 1.17 KB
/
schema_postgres.sql
File metadata and controls
48 lines (45 loc) · 1.17 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
CREATE TABLE tags (
id serial,
tag varchar(100) NOT NULL,
hits integer NOT NULL,
UNIQUE (tag)
);
CREATE TABLE uploads (
id serial,
"user" varchar(80) default NULL,
description text NOT NULL,
tags text NOT NULL,
"public" NUMERIC(1) NOT NULL,
name varchar(60) default NULL
);
CREATE TABLE users (
id serial UNIQUE,
pseudo varchar(20) NOT NULL,
"session" varchar(40) default NULL,
password varchar(30) default NULL,
membre NUMERIC(1) NOT NULL,
ip varchar(15) NOT NULL
);
-- Replace the update on duplicate key
CREATE FUNCTION insert_on_duplicate_key_tags(tag_update TEXT, hits_tag INT) RETURNS VOID AS
$$
BEGIN
LOOP
-- first try to update the key
UPDATE tags SET hits = hits_tag WHERE tag=tag_update;
IF found THEN
RETURN;
END IF;
-- not there, so try to insert the key
-- if someone else inserts the same key concurrently,
-- we could get a unique-key failure
BEGIN
INSERT INTO tags (tag, hits) VALUES (tag_update, hits_tag);
RETURN;
EXCEPTION WHEN unique_violation THEN
-- do nothing, and loop to try the UPDATE again
END;
END LOOP;
END;
$$
LANGUAGE plpgsql;