-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sql
151 lines (125 loc) · 3.62 KB
/
install.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
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
CREATE OR REPLACE DATABASE vetta_validation
COMMENT 'Vettabase data validation library
https://github.com/Vettabase/mariadb-data-validation'
;
USE vetta_validation;
DELIMITER ||
-- Generic Functions
-- =================
CREATE FUNCTION number_is_one_of(i_value INT, i_array JSON)
RETURNS BOOL
DETERMINISTIC
CONTAINS SQL
COMMENT "Return whether the first INT argument is one of the values in the JSON array passed as the second argument.
Example:
SELECT number_is_one_of(1, JSON_ARRAY(1, 2, 3));
"
BEGIN
RETURN JSON_CONTAINS(i_array, i_value);
END ||
CREATE FUNCTION string_is_one_of(i_value TEXT, i_array JSON)
RETURNS BOOL
DETERMINISTIC
CONTAINS SQL
COMMENT "Return whether the first string argument is one of the values in the JSON array passed as the second argument.
Example:
SELECT string_is_one_of('one', JSON_ARRAY('one', 'two', 'three'));
"
BEGIN
RETURN JSON_CONTAINS(i_array, JSON_QUOTE(i_value));
END ||
CREATE FUNCTION string_length_between(i_value TEXT, i_min INT, i_max INT)
RETURNS BOOL
DETERMINISTIC
CONTAINS SQL
COMMENT "Return whether the first string argument length is between the min and max arguments.
Example:
SELECT string_length_between('qwertyuiop', 3, 50);
"
BEGIN
DECLARE v_length INTEGER SIGNED
DEFAULT CHAR_LENGTH(i_value);
RETURN v_length >= i_min AND v_length <= i_max;
END ||
-- Generic INT Functions
-- =====================
CREATE FUNCTION is_multiple_of(i_dividend INT, i_divisor INT)
RETURNS BOOL
DETERMINISTIC
CONTAINS SQL
COMMENT 'Return whether the first argument can be divided by the second, obtaining an integer result.'
BEGIN
IF i_divisor = 0 THEN
RETURN FALSE;
END IF;
RETURN NOT (i_dividend MOD i_divisor);
END ||
CREATE FUNCTION is_even(i_value INT)
RETURNS BOOL
DETERMINISTIC
CONTAINS SQL
COMMENT 'Return whether the integer argument is even (can be divided by 2).'
BEGIN
RETURN NOT (i_value MOD 2);
END ||
CREATE FUNCTION is_odd(i_value INT)
RETURNS BOOL
DETERMINISTIC
CONTAINS SQL
COMMENT 'Return whether the integer argument is odd (cannot be divided by 2).'
BEGIN
RETURN i_value MOD 2;
END ||
-- Generic String Functions
-- ========================
CREATE FUNCTION is_digits(i_value TEXT)
RETURNS BOOL
DETERMINISTIC
CONTAINS SQL
COMMENT 'Return whether the string argument consists of digits only.'
BEGIN
RETURN i_value REGEXP '^[[:digit:]]+$';
END ||
CREATE FUNCTION is_alphabetic(i_value TEXT)
RETURNS BOOL
DETERMINISTIC
CONTAINS SQL
COMMENT 'Return whether the string argument consists of letters only.'
BEGIN
RETURN i_value REGEXP '^[[:alpha:]]+$';
END ||
CREATE FUNCTION is_alphanumeric(i_value TEXT)
RETURNS BOOL
DETERMINISTIC
CONTAINS SQL
COMMENT 'Return whether the string argument consists of alphanumeric characters only.'
BEGIN
RETURN i_value REGEXP '^[[:alnum:]]+$';
END ||
CREATE FUNCTION is_lower(i_value TEXT)
RETURNS BOOL
DETERMINISTIC
CONTAINS SQL
COMMENT 'Return whether the string argument consists of lowercase letters only.'
BEGIN
RETURN i_value REGEXP '^[[:lower:]]+$';
END ||
CREATE FUNCTION is_upper(i_value TEXT)
RETURNS BOOL
DETERMINISTIC
CONTAINS SQL
COMMENT 'Return whether the string argument consists of uppercase letters only.'
BEGIN
RETURN i_value REGEXP '^[[:upper:]]+$';
END ||
-- Specific Functions
-- ==================
CREATE FUNCTION is_email(i_email TEXT)
RETURNS BOOL
DETERMINISTIC
CONTAINS SQL
COMMENT 'Return whether the argument is a valid email address.'
BEGIN
RETURN i_email LIKE '_%@_%.__%';
END ||
DELIMITER ;