summaryrefslogtreecommitdiffabout
path: root/src/ellinika/sql.scm
blob: 5867d28e1aee841e6db504df16d4dab99e4e7336 (plain)
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
;;;; This file is part of Ellinika project.
;;;; Copyright (C) 2011 Sergey Poznyakoff
;;;;
;;;; Ellinika is free software; you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation; either version 3 of the License, or
;;;; (at your option) any later version.
;;;;
;;;; Ellinika is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;; GNU General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
;;;;
(define-module (ellinika sql))

(use-modules (srfi srfi-1)
             (ellinika elmorph)
	     (gamma sql))

(re-export sql-catch-failure sql-ignore-failure)

(define-public ellinika:sql-verbose #f)
(define-public ellinika:sql-conn #f)
(define-public ellinika:sql-dry-run #f)

(define-public (ellinika:sql-connect arg)
  (cond
   (ellinika:sql-dry-run #t)
   (else
    (set! ellinika:sql-conn (sql-open-connection arg))
    (if ellinika:sql-conn
	(sql-query ellinika:sql-conn "SET NAMES utf8"))
    ellinika:sql-conn)))

(define-public (ellinika:sql-disconnect)
  (if ellinika:sql-conn (sql-close-connection ellinika:sql-conn)))


(define (->string arg)
  (cond
   ((string? arg) arg)
   ((elstr? arg) (elstr->string arg))
   ((number? arg) (number->string arg))
   ((bool? arg) (if arg "true" "false"))
   (else
    (error "Unhandled argument type: ~S" arg))))
      

;; Format specifiers:
;;  ~A  -  escaped string
;;  ~Q  -  escaped and quoted string; NULL if argument is #f
;;  ~N  -  unescaped number
;;  ~<anychar> - <anychar>
(define-public (ellinika:format-sql-query fmt args)
  (let* ((fmtlist (string-split fmt #\~))
	 (segments (reverse
		    (fold
		     (lambda (elt prev)
		       (if (string-null? elt)
			   (cons (string-append (car prev) "~") (cdr prev))
			   (let ((ch (string-ref elt 0)))
			     (case ch
			       ((#\A #\Q #\N) (cons elt prev))
			       (else
				(cons (string-append (car prev) elt)
				      (cdr prev)))))))
		     '()
		     (cdr fmtlist)))))
    (let ((nseg (length segments))
	  (narg (length args)))
      (cond
       ((> nseg narg)
	(error "Too few arguments for format \"~S\": ~S/~S"
	       fmt nseg narg))
       ((< nseg narg)
	(error "Too many arguments for format \"~S\": ~S/~S"
	       fmt nseg narg))
       (else
	(apply
	 string-append
	 (car fmtlist)
	 (map
	  (lambda (seg arg)
	    (string-append 
	     (case (string-ref seg 0)
	       ((#\A) (utf8-escape (->string arg)))
	       ((#\Q) (if (not arg)
			  "NULL"
			  (string-append "\"" (utf8-escape (->string arg))
					 "\"")))
	       ((#\N) (->string arg)))
	     (substring seg 1)))
	  segments
	  args)))))))
	  
(define-public (ellinika:sql-query fmt . args)
  (let ((query (ellinika:format-sql-query fmt args)))
    (cond
     (ellinika:sql-verbose
      (format #t "QUERY: ~A~%" query)
      (if (not ellinika:sql-dry-run)
	  (let ((res (sql-query ellinika:sql-conn query)))
	    (format #t "RES: ~A~%" res)
	    res)))
     (ellinika:sql-dry-run)
     (else
      (sql-query ellinika:sql-conn query)))))

Return to:

Send suggestions and report system problems to the System administrator.