File: sql-classes.lisp

package info (click to toggle)
cl-umlisp 1%3A2007ac.2-6
  • links: PTS, VCS
  • area: contrib
  • in suites: bookworm, bullseye, buster, jessie, jessie-kfreebsd, sid, stretch, wheezy
  • size: 296 kB
  • ctags: 418
  • sloc: lisp: 3,593; makefile: 55
file content (1144 lines) | stat: -rw-r--r-- 51,497 bytes parent folder | download | duplicates (3)
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
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: umlisp -*-
;;;; *************************************************************************
;;;; FILE IDENTIFICATION
;;;;
;;;; Name:     sql-classes.lisp
;;;; Purpose:  Routines for reading UMLS objects from SQL database
;;;; Author:   Kevin M. Rosenberg
;;;; Created:  Apr 2000
;;;;
;;;; $Id$
;;;;
;;;; This file, part of UMLisp, is
;;;;    Copyright (c) 2000-2006 by Kevin M. Rosenberg, M.D.
;;;;
;;;; UMLisp users are granted the rights to distribute and use this software
;;;; as governed by the terms of the GNU General Public License.
;;;; *************************************************************************

(in-package #:umlisp)


(defvar *current-srl* nil)
(defun current-srl ()
  *current-srl*)
(defun current-srl! (srl)
  (setq *current-srl* srl))

(defmacro query-string (table fields srl where-name where-value
                        &key (lrl "KCUILRL") single distinct order like limit
                        filter)
  (let* ((%%fields (format nil "SELECT ~A~{~:@(~A~)~^,~} FROM ~:@(~A~)"
                           (if distinct "DISTINCT " "") fields table))
         (%%order (if order (format nil " ORDER BY ~{~:@(~A~) ~(~A~)~^,~}"
                                    order)
                      ""))
         (%%lrl (format nil " AND ~:@(~A~)<=" lrl))
         (%%where (when where-name
                    (format nil " WHERE ~:@(~A~)~A" where-name
                          (if like " like " ""))))
         (%filter (gensym "FILTER-"))
         (%single (gensym "SINGLE-"))
         (%limit (gensym "LIMIT-")))
    `(let ((,%limit ,limit)
           (,%single ,single)
           (,%filter ,filter))
       (concatenate
        'string
        ,%%fields
        ,@(when %%where (list %%where))
        ,@(when %%where
            `((typecase ,where-value
                #+ignore
                (fixnum
                 (concatenate 'string "='" (prefixed-fixnum-string ,where-value #\0 10) "'"))
                (number
                 (concatenate 'string "='" (write-to-string ,where-value) "'"))
                (null
                 " IS NULL")
                (t
                 (format nil ,(if like "'%~A%'" "='~A'") ,where-value)))))
        (if ,%filter (concatenate 'string
                                  ,(if %%where " AND " " WHERE ")
                                  ,%filter) "")
        (if ,srl (concatenate 'string ,%%lrl (write-to-string ,srl)) "")
        ,@(when %%order (list %%order))
        (cond
         ((and ,%single ,%limit)
          (error  "Can't set single (~S) and limit (~S)" ,%single ,%limit))
         (,%single
          " LIMIT 1")
         (,%limit
          (format nil " LIMIT ~D" ,%limit))
         (t
          ""))))))

(defun query-string-eval (table fields srl where-name where-value
                          &key (lrl "KCUILRL") single distinct order like limit filter)
  (when single (setq limit 1))
  (concatenate
   'string
   (format nil "SELECT ~A~{~:@(~A~)~^,~} FROM ~:@(~A~)"
           (if distinct "DISTINCT " "") fields table)
   (if where-name (format nil " WHERE ~:@(~A~)" where-name) "")
   (if where-name
       (format nil
               (typecase where-value
                 (number "='~D'")
                 (null " IS NULL")
                 (t
                  (if like " LINK '%~A%""='~A'")))
               where-value)
       "")
   (if filter (concatenate 'string " AND " filter) nil)
   (if srl (format nil " AND ~:@(~A~)<=~D" lrl srl) "")
   (if order (format nil " ORDER BY ~{~:@(~A~) ~(~A~)~^,~}" order) "")
   (if limit (format nil " LIMIT ~D" limit) "")))


(defmacro umlisp-query (table fields srl where-name where-value
                     &key (lrl "KCUILRL") single distinct order like
                     limit filter (query-cmd 'mutex-sql-query))
  "Query the UMLisp database. Return a list of umlisp objects whose name
is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
  `(,query-cmd
    (query-string ,table ,fields ,srl ,where-name ,where-value
     :lrl ,lrl :single ,single :distinct ,distinct :order ,order :like ,like
     :filter ,filter :limit ,limit)))

(defmacro umlisp-query-eval (table fields srl where-name where-value
                     &key (lrl "KCUILRL") single distinct order like
                     filter limit)
  "Query the UMLisp database. Return a list of umlisp objects whose name
is OBJNAME from TABLE where WHERE-NAME field = WHERE-VALUE with FIELDS"
  `(mutex-sql-query
    (query-string-eval ,table ,fields ,srl ,where-name ,where-value
     :lrl ,lrl :single ,single :distinct ,distinct :order ,order :like ,like
     :filter ,filter :limit ,limit)))

;; only WHERE-VALUE and SRL are evaluated
(defmacro collect-umlisp-query ((table fields srl where-name where-value
                                    &key (lrl "KCUILRL") distinct single
                                    order like (query-cmd 'mutex-sql-query)
                                    filter limit)
                                &body body)
  (let ((value (gensym))
        (r (gensym)))
    (if single
        (if (and limit (> limit 1))
            (error "Can't set limit along with single.")
          `(let* ((,value ,where-value)
                  (tuple (car (umlisp-query ,table ,fields ,srl ,where-name ,value
                                            :lrl ,lrl :single ,single
                                            :distinct ,distinct :order ,order
                                            :like ,like :filter ,filter
                                            :query-cmd ,query-cmd))))
             ,@(unless where-name `((declare (ignore ,value))))
             (when tuple
               (destructuring-bind ,fields tuple
                 ,@body))))
        `(let ((,value ,where-value))
           ,@(unless where-name `((declare (ignore ,value))))
           (let ((,r '()))
             (dolist (tuple (umlisp-query ,table ,fields ,srl ,where-name ,value
                                          :lrl ,lrl :single ,single :distinct ,distinct
                                          :order ,order :filter ,filter :like ,like
                                          :limit ,limit))
               (push (destructuring-bind ,fields tuple ,@body) ,r))
             (nreverse ,r))
           #+ignore
           (loop for tuple in
                 (umlisp-query ,table ,fields ,srl ,where-name ,value
                               :lrl ,lrl :single ,single :distinct ,distinct
                               :order ,order :like ,like :filter ,filter :limit ,limit)
               collect (destructuring-bind ,fields tuple ,@body))))))

(defmacro collect-umlisp-query-eval ((table fields srl where-name where-value
                                         &key (lrl "KCUILRL") distinct single
                                         order like filter limit)
                                  &body body)
  (let ((value (gensym))
        (r (gensym))
        (eval-fields (cadr fields)))
    (if single
        `(let* ((,value ,where-value)
                (tuple (car (umlisp-query-eval ,table ,fields ,srl ,where-name ,value
                                               :lrl ,lrl :single ,single
                                               :distinct ,distinct :order ,order
                                               :like ,like :filter ,filter
                                               :limit ,limit))))
          (when tuple
            (destructuring-bind ,eval-fields tuple
              ,@body)))
        `(let ((,value ,where-value)
               (,r '()))
           (dolist (tuple (umlisp-query-eval ,table ,fields ,srl ,where-name ,value
                                             :lrl ,lrl :single ,single :distinct ,distinct
                                             :order ,order :like ,like
                                             :filter ,filter :limit ,limit))
             (push (destructuring-bind ,eval-fields tuple ,@body) ,r))
           (nreverse ,r)
           #+ignore
           (loop for tuple in
                 (umlisp-query-eval ,table ,fields ,srl ,where-name ,value
                                    :lrl ,lrl :single ,single :distinct ,distinct
                                    :order ,order :like ,like :filter ,filter
                                    :limit ,limit)
               collect (destructuring-bind ,eval-fields tuple ,@body))))))

;;;
;;; Read from SQL database

(defmacro ensure-cui-integer (cui)
  `(if (stringp ,cui)
    (setq ,cui (parse-cui ,cui))
    ,cui))

(defmacro ensure-lui-integer (lui)
  `(if (stringp ,lui)
    (setq ,lui (parse-lui ,lui))
    ,lui))

(defmacro ensure-sui-integer (sui)
  `(if (stringp ,sui)
    (setq ,sui (parse-sui ,sui))
    ,sui))

(defmacro ensure-aui-integer (aui)
  `(if (stringp ,aui)
    (setq ,aui (parse-aui ,aui))
    ,aui))

(defmacro ensure-rui-integer (rui)
  `(if (stringp ,rui)
    (setq ,rui (parse-rui ,rui))
    ,rui))

(defmacro ensure-tui-integer (tui)
  `(if (stringp ,tui)
    (setq ,tui (parse-tui ,tui))
    ,tui))

(defmacro ensure-eui-integer (eui)
  `(if (stringp ,eui)
    (setq ,eui (parse-eui ,eui))
    ,eui))

(defun make-ucon-cui (cui)
  (ensure-cui-integer cui)
  (when cui
    (make-instance 'ucon :cui cui)))

(defun find-ucon-cui (cui &key (srl *current-srl*) without-pfstr)
  "Find ucon for a cui. If set SAB, the without-pfstr is on by default"
  (ensure-cui-integer cui)
  (unless cui (return-from find-ucon-cui nil))

  (if without-pfstr
      (collect-umlisp-query (mrconso (kcuilrl) srl cui cui :single t)
                            (make-instance 'ucon :cui cui :lrl (ensure-integer kcuilrl)
                                           :pfstr nil)))
   (or
     (collect-umlisp-query (mrconso (kcuilrl str) srl cui cui :single t :filter "KPFENG=1")
        (make-instance 'ucon :cui cui :pfstr str
                       :lrl kcuilrl))
     (collect-umlisp-query (mrconso (kcuilrl str) srl cui cui :single t)
        (make-instance 'ucon :cui cui :pfstr str
                       :lrl kcuilrl))))

(defun find-uconso-cui (cui &key sab (srl *current-srl*))
  "Find uconso for a cui."
  (ensure-cui-integer cui)
  (unless cui (return-from find-uconso-cui nil))

  (collect-umlisp-query (mrconso (lat ts lui stt sui ispref aui saui scui sdui sab tty code str
                                      srl suppress cvf kpfeng kcuisui kcuilui kcuilrl
                                      kluilrl ksuilrl) srl cui cui
                                      :filter (if sab (concatenate 'string "SAB='" sab "'") nil))
    (make-instance 'uconso :cui cui :lat lat :ts ts :lui lui :stt stt :sui sui :ispref ispref
                   :aui aui :saui saui :scui scui :sdui sdui :sab sab :tty tty :code code
                   :str str :srl srl :suppress suppress :cvf cvf :kpfeng kpfeng
                   :kcuisui kcuisui :kcuilui kcuilui :kcuilrl kcuilrl :kluilrl kluilrl
                   :ksuilrl ksuilrl)))


(defun find-uconso-code (code &key first sab (srl *current-srl*) (like nil))
  "Return list of uconso objects that match code. Optional, filter for SAB. Optionally, use SQL's LIKE syntax"
  (collect-umlisp-query (mrconso (cui sab) srl code code :like like :distinct t
                                 :lrl klrl
                                 :filter (if sab (concatenate 'string "SAB='" sab "'") nil))
    (let ((uconsos (find-uconso-cui cui :sab sab :srl srl)))
      (if first
          (first uconsos)
          uconsos))))

(defun find-uconso-sui (sui &key sab (srl *current-srl*))
  "Find uconso for a sui. If set SAB, the without-pfstr is on by default"
  (ensure-sui-integer sui)
  (unless (and sui (stringp sab))
    (return-from find-uconso-sui nil))

  (collect-umlisp-query (mrconso (cui lat ts lui stt sui ispref aui saui scui sdui sab tty code str
                                      srl suppress cvf kpfeng kcuisui kcuilui kcuilrl
                                      kluilrl ksuilrl) srl sui sui
                                      :distinct t
                                      :filter (if sab (concatenate 'string "SAB='" sab "'") nil))
    (make-instance 'uconso :cui cui :lat lat :ts ts :lui lui :stt stt :sui sui :ispref ispref
                   :aui aui :saui saui :scui scui :sdui sdui :sab sab :tty tty :code code
                   :str str :srl srl :suppress suppress :cvf cvf :kpfeng kpfeng
                   :kcuisui kcuisui :kcuilui kcuilui :kcuilrl kcuilrl :kluilrl kluilrl
                   :ksuilrl ksuilrl)))

(defun find-pfstr-cui (cui &key (srl *current-srl*))
  "Find preferred string for a cui"
  (ensure-cui-integer cui)
  (or
   (collect-umlisp-query (mrconso (str) srl cui cui :distinct t
                                  :filter " KPFENG=1" :single t)
      str)
   (collect-umlisp-query (mrconso (str) srl cui cui :distinct t
                                  :single t)
                         str)))

(defun find-lrl-cui (cui &key (srl *current-srl*))
  "Find LRL for a cui"
  (ensure-cui-integer cui)
  (collect-umlisp-query (mrconso (kcuilrl) srl cui cui :distinct t :single t)
                        (ensure-integer kcuilrl)))

(defun find-ucon-lui (lui &key (srl *current-srl*))
  "Find list of ucon for lui"
  (ensure-lui-integer lui)
  (unless lui (return-from find-ucon-lui nil))
  (or
   (collect-umlisp-query (mrconso (cui kcuilrl str) srl lui lui
                                  :filter " KPFENG=1" :single t)
      (make-instance 'ucon :cui (ensure-integer cui)
                     :pfstr str :lrl (ensure-integer kcuilrl)))
   (collect-umlisp-query (mrconso (cui kcuilrl str) srl lui lui
                                  :single t)
      (make-instance 'ucon :cui (ensure-integer cui)
                     :pfstr str :lrl (ensure-integer kcuilrl)))))

(defun find-ucon-sui (sui &key (srl *current-srl*))
  "Find list of ucon for sui"
  (ensure-sui-integer sui)
  (collect-umlisp-query (mrconso (cui kcuilrl) srl sui sui :distinct t)
    (make-instance 'ucon :cui (ensure-integer cui) :pfstr (find-pfstr-cui cui)
                   :lrl (ensure-integer kcuilrl))))

(defun find-ucon-aui (aui &key (srl *current-srl*))
  "Find list of ucon for aui"
  (ensure-aui-integer aui)
  (collect-umlisp-query (mrconso (cui kcuilrl) srl aui aui :distinct t)
    (make-instance 'ucon :cui (ensure-integer cui) :pfstr (find-pfstr-cui cui)
                   :lrl (ensure-integer kcuilrl))))

(defun find-ucon-cuisui (cui sui &key (srl *current-srl*))
  "Find ucon for cui/sui"
  (ensure-cui-integer cui)
  (ensure-sui-integer sui)
  (when (and cui sui)
    (collect-umlisp-query (mrconso (kcuilrl) srl kcuisui
                              (make-cuisui cui sui))
      (make-instance 'ucon :cui cui
                     :pfstr (find-pfstr-cui cui)
                     :lrl (ensure-integer kcuilrl)))))

(defun find-ucon-str (str &key (srl *current-srl*))
  "Find ucon that are exact match for str"
  (collect-umlisp-query (mrconso (cui kcuilrl) srl str str :distinct t)
    (make-instance 'ucon :cui (ensure-integer cui) :pfstr (find-pfstr-cui cui)
                   :lrl (ensure-integer kcuilrl))))

(defun find-ucon-all (&key (srl *current-srl*))
  "Return list of all ucon's"
  (with-sql-connection (db)
    (clsql:map-query
     'list
     #'(lambda (tuple)
         (destructuring-bind (cui cuilrl) tuple
             (make-instance 'ucon :cui (ensure-integer cui)
                            :pfstr (find-pfstr-cui cui)
                            :lrl (ensure-integer cuilrl))))
     (query-string mrconso (cui kcuilrl) srl nil nil
                   :order (cui asc) :distinct t)
     :database db)))

(defun find-ucon-all2 (&key (srl *current-srl*))
  "Return list of all ucon's"
  (collect-umlisp-query (mrconso (cui kcuilrl) srl nil nil :order (cui asc)
                            :distinct t)
    (make-instance 'ucon :cui (ensure-integer cui)
                   :pfstr (find-pfstr-cui cui)
                   :lrl (ensure-integer kcuilrl))))

(defun find-cui-ucon-all (&key (srl *current-srl*))
  "Return list of CUIs for all ucons"
  (collect-umlisp-query (mrconso (cui) srl nil nil :order (cui asc)
                               :distinct t)
                        cui))

(defun map-ucon-all (fn &key (srl *current-srl*))
  "Map a function over all ucon's"
  (with-sql-connection (db)
    (clsql:map-query
     nil
     #'(lambda (tuple)
         (destructuring-bind (cui cuilrl) tuple
           (funcall fn (make-instance 'ucon :cui (ensure-integer cui)
                                      :pfstr (find-pfstr-cui cui)
                                      :lrl (ensure-integer cuilrl)))))
     (query-string mrconso (cui kcuilrl) srl nil nil :order (cui asc)
                   :distinct t)
     :database db)))


(defun find-udef-cui (cui &key (srl *current-srl*))
  "Return a list of udefs for cui"
  (ensure-cui-integer cui)
  (collect-umlisp-query (mrdef (sab def suppress) srl cui cui :lrl "KSRL")
    (make-instance 'udef :sab sab :def def :suppress suppress)))

(defun find-udoc-key (key)
  "Return list of abbreviation documentation for a key"
  (collect-umlisp-query (mrdoc (value type expl) nil dockey key)
    (make-instance 'udoc :dockey key :dvalue value :dtype type :expl expl)))

(defun find-udoc-value (value)
  "Return abbreviation documentation"
  (collect-umlisp-query (mrdoc (dockey type expl) nil value value)
    (make-instance 'udoc :dockey dockey :dvalue value :dtype type :expl expl)))

(defun find-udoc-key-value (dockey value)
  (collect-umlisp-query (mrdoc (type expl) nil dockey dockey :filter (format nil "VALUE='~A'" value))
      (make-instance 'udoc :dockey dockey :dvalue value :dtype type :expl expl)))

(defun find-udoc-all ()
  "Return all abbreviation documentation"
  (collect-umlisp-query (mrdoc (dockey value type expl) nil nil nil)
    (make-instance 'udoc :dockey dockey :dvalue value :dtype type :expl expl)))

(defun find-usty-cui (cui &key (srl *current-srl*))
  "Return a list of usty for cui"
  (ensure-cui-integer cui)
  (collect-umlisp-query (mrsty (tui sty) srl cui cui :lrl "KLRL")
    (make-instance 'usty :tui (ensure-integer tui) :sty sty)))

(defun find-usty-word (word &key (srl *current-srl*))
  "Return a list of usty that match word"
  (collect-umlisp-query (mrsty (tui sty) srl sty word :lrl klrl :like t
                            :distinct t)
    (make-instance 'usty :tui (ensure-integer tui) :sty sty)))

(defun find-urel-cui (cui &key (srl *current-srl*) filter without-pfstr2)
  "Return a list of urel for cui"
  (ensure-cui-integer cui)
  (collect-umlisp-query (mrrel (aui1 rel stype1 cui2 aui2 stype2 rela rui srui sab sl rg dir suppress cvf)
                               srl cui1 cui :lrl "KSRL" :filter filter)
    (let ((rel
      (make-instance 'urel :cui1 cui :aui1 (ensure-integer aui1) :stype1 stype1 :rel rel
                     :cui2 (ensure-integer cui2) :aui2 (ensure-integer aui2) :stype2 stype2
                     :rui (ensure-integer rui) :srui srui :rela rela :sab sab :sl sl :rg rg :dir dir
                     :suppress suppress :cvf cvf)))
      (unless without-pfstr2
        (setf (slot-value rel 'pfstr2) (find-pfstr-cui cui2)))
      rel)))

(defun find-urel-rui (rui &key (srl *current-srl*))
  "Return the urel for a rui"
  (ensure-rui-integer rui)
  (collect-umlisp-query (mrrel (aui1 rel stype1 cui1 cui2 aui2 stype2 rela rui srui sab sl rg dir suppress cvf)
                               srl rui rui :lrl "KSRL" :single t)
    (make-instance 'urel :cui1 cui1 :aui1 (ensure-integer aui1) :stype1 stype1 :rel rel
                   :cui2 (ensure-integer cui2) :aui2 (ensure-integer aui2) :stype2 stype2
                   :rui (ensure-integer rui) :srui srui :rela rela :sab sab :sl sl :rg rg :dir dir
                   :suppress suppress :cvf cvf :pfstr2 (find-pfstr-cui cui2))))

(defun find-cui2-urel-cui (cui &key (srl *current-srl*))
  "Return a list of urel for cui"
  (ensure-cui-integer cui)
  (collect-umlisp-query (mrrel (cui2) srl cui1
                               cui :lrl "KSRL")
                        cui2))

(defun find-urel-cui2 (cui2 &key (srl *current-srl*))
  "Return a list of urel for cui2"
  (ensure-cui-integer cui2)
  (collect-umlisp-query (mrrel (rel cui1 aui1 stype1 aui2 stype2 rela rui srui sab sl rg dir suppress cvf)
                               srl cui2 cui2 :lrl "KSRL")
    (make-instance 'urel :cui2 cui2 :rel rel :aui2 (ensure-integer aui2)
                   :stype2 stype2 :rui (ensure-integer rui) :srui srui
                   :stype1 stype1 :cui1 (ensure-integer cui1)
                   :aui1 (ensure-integer aui1)
                   :rela rela :sab sab :sl sl :rg rg :dir dir :suppress suppress :cvf cvf
                   :pfstr2 (find-pfstr-cui cui2))))

(defun find-ucon-rel-cui2 (cui2 &key (srl *current-srl*))
  (ensure-cui-integer cui2)
  (loop for cui in (remove-duplicates
                    (mapcar #'cui1 (find-urel-cui2 cui2 :srl srl)))
        collect (find-ucon-cui cui :srl srl)))

(defun find-ucoc-cui (cui &key (srl *current-srl*))
  "Return a list of ucoc for cui"
  (ensure-cui-integer cui)
  (collect-umlisp-query (mrcoc (aui1 cui2 aui2 sab cot cof coa) srl cui1
                            cui :lrl klrl :order (cof asc))
    (setq cui2 (ensure-integer cui2))
    (when (eql 0 cui2) (setq cui2 nil))
    (make-instance 'ucoc :cui1 cui :aui1 (ensure-integer aui1)
                   :cui2 (ensure-integer cui2) :aui2 (ensure-integer aui2)
                   :cot cot :cof (ensure-integer cof) :coa coa :sab sab
                   :pfstr2 (find-pfstr-cui cui2))))

(defun find-ucoc-cui2 (cui2 &key (srl *current-srl*))
  "Return a list of ucoc for cui2"
  (ensure-cui-integer cui2)
  (collect-umlisp-query (mrcoc (cui1 aui1 aui2 sab cot cof coa) srl cui2
                            cui2 :lrl klrl :order (cof asc))
    (when (zerop cui2) (setq cui2 nil))
    (make-instance 'ucoc :cui1 (ensure-integer cui1) :cui2 cui2
                   :aui1 (ensure-integer aui1) :aui2 (ensure-integer aui2)
                   :sab sab :cot cot :cof (ensure-integer cof) :coa coa
                   :pfstr2 (find-pfstr-cui cui2))))

(defun find-ucon-coc-cui2 (cui2 &key (srl *current-srl*))
  "List of ucon with co-occurance cui2"
  (ensure-cui-integer cui2)
  (mapcar
   #'(lambda (cui) (find-ucon-cui cui :srl srl))
   (remove-duplicates (mapcar #'cui1 (find-ucoc-cui2 cui2 :srl srl)))))


(defun find-uterm-cui (cui &key (srl *current-srl*))
  "Return a list of uterm for cui"
  (ensure-cui-integer cui)
  (collect-umlisp-query (mrconso (lui lat ts kluilrl) srl cui cui
                            :lrl kluilrl :distinct t)
    (make-instance 'uterm :lui (ensure-integer lui) :cui cui
                   :lat lat :ts ts :lrl (ensure-integer kluilrl))))

(defun find-uterm-lui (lui &key (srl *current-srl*))
  "Return a list of uterm for lui"
  (ensure-lui-integer lui)
  (collect-umlisp-query (mrconso (cui lat ts kluilrl) srl lui lui
                             :lrl kluilrl :distinct t)
    (make-instance 'uterm :cui (ensure-integer cui) :lui lui
                   :lat lat :ts ts :lrl (ensure-integer kluilrl))))

(defun find-uterm-cuilui (cui lui &key (srl *current-srl*))
  "Return single uterm for cui/lui"
  (ensure-cui-integer cui)
  (ensure-lui-integer lui)
  (collect-umlisp-query (mrconso (lat ts kluilrl) srl kcuilui
                             (make-cuilui cui lui)
                             :lrl kluilrl :single t)
    (make-instance 'uterm :cui cui :lui lui :lat lat :ts ts
                   :lrl (ensure-integer kluilrl))))

(defun find-ustr-cuilui (cui lui &key (srl *current-srl*))
  "Return a list of ustr for cui/lui"
  (ensure-cui-integer cui)
  (ensure-lui-integer lui)
  (collect-umlisp-query (mrconso (sui stt str suppress ksuilrl) srl kcuilui
                                 (make-cuilui cui lui) :lrl ksuilrl)
                (make-instance 'ustr :sui (ensure-integer sui) :cui cui :lui lui
                   :cuisui (make-cuisui cui sui) :stt stt :str str :suppress suppress
                   :lrl (ensure-integer ksuilrl))))

(defun find-ustr-cuisui (cui sui &key (srl *current-srl*))
  "Return the single ustr for cuisui"
  (ensure-cui-integer cui)
  (ensure-sui-integer sui)
  (collect-umlisp-query (mrconso (lui stt str suppress ksuilrl) srl kcuisui
                            (make-cuisui cui sui) :lrl lsuilrl :single t)
    (make-instance 'ustr :sui sui :cui cui :cuisui (make-cuisui cui sui)
                   :lui (ensure-integer lui) :stt stt :str str :suppress suppress
                   :lrl (ensure-integer ksuilrl))))

(defun find-ustr-sui (sui &key (srl *current-srl*))
  "Return the list of ustr for sui"
  (ensure-sui-integer sui)
  (collect-umlisp-query (mrconso (cui lui stt str suppress ksuilrl) srl sui sui
                            :lrl ksuilrl)
    (make-instance 'ustr :sui sui :cui cui :stt stt :str str
                   :cuisui (make-cuisui (ensure-integer cui) sui)
                   :suppress suppress
                   :lui (ensure-integer lui) :lrl (ensure-integer ksuilrl))))

(defun find-ustr-sab (sab &key (srl *current-srl*))
  "Return the list of ustr for sab"
  (collect-umlisp-query (mrconso (kcuisui) srl sab sab :lrl srl)
    (let ((cuisui (ensure-integer kcuisui)))
      (apply #'find-ustr-cuisui
             (append
              (multiple-value-list (decompose-cuisui cuisui))
              (list :srl srl))))))

(defun find-ustr-all (&key (srl *current-srl*))
  "Return list of all ustr's"
    (with-sql-connection (db)
      (clsql:map-query
       'list
       #'(lambda (tuple)
           (destructuring-bind (cui lui sui stt ksuilrl suppress) tuple
             (make-instance 'ustr :cui (ensure-integer cui)
                            :lui (ensure-integer lui) :sui (ensure-integer sui)
                            :stt stt :str (find-pfstr-cui cui)
                            :cuisui (make-cuisui (ensure-integer cui)
                                                 (ensure-integer sui))
                            :suppress suppress
                            :lrl (ensure-integer ksuilrl))))
       (query-string mrconso (cui lui sui stt ksuilrl) srl nil nil :lrl ksuilrl
                     :distinct t
                     :order (sui asc))
       :database db)))

(defun find-string-sui (sui &key (srl *current-srl*))
  "Return the string associated with sui"
  (ensure-sui-integer sui)
  (collect-umlisp-query (mrconso (str) srl sui sui :lrl ksuilrl :single t)
    str))

(defun find-uso-cuisui (cui sui &key (srl *current-srl*))
  (ensure-sui-integer sui)
  (ensure-cui-integer cui)
  (collect-umlisp-query (mrconso (aui sab code srl tty saui sdui scui lat str)
                                 srl kcuisui (make-cuisui cui sui) :lrl srl)
    (make-instance 'uso :aui aui :sab sab :code code :srl srl :tty tty
                   :cui cui :sui sui :saui saui :sdui sdui :scui scui
                   :lat lat :str str)))

(defun find-uso-cui (cui &key (srl *current-srl*) (english-only nil) limit)
  (ensure-cui-integer cui)
  (collect-umlisp-query (mrconso (aui sab code srl tty saui sdui scui lat str sui)
                                 srl cui cui :lrl srl :limit limit
                                 :filter (when english-only "LAT='ENG'"))
     (make-instance 'uso :aui aui :sab sab :code code :srl srl :tty tty
                    :cui cui :sui sui :saui saui :sdui sdui :scui scui
                    :lat lat :str str)))

(defun find-uso-aui (aui &key (srl *current-srl*))
  (ensure-sui-integer aui)
  (collect-umlisp-query (mrconso (sab cui sui code srl tty saui sdui scui lat
                                      str) srl aui aui :lrl srl :single t)
    (make-instance 'uso :aui aui :cui cui :sab sab :code code :srl srl :tty tty
                   :sui sui :saui saui :sdui sdui :scui scui :lat lat
                   :str str)))

(defun find-uhier-cui (cui &key (srl *current-srl*))
  (ensure-cui-integer cui)
  (collect-umlisp-query (mrhier (aui cxn paui sab rela ptr hcd cvf)
                            srl cui cui :lrl ksrl)
    (make-instance 'uhier :cui cui :aui (ensure-integer aui)
                   :cxn (ensure-integer cxn)
                   :paui (ensure-integer paui)
                   :sab sab :rela rela :ptr ptr :hcd hcd :cvf cvf)))

(defun find-uhier-all (&key (srl *current-srl*))
  (collect-umlisp-query (mrhier (cui aui cxn paui sab rela ptr hcd cvf)
                            srl nil nil :lrl ksrl)
    (make-instance 'uhier :cui cui :aui (ensure-integer aui)
                   :cxn (ensure-integer cxn)
                   :paui (ensure-integer paui)
                   :sab sab :rela rela :ptr ptr :hcd hcd :cvf cvf)))

(defun find-usat-ui (cui &optional lui sui &key (srl *current-srl*))
  (ensure-cui-integer cui)
  (ensure-lui-integer lui)
  (ensure-sui-integer sui)
  (let ((ls "SELECT CODE,ATN,SAB,ATV FROM MRSAT WHERE "))
    (cond
      (sui (string-append ls "KCUISUI='"
                          (integer-string (make-cuisui cui sui) 14)
                          "'"))
      (lui (string-append ls "KCUILUI='"
                          (integer-string (make-cuilui cui lui) 14)
                          "' and sui='0'"))
      (t (string-append ls "cui='" (prefixed-fixnum-string cui nil 7)
                        "' and lui='0' and sui='0'")))
    (when srl
      (string-append ls " and KSRL<=" (prefixed-fixnum-string srl nil 3)))
    (loop for tuple in (mutex-sql-query ls) collect
          (destructuring-bind (code atn sab atv) tuple
            (make-instance 'usat :code code :atn atn :sab sab :atv atv)))))

(defun find-usty-tui (tui)
  "Find usty for tui"
  (ensure-tui-integer tui)
  (collect-umlisp-query (mrsty (sty) nil tui tui :single t)
    (make-instance 'usty :tui tui :sty sty)))

(defun find-usty-sty (sty)
  "Find usty for a sty"
  (collect-umlisp-query (mrsty (tui) nil sty sty :single t)
    (make-instance 'usty :tui (ensure-integer tui) :sty sty)))

(defun find-usty-all ()
  "Return list of usty's for all semantic types"
  (collect-umlisp-query (mrsty (tui) nil nil nil :distinct t)
    (find-usty-tui tui)))

(defun find-usab-all ()
  "Return all usab objects"
  (collect-umlisp-query (mrsab (vcui rcui vsab rsab son sf sver vstart vend imeta
                                  rmeta slc scc srl tfr cfr cxty ttyl atnl lat
                                  cenc curver sabin ssn scit) nil nil nil)
    (make-instance 'usab :vcui (ensure-integer vcui)
                   :rcui (ensure-integer rcui) :vsab vsab :rsab rsab :son son
                   :sf sf :sver sver :vstart vstart :vend vend :imeta imeta
                   :rmeta rmeta :slc slc :scc scc  :srl (ensure-integer srl)
                   :tfr (ensure-integer tfr) :cfr (ensure-integer cfr)
                   :cxty cxty :ttyl ttyl :atnl atnl :lat lat :cenc cenc
                   :curver curver :sabin sabin :ssn ssn :scit scit)))

(defun find-usab-by-key (key-name key)
  "Find usab for a key"
  (collect-umlisp-query-eval ('mrsab '(vcui rcui vsab rsab son sf sver vstart
                                       vend imeta rmeta slc scc srl tfr cfr cxty
                                       ttyl atnl lat cenc curver sabin
                                       ssn scit)
                                     nil key-name key :single t)
     (make-instance 'usab :vcui (ensure-integer vcui)
                    :rcui (ensure-integer rcui) :vsab vsab :rsab rsab :son son
                    :sf sf :sver sver :vstart vstart :vend vend :imeta imeta
                    :rmeta rmeta :slc slc :scc scc :srl (ensure-integer srl)
                    :tfr (ensure-integer tfr) :cfr (ensure-integer cfr)
                    :cxty cxty :ttyl ttyl :atnl atnl :lat lat :cenc cenc
                    :curver curver :sabin sabin
                    :ssn ssn :scit scit)))

(defun find-usab-rsab (rsab)
  "Find usab for rsab"
  (find-usab-by-key 'rsab rsab))

(defun find-usab-vsab (vsab)
  "Find usab for vsab"
  (find-usab-by-key 'vsab vsab))

(defun find-cui-max ()
  (ensure-integer (caar (mutex-sql-query "select max(CUI) from MRCON"))))

(defun find-umap-cui (cui)
  (ensure-cui-integer cui)
  (collect-umlisp-query (mrmap (mapsetsab mapsubsetid maprank fromid fromsid fromexpr
                                          fromtype fromrule fromres rel rela toid tosid
                                          toexpr totype torule tores maprule maptype
                                          mapatn mapatv cvf)
                               nil mapsetcui cui)
    (make-instance 'umap :mapsetcui cui :mapsetsab mapsetsab :mapsubsetid mapsubsetid
                   :maprank (ensure-integer maprank) :fromid fromid :fromsid fromsid
                   :fromexpr fromexpr :fromtype fromtype :fromrule fromrule :fromres fromres
                   :rel rel :rela rela :toid toid :tosid tosid :toexpr toexpr :totype totype
                   :torule torule :tores tores :maprule maprule :maptype maptype :mapatn mapatn
                   :mapatv mapatv :cvf cvf)))

(defun find-usmap-cui (cui)
  (ensure-cui-integer cui)
  (collect-umlisp-query (mrsmap (mapsetsab fromexpr fromtype rel rela toexpr totype cvf)
                               nil mapsetcui cui)
    (make-instance 'usmap :mapsetcui cui :mapsetsab mapsetsab
                   :fromexpr fromexpr :fromtype fromtype
                   :rel rel :rela rela :toexpr toexpr :totype totype
                   :cvf cvf)))

;;;; Cross table find functions

(defun find-ucon-tui (tui &key (srl *current-srl*))
  "Find list of ucon for tui"
  (ensure-tui-integer tui)
  (collect-umlisp-query (mrsty (cui) srl tui tui :lrl klrl :order (cui asc))
    (find-ucon-cui (ensure-integer cui) :srl srl)))

(defun mrconso-query-word-cui (word sab srl like)
  (format nil "SELECT DISTINCT c.cui FROM MRCONSO c,MRXW_ENG x WHERE x.WD~A'~A' AND x.cui=c.cui~A~A"
          (if like " LIKE " "=")
          (clsql-sys::sql-escape-quotes word)
          (etypecase sab
            (string
             (format nil " AND c.sab='~A'" (clsql-sys::sql-escape-quotes sab)))
            (cons
             (format nil " AND c.sab IN (~{'~A'~^,~})"
                     (mapcar 'clsql-sys::sql-escape-quotes sab)))
            (null
             ""))
          (if srl (format nil " AND KCUILRL <= ~A" srl) "")))

(defun mrconso-query-word-sui (word sab srl like)
  (format nil "SELECT DISTINCT c.sui FROM MRCONSO c,MRXW_ENG x WHERE x.WD~A'~A' AND x.sui=c.sui~A~A"
          (if like " LIKE " "=")
          (clsql-sys::sql-escape-quotes word)
          (etypecase sab
            (string
             (format nil " AND c.sab='~A'" (clsql-sys::sql-escape-quotes sab)))
            (cons
             (format nil " AND c.sab IN (~{'~A'~^,~})"
                     (mapcar 'clsql-sys::sql-escape-quotes sab)))
            (null
             ""))
          (if srl (format nil " AND KCUILRL <= ~A" srl) "")))

(defun find-uconso-word (word &key sab (srl *current-srl*) (like nil))
  "Return list of uconso that match word. Optionally, matching SAB. Optionally, use SQL's LIKE syntax"
  (cond
    (sab
     (let ((sui-query (mrconso-query-word-sui word sab srl like))
           (uconsos nil))
       (dolist (sui (remove-duplicates (sort (mapcar 'car (mutex-sql-query sui-query)) #'<)))
         (setq uconsos (nconc uconsos (find-uconso-sui sui :sab sab))))
       (remove-duplicates uconsos :key 'cui)))
    (t
     (collect-umlisp-query-eval ('mrxw_eng '(cui) srl 'wd word :like like :distinct t
                                           :lrl 'klrl :order '(cui asc))
       (find-uconso-cui cui :srl srl)))))

(defun find-ucon-word (word &key sab (srl *current-srl*) (like nil))
  "Return list of ucon that match word in matching SAB. Optionally, use SQL's LIKE syntax"
  (cond
    (sab
     (let ((query (mrconso-query-word-cui word sab srl like)))
       (loop for tuple in (mutex-sql-query query)
             collect (make-instance 'ucon :cui (first tuple)))))
    (t
     (collect-umlisp-query-eval ('mrxw_eng '(cui) srl 'wd word :like like :distinct t
                                           :lrl 'klrl :order '(cui asc))
       (find-ucon-cui cui :srl srl)))))

(defun find-ucon-normalized-word (word &key (srl *current-srl*) (like nil))
  "Return list of ucons that match word, optionally use SQL's LIKE syntax"
  (collect-umlisp-query-eval ('mrxnw_eng '(cui) srl 'nwd word :like like :distinct t
                                      :lrl 'klrl :order '(cui asc))
    (find-ucon-cui cui :srl srl)))

(defun find-cui-normalized-word (word &key (srl *current-srl*) (like nil))
  "Return list of cui that match word, optionally use SQL's LIKE syntax"
  (collect-umlisp-query-eval ('mrxnw_eng '(cui) srl 'nwd word :like like :distinct t
                                         :lrl 'klrl :order '(cui asc))
                             cui))

(defun find-lui-normalized-word (word &key (srl *current-srl*) (like nil))
  "Return list of cui that match word, optionally use SQL's LIKE syntax"
  (collect-umlisp-query-eval ('mrxnw_eng '(lui) srl 'nwd word :like like :distinct t
                                         :lrl 'klrl :order '(cui asc))
                             lui))

(defun find-sui-normalized-word (word &key (srl *current-srl*) (like nil))
  "Return list of cui that match word, optionally use SQL's LIKE syntax"
  (collect-umlisp-query-eval ('mrxnw_eng '(sui) srl 'nwd word :like like :distinct t
                                         :lrl 'klrl :order '(cui asc))
                             sui))

(defun find-ustr-word (word &key sab (srl *current-srl*) (like nil))
  "Return list of ustr that match word in matching SAB. Optionally, use SQL's LIKE syntax"
  (cond
    (sab
     (let ((query (format nil "SELECT c.sui,c.cui,c.lui,c.str,c.lrl,c.stt,c.suppress,c.cuisui FROM MRCONSO c,MRXW_ENG x WHERE x.WD ~A '~A' AND x.cui=c.cui AND x.lui=c.lui AND x.sui=c.sui~A~A"
                          (if like "LIKE" "=")
                          (clsql-sys::sql-escape-quotes word)
                          (typecase sab
                            (string
                             (format nil " AND c.sab='~A'" (clsql-sys::sql-escape-quotes sab)))
                            (cons
                             (format nil " AND c.sab IN (~('~A'~^,~))"
                                     (mapcar 'clsql-sys::sql-escape-quotes sab)))
                            (null
                             ""))
                          (if srl (format nil " AND KCUILRL <= ~D" srl) ""))))
       (loop for tuple in (mutex-sql-query query)
             collect (destructuring-bind (sui cui lui str lrl stt suppress cuisui) tuple
                       (make-instance 'ustr :sui sui :cui cui :lui lui :str str :lrl lrl
                                      :stt stt :suppress suppress :cuisui cuisui)))))
    (t
     (collect-umlisp-query (mrxw_eng (cui sui) srl wd word :lrl klrl
                                     :order (cui asc sui asc))
       (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) :srl srl)))))

(defun find-ustr-normalized-word (word &key (srl *current-srl*))
  "Return list of ustrs that match word"
  (collect-umlisp-query (mrxnw_eng (cui sui) srl nwd word :lrl klrl
                                 :order (cui asc sui asc))
    (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) :srl srl)))

(defun find-uterm-word (word &key (srl *current-srl*))
  "Return list of uterms that match word"
  (collect-umlisp-query (mrxw_eng (cui lui) srl wd word :lrl klrl
                               :order (cui asc lui asc))
    (find-uterm-cuilui (ensure-integer cui) (ensure-integer lui) :srl srl)))

(defun find-uterm-normalized-word (word &key (srl *current-srl*))
  "Return list of uterms that match word"
  (collect-umlisp-query (mrxnw_eng (cui lui) srl nwd word :lrl klrl
                                 :order (cui asc lui asc))
    (find-uterm-cuilui (ensure-integer cui) (ensure-integer lui) :srl srl)))

(defun find-ucon-noneng-word (word &key (srl *current-srl*) (like nil))
  "Return list of ucons that match non-english word"
  (collect-umlisp-query-eval ('mrxw_noneng '(cui) srl 'wd word :like like
                                        :distinct t :lrl 'klrl :order '(cui asc))
    (find-ucon-cui cui :srl srl)))

(defun find-ustr-noneng-word (word &key (srl *current-srl*))
  "Return list of ustrs that match non-english word"
  (collect-umlisp-query (mrxw_noneng (cui sui) srl wd word :lrl klrl
                                  :order (cui asc sui asc))
    (find-ustr-cuisui (ensure-integer cui) (ensure-integer sui) :srl srl)))

;; Special tables

(defun find-usrl-all ()
  (collect-umlisp-query (usrl (sab srl) nil nil nil :order (sab asc))
    (make-instance 'usrl :sab sab :srl (ensure-integer srl))))

;;; Multiword lookup and score functions

(defun find-uobj-multiword (str obj-lookup-fun sort-fun key srl
                                only-exact-if-match limit &key extra-lookup-args)
  (let ((uobjs '()))
    (dolist (word (delimited-string-to-list str #\space))
      (setq uobjs (append uobjs (apply obj-lookup-fun word :srl srl extra-lookup-args))))
    (let ((sorted
           (funcall sort-fun str
                    (delete-duplicates uobjs :test #'= :key key))))
      (let ((len (length sorted)))
        (cond
         ((zerop len)
          (return-from find-uobj-multiword nil))
         ((and only-exact-if-match (multiword-match str (pfstr (first sorted))))
          (first sorted))
         (limit
          (if (and (plusp limit) (> len limit))
              (subseq sorted 0 limit)
            limit))
         (t
          sorted))))))

(defun find-ucon-multiword (str &key (srl *current-srl*)
                                     (only-exact-if-match t)
                                     limit
                                     sab)
  (find-uobj-multiword str #'find-ucon-word #'sort-score-pfstr-str
                       #'cui srl only-exact-if-match limit
                       :extra-lookup-args (list :sab sab)))

(defun find-uconso-multiword (str &key (srl *current-srl*)
                                     (only-exact-if-match t)
                                     limit
                                     sab)
  (find-uobj-multiword str #'find-uconso-word #'sort-score-pfstr-str
                       #'cui srl only-exact-if-match limit
                       :extra-lookup-args (list :sab sab)))

(defun find-uterm-multiword (str &key (srl *current-srl*)
                                      (only-exact-if-match t)
                                      limit)
  (find-uobj-multiword str #'find-uterm-word #'sort-score-pfstr-str
                       #'lui srl only-exact-if-match limit))

(defun find-ustr-multiword (str &key (srl *current-srl*)
                                     (only-exact-if-match t)
                                     limit
                                     sab)
  (find-uobj-multiword str #'find-ustr-word #'sort-score-ustr-str
                       #'sui srl only-exact-if-match limit
                       :extra-lookup-args (list :sab sab)))

(defun sort-score-pfstr-str (str uobjs)
  "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
  (sort-score-umlsclass-str uobjs str #'pfstr))

(defun sort-score-ustr-str (str ustrs)
  "Return list of sorted and scored ucons. Score by match of str to ucon-pfstr"
  (sort-score-umlsclass-str ustrs str #'str))

(defun sort-score-umlsclass-str (objs str lookup-func)
  "Sort a list of objects based on scoring to a string"
  (let ((scored '()))
    (dolist (obj objs)
      (push (list obj (score-multiword-match str (funcall lookup-func obj)))
       scored))
    (mapcar #'car (sort scored #'> :key #'cadr))))


;;; LEX SQL functions

(defun find-lexterm-eui (eui)
  (ensure-eui-integer eui)
  (collect-umlisp-query (lrwd (wrd) nil eui eui :single t)
    (make-instance 'lexterm :eui eui :wrd wrd)))

(defun find-lexterm-word (wrd)
  (collect-umlisp-query (lrwd (eui) nil wrd wrd)
    (make-instance 'lexterm :eui (ensure-integer eui)
                   :wrd (copy-seq wrd))))

;; LEX SQL Read functions

(defun find-labr-eui (eui)
  (ensure-eui-integer eui)
  (collect-umlisp-query (lrabr (bas abr eui2 bas2) nil eui eui)
    (make-instance 'labr :eui eui :bas bas :abr abr :bas2 bas2
                   :eui2 (ensure-integer eui2))))

(defun find-labr-bas (bas)
  (collect-umlisp-query (labr (eui abr eui2 bas2) nil bas bas)
    (make-instance 'labr :eui (ensure-integer eui) :abr abr :bas2 bas2
                   :bas (copy-seq bas) :eui2 (ensure-integer eui2))))

(defun find-lagr-eui (eui)
  (ensure-eui-integer eui)
  (collect-umlisp-query (lragr (str sca agr cit bas) nil eui eui)
    (make-instance 'lagr :eui eui :str str :sca sca :agr agr
                   :cit cit :bas bas)))

(defun find-lcmp-eui (eui)
  (ensure-eui-integer eui)
  (collect-umlisp-query (lrcmp (bas sca com) nil eui eui)
    (make-instance 'lcmp :eui eui :bas bas :sca sca :com com)))

(defun find-lmod-eui (eui)
  (ensure-eui-integer eui)
  (collect-umlisp-query (lrmod (bas sca psn_mod fea) nil eui eui)
    (make-instance 'lmod :eui eui :bas bas :sca sca :psnmod psn_mod :fea fea)))

(defun find-lnom-eui (eui)
  (ensure-eui-integer eui)
  (collect-umlisp-query (lrnom (bas sca eui2 bas2 sca2) nil eui eui)
    (make-instance 'lnom :eui eui :bas bas :sca sca :bas2 bas2 :sca2 sca2
                   :eui2 (ensure-integer eui2))))

(defun find-lprn-eui (eui)
  (ensure-eui-integer eui)
  (collect-umlisp-query (lrprn (bas num gnd cas pos qnt fea) nil eui eui)
    (make-instance 'lprn :eui eui :bas bas :num num :gnd gnd
                   :cas cas :pos pos :qnt qnt :fea fea)))

(defun find-lprp-eui (eui)
  (ensure-eui-integer eui)
  (collect-umlisp-query (lrprp (bas str sca fea) nil eui eui)
    (make-instance 'lprp :eui eui :bas bas :str str :sca sca :fea fea)))

(defun find-lspl-eui (eui)
  (ensure-eui-integer eui)
  (collect-umlisp-query (lrspl (spv bas) nil eui eui)
    (make-instance 'lspl :eui eui :spv spv :bas bas)))

(defun find-ltrm-eui (eui)
  (ensure-eui-integer eui)
  (collect-umlisp-query (lrtrm (bas gen) nil eui eui)
    (make-instance 'ltrm :eui eui :bas bas :gen gen)))

(defun find-ltyp-eui (eui)
  (ensure-eui-integer eui)
  (collect-umlisp-query (lrtyp (bas sca typ) nil eui eui)
    (make-instance 'ltyp :eui eui :bas bas :sca sca :typ typ)))

(defun find-lwd-wrd (wrd)
  (make-instance 'lwd :wrd wrd
                 :euilist (collect-umlisp-query (lrwd (eui) nil wrd wrd)
                                                (ensure-integer eui))))

;;; Semantic Network SQL access functions

(defun find-sdef-ui (ui)
  (collect-umlisp-query (srdef (rt sty_rl stn_rtn def ex un rh abr rin)
                            nil ui ui :single t)
    (make-instance 'sdef :rt rt :ui ui :styrl sty_rl :stnrtn stn_rtn
                   :def def :ex ex :un un :rh rh :abr abr :rin rin)))

(defun find-sstre1-ui (ui)
  (collect-umlisp-query (srstre1 (ui2 ui3) nil ui ui)
    (make-instance 'sstre1 :ui ui :ui2 (ensure-integer ui2)
                   :ui3 (ensure-integer ui3))))

(defun find-sstre1-ui2 (ui2)
  (collect-umlisp-query (srstre1 (ui ui3) nil ui2 ui2)
    (make-instance 'sstre1 :ui (ensure-integer ui) :ui2 ui2
                   :ui3 (ensure-integer ui3))))

(defun find-sstr-rl (rl)
  (collect-umlisp-query (srstre (sty_rl sty_rl2 ls) nil rl rl)
    (make-instance 'sstr :rl rl :styrl sty_rl :styrl2 sty_rl2 :ls ls)))

(defun find-sstre2-sty (sty)
  (collect-umlisp-query (srstre2 (rl sty2) nil sty sty)
    (make-instance 'sstre2 :sty (copy-seq sty) :rl rl :sty2 sty2)))

(defun find-sstr-styrl (styrl)
  (collect-umlisp-query (srstr (rl sty_rl2 ls) nil styrl styrl)
    (make-instance 'sstr :styrl styrl :rl rl :styrl2 sty_rl2 :ls ls)))


;;; **************************
;;; Local Classes
;;; **************************


(defun make-ustats ()
  (with-sql-connection (conn)
    (ignore-errors (sql-execute "drop table USTATS" conn))
    (sql-execute "create table USTATS (NAME varchar(160), COUNT bigint, SRL integer)" conn)

    (dotimes (srl 5)
      (insert-ustats-count conn "Concept Count" "MRCONSO" "distinct CUI" "KCUILRL" srl)
      (insert-ustats-count conn "Term Count" "MRCONSO" "distinct KCUILUI" "KCUILRL" srl)
      (insert-ustats-count conn "Distinct Term Count" "MRCONSO" "distinct LUI" "KLUILRL" srl)
      (insert-ustats-count conn "String Count" "MRCONSO" "*" "KSUILRL" srl)
      (insert-ustats-count conn "Distinct String Count" "MRCONSO" "distinct SUI" "KSUILRL" srl)
      (insert-ustats-count conn "Hierarchcy" "MRHIER" "*" "KSRL" srl)
      (insert-ustats-count conn "Mappings" "MRMAP" "*" "KSRL" srl)
      (insert-ustats-count conn "Simple Mappings" "MRSMAP" "*" "KSRL" srl)
      (insert-ustats-count conn "Co-occuring Concept Count" "MRCOC" "*" "KLRL" srl)
      (insert-ustats-count conn "Definition Count" "MRDEF" "*" "KSRL" srl)
      (insert-ustats-count conn "Rank Count" "MRRANK" "*" "KSRL" srl)
      (insert-ustats-count conn "Relationship Count" "MRREL" "*" "KSRL" srl)
      (insert-ustats-count conn "Semantic Type Count" "MRSTY" "*" "KLRL" srl)
      (insert-ustats-count conn "Simple Attribute Count" "MRSAT" "*" "KSRL" srl)
      (insert-ustats-count conn "Source Abbreviation Count" "MRSAB" "*" "SRL" srl)
      (insert-ustats-count conn "Word Index Count" "MRXW_ENG" "*" "KLRL" srl)
      (insert-ustats-count conn "Normalized Word Index Count" "MRXNW_ENG" "*" "KLRL" srl)
      (insert-ustats-count conn "Normalized String Index Count" "MRXNS_ENG" "*" "KLRL" srl))
    (sql-execute "create index USTATS_SRL on USTATS (SRL)" conn))
  (find-ustats-all))

(defun insert-ustats-count (conn name table count-variable srl-control srl)
  (insert-ustats conn name (find-count-table conn table srl count-variable srl-control) srl))

(defun find-count-table (conn table srl count-variable srl-control)
  (cond
   ((stringp srl-control)
    (ensure-integer
     (caar (sql-query (format nil "select count(~a) from ~a where ~a <= ~d"
                              count-variable table srl-control srl)
                      conn))))
   ((null srl-control)
    (ensure-integer
     (caar (sql-query (format nil "select count(~a) from ~a"
                              count-variable table )
                      conn))))
   (t
    (error "Unknown srl-control")
    0)))

(defun insert-ustats (conn name count srl)
  (sql-execute (format nil "insert into USTATS (name,count,srl) values ('~a',~d,~d)"
                       name count (if srl srl 3))
               conn))

(defun find-ustats-all (&key (srl *current-srl*))
  (if srl
      (collect-umlisp-query (ustats (name count srl) nil srl srl
                                    :order (name asc))
                            (make-instance 'ustats :name name
                                           :hits (ensure-integer count)
                                           :srl (ensure-integer srl)))
    (collect-umlisp-query (ustats (name count srl) nil nil nil
                                  :order (name asc))
                          (make-instance 'ustats :name name
                                         :hits (ensure-integer count)
                                         :srl (ensure-integer srl)))))

(defun find-ustats-srl (srl)
  (collect-umlisp-query (ustats (name count) nil srl srl :order (name asc))
                           (make-instance 'ustats :name name :hits (ensure-integer count))))