forked from ehb54/us3_sql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
us3_procedures.sql
540 lines (426 loc) · 14.1 KB
/
us3_procedures.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
--
-- us3_procedures.sql
--
-- Script to set up the MySQL stored procedures for the US3 system
-- Run as us3admin
--
DELIMITER $$
-- Called by other procedures to set up environment
DROP PROCEDURE IF EXISTS config$$
CREATE PROCEDURE config()
BEGIN
SET @SQL_MODE = 'traditional';
SET @ADMIN_EMAIL = '[email protected]';
SET @USERNAME = LEFT( USER(), LOCATE( '@', USER() ) - 1 );
SELECT timediff( UTC_TIMESTAMP(), CURRENT_TIMESTAMP() ) INTO @UTC_DIFF;
-- Some error codes
SET @OK = 0;
SET @ERROR = -1;
SET @NOT_CONNECTED = 1;
SET @DUP_EMAIL = 101;
SET @NO_ACCT = 102;
SET @INACTIVE = 103;
SET @BADPASS = 104;
SET @EMPTY = 105;
SET @BAD_CHECKSUM = 106;
SET @NOTPERMITTED = 201;
SET @BADOPERATOR = 202;
SET @BADLABLOCATION = 203;
SET @BADGUID = 204;
SET @NOROWS = 301;
SET @INSERTNULL = 401;
SET @INSERTDUP = 402;
SET @DUPFIELD = 403;
SET @CONSTRAINT_FAILED = 404;
SET @NO_BUFFER = 501;
SET @NO_COMPONENT = 502;
SET @NO_ROTOR = 503;
SET @NO_ANALYTE = 504;
SET @NO_LAB = 505;
SET @NO_PERSON = 506;
SET @NO_MODEL = 507;
SET @NO_EXPERIMENT = 508;
SET @NO_RAWDATA = 509;
SET @NO_EDITDATA = 510;
SET @NO_SOLUTION = 511;
SET @CALIB_IN_USE = 512; -- the rotor calibration profile is in use
SET @ROTOR_IN_USE = 513; -- the rotor is in use
SET @NO_NOISE = 514;
SET @NO_PROJECT = 515;
SET @BUFFER_IN_USE = 516;
SET @ANALYTE_IN_USE = 517;
SET @SOLUTION_IN_USE = 518;
SET @NO_CALIB = 519;
SET @NO_REPORT = 520;
SET @NO_REPORT_TRIPLE = 521;
SET @NO_REPORT_DOCUMENT = 522;
SET @NO_DOCUMENT_LINK = 523;
SET @MORE_THAN_SINGLE_ROW = 524;
SET @NO_MRECS = 525;
SET @NO_PROTOCOL = 526;
SET @NO_ROTOR_CAL = 527;
SET @INSTRUMENT_IN_USE = 553;
SET @PROTOCOL_IN_USE = 554;
SET @NO_AUTOFLOW_RECORD = 555;
-- Some user levels
SET @US3_USER = 0;
SET @US3_PRIV = 1;
SET @US3_ANALYST = 2;
SET @US3_ADMIN = 3;
SET @US3_SUPER = 4;
END$$
-- Returns the most recent error number
DROP FUNCTION IF EXISTS last_errno$$
CREATE FUNCTION last_errno()
RETURNS INT
NO SQL
BEGIN
RETURN( @US3_LAST_ERRNO );
END$$
-- Returns the most recent error text message
DROP FUNCTION IF EXISTS last_error$$
CREATE FUNCTION last_error()
RETURNS TEXT
NO SQL
BEGIN
RETURN( @US3_LAST_ERROR );
END$$
-- Returns the first automatically generated value successfully
-- inserted for an AUTO_INCREMENT column in the most recent
-- INSERT statement, or 0 if no rows were inserted
DROP FUNCTION IF EXISTS last_insertID$$
CREATE FUNCTION last_insertID()
RETURNS TEXT
NO SQL
BEGIN
RETURN( @LAST_INSERT_ID );
END$$
-- Returns the most recent @DEBUG statement
DROP FUNCTION IF EXISTS last_debug$$
CREATE FUNCTION last_debug()
RETURNS TEXT
NO SQL
BEGIN
RETURN( @DEBUG );
END$$
DROP FUNCTION IF EXISTS timestamp2UTC$$
CREATE FUNCTION timestamp2UTC( p_ts TIMESTAMP )
RETURNS TIMESTAMP
NO SQL
BEGIN
DECLARE count_gmt INT;
DECLARE utcTime TIMESTAMP;
-- Let's see if timezone support is there
SELECT COUNT(*) INTO count_gmt
FROM mysql.time_zone_name
WHERE name LIKE '%GMT%';
IF ( count_gmt > 0 ) THEN
-- yes, it looks like timezone support is there
SELECT CONVERT_TZ( p_ts, @@global.time_zone, 'GMT') INTO utcTime;
ELSE
-- no, not there
SELECT ADDTIME( p_ts, @UTC_DIFF ) INTO utcTime;
END IF;
RETURN( utcTime );
END$$
-- Checks the user with the passed GUID and password
DROP FUNCTION IF EXISTS check_user$$
CREATE FUNCTION check_user( p_personGUID CHAR(36),
p_password VARCHAR(80) )
RETURNS INT
READS SQL DATA
BEGIN
DECLARE count_user INT;
DECLARE md5_pw VARCHAR(80);
DECLARE l_password VARCHAR(80);
DECLARE activated INT;
call config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
SET @LAST_INSERT_ID = 0;
SET @US3_ID = NULL;
SET @FNAME = NULL;
SET @LNAME = NULL;
SET @PHONE = NULL;
SET @EMAIL = NULL;
SET @personGUID = NULL;
SET @USERLEVEL = NULL;
SET md5_pw = MD5( p_password );
SELECT COUNT(*)
INTO count_user
FROM people
WHERE personGUID = p_personGUID;
IF ( TRIM( p_personGUID ) = '' ) THEN
SET @US3_LAST_ERRNO = @EMPTY;
SET @US3_LAST_ERROR = CONCAT( 'MySQL: The personGUID parameter to the ',
'check_user function cannot be empty' );
ELSEIF ( count_user = 0 ) THEN
SET @US3_LAST_ERRNO = @NO_ACCT;
SET @US3_LAST_ERROR = CONCAT( 'MySQL: The account identified by personGUID ',
p_personGUID,
' is not set up correctly. ',
'Please contact the administrator: ',
@ADMIN_EMAIL );
ELSE
/* At this point we should have exactly 1 record */
SELECT personID, password, fname, lname, phone, email, userlevel, activated
INTO @US3_ID, l_password, @FNAME, @LNAME, @PHONE, @EMAIL, @USERLEVEL, activated
FROM people
WHERE personGUID = p_personGUID;
SET @personGUID = p_personGUID;
IF ( l_password != md5_pw ) THEN
SET @US3_LAST_ERRNO = @BADPASS;
SET @US3_LAST_ERROR = 'MySQL: Invalid password';
SET @US3_ID = NULL;
SET @FNAME = NULL;
SET @LNAME = NULL;
SET @PHONE = NULL;
SET @EMAIL = NULL;
SET @personGUID = NULL;
SET @USERLEVEL = NULL;
ELSEIF ( activated = false ) THEN
SET @US3_LAST_ERRNO = @INACTIVE;
SET @US3_LAST_ERROR = CONCAT( 'MySQL: This account has not been activated yet. ',
'Please activate your account first. ',
'The activation code was sent to your e-mail address: ',
p_email);
SET @US3_ID = NULL;
SET @FNAME = NULL;
SET @LNAME = NULL;
SET @PHONE = NULL;
SET @EMAIL = NULL;
SET @personGUID = NULL;
SET @USERLEVEL = NULL;
ELSE
-- Successful login
UPDATE people
SET lastLogin = NOW()
WHERE personID = @US3_ID;
SET @US3_LAST_ERROR = '';
SET @US3_LAST_ERRNO = @OK;
END IF;
END IF;
RETURN( @US3_LAST_ERRNO );
END$$
-- Checks the user with the passed email and password
DROP FUNCTION IF EXISTS check_user_email$$
CREATE FUNCTION check_user_email( p_email VARCHAR(63),
p_password VARCHAR(80) )
RETURNS INT
READS SQL DATA
BEGIN
DECLARE count_user INT;
DECLARE md5_pw VARCHAR(80);
DECLARE l_password VARCHAR(80);
DECLARE activated INT;
call config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
SET @LAST_INSERT_ID = 0;
SET @US3_ID = NULL;
SET @FNAME = NULL;
SET @LNAME = NULL;
SET @PHONE = NULL;
SET @EMAIL = NULL;
SET @personGUID = NULL;
SET @USERLEVEL = NULL;
SET md5_pw = MD5( p_password );
SELECT COUNT(*)
INTO count_user
FROM people
WHERE email = p_email;
IF ( TRIM( p_email ) = '' ) THEN
SET @US3_LAST_ERRNO = @EMPTY;
SET @US3_LAST_ERROR = CONCAT( 'MySQL: The email parameter to the check_user_email ',
'function cannot be empty' );
ELSEIF ( count_user > 1 ) THEN
SET @US3_LAST_ERRNO = @DUP_EMAIL;
SET @US3_LAST_ERROR = CONCAT( 'MySQL: There was a problem with duplicate email addresses. ',
'Please contact the administrator: ',
@ADMIN_EMAIL );
ELSEIF ( count_user < 1 ) THEN
SET @US3_LAST_ERRNO = @NO_ACCT;
SET @US3_LAST_ERROR = CONCAT( 'MySQL: The account for ',
p_email,
' is not set up correctly. ',
'Please contact the administrator: ',
@ADMIN_EMAIL );
ELSE
-- At this point we should have exactly 1 record
SELECT personID, password, fname, lname, phone, personGUID, userlevel, activated
INTO @US3_ID, l_password, @FNAME, @LNAME, @PHONE, @personGUID, @USERLEVEL, activated
FROM people
WHERE email = p_email;
SET @EMAIL = p_email;
IF ( l_password != md5_pw ) THEN
SET @US3_LAST_ERRNO = @BADPASS;
SET @US3_LAST_ERROR = 'MySQL: Invalid password';
SET @US3_ID = NULL;
SET @FNAME = NULL;
SET @LNAME = NULL;
SET @PHONE = NULL;
SET @EMAIL = NULL;
SET @personGUID = NULL;
SET @USERLEVEL = NULL;
ELSEIF ( activated = false ) THEN
SET @US3_LAST_ERRNO = @INACTIVE;
SET @US3_LAST_ERROR = CONCAT( 'MySQL: This account has not been activated yet. ',
'Please activate your account first. ',
'The activation code was sent to your e-mail address: ',
p_email);
SET @US3_ID = NULL;
SET @FNAME = NULL;
SET @LNAME = NULL;
SET @PHONE = NULL;
SET @EMAIL = NULL;
SET @personGUID = NULL;
SET @USERLEVEL = NULL;
ELSE
-- Successful login
UPDATE people
SET lastLogin = NOW()
WHERE personID = @US3_ID;
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
END IF;
END IF;
RETURN( @US3_LSAST_ERRNO );
END$$
-- Verifies that user identified by guid is the same as the
-- logged in user
DROP FUNCTION IF EXISTS verify_user$$
CREATE FUNCTION verify_user( p_personGUID CHAR(36),
p_password VARCHAR(80) )
RETURNS INT
READS SQL DATA
BEGIN
DECLARE status INT;
CALL config();
SET status = @OK;
IF ( @US3_ID IS NULL ) THEN
SET status = check_user( p_personGUID, p_password );
END IF;
RETURN( status );
END$$
-- Same as verify_user(), but also verifies userlevel
DROP FUNCTION IF EXISTS verify_userlevel$$
CREATE FUNCTION verify_userlevel( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_userlevel INT )
RETURNS INT
READS SQL DATA
BEGIN
DECLARE status INT;
CALL config();
SET status = verify_user( p_personGUID, p_password );
IF ( status = @OK && @USERLEVEL < p_userlevel ) THEN
SET @US3_LAST_ERRNO = @NOTPERMITTED;
SET @US3_LAST_ERROR = 'MySQL: Not permitted at that userlevel';
SET status = @NOTPERMITTED;
END IF;
RETURN( status );
END$$
-- Verifies that user identified by email is the same as the
-- logged in user
DROP FUNCTION IF EXISTS verify_user_email$$
CREATE FUNCTION verify_user_email( p_email VARCHAR(63),
p_password VARCHAR(80) )
RETURNS INT
READS SQL DATA
BEGIN
DECLARE count_user INT;
DECLARE status INT;
CALL config();
SET status = @OK;
IF ( @US3_ID IS NULL ) THEN
SET status = check_user_email( p_email, p_password );
END IF;
RETURN( status );
END$$
-- Validates the user either by personGUID or email and password
-- Returns status, personGUID and email
DROP PROCEDURE IF EXISTS validate_user$$
CREATE PROCEDURE validate_user( p_personGUID CHAR(36),
p_email VARCHAR(63),
p_password VARCHAR(80) )
READS SQL DATA
BEGIN
DECLARE status INT;
call config();
-- Let's see if we can validate with the guid
SET status = check_user( p_personGUID, p_password );
IF ( @US3_ID IS NOT NULL ) THEN
-- We've validated with the guid
SELECT @OK AS status;
SELECT @personGUID AS personGUID, @EMAIL AS email;
ELSEIF ( TRIM( p_email ) = '' ) THEN
-- Then we were trying to validate with the guid, but failed
SELECT @US3_LAST_ERRNO AS status;
SELECT p_personGUID AS personGUID, p_email AS email;
ELSE
-- Email is not empty; can we validate using it?
SET status = check_user_email( p_email, p_password );
IF ( @US3_ID IS NOT NULL ) THEN
-- We've validated with the email
SELECT @OK AS status;
SELECT @personGUID AS personGUID, @EMAIL AS email;
ELSE
-- No, can't validate either way
SELECT @US3_LAST_ERRNO AS status;
SELECT p_personGUID AS personGUID, p_email AS email;
END IF;
END IF;
END$$
DROP FUNCTION IF EXISTS check_GUID$$
CREATE FUNCTION check_GUID( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_tableGUID CHAR(36) )
RETURNS INT
READS SQL DATA
BEGIN
DECLARE pattern CHAR(100);
DECLARE GUID_formatOK INT;
CALL config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
SET pattern = '^(([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12})$';
IF ( verify_user( p_personGUID, p_password ) = @OK ) THEN
-- Does the GUID fit the pattern?
SET p_tableGUID = TRIM( p_tableGUID );
SELECT p_tableGUID REGEXP pattern
INTO GUID_formatOK;
-- Now calculate return status
IF ( p_tableGUID = '' ) THEN
SET @US3_LAST_ERRNO = @EMPTY;
SET @US3_LAST_ERROR = CONCAT( 'MySQL: The GUID parameter to the check_GUID ',
'function cannot be empty' );
ELSEIF ( NOT GUID_formatOK ) THEN
SET @US3_LAST_ERRNO = @BADGUID;
SET @US3_LAST_ERROR = 'MySQL: The specified GUID is not the correct format';
ELSE
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
END IF;
END IF;
RETURN( @US3_LAST_ERRNO );
END$$
SOURCE us3_people_procs.sql
SOURCE us3_buffer_procs.sql
SOURCE us3_analyte_procs.sql
SOURCE us3_exp_procs.sql
SOURCE us3_expdata_procs.sql
SOURCE us3_proj_procs.sql
SOURCE us3_hardware_procs.sql
SOURCE us3_solution_procs.sql
SOURCE us3_spectrum_procs.sql
SOURCE us3_model_procs.sql
SOURCE us3_mrecs_procs.sql
SOURCE us3_noise_procs.sql
SOURCE us3_report_procs.sql
SOURCE us3_timestate_procs.sql
SOURCE us3_eprofile_procs.sql
SOURCE us3_protocol_procs.sql
SOURCE us3_aprofile_procs.sql
SOURCE us3_autoflow_procs.sql
SOURCE us3_refscan_procs.sql
DELIMITER ;