1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
6 ;;;; Base SQL functions
8 ;;;; This file is part of CLSQL.
10 ;;;; CLSQL users are granted the rights to distribute and use this software
11 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
12 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
13 ;;;; *************************************************************************
15 (in-package #:clsql-base-sys)
19 (defgeneric query (query-expression &key database result-types flatp)
21 "Execute the SQL query expression QUERY-EXPRESSION on the given
22 DATABASE which defaults to *default-database*. RESULT-TYPES is a list
23 of symbols such as :string and :integer, one for each field in the
24 query, which are used to specify the types to return. The FLATP
25 argument, which has a default value of nil, specifies if full
26 bracketed results should be returned for each matched entry. If FLATP
27 is nil, the results are returned as a list of lists. If FLATP is t,
28 the results are returned as elements of a list, only if there is only
29 one result per row. Returns a list of lists of values of the result of
30 that expression and a list of field names selected in sql-exp."))
32 (defmethod query ((query-expression string) &key (database *default-database*)
33 (result-types nil) (flatp nil))
34 (record-sql-action query-expression :query database)
35 (let* ((res (database-query query-expression database result-types))
36 (res (if (and flatp (= (length
37 (slot-value query-expression 'selections))
41 (record-sql-action res :result database)
46 (defgeneric execute-command (expression &key database)
48 "Executes the SQL command specified by EXPRESSION for the database
49 specified by DATABASE, which has a default value of
50 *DEFAULT-DATABASE*. The argument EXPRESSION may be any SQL statement
51 other than a query. To run a stored procedure, pass an appropriate
52 string. The call to the procedure needs to be wrapped in a BEGIN END
55 (defmethod execute-command ((sql-expression string)
56 &key (database *default-database*))
57 (record-sql-action sql-expression :command database)
58 (let ((res (database-execute-command sql-expression database)))
59 (record-sql-action res :result database))
62 (defmacro do-query (((&rest args) query-expression
63 &key (database '*default-database*) (result-types nil))
65 "Repeatedly executes BODY within a binding of ARGS on the attributes
66 of each record resulting from QUERY. The return value is determined by
67 the result of executing BODY. The default value of DATABASE is
69 (let ((result-set (gensym))
73 `(let ((,db ,database))
74 (multiple-value-bind (,result-set ,columns)
75 (database-query-result-set ,query-expression ,db
76 :full-set nil :result-types ,result-types)
79 (do ((,row (make-list ,columns)))
80 ((not (database-store-next-row ,result-set ,db ,row))
82 (destructuring-bind ,args ,row
84 (database-dump-result-set ,result-set ,db)))))))
86 (defun map-query (output-type-spec function query-expression
87 &key (database *default-database*)
89 "Map the function over all tuples that are returned by the query in
90 query-expression. The results of the function are collected as
91 specified in output-type-spec and returned like in MAP."
92 (macrolet ((type-specifier-atom (type)
93 `(if (atom ,type) ,type (car ,type))))
94 (case (type-specifier-atom output-type-spec)
96 (map-query-for-effect function query-expression database result-types))
98 (map-query-to-list function query-expression database result-types))
99 ((simple-vector simple-string vector string array simple-array
100 bit-vector simple-bit-vector base-string
102 (map-query-to-simple output-type-spec function query-expression database result-types))
104 (funcall #'map-query (cmucl-compat:result-type-or-lose output-type-spec t)
105 function query-expression :database database :result-types result-types)))))
107 (defun map-query-for-effect (function query-expression database result-types)
108 (multiple-value-bind (result-set columns)
109 (database-query-result-set query-expression database :full-set nil
110 :result-types result-types)
113 (do ((row (make-list columns)))
114 ((not (database-store-next-row result-set database row))
116 (apply function row))
117 (database-dump-result-set result-set database)))))
119 (defun map-query-to-list (function query-expression database result-types)
120 (multiple-value-bind (result-set columns)
121 (database-query-result-set query-expression database :full-set nil
122 :result-types result-types)
125 (let ((result (list nil)))
126 (do ((row (make-list columns))
127 (current-cons result (cdr current-cons)))
128 ((not (database-store-next-row result-set database row))
130 (rplacd current-cons (list (apply function row)))))
131 (database-dump-result-set result-set database)))))
134 (defun map-query-to-simple (output-type-spec function query-expression database result-types)
135 (multiple-value-bind (result-set columns rows)
136 (database-query-result-set query-expression database :full-set t
137 :result-types result-types)
141 ;; We know the row count in advance, so we allocate once
143 (cmucl-compat:make-sequence-of-type output-type-spec rows))
144 (row (make-list columns))
145 (index 0 (1+ index)))
146 ((not (database-store-next-row result-set database row))
148 (declare (fixnum index))
149 (setf (aref result index)
150 (apply function row)))
151 ;; Database can't report row count in advance, so we have
152 ;; to grow and shrink our vector dynamically
154 (cmucl-compat:make-sequence-of-type output-type-spec 100))
155 (allocated-length 100)
156 (row (make-list columns))
157 (index 0 (1+ index)))
158 ((not (database-store-next-row result-set database row))
159 (cmucl-compat:shrink-vector result index))
160 (declare (fixnum allocated-length index))
161 (when (>= index allocated-length)
162 (setq allocated-length (* allocated-length 2)
163 result (adjust-array result allocated-length)))
164 (setf (aref result index)
165 (apply function row))))
166 (database-dump-result-set result-set database)))))