-
Notifications
You must be signed in to change notification settings - Fork 2
/
us3_model_procs.sql
910 lines (688 loc) · 26.3 KB
/
us3_model_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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
--
-- us3_model_procs.sql
--
-- Script to set up the MySQL stored procedures for the US3 system
-- These are related to the model tables
-- Run as us3admin
--
DELIMITER $$
-- Verifies that the user has permission to view or modify
-- the specified model
DROP FUNCTION IF EXISTS verify_model_permission$$
CREATE FUNCTION verify_model_permission( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_modelID INT )
RETURNS INT
READS SQL DATA
BEGIN
DECLARE count_models INT;
DECLARE count_permissions INT;
DECLARE status INT;
CALL config();
SET status = @ERROR;
SET @US3_LAST_ERROR = 'MySQL: error verifying model permission';
SELECT COUNT(*)
INTO count_models
FROM model
WHERE modelID = p_modelID;
SELECT COUNT(*)
INTO count_permissions
FROM modelPerson
WHERE modelID = p_modelID
AND personID = @US3_ID;
IF ( count_models = 0 ) THEN
SET @US3_LAST_ERRNO = @NO_MODEL;
SET @US3_LAST_ERROR = 'MySQL: the specified model does not exist';
SET status = @NO_MODEL;
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 model';
SET status = @NOTPERMITTED;
END IF;
RETURN( status );
END$$
-- Returns the count of models associated with p_ID
-- If p_ID = 0, retrieves count of all models in db
-- Regular user can only get count of his own models
DROP FUNCTION IF EXISTS count_models$$
CREATE FUNCTION count_models( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_ID INT )
RETURNS INT
READS SQL DATA
BEGIN
DECLARE count_models INT;
CALL config();
SET count_models = 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_models
FROM modelPerson
WHERE personID = p_ID;
ELSE
SELECT COUNT(*)
INTO count_models
FROM modelPerson;
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 models';
ELSE
-- This person is asking about his own models
-- Ignore p_ID and return user's own
SELECT COUNT(*)
INTO count_models
FROM modelPerson
WHERE personID = @US3_ID;
END IF;
END IF;
RETURN( count_models );
END$$
-- Returns the count of models associated with p_ID with a given editedDataID
-- p_ID cannot be 0
-- Admin user can get count of models that belong to others
DROP FUNCTION IF EXISTS count_models_by_editID$$
CREATE FUNCTION count_models_by_editID( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_ID INT,
p_editID INT )
RETURNS INT
READS SQL DATA
BEGIN
DECLARE count_models INT;
CALL config();
SET count_models = 0;
IF ( p_ID <= 0 ) THEN
-- Gotta have a real ID
RETURN ( 0 );
ELSEIF ( verify_userlevel( p_personGUID, p_password, @US3_ADMIN ) = @OK ) THEN
-- This is an admin; he can get more info
SELECT COUNT(*)
INTO count_models
FROM modelPerson, model
WHERE personID = p_ID
AND modelPerson.modelID = model.modelID
AND editedDataID = p_editID;
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 models';
ELSE
-- This person is asking about his own models
-- Ignore p_ID and return user's own
SELECT COUNT(*)
INTO count_models
FROM modelPerson, model
WHERE personID = @US3_ID
AND modelPerson.modelID = model.modelID
AND editedDataID = p_editID;
END IF;
END IF;
RETURN( count_models );
END$$
-- Returns the count of models associated with p_ID with a given runID
-- p_ID cannot be 0
-- Admin user can get count of models that belong to others
DROP FUNCTION IF EXISTS count_models_by_runID$$
CREATE FUNCTION count_models_by_runID( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_ID INT,
p_runID VARCHAR(250) )
RETURNS INT
READS SQL DATA
BEGIN
DECLARE count_models INT;
DECLARE run_pattern VARCHAR(254);
CALL config();
SET count_models = 0;
SET run_pattern = CONCAT( p_runID, '.%' );
IF ( p_ID <= 0 ) THEN
-- Gotta have a real ID
RETURN ( 0 );
ELSEIF ( verify_userlevel( p_personGUID, p_password, @US3_ADMIN ) = @OK ) THEN
-- This is an admin; he can get more info
SELECT COUNT(*)
INTO count_models
FROM modelPerson, model
WHERE personID = p_ID
AND modelPerson.modelID = model.modelID
AND description LIKE run_pattern ;
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 models';
ELSE
-- This person is asking about his own models
-- Ignore p_ID and return user's own
SELECT COUNT(*)
INTO count_models
FROM modelPerson, model
WHERE personID = @US3_ID
AND modelPerson.modelID = model.modelID
AND description LIKE run_pattern ;
END IF;
END IF;
RETURN( count_models );
END$$
-- INSERTs a new model with the specified information
DROP PROCEDURE IF EXISTS new_model$$
CREATE PROCEDURE new_model ( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_modelGUID CHAR(36),
p_description TEXT,
p_xml LONGTEXT,
p_variance DOUBLE,
p_meniscus DOUBLE,
p_editGUID CHAR(36),
p_ownerID INT )
MODIFIES SQL DATA
BEGIN
DECLARE l_modelID INT;
DECLARE l_count_editID INT;
DECLARE l_editID 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;
-- Translate editGUID into editedDataID
SET l_editID = 1; -- default to special "unassigned" record
SELECT COUNT(*)
INTO l_count_editID
FROM editedData
WHERE editGUID = p_editGUID;
IF ( l_count_editID > 0 ) THEN
SELECT editedDataID
INTO l_editID
FROM editedData
WHERE editGUID = p_editGUID
LIMIT 1;
END IF;
IF ( ( verify_user( p_personGUID, p_password ) = @OK ) &&
( check_GUID ( p_personGUID, p_password, p_modelGUID ) = @OK ) ) THEN
INSERT INTO model SET
editedDataID = l_editID,
modelGUID = p_modelGUID,
description = p_description,
xml = p_xml,
variance = p_variance,
meniscus = p_meniscus,
lastUpdated = NOW();
IF ( duplicate_key = 1 ) THEN
SET @US3_LAST_ERRNO = @INSERTDUP;
SET @US3_LAST_ERROR = "MySQL: Duplicate entry for modelGUID field";
ELSEIF ( null_field = 1 ) THEN
SET @US3_LAST_ERRNO = @INSERTNULL;
SET @US3_LAST_ERROR = "MySQL: NULL value for modelGUID field";
ELSE
SET @LAST_INSERT_ID = LAST_INSERT_ID();
INSERT INTO modelPerson SET
modelID = @LAST_INSERT_ID,
personID = p_ownerID;
END IF;
END IF;
SELECT @US3_LAST_ERRNO AS status;
END$$
-- UPDATEs an existing model with the specified information
DROP PROCEDURE IF EXISTS update_model$$
CREATE PROCEDURE update_model ( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_modelID INT,
p_description TEXT,
p_xml LONGTEXT,
p_variance DOUBLE,
p_meniscus DOUBLE,
p_editGUID CHAR(36) )
MODIFIES SQL DATA
BEGIN
DECLARE not_found TINYINT DEFAULT 0;
DECLARE l_count_editID INT;
DECLARE l_editID INT;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET not_found = 1;
CALL config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
-- Translate editGUID into editedDataID
SET l_editID = 1; -- default to special "unassigned" record
SELECT COUNT(*) -- see if we can find the existing record
INTO l_count_editID
FROM model
WHERE modelID = p_modelID;
IF ( l_count_editID > 0 ) THEN
SELECT editedDataID -- replace default with what's there already
INTO l_editID
FROM model
WHERE modelID = p_modelID;
END IF;
SELECT COUNT(*) -- See if the supplied parameter really does point to a record
INTO l_count_editID
FROM editedData
WHERE editGUID = p_editGUID;
IF ( l_count_editID > 0 ) THEN
SELECT editedDataID
INTO l_editID
FROM editedData
WHERE editGUID = p_editGUID
LIMIT 1;
END IF;
IF ( verify_model_permission( p_personGUID, p_password, p_modelID ) = @OK ) THEN
UPDATE model SET
editedDataID = l_editID,
description = p_description,
xml = p_xml,
variance = p_variance,
meniscus = p_meniscus,
lastUpdated = NOW()
WHERE modelID = p_modelID;
IF ( not_found = 1 ) THEN
SET @US3_LAST_ERRNO = @NO_MODEL;
SET @US3_LAST_ERROR = "MySQL: No model 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 modelID associated with the given modelGUID
DROP PROCEDURE IF EXISTS get_modelID$$
CREATE PROCEDURE get_modelID ( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_modelGUID CHAR(36) )
READS SQL DATA
BEGIN
DECLARE count_models INT;
CALL config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
SET count_models = 0;
IF ( verify_user( p_personGUID, p_password ) = @OK ) THEN
SELECT COUNT(*)
INTO count_models
FROM model
WHERE modelGUID = p_modelGUID;
IF ( TRIM( p_modelGUID ) = '' ) THEN
SET @US3_LAST_ERRNO = @EMPTY;
SET @US3_LAST_ERROR = CONCAT( 'MySQL: The modelGUID parameter to the ',
'get_modelID function cannot be empty' );
ELSEIF ( count_models < 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 modelID
FROM model
WHERE modelGUID = p_modelGUID;
END IF;
END IF;
END$$
-- Returns the modelID and description of all models associated with p_ID
-- If p_ID = 0, retrieves information about all models in db
-- Regular user can only get info about his own models
DROP PROCEDURE IF EXISTS get_model_desc$$
CREATE PROCEDURE get_model_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_models( 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 m.modelID, modelGUID, description, m.variance, m.meniscus,
editGUID, m.editedDataID,
timestamp2UTC( m.lastUpdated ) AS UTC_lastUpdated,
MD5( xml ) AS checksum, LENGTH( xml ) AS size
FROM modelPerson, model m, editedData
WHERE modelPerson.modelID = m.modelID
AND m.editedDataID = editedData.editedDataID
AND modelPerson.personID = p_ID
ORDER BY m.modelID DESC;
ELSE
SELECT m.modelID, modelGUID, description, m.variance, m.meniscus,
editGUID, m.editedDataID,
timestamp2UTC( m.lastUpdated ) AS UTC_lastUpdated,
MD5( xml ) AS checksum, LENGTH( xml ) AS size
FROM modelPerson, model m, editedData
WHERE modelPerson.modelID = m.modelID
AND m.editedDataID = editedData.editedDataID
ORDER BY m.modelID 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 model';
SELECT @US3_LAST_ERRNO AS status;
ELSEIF ( count_models( 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 m.modelID, modelGUID, description, m.variance, m.meniscus,
editGUID, m.editedDataID,
timestamp2UTC( m.lastUpdated ) AS UTC_lastUpdated,
MD5( xml ) AS checksum, LENGTH( xml ) AS size
FROM modelPerson, model m, editedData
WHERE modelPerson.modelID = m.modelID
AND m.editedDataID = editedData.editedDataID
AND modelPerson.personID = @US3_ID
ORDER BY m.modelID DESC;
END IF;
END IF;
END$$
-- Returns the modelID and description of all models associated with p_ID and description
-- If p_ID = 0, retrieves information about all models in db
-- Regular user can only get info about his own models
DROP PROCEDURE IF EXISTS get_model_desc_auto$$
CREATE PROCEDURE get_model_desc_auto ( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_ID INT,
p_description VARCHAR(200) )
READS SQL DATA
BEGIN
DECLARE desc_pattern VARCHAR(254);
CALL config();
SET desc_pattern = CONCAT( p_description, '%2DSA-FM%' );
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_models( 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 m.modelID, modelGUID, description, m.variance, m.meniscus,
editGUID, m.editedDataID,
timestamp2UTC( m.lastUpdated ) AS UTC_lastUpdated,
MD5( xml ) AS checksum, LENGTH( xml ) AS size
FROM modelPerson, model m, editedData
WHERE modelPerson.modelID = m.modelID
AND m.editedDataID = editedData.editedDataID
AND modelPerson.personID = p_ID
AND m.description LIKE desc_pattern
ORDER BY m.modelID DESC;
ELSE
SELECT m.modelID, modelGUID, description, m.variance, m.meniscus,
editGUID, m.editedDataID,
timestamp2UTC( m.lastUpdated ) AS UTC_lastUpdated,
MD5( xml ) AS checksum, LENGTH( xml ) AS size
FROM modelPerson, model m, editedData
WHERE modelPerson.modelID = m.modelID
AND m.editedDataID = editedData.editedDataID
AND m.description LIKE desc_pattern
ORDER BY m.modelID 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 model';
SELECT @US3_LAST_ERRNO AS status;
ELSEIF ( count_models( 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 m.modelID, modelGUID, description, m.variance, m.meniscus,
editGUID, m.editedDataID,
timestamp2UTC( m.lastUpdated ) AS UTC_lastUpdated,
MD5( xml ) AS checksum, LENGTH( xml ) AS size
FROM modelPerson, model m, editedData
WHERE modelPerson.modelID = m.modelID
AND m.editedDataID = editedData.editedDataID
AND modelPerson.personID = @US3_ID
AND m.description LIKE desc_pattern
ORDER BY m.modelID DESC;
END IF;
END IF;
END$$
-- Returns the modelID and description of all models associated with p_ID and editID
-- Unlike get_model_desc, in this function p_ID cannot be 0
-- Admin can view p_ID/editID combinations that belong to anyone
DROP PROCEDURE IF EXISTS get_model_desc_by_editID$$
CREATE PROCEDURE get_model_desc_by_editID ( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_ID INT,
p_editID INT )
READS SQL DATA
BEGIN
CALL config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
IF ( p_ID <= 0 ) THEN
-- Gotta have a real ID
SET @US3_LAST_ERRNO = @EMPTY;
SET @US3_LAST_ERROR = 'MySQL: The ID cannot be 0';
SELECT @US3_LAST_ERRNO AS status;
ELSEIF ( verify_userlevel( p_personGUID, p_password, @US3_ADMIN ) = @OK ) THEN
-- This is an admin; he can get info that belongs to others
IF ( count_models_by_editID( p_personGUID, p_password, p_ID, p_editID ) < 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 m.modelID, modelGUID, description, m.variance, m.meniscus,
editGUID, m.editedDataID,
timestamp2UTC( m.lastUpdated ) AS UTC_lastUpdated,
MD5( xml ) AS checksum, LENGTH( xml ) AS size
FROM modelPerson, model m, editedData
WHERE personID = p_ID
AND modelPerson.modelID = m.modelID
AND m.editedDataID = p_editID
AND m.editedDataID = editedData.editedDataID
ORDER BY m.modelID DESC;
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 model';
SELECT @US3_LAST_ERRNO AS status;
ELSEIF ( count_models_by_editID( p_personGUID, p_password, @US3_ID, p_editID ) < 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 m.modelID, modelGUID, description, m.variance, m.meniscus,
editGUID, m.editedDataID,
timestamp2UTC( m.lastUpdated ) AS UTC_lastUpdated,
MD5( xml ) AS checksum, LENGTH( xml ) AS size
FROM modelPerson, model m, editedData
WHERE personID = @US3_ID
AND modelPerson.modelID = m.modelID
AND m.editedDataID = p_editID
AND m.editedDataID = editedData.editedDataID
ORDER BY m.modelID DESC;
END IF;
END IF;
END$$
-- Returns the modelID and description of all models associated with p_ID and runID
-- Unlike get_model_desc, in this function p_ID cannot be 0
-- Admin can view p_ID/runID combinations that belong to anyone
DROP PROCEDURE IF EXISTS get_model_desc_by_runID$$
CREATE PROCEDURE get_model_desc_by_runID ( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_ID INT,
p_runID VARCHAR(250) )
READS SQL DATA
BEGIN
DECLARE run_pattern VARCHAR(254);
CALL config();
SET run_pattern = CONCAT( p_runID, '.%' );
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
IF ( p_ID <= 0 ) THEN
-- Gotta have a real ID
SET @US3_LAST_ERRNO = @EMPTY;
SET @US3_LAST_ERROR = 'MySQL: The ID cannot be 0';
SELECT @US3_LAST_ERRNO AS status;
ELSEIF ( verify_userlevel( p_personGUID, p_password, @US3_ADMIN ) = @OK ) THEN
-- This is an admin; he can get info that belongs to others
IF ( count_models_by_runID( p_personGUID, p_password, p_ID, p_runID ) < 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 m.modelID, modelGUID, description, m.variance, m.meniscus,
editGUID, m.editedDataID,
timestamp2UTC( m.lastUpdated ) AS UTC_lastUpdated,
MD5( xml ) AS checksum, LENGTH( xml ) AS size
FROM modelPerson, model m, editedData
WHERE personID = p_ID
AND modelPerson.modelID = m.modelID
AND m.editedDataID = editedData.editedDataID
AND description LIKE run_pattern
ORDER BY m.modelID DESC;
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 model';
SELECT @US3_LAST_ERRNO AS status;
ELSEIF ( count_models_by_runID( p_personGUID, p_password, @US3_ID, p_runID ) < 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 m.modelID, modelGUID, description, m.variance, m.meniscus,
editGUID, m.editedDataID,
timestamp2UTC( m.lastUpdated ) AS UTC_lastUpdated,
MD5( xml ) AS checksum, LENGTH( xml ) AS size
FROM modelPerson, model m, editedData
WHERE personID = @US3_ID
AND modelPerson.modelID = m.modelID
AND m.editedDataID = editedData.editedDataID
AND description LIKE run_pattern
ORDER BY m.modelID DESC;
END IF;
END IF;
END$$
-- Returns a more complete list of information about one model
DROP PROCEDURE IF EXISTS get_model_info$$
CREATE PROCEDURE get_model_info ( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_modelID INT )
READS SQL DATA
BEGIN
DECLARE count_models INT;
CALL config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
SELECT COUNT(*)
INTO count_models
FROM model
WHERE modelID = p_modelID;
IF ( verify_model_permission( p_personGUID, p_password, p_modelID ) = @OK ) THEN
IF ( count_models = 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 modelGUID, description, xml, variance, meniscus, personID,
timestamp2UTC( lastUpdated ) AS UTC_lastUpdated,
MD5( xml ) AS checksum, LENGTH( xml ) AS size
FROM model m, modelPerson mp
WHERE m.modelID = mp.modelID
AND m.modelID = p_modelID;
END IF;
ELSE
SELECT @US3_LAST_ERRNO AS status;
END IF;
END$$
-- Get model descriptions for editedDataID
DROP PROCEDURE IF EXISTS get_modelDescsIDs$$
CREATE PROCEDURE get_modelDescsIDs ( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_editID INT )
READS SQL DATA
BEGIN
DECLARE count_models INT;
CALL config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
SELECT COUNT(*)
INTO count_models
FROM model
WHERE editedDataID = p_editID;
IF ( count_models = 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 description, modelID, lastUpdated
FROM model
WHERE editedDataID = p_editID;
END IF;
END$$
-- DELETEs a model, plus information in related tables
DROP PROCEDURE IF EXISTS delete_model$$
CREATE PROCEDURE delete_model ( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_modelID INT )
MODIFIES SQL DATA
BEGIN
CALL config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
IF ( verify_model_permission( p_personGUID, p_password, p_modelID ) = @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 noise
FROM model
LEFT JOIN noise ON ( noise.modelID = model.modelID )
WHERE model.modelID = p_modelID;
DELETE FROM modelPerson
WHERE modelID = p_modelID;
DELETE FROM model
WHERE modelID = p_modelID;
END IF;
SELECT @US3_LAST_ERRNO AS status;
END$$