File: connect.lisp

package info (click to toggle)
cl-hyperobject 2.11.0-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 300 kB
  • ctags: 224
  • sloc: lisp: 1,820; xml: 215; makefile: 161; sh: 28
file content (90 lines) | stat: -rw-r--r-- 2,483 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
;;;; -- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: umlisp -*-
;;;; *************************************************************************
;;;; FILE IDENTIFICATION
;;;;
;;;; Name:          connect.lisp
;;;; Purpose:       Low-level SQL routines data for UMLisp
;;;; Programmer:    Kevin M. Rosenberg
;;;; Date Started:  Apr 2000
;;;;
;;;; $Id: connect.lisp 9355 2004-05-13 18:01:13Z kevin $
;;;;
;;;; This file is Copyright (c) 2000-2003 by Kevin M. Rosenberg
;;;; *************************************************************************

(in-package #:hyperobject)

(defvar *ho-sql-db* "ho")
(defun ho-sql-db ()
  *ho-sql-db*)
(defun ho-sql-db! (dbname)
  (sql-disconnect-pooled)
  (setq *ho-sql-db* dbname))

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

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

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

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

(defun sql-connect ()
  "Connect to HO database, automatically used pooled connections"
  (clsql:connect `(,(ho-sql-host) ,(ho-sql-db) ,(ho-sql-user) ,(ho-sql-passwd)) 
		 :database-type *ho-sql-type* :pool t))

(defun sql-disconnect (conn)
  "Disconnect from HO 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 (types :auto))
  (clsql:query cmd :database conn :types types))

(defun sql-execute (cmd conn)
  (clsql:execute-command cmd :database 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 (types :auto))
  (with-mutex-sql (conn)
    (sql-query cmd conn :types types)))