-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathmytap-schemata.sql
183 lines (141 loc) · 4.96 KB
/
mytap-schemata.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
-- SCHEMA
-- ======
USE tap;
DELIMITER //
/****************************************************************************/
-- has_schema( schema)
DROP FUNCTION IF EXISTS _has_schema //
CREATE FUNCTION _has_schema(sname VARCHAR(64))
RETURNS BOOLEAN
DETERMINISTIC
COMMENT 'Boolean test for existence of named schema.'
BEGIN
DECLARE ret BOOLEAN;
SELECT 1 INTO ret
FROM `information_schema`.`schemata`
WHERE `schema_name` = sname;
RETURN COALESCE(ret, 0);
END //
-- has_schema( schema, description )
DROP FUNCTION IF EXISTS has_schema //
CREATE FUNCTION has_schema(sname VARCHAR(64), description TEXT)
RETURNS TEXT
DETERMINISTIC
COMMENT 'Confirm named schema exists.'
BEGIN
IF description = '' THEN
SET description = CONCAT('Schema ', quote_ident(sname), ' should exist');
END IF;
RETURN ok(_has_schema(sname), description);
END //
-- hasnt_schema( schema, description )
DROP FUNCTION IF EXISTS hasnt_schema //
CREATE FUNCTION hasnt_schema(sname VARCHAR(64), description TEXT)
RETURNS TEXT
DETERMINISTIC
COMMENT 'Confirm named schema does not exist.'
BEGIN
IF description = '' THEN
SET description = CONCAT('Schema ', quote_ident(sname), ' should not exist');
END IF;
RETURN ok(NOT _has_schema(sname), description);
END //
/****************************************************************************/
-- DEFAULT SCHEMA COLLATION DEFINITIONS
-- _schema_collation_is( schema, collation )
DROP FUNCTION IF EXISTS _schema_collation_is //
CREATE FUNCTION _schema_collation_is(sname VARCHAR(64))
RETURNS VARCHAR(32)
DETERMINISTIC
COMMENT 'Internal function to get the default collation for a named schema.'
BEGIN
DECLARE ret VARCHAR(32);
SELECT `default_collation_name` INTO ret
FROM `information_schema`.`schemata`
WHERE `schema_name` = sname;
RETURN COALESCE(ret, NULL);
END //
-- schema_collation_is( schema, collation, description )
DROP FUNCTION IF EXISTS schema_collation_is //
CREATE FUNCTION schema_collation_is(sname VARCHAR(64), cname VARCHAR(32), description TEXT)
RETURNS TEXT
DETERMINISTIC
COMMENT 'Confirm the default collation for a schema matches value provided.'
BEGIN
IF description = '' THEN
SET description = CONCAT('Schema ', quote_ident(sname), ' should have Collation ', qv(cname));
END IF;
IF NOT _has_schema(sname) THEN
RETURN CONCAT(ok(FALSE, description), '\n',
diag(CONCAT('Schema ', quote_ident(sname), ' does not exist')));
END IF;
IF NOT _has_collation(cname) THEN
RETURN CONCAT(ok(FALSE, description), '\n',
diag(CONCAT('Collation ', quote_ident(cname), ' is not available')));
END IF;
RETURN eq(_schema_collation_is(sname), cname , description);
END //
/****************************************************************************/
-- DEFAULT CHARACTER SET DEFINITION
-- _schema_charset_is( schema, charset )
DROP FUNCTION IF EXISTS _schema_charset_is //
CREATE FUNCTION _schema_charset_is(sname VARCHAR(64))
RETURNS VARCHAR(32)
DETERMINISTIC
COMMENT 'Internal fuction to return the default collation for a named schema.'
BEGIN
DECLARE ret VARCHAR(32);
SELECT `default_character_set_name` INTO ret
FROM `information_schema`.`schemata`
WHERE `schema_name` = sname;
RETURN COALESCE(ret, NULL);
END //
-- schema_charset_is( schema, charset, description )
DROP FUNCTION IF EXISTS schema_charset_is //
CREATE FUNCTION schema_charset_is(sname VARCHAR(64), cname VARCHAR(32), description TEXT)
RETURNS TEXT
DETERMINISTIC
COMMENT 'Confirm the default character set for a schema matches value provided.'
BEGIN
IF description = '' THEN
SET description = CONCAT('Schema ', quote_ident(sname),
' should use Character Set ', quote_ident(cname));
END IF;
IF NOT _has_schema(sname) THEN
RETURN CONCAT(ok(FALSE, description), '\n',
diag(CONCAT('Schema ', quote_ident(sname), ' does not exist')));
END IF;
IF NOT _has_charset(cname) THEN
RETURN CONCAT(ok(FALSE, description), '\n',
diag(CONCAT('Character Set ', quote_ident(cname), ' is not available')));
END IF;
RETURN eq(_schema_charset_is(sname), cname, description);
END //
-- alias
DROP FUNCTION IF EXISTS schema_character_set_is //
CREATE FUNCTION schema_character_set_is(sname VARCHAR(64), cname VARCHAR(32), description TEXT)
RETURNS TEXT
DETERMINISTIC
COMMENT 'Alias for schema_charset_is(sname, cname, description).'
BEGIN
RETURN schema_charset_is(sname, cname, description);
END //
/****************************************************************/
DROP FUNCTION IF EXISTS schemas_are //
CREATE FUNCTION schemas_are(want TEXT, description TEXT)
RETURNS TEXT
DETERMINISTIC
BEGIN
SET @want = want;
SET @have = (SELECT GROUP_CONCAT('`', `schema_name` ,'`')
FROM `information_schema`.`schemata`);
IF description = '' THEN
SET description = 'The correct Schemas should be defined';
END IF;
CALL _populate_want(@want);
CALL _populate_have(@have);
SET @missing = (SELECT _missing(@have));
SET @extras = (SELECT _extra(@want));
RETURN _are('schemas', @extras, @missing, description);
END //
DELIMITER ;