-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.sql
80 lines (68 loc) · 2.51 KB
/
create.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
CREATE TABLE playerdata
(
uuid binary(16) NOT NULL,
username varchar(16) NOT NULL,
username_lower varchar(16) GENERATED ALWAYS AS (lower(username)) STORED,
first_address int unsigned NOT NULL comment 'ipv4 address stored as an unsigned int',
last_address int unsigned NOT NULL comment 'ipv4 address stored as an unsigned int',
first_login timestamp NOT NULL DEFAULT current_timestamp(),
last_login timestamp NOT NULL DEFAULT current_timestamp(),
password varchar(256) NULL,
salt varchar(10) NULL,
account_type ENUM ('PREMIUM','CRACKED') NOT NULL,
CONSTRAINT playerdata_uuid_uindex
UNIQUE (uuid)
) COMMENT 'describes the essential data from a player';
CREATE INDEX playerdata_username_index ON playerdata (username);
CREATE INDEX playerdata_usernamelower_index ON playerdata (username_lower);
ALTER TABLE playerdata
ADD PRIMARY KEY (uuid);
DELIMITER $$
CREATE TRIGGER before_playerdata_update
BEFORE UPDATE
ON playerdata
FOR EACH ROW
BEGIN
SET NEW.account_type = upper(NEW.account_type);
IF (NEW.account_type != 'PREMIUM' AND NEW.account_type != 'CRACKED') THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Invalid account_type';
END IF;
END$$
CREATE TRIGGER before_playerdata_insert
BEFORE INSERT
ON playerdata
FOR EACH ROW
BEGIN
SET NEW.account_type = upper(NEW.account_type);
IF (NEW.account_type != 'PREMIUM' AND NEW.account_type != 'CRACKED') THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Invalid account_type';
END IF;
END$$
CREATE FUNCTION UUID_TO_BIN(_uuid BINARY(36))
RETURNS BINARY(16)
LANGUAGE SQL DETERMINISTIC
CONTAINS SQL SQL SECURITY INVOKER
RETURN
UNHEX(CONCAT(
SUBSTR(_uuid, 15, 4),
SUBSTR(_uuid, 10, 4),
SUBSTR(_uuid, 1, 8),
SUBSTR(_uuid, 20, 4),
SUBSTR(_uuid, 25)));
$$
CREATE FUNCTION UUID_FROM_BIN(_bin BINARY(16))
RETURNS BINARY(36)
LANGUAGE SQL DETERMINISTIC
CONTAINS SQL SQL SECURITY INVOKER
RETURN
LCASE(CONCAT_WS('-',
HEX(SUBSTR(_bin, 5, 4)),
HEX(SUBSTR(_bin, 3, 2)),
HEX(SUBSTR(_bin, 1, 2)),
HEX(SUBSTR(_bin, 9, 2)),
HEX(SUBSTR(_bin, 11))
));
$$
DELIMITER ;