-
Notifications
You must be signed in to change notification settings - Fork 2
/
us3_aprofile_procs.sql
235 lines (171 loc) · 6.18 KB
/
us3_aprofile_procs.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
--
-- us3_aprofile_procs.sql
--
-- Script to set up the MySQL stored procedures for the US3 system
-- These are procedures related to the analysisprofile data
-- Run as us3admin
--
DELIMITER $$
-- Routines that deal with an analysis profile
-- Returns the count of all analysis profiles
DROP FUNCTION IF EXISTS count_aprofiles$$
CREATE FUNCTION count_aprofiles( p_personGUID CHAR(36),
p_password VARCHAR(80) )
RETURNS INT
READS SQL DATA
BEGIN
DECLARE count_aprofiles INT;
CALL config();
SET count_aprofiles = 0;
SELECT COUNT(*)
INTO count_aprofiles
FROM analysisprofile;
RETURN( count_aprofiles );
END$$
-- INSERTs new analysis profile information
DROP PROCEDURE IF EXISTS new_aprofile$$
CREATE PROCEDURE new_aprofile ( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_aprofileGUID CHAR(36),
p_name VARCHAR(160),
p_xml LONGTEXT,
p_reportMask LONGTEXT,
p_combPlots LONGTEXT )
MODIFIES SQL DATA
BEGIN
DECLARE l_aprofileID INT;
DECLARE duplicate_key TINYINT DEFAULT 0;
DECLARE null_field TINYINT DEFAULT 0;
DECLARE CONTINUE HANDLER FOR 1062
SET duplicate_key = 1;
DECLARE CONTINUE HANDLER FOR 1048
SET null_field = 1;
CALL config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
SET @LAST_INSERT_ID = -1;
IF ( ( verify_user( p_personGUID, p_password ) = @OK ) &&
( check_GUID ( p_personGUID, p_password, p_aprofileGUID ) = @OK ) ) THEN
INSERT INTO analysisprofile SET
aprofileGUID = p_aprofileGUID,
name = p_name,
xml = p_xml,
dateUpdated = NOW(),
reportMask = p_reportMask,
combinedPlots = p_combPlots;
IF ( duplicate_key = 1 ) THEN
SET @US3_LAST_ERRNO = @INSERTDUP;
SET @US3_LAST_ERROR = "MySQL: Duplicate entry for aprofileGUID/name field(s)";
ELSEIF ( null_field = 1 ) THEN
SET @US3_LAST_ERRNO = @INSERTNULL;
SET @US3_LAST_ERROR = "MySQL: Attempt to insert NULL value in the analysisprofile table";
ELSE
SET @LAST_INSERT_ID = LAST_INSERT_ID();
END IF;
END IF;
SELECT @US3_LAST_ERRNO AS status;
END$$
-- Returns the aprofileID and base information for all analysis profiles
DROP PROCEDURE IF EXISTS get_aprofile_desc$$
CREATE PROCEDURE get_aprofile_desc ( p_personGUID CHAR(36),
p_password VARCHAR(80) )
READS SQL DATA
BEGIN
CALL config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
IF ( count_aprofiles( p_personGUID, p_password ) < 1 ) THEN
SET @US3_LAST_ERRNO = @NOROWS;
SET @US3_LAST_ERROR = 'MySQL: no rows returned';
SELECT @US3_LAST_ERRNO AS status;
ELSE
SELECT @OK AS status;
SELECT aprofileID, aprofileGUID, name, xml,
timestamp2UTC( dateUpdated ) AS UTC_lastUpdated, reportMask
FROM analysisprofile
ORDER BY aprofileID DESC;
END IF;
END$$
-- Returns the aprofileID and base information for one analysis profile
-- as identified by GUID
DROP PROCEDURE IF EXISTS get_aprofile_info$$
CREATE PROCEDURE get_aprofile_info ( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_aprofileGUID CHAR(36) )
READS SQL DATA
BEGIN
DECLARE count_aprofiles INT;
CALL config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
SELECT COUNT(*)
INTO count_aprofiles
FROM analysisprofile
WHERE aprofileGUID = p_aprofileGUID;
IF ( count_aprofiles = 0 ) THEN
SET @US3_LAST_ERRNO = @NOROWS;
SET @US3_LAST_ERROR = 'MySQL: no rows returned';
SELECT @US3_LAST_ERRNO AS status;
ELSEIF ( count_aprofiles > 1 ) THEN
SET @US3_LAST_ERRNO = @MORE_THAN_SINGLE_ROW;
SET @US3_LAST_ERROR = 'MySQL: more than a single row for an analysis profile';
SELECT @US3_LAST_ERRNO AS status;
ELSE
SELECT @OK AS status;
SELECT aprofileID, name, xml,
timestamp2UTC( dateUpdated ) AS UTC_lastUpdated, reportMask, combinedPlots
FROM analysisprofile
WHERE aprofileGUID = p_aprofileGUID;
END IF;
END$$
-- Returns the aprofileID and base information for one analysis profile
-- as identified by database ID
DROP PROCEDURE IF EXISTS get_aprofile_info_byID$$
CREATE PROCEDURE get_aprofile_info_byID ( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_aprofileID INT(11) )
READS SQL DATA
BEGIN
DECLARE count_aprofiles INT;
CALL config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
SELECT COUNT(*)
INTO count_aprofiles
FROM analysisprofile
WHERE aprofileID = p_aprofileID;
IF ( count_aprofiles = 0 ) THEN
SET @US3_LAST_ERRNO = @NOROWS;
SET @US3_LAST_ERROR = 'MySQL: no rows returned';
SELECT @US3_LAST_ERRNO AS status;
ELSEIF ( count_aprofiles > 1 ) THEN
SET @US3_LAST_ERRNO = @MORE_THAN_SINGLE_ROW;
SET @US3_LAST_ERROR = 'MySQL: more than a single row for an analysis profile';
SELECT @US3_LAST_ERRNO AS status;
ELSE
SELECT @OK AS status;
SELECT aprofileGUID, name, xml,
timestamp2UTC( dateUpdated ) AS UTC_lastUpdated, reportMask
FROM analysisprofile
WHERE aprofileID = p_aprofileID;
END IF;
END$$
-- DELETEs an aprofile, plus information in related tables
DROP PROCEDURE IF EXISTS delete_aprofile$$
CREATE PROCEDURE delete_aprofile ( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_aprofileID INT )
MODIFIES SQL DATA
BEGIN
DECLARE count_aprofiles INT;
CALL config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
IF ( verify_aprofile_permission( p_personGUID, p_password, p_aprofileID ) = @OK ) THEN
-- Make sure records match if they have related tables or not
-- Have to do it in a couple of stages because of the constraints
DELETE FROM aprofile
WHERE aprofileID = p_aprofileID;
END IF;
SELECT @US3_LAST_ERRNO AS status;
END$$