File: sql.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 (122 lines) | stat: -rw-r--r-- 3,348 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
;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: umlisp -*-
;;;; *************************************************************************
;;;; FILE IDENTIFICATION
;;;;
;;;; Name:     sql.lisp
;;;; Purpose:  Low-level SQL routines data for UMLisp
;;;; 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 +umls-sql-map+
    '((:2004aa . "KUMLS2004AA")
      (:2006ac . "KUMLS2006AC")
      (:2006ad . "MTS2006AD")))
(defvar +default-umls-db+ "MTS2006AD")


(defun lookup-db-name (db)
  (cdr (assoc (ensure-keyword db) +umls-sql-map+)))

(defvar *umls-sql-db* +default-umls-db+)
(defun umls-sql-db ()
  *umls-sql-db*)

(defun umls-sql-db! (db)
  (etypecase db
    (string
     (setq *umls-sql-db* db))
    (keyword
     (setq *umls-sql-db* (lookup-db-name db)))))

(defvar *umls-sql-user* "secret")
(defun umls-sql-user ()
  *umls-sql-user*)
(defun umls-sql-user! (u)
  (sql-disconnect-pooled)
  (setq *umls-sql-user* u))

(defvar *umls-sql-passwd* "secret")
(defun umls-sql-passwd ()
  *umls-sql-passwd*)
(defun umls-sql-passwd! (p)
  (sql-disconnect-pooled)
  (setq *umls-sql-passwd* p))

(defvar *umls-sql-host* "localhost")
(defun umls-sql-host ()
  *umls-sql-host*)
(defun umls-sql-host! (h)
  (sql-disconnect-pooled)
  (setq *umls-sql-host* h))

(defvar *umls-sql-type* :mysql)
(defun umls-sql-type ()
  *umls-sql-type*)
(defun umls-sql-type! (h)
  (sql-disconnect-pooled)
  (setq *umls-sql-type* h))

(defun umls-connection-spec ()
  (list *umls-sql-host* *umls-sql-db*
        *umls-sql-user* *umls-sql-passwd*))

(defun sql-connect ()
  "Connect to UMLS database, automatically used pooled connections"
  (clsql:connect (umls-connection-spec)
                 :database-type *umls-sql-type* :pool t))

(defun sql-disconnect (conn)
  "Disconnect from UMLS database, but put connection back into pool"
  (clsql:disconnect :database conn))

(defun sql-disconnect-pooled ()
  (clsql:disconnect-pooled))

(defmacro with-sql-connection ((conn) &body body)
  `(let ((,conn (sql-connect)))
     (unwind-protect
         (progn ,@body)
       (when ,conn (clsql:disconnect :database ,conn)))))

(defun sql-query (cmd conn &key (result-types :auto))
  (clsql:query cmd :database conn :result-types result-types :field-names nil))

(defun sql-execute (cmd conn)
  (clsql:execute-command cmd :database conn))

(defun sql (stmt conn)
  (if (string-equal "SELECT" (subseq stmt 0 6))
      (sql-query stmt conn)
    (sql-execute stmt conn)))

(defun umls-sql (stmt)
  (check-type stmt string)
  (with-sql-connection (conn)
    (sql stmt conn)))

;;; Pool of open connections

(defmacro with-mutex-sql ((conn) &body body)
  `(let ((,conn (sql-connect)))
     (unwind-protect
         (progn ,@body)
       (when ,conn (sql-disconnect ,conn)))))

(defun mutex-sql-execute (cmd)
  (with-mutex-sql (conn)
    (sql-execute cmd conn)))

(defun mutex-sql-query (cmd &key (result-types :auto))
  (with-mutex-sql (conn)
    (sql-query cmd conn :result-types result-types)))