-
Notifications
You must be signed in to change notification settings - Fork 2
/
us3_analyte_procs.sql
549 lines (408 loc) · 15 KB
/
us3_analyte_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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
--
-- us3_analyte_procs.sql
--
-- Script to set up the MySQL stored procedures for the US3 system
-- These are related to the analyte tables
-- Run as us3admin
--
DELIMITER $$
-- Verifies that the user has permission to view or modify
-- the specified analyte
DROP FUNCTION IF EXISTS verify_analyte_permission$$
CREATE FUNCTION verify_analyte_permission( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_analyteID INT )
RETURNS INT
READS SQL DATA
BEGIN
DECLARE count_analytes INT;
DECLARE count_permissions INT;
DECLARE status INT;
CALL config();
SET status = @ERROR;
SET @US3_LAST_ERROR = 'MySQL: error verifying analyte permission';
SELECT COUNT(*)
INTO count_analytes
FROM analyte
WHERE analyteID = p_analyteID;
SELECT COUNT(*)
INTO count_permissions
FROM analytePerson
WHERE analyteID = p_analyteID
AND personID = @US3_ID;
IF ( count_analytes = 0 ) THEN
SET @US3_LAST_ERRNO = @NO_ANALYTE;
SET @US3_LAST_ERROR = 'MySQL: the specified analyte does not exist';
SET status = @NO_ANALYTE;
ELSEIF ( verify_userlevel( p_personGUID, p_password, @US3_ADMIN ) = @OK ) THEN
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
SET status = @OK;
ELSEIF ( ( verify_user( p_personGUID, p_password ) = @OK ) &&
( count_permissions > 0 ) ) THEN
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
SET status = @OK;
ELSE
SET @US3_LAST_ERRNO = @NOTPERMITTED;
SET @US3_LAST_ERROR = 'MySQL: you do not have permission to view or modify this analyte';
SET status = @NOTPERMITTED;
END IF;
RETURN( status );
END$$
-- Returns the count of analytes associated with p_ID
-- If p_ID = 0, retrieves count of all analytes in db
-- Regular user can only get count of his own analytes
DROP FUNCTION IF EXISTS count_analytes$$
CREATE FUNCTION count_analytes( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_ID INT )
RETURNS INT
READS SQL DATA
BEGIN
DECLARE count_analytes INT;
CALL config();
SET count_analytes = 0;
IF ( verify_userlevel( p_personGUID, p_password, @US3_ADMIN ) = @OK ) THEN
-- This is an admin; he can get more info
IF ( p_ID > 0 ) THEN
SELECT COUNT(*)
INTO count_analytes
FROM analytePerson
WHERE personID = p_ID;
ELSE
SELECT COUNT(*)
INTO count_analytes
FROM analytePerson;
END IF;
ELSEIF ( verify_user( p_personGUID, p_password ) = @OK ) THEN
IF ( (p_ID != 0) && (p_ID != @US3_ID) ) THEN
-- Uh oh, can't do that
SET @US3_LAST_ERRNO = @NOTPERMITTED;
SET @US3_LAST_ERROR = 'MySQL: you do not have permission to view those analytes';
ELSE
-- This person is asking about his own analytes
-- Ignore p_ID and return user's own
SELECT COUNT(*)
INTO count_analytes
FROM analytePerson
WHERE personID = @US3_ID;
END IF;
END IF;
RETURN( count_analytes );
END$$
-- INSERTs a new analyte with the specified information
DROP PROCEDURE IF EXISTS new_analyte$$
CREATE PROCEDURE new_analyte ( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_analyteGUID CHAR(36),
p_type VARCHAR(16),
p_sequence TEXT,
p_vbar FLOAT,
p_description TEXT,
p_spectrum TEXT,
p_mweight FLOAT,
p_gradform TINYINT,
p_ownerID INT )
MODIFIES SQL DATA
BEGIN
DECLARE l_analyteID 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 = 0;
IF ( ( verify_user( p_personGUID, p_password ) = @OK ) &&
( check_GUID ( p_personGUID, p_password, p_analyteGUID ) = @OK ) ) THEN
INSERT INTO analyte SET
analyteGUID = p_analyteGUID,
type = p_type,
sequence = p_sequence,
vbar = p_vbar,
description = p_description,
spectrum = p_spectrum,
molecularWeight = p_mweight,
gradientForming = p_gradform ;
IF ( duplicate_key = 1 ) THEN
SET @US3_LAST_ERRNO = @INSERTDUP;
SET @US3_LAST_ERROR = "MySQL: Duplicate entry for analyteGUID field";
ELSEIF ( null_field = 1 ) THEN
SET @US3_LAST_ERRNO = @INSERTNULL;
SET @US3_LAST_ERROR = "MySQL: NULL value for analyteGUID field";
ELSE
SET @LAST_INSERT_ID = LAST_INSERT_ID();
INSERT INTO analytePerson SET
analyteID = @LAST_INSERT_ID,
personID = p_ownerID;
END IF;
END IF;
SELECT @US3_LAST_ERRNO AS status;
END$$
-- UPDATEs an existing analyte with the specified information
DROP PROCEDURE IF EXISTS update_analyte$$
CREATE PROCEDURE update_analyte ( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_analyteID INT,
p_type VARCHAR(16),
p_sequence TEXT,
p_vbar FLOAT,
p_description TEXT,
p_spectrum TEXT,
p_mweight FLOAT,
p_gradform TINYINT )
MODIFIES SQL DATA
BEGIN
DECLARE not_found TINYINT DEFAULT 0;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET not_found = 1;
CALL config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
IF ( verify_analyte_permission( p_personGUID, p_password, p_analyteID ) = @OK ) THEN
UPDATE analyte SET
type = p_type,
sequence = p_sequence,
vbar = p_vbar,
description = p_description,
spectrum = p_spectrum,
molecularWeight = p_mweight,
gradientForming = p_gradform
WHERE analyteID = p_analyteID;
IF ( not_found = 1 ) THEN
SET @US3_LAST_ERRNO = @NO_ANALYTE;
SET @US3_LAST_ERROR = "MySQL: No analyte with that ID exists";
ELSE
SET @LAST_INSERT_ID = LAST_INSERT_ID();
END IF;
END IF;
SELECT @US3_LAST_ERRNO AS status;
END$$
-- Returns the analyteID associated with the given analyteGUID
DROP PROCEDURE IF EXISTS get_analyteID$$
CREATE PROCEDURE get_analyteID ( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_analyteGUID CHAR(36) )
READS SQL DATA
BEGIN
DECLARE count_anal INT;
CALL config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
SET count_anal = 0;
IF ( verify_user( p_personGUID, p_password ) = @OK ) THEN
SELECT COUNT(*)
INTO count_anal
FROM analyte
WHERE analyteGUID = p_analyteGUID;
IF ( TRIM( p_analyteGUID ) = '' ) THEN
SET @US3_LAST_ERRNO = @EMPTY;
SET @US3_LAST_ERROR = CONCAT( 'MySQL: The analyteGUID parameter to the get_analyteID ',
'function cannot be empty' );
ELSEIF ( count_anal < 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 analyteID
FROM analyte
WHERE analyteGUID = p_analyteGUID;
END IF;
END IF;
END$$
-- Returns the analyteID, description, and type of all analytes associated with p_ID
-- If p_ID = 0, retrieves information about all analytes in db
-- Regular user can only get info about his own analytes
DROP PROCEDURE IF EXISTS get_analyte_desc$$
CREATE PROCEDURE get_analyte_desc ( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_ID INT )
READS SQL DATA
BEGIN
CALL config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
IF ( verify_userlevel( p_personGUID, p_password, @US3_ADMIN ) = @OK ) THEN
-- This is an admin; he can get more info
IF ( count_analytes( p_personGUID, p_password, p_ID ) < 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;
IF ( p_ID > 0 ) THEN
SELECT a.analyteID, description, type, gradientForming
FROM analyte a, analytePerson
WHERE a.analyteID = analytePerson.analyteID
AND analytePerson.personID = p_ID
ORDER BY a.analyteID DESC;
ELSE
SELECT a.analyteID, description, type, gradientForming
FROM analyte a, analytePerson
WHERE a.analyteID = analytePerson.analyteID
ORDER BY a.analyteID DESC;
END IF;
END IF;
ELSEIF ( verify_user( p_personGUID, p_password ) = @OK ) THEN
IF ( (p_ID != 0) && (p_ID != @US3_ID) ) THEN
-- Uh oh, can't do that
SET @US3_LAST_ERRNO = @NOTPERMITTED;
SET @US3_LAST_ERROR = 'MySQL: you do not have permission to view this analyte';
SELECT @US3_LAST_ERRNO AS status;
ELSEIF ( count_analytes( p_personGUID, p_password, @US3_ID ) < 1 ) THEN
SET @US3_LAST_ERRNO = @NOROWS;
SET @US3_LAST_ERROR = 'MySQL: no rows returned';
SELECT @US3_LAST_ERRNO AS status;
ELSE
-- Ok, user wants his own info
SELECT @OK AS status;
SELECT a.analyteID, description, type
FROM analyte a, analytePerson
WHERE a.analyteID = analytePerson.analyteID
AND analytePerson.personID = @US3_ID
ORDER BY a.analyteID DESC;
END IF;
END IF;
END$$
-- Returns a more complete list of information about one analyte
DROP PROCEDURE IF EXISTS get_analyte_info$$
CREATE PROCEDURE get_analyte_info ( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_analyteID INT )
READS SQL DATA
BEGIN
DECLARE count_analytes INT;
CALL config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
SELECT COUNT(*)
INTO count_analytes
FROM analyte
WHERE analyteID = p_analyteID;
IF ( verify_analyte_permission( p_personGUID, p_password, p_analyteID ) = @OK ) THEN
IF ( count_analytes = 0 ) 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 analyteGUID, type, sequence, vbar, description, spectrum, molecularWeight, gradientForming, personID
FROM analyte a, analytePerson ap
WHERE a.analyteID = ap.analyteID
AND a.analyteID = p_analyteID;
END IF;
ELSE
SELECT @US3_LAST_ERRNO AS status;
END IF;
END$$
-- DELETEs a analyte, plus information in related tables
DROP PROCEDURE IF EXISTS delete_analyte$$
CREATE PROCEDURE delete_analyte ( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_analyteID INT )
MODIFIES SQL DATA
BEGIN
DECLARE count_analytes INT;
CALL config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
IF ( verify_analyte_permission( p_personGUID, p_password, p_analyteID ) = @OK ) THEN
-- Find out if this analyte is used in any solution first
SELECT COUNT(*) INTO count_analytes
FROM solutionAnalyte
WHERE analyteID = p_analyteID;
IF ( count_analytes = 0 ) THEN
DELETE FROM analytePerson
WHERE analyteID = p_analyteID;
DELETE FROM spectrum
WHERE componentID = p_analyteID
AND componentType = 'Analyte';
DELETE FROM analyte
WHERE analyteID = p_analyteID;
ELSE
SET @US3_LAST_ERRNO = @ANALYTE_IN_USE;
SET @US3_last_ERROR = 'The analyte is in use in a solution';
END IF;
END IF;
SELECT @US3_LAST_ERRNO AS status;
END$$
-- Procedures regarding nucleotide information
-- Returns complete list of nucleotide information about an analyte
DROP PROCEDURE IF EXISTS get_nucleotide_info$$
CREATE PROCEDURE get_nucleotide_info ( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_analyteID INT )
READS SQL DATA
BEGIN
DECLARE count_analytes INT;
CALL config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
SELECT COUNT(*)
INTO count_analytes
FROM analyte
WHERE analyteID = p_analyteID;
IF ( verify_analyte_permission( p_personGUID, p_password, p_analyteID ) = @OK ) THEN
IF ( count_analytes = 0 ) 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 doubleStranded, complement, _3prime, _5prime,
sodium, potassium, lithium, magnesium, calcium
FROM analyte
WHERE analyteID = p_analyteID;
END IF;
ELSE
SELECT @US3_LAST_ERRNO AS status;
END IF;
END$$
-- UPDATEs an existing analyte with the specified information
DROP PROCEDURE IF EXISTS set_nucleotide_info$$
CREATE PROCEDURE set_nucleotide_info ( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_analyteID INT,
p_doubleStranded TINYINT,
p_complement TINYINT,
p__3prime TINYINT,
p__5prime TINYINT,
p_sodium DOUBLE,
p_potassium DOUBLE,
p_lithium DOUBLE,
p_magnesium DOUBLE,
p_calcium DOUBLE )
MODIFIES SQL DATA
BEGIN
DECLARE not_found TINYINT DEFAULT 0;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET not_found = 1;
CALL config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
IF ( verify_analyte_permission( p_personGUID, p_password, p_analyteID ) = @OK ) THEN
UPDATE analyte SET
doubleStranded = p_doubleStranded,
complement = p_complement,
_3prime = p__3prime,
_5prime = p__5prime,
sodium = p_sodium,
potassium = p_potassium,
lithium = p_lithium,
magnesium = p_magnesium,
calcium = p_calcium
WHERE analyteID = p_analyteID;
IF ( not_found = 1 ) THEN
SET @US3_LAST_ERRNO = @NO_ANALYTE;
SET @US3_LAST_ERROR = "MySQL: No analyte with that ID exists";
ELSE
SET @LAST_INSERT_ID = LAST_INSERT_ID();
END IF;
END IF;
SELECT @US3_LAST_ERRNO AS status;
END$$