/usr/share/gauche-0.9/0.9.5/lib/dbi.scm is in gauche 0.9.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | ;;;
;;; dbi - common database interface layer
;;;
;;; Copyright (c) 2003-2005 Scheme Arts, L.L.C., All rights reserved.
;;; Copyright (c) 2003-2005 Time Intermedia Corporation, All rights reserved.
;;;
;;; Redistribution and use in source and binary forms, with or without
;;; modification, are permitted provided that the following conditions
;;; are met:
;;;
;;; 1. Redistributions of source code must retain the above copyright
;;; notice, this list of conditions and the following disclaimer.
;;;
;;; 2. Redistributions in binary form must reproduce the above copyright
;;; notice, this list of conditions and the following disclaimer in the
;;; documentation and/or other materials provided with the distribution.
;;;
;;; 3. Neither the name of the authors nor the names of its contributors
;;; may be used to endorse or promote products derived from this
;;; software without specific prior written permission.
;;;
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;;;
(define-module dbi
(use text.sql)
(use file.util)
(use srfi-1)
(use srfi-13)
(use util.match)
(extend util.relation)
(export <dbi-error> <dbi-nonexistent-driver-error>
<dbi-unsupported-error> <dbi-parameter-error>
<dbi-driver> <dbi-connection> <dbi-query>
dbi-connect dbi-close dbi-prepare dbi-execute dbi-do
dbi-open? dbi-parse-dsn dbi-make-driver
dbi-prepare-sql dbi-escape-sql dbi-list-drivers
dbi-make-connection dbi-execute-using-connection
;; compatibility
dbi-make-query dbi-execute-query dbi-get-value
<dbi-exception> <dbi-result-set>))
(select-module dbi)
;;;==============================================================
;;; DBI conditions
;;;
;; Root of dbi-related errors
(define-condition-type <dbi-error> <error> #f)
;; Failed to load the specified driver
(define-condition-type <dbi-nonexistent-driver-error> <dbi-error> #f
(driver-name))
;; Feature not supported
(define-condition-type <dbi-unsupported-error> <dbi-error> #f)
;; Parameter mismatch between a prepared query and its execution.
(define-condition-type <dbi-parameter-error> <dbi-error> #f)
;;;==============================================================
;;; DBI object definitions
;;;
;; <dbi-driver> is the base class of database drivers; a database
;; driver implements actual interface to a specific database system.
;; Each dbd.* module provides the concrete implementation.
;;
;; Usually the singleton instance of a concrete driver is created
;; implicitly by dbi-connect with the data-source string. So the
;; user hardly need to see <dbi-driver> object.
(define-class <dbi-driver> ()
((driver-name :init-keyword :driver-name) ; "mysql" "pg" etc.
))
;; <dbi-connection> : represents a connection to the database system.
;; All the transactions must be done while the connection is 'open'.
(define-class <dbi-connection> ()
((open :init-value #t) ;; this slot is for backward compatibility.
;; do not count on this. will be removed.
))
;; <dbi-query> : represents a prepared query.
(define-class <dbi-query> ()
((connection :init-keyword :connection)
(prepared :init-keyword :prepared)
(open :init-value #t) ;; this slot is for backward compatibility.
;; do not count on this. will be removed.
))
;;;==============================================================
;;; User-level APIs
;;;
;; Establish a connection to the data source specified by DSN,
;; and returns a connection object.
;; DSN is the data source name, which can have the following syntax.
;; "dbi:driver-type"
;; "dbi:driver-type:connection-options"
;; Connection-options is like "name1=value1;name2=value2;...".
;;
(define (dbi-connect dsn . args)
(receive (driver-name options option-alist) (dbi-parse-dsn dsn)
(apply dbi-make-connection
(dbi-make-driver driver-name) options option-alist args)))
;; Prepares SQL statement and returns a closure, which executes
;; the statement when called. If the given SQL statement takes parameters,
;; the closure takes the same number of arguments as of the parameters.
;;
;; The default method uses text.sql to parse the SQL statement, and calls
;; legacy dbd API. This will go away once the drivers switched to the
;; new dbd API.
(define-method dbi-prepare ((c <dbi-connection>) (sql <string>)
:key (pass-through #f))
(let1 prepared (if pass-through
(^ args
(unless (null? args)
(error <dbi-parameter-error>
"parameter is given to the pass through sql:" sql))
sql)
(dbi-prepare-sql c sql))
(make <dbi-query> :connection c :prepared prepared)))
(define-method dbi-execute ((q <dbi-query>) . params)
(dbi-execute-using-connection (ref q 'connection) q params))
(define-method dbi-execute-using-connection ((c <dbi-connection>)
(q <dbi-query>) params)
(dbi-execute-query c (apply (ref q 'prepared) params)))
;; Does preparation and execution at once. The driver may overload this.
(define-method dbi-do ((c <dbi-connection>) sql options . args)
(unless (proper-list? options)
(error "dbi-do: bad option list:" options))
(apply dbi-execute (apply dbi-prepare c sql options) args))
(define-method dbi-do ((c <dbi-connection>) sql)
(dbi-do c sql '()))
;; Returns a string safe to be embedded in SQL.
;; (dbi-escape-sql c "Don't know") => "'Don''t know'"
;; What's "safe" depends on the underlying DBMS. The default procedure
;; only escapes a single quote by repeating it. If the DBMS has other
;; type of escaping mechanism, the driver should overload this with
;; a proper escaping method.
(define-method dbi-escape-sql ((c <dbi-connection>) str)
(regexp-replace-all #/'/ str "''"))
;; Returns a list of available dbd.* backends. Each entry is
;; a cons of a module name and its driver name.
(define (dbi-list-drivers)
(library-map 'dbd.* (^[m p] m)))
;;;==============================================================
;;; DBD-level APIs
;;;
;; Subclass SHOULD implement this.
(define-method dbi-make-connection ((d <dbi-driver>)
(options <string>)
(option-alist <list>)
:key (username "") (password ""))
;; The default method here is just a temporary one to use
;; older dbd drivers. Will go away once the drivers catch up
;; the new interface.
;; Calls deprecated dbi-make-connection API.
(dbi-make-connection d username password (or options "")))
;; Usually the subclass should define these for the connection
;; and result set objects.
(define-method dbi-open? (obj) #t)
(define-method dbi-close (obj) (undefined))
;;;===================================================================
;;; Low-level utilities
;;;
;; Parse data source name. Returns
;; (driver-name, option-string, option-alist)
;;
(define (dbi-parse-dsn data-source-name)
(rxmatch-case data-source-name
(#/^dbi:([\w-]+)(?::(.*))?$/ (#f driver options)
(if (and options (not (string-null? options)))
(let1 alist (map (^[nv] (receive (n v) (string-scan nv "=" 'both)
(if n (cons n v) (cons nv #t))))
(string-split options #\;))
(values driver options alist))
(values driver "" '())))
(else (error <dbi-error> "bad data source name spec:" data-source-name))))
;; Loads a concrete driver module, and returns an instance of
;; the driver.
(define (dbi-make-driver driver-name)
(let* ([module (string->symbol #"dbd.~driver-name")]
[path (module-name->path module)]
[class-name (string->symbol #"<~|driver-name|-driver>")])
(or (and-let* ([ (library-exists? path :strict? #t) ]
[driver-class
(begin (eval `(require ,(path-sans-extension path))
(current-module))
(global-variable-ref module class-name #f))])
(make driver-class :driver-name driver-name))
(errorf <dbi-nonexistent-driver-error>
:driver-name driver-name
"couldn't load driver dbd.~a" driver-name))))
;; Default prepared-SQL handler
;; dbi-prepare-sql returns a procedure, which generates a complete sql
;; when called with binding values to the parameters.
(define (dbi-prepare-sql conn sql)
(let* ([tokens (sql-tokenize sql)]
[num-params (count (^[elt]
(match elt
[('parameter (? integer?)) #t]
[('parameter (? string? name))
(errorf <dbi-unsupported-error>
"Named parameter (:~a) isn't supported yet" name)]
[else #f]))
tokens)])
(^ args
(unless (= (length args) num-params)
(error <dbi-parameter-error>
"wrong number of parameters given to an SQL:" sql))
(call-with-output-string
(^p (with-port-locking p
(cut generate-sql/parameters conn tokens args p sql)))))))
(define (generate-sql/parameters conn tokens args p sql)
(let loop ([tokens tokens]
[args args]
[delim #t])
(unless (null? tokens)
(match (car tokens)
[(? char? x)
(display x p)
(loop (cdr tokens) args #t)]
[(? symbol? x)
(unless delim (write-char #\space p))
(display x p)
(loop (cdr tokens) args #f)]
[(? string? x)
(unless delim (write-char #\space p))
(display x p)
(loop (cdr tokens) args #f)]
[('delimited x)
(unless delim (write-char #\space p))
(format p "\"~a\"" (regexp-replace-all #/\"/ x "\"\""))
(loop (cdr tokens) args #f)]
[('string x)
(unless delim (write-char #\space p))
(format p "'~a'" (regexp-replace-all #/'/ x "''"))
(loop (cdr tokens) args #f)]
[('number x)
(unless delim (write-char #\space p))
(display x p)
(loop (cdr tokens) args #f)]
[('parameter n)
(unless delim (write-char #\space p))
(let* ([argval (car args)]
[s (cond
[(not argval) "NULL"]
[(string? argval)
#"'~(dbi-escape-sql conn argval)'"]
[(symbol? argval)
#"'~(dbi-escape-sql conn (symbol->string argval))'"]
[(real? argval) (number->string argval)]
[else (error <dbi-parameter-error>
"bad type of parameter for SQL:" argval)])])
(display s p))
(loop (cdr tokens) (cdr args) #f)]
[('bitstring x)
(unless delim (write-char #\space p))
(format p "B'~a'" x)
(loop (cdr tokens) args #f)]
[('hexstring x)
(unless delim (write-char #\space p))
(format p "X'~a'" x)
(loop (cdr tokens) args #f)]
[else
(errorf <dbi-unsupported-error>
"unsupported SQL token ~a in ~s" (car tokens) sql)]
))))
;;;==============================================================
;;; Backward compatibility stuff
;;;
;; These are provided for compatibility with dbi-0.1.5 and dbd-*
;; modules that depend on it. The newly written code shouldn't use
;; these interface. Will be gone in a few releases.
(define <dbi-exception> <dbi-error>)
(define-class <dbi-result-set> ()
((open :init-value #t)))
(define-method dbi-get-value ((r <sequence>) (n <integer>))
(ref r n))
;; Older API
(define-method dbi-make-connection ((d <dbi-driver>)
(user <string>)
(pass <string>)
(options <string>))
(dbi-connect #"dbi:~(slot-ref d 'driver-name):~options"
:username user :password pass))
(define-method dbi-make-query ((c <dbi-connection>) . _)
(make <dbi-query> :connection c))
(define-method dbi-execute-query ((q <dbi-query>) (s <string>))
(dbi-do (ref q 'connection) s '(:pass-through #t)))
|